Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import { FunctionDeclaration, Project, SourceFile, SyntaxKind } from 'ts-morph';
import { extractDateFromComments } from '../utils';
import { validate as uuidValidate } from 'uuid';
const KS_ACTIVATED_METHOD = `isActivated`;
export interface ICoreOptions {
targetId?: string;
ksFilePath?: string;
thresholdDate?: Date;
}
interface IFindKSResult {
ksDecls: FunctionDeclaration[];
guids: string[];
}
// graduate ks before 180 days by default
const defaultDate = new Date(Date.now() - 1000 * 60 * 60 * 24 * 180);
export function findKSDeclaration(project: Project, options: ICoreOptions): IFindKSResult {
// May have multiple decls with the same id, so it's an array
const result: IFindKSResult = {
ksDecls: [],
guids: []
};
const { targetId, ksFilePath, thresholdDate = defaultDate } = options;
// find file with 'KS_ACTIVATED_METHOD'
let ksFiles: SourceFile[];
if (ksFilePath) {
try {
ksFiles.push(project.getSourceFileOrThrow(ksFilePath));
} catch (err) {
console.error(`Invalid KS file path: ${ksFilePath}`);
}
} else {
// Declarations can only appear where we have KS imports
ksFiles = project.getSourceFiles().filter((f) => {
const importedNames = f.getDescendantsOfKind(SyntaxKind.ImportSpecifier).map((im) => im.getName());
const hasKsImported: boolean = importedNames.some((name) => /killswitch/i.test(name));
return hasKsImported && f.getText().includes(`.${KS_ACTIVATED_METHOD}`);
});
}
ksFiles.forEach((ksFile) => {
const funDecls = ksFile.getChildrenOfKind(SyntaxKind.FunctionDeclaration);
funDecls.forEach((funDecl) => {
const guid = getGuid(funDecl);
// if targetId is provided, it should be matched with the ks id
if (targetId) {
if (guid === targetId) {
result.ksDecls.push(funDecl);
result.guids.push(guid);
}
} else if (uuidValidate(guid)) {
// otherwise we graduate based on the date
// if the second argument exists, it should be the date
let dateString = getDate(funDecl);
if (isNaN(Date.parse(dateString))) {
dateString = '';
}
if (!dateString) {
dateString = extractDateFromComments(funDecl);
}
if (dateString) {
const parsedDate = new Date(dateString);
if (parsedDate < thresholdDate) {
result.ksDecls.push(funDecl);
result.guids.push(guid);
}
}
}
});
});
return result;
}
export function getGuid(funDecl: FunctionDeclaration): string {
const returnStatement = funDecl.getFirstDescendantByKind(SyntaxKind.ReturnStatement);
const callExp = returnStatement?.getExpressionIfKind(SyntaxKind.CallExpression);
const accessExp = callExp?.getExpressionIfKind(SyntaxKind.PropertyAccessExpression);
if (accessExp.getName() !== KS_ACTIVATED_METHOD) {
return;
}
// TODO: should we also handle Guid.parse(ID) ?
const firstArgument = callExp?.getArguments()[0]?.getText();
return firstArgument.substring(1, firstArgument.length - 1);
}
function getDate(funDecl: FunctionDeclaration): string {
const returnStatement = funDecl.getFirstDescendantByKind(SyntaxKind.ReturnStatement);
const callExp = returnStatement?.getExpressionIfKind(SyntaxKind.CallExpression);
return callExp?.getArguments()[1]?.getText();
}
|