Skip to content

Commit

Permalink
Create script to check for unused fields
Browse files Browse the repository at this point in the history
Refs: #20
  • Loading branch information
nene committed Jan 19, 2024
1 parent 1ace0e5 commit 8ecd1db
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"build": "yarn clean && tsc",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"pretty": "prettier -w src/ test/",
"ts:check": "tsc --noEmit"
"ts:check": "tsc --noEmit",
"analyze-field-access": "ts-node scripts/analyze-field-access.ts src/index.ts"
},
"keywords": [
"prettier",
Expand Down
113 changes: 113 additions & 0 deletions scripts/analyze-field-access.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import * as ts from "typescript";

/**
* This script analyzes the syntax map files in src/syntax/ dir
* and checks if all fields are used.
*/

const fileNames = process.argv.slice(2);
const options = {
target: ts.ScriptTarget.ES2015,
module: ts.ModuleKind.CommonJS,
};

let program = ts.createProgram(fileNames, options);
let checker = program.getTypeChecker();

for (const sourceFile of program.getSourceFiles()) {
if (!sourceFile.isDeclarationFile) {
if (isSyntaxMapFile(sourceFile.fileName)) {
ts.forEachChild(sourceFile, (node) => analyze(node, sourceFile));
}
}
}

function isSyntaxMapFile(fileName: string): boolean {
return (
fileName.includes("src/syntax/") && !fileName.endsWith("transformMap.ts")
);
}

function analyze(node: ts.Node, sourceFile: ts.SourceFile) {
if (ts.isVariableStatement(node)) {
const mapDeclaration = node.declarationList.declarations.find(
(declaration) => /\wMap$/.test(declaration.name.getText(sourceFile)),
);
if (
mapDeclaration?.initializer &&
ts.isObjectLiteralExpression(mapDeclaration.initializer)
) {
ts.forEachChild(mapDeclaration.initializer, (node) =>
analyzeObjectProperty(node, sourceFile),
);
}
}
}

function analyzeObjectProperty(node: ts.Node, sourceFile: ts.SourceFile) {
if (!ts.isPropertyAssignment(node) || !ts.isArrowFunction(node.initializer)) {
return;
}

const fields = extractExpectedFields(node.initializer);
const fieldMap = Object.fromEntries(fields.map((field) => [field, true]));
visitAll(node, (child) => {
if (ts.isStringLiteral(child)) {
fieldMap[child.text] = false;
}
if (ts.isPropertyAccessExpression(child)) {
if (ts.isIdentifier(child.name)) {
const text = child.name.escapedText.toString();
fieldMap[text] = false;
}
}
});

const missingFields = Object.entries(fieldMap)
.filter(([, value]) => value)
.map(([key]) => key);

if (missingFields.length > 0) {
console.log(``);
console.log(`Unused fields: ${missingFields.join(", ")}`);
console.log(
`In file: ${sourceFile.fileName.replace(/.*\//, "src/syntax/")}`,
);
console.log(node.getText(sourceFile));
}
}

function extractExpectedFields(node: ts.ArrowFunction): string[] {
if (node.parameters.length === 0) {
return []; // For cases like the empty node
}
const symbol = checker.getSymbolAtLocation(node.parameters[0].name);
if (!symbol) {
throw new Error("No symbol for function parameter found");
}
const type = checker.getTypeOfSymbolAtLocation(
symbol,
symbol.valueDeclaration!,
);
if (!type) {
throw new Error("No type of function parameter found");
}
const argType = type.aliasTypeArguments?.[1];
if (!argType) {
throw new Error("No second type argument of function parameter found");
}
if (argType.isLiteral()) {
return [argType.value as string];
} else if (argType.isUnion()) {
return argType.types.map((t) =>
t.isLiteral() ? (t.value as string) : "<unknown>",
);
} else {
throw new Error(`Expected type: ${checker.typeToString(argType)}`);
}
}

function visitAll(node: ts.Node, cb: (node: ts.Node) => void) {
cb(node);
node.forEachChild((child) => visitAll(child, cb));
}

0 comments on commit 8ecd1db

Please sign in to comment.