-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,102 @@ | ||
import { parse } from "@typescript-eslint/typescript-estree"; | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
parse, | ||
} from "@typescript-eslint/typescript-estree"; | ||
import fs from "fs/promises"; | ||
|
||
export async function parseFiles(files: string[]): Promise<string[]> { | ||
const queries: string[] = []; | ||
interface QueryInfo { | ||
query: string; | ||
location: { line: number; column: number }; | ||
} | ||
|
||
export async function parseFiles(files: string[]): Promise<QueryInfo[]> { | ||
const queries: QueryInfo[] = []; | ||
|
||
for (const file of files) { | ||
console.log(`Parsing file: ${file}`); | ||
const content = await fs.readFile(file, "utf-8"); | ||
const ast = parse(content, { | ||
jsx: true, | ||
range: true, | ||
}); | ||
const ast = parse(content, { jsx: true, loc: true }); | ||
|
||
const variableMap = new Map<string, string>(); | ||
|
||
traverseAST(ast, (node) => { | ||
if ( | ||
node.type === "CallExpression" && | ||
node.callee.type === "MemberExpression" && | ||
node.callee.property.type === "Identifier" && | ||
node.callee.property.name === "generateAndSend" && | ||
node.arguments[0]?.type === "Literal" | ||
node.type === AST_NODE_TYPES.VariableDeclarator && | ||
node.id.type === AST_NODE_TYPES.Identifier && | ||
node.init?.type === AST_NODE_TYPES.Literal && | ||
typeof node.init.value === "string" | ||
) { | ||
queries.push(node.arguments[0].value as string); | ||
variableMap.set(node.id.name, node.init.value); | ||
} | ||
|
||
if (isGenerateAndSendCall(node)) { | ||
const arg = node.arguments[0]; | ||
if ( | ||
arg.type === AST_NODE_TYPES.Literal && | ||
typeof arg.value === "string" | ||
) { | ||
queries.push({ | ||
query: arg.value, | ||
location: arg.loc!.start, | ||
}); | ||
} else if (arg.type === AST_NODE_TYPES.Identifier) { | ||
const variableValue = variableMap.get(arg.name); | ||
if (variableValue) { | ||
queries.push({ | ||
query: variableValue, | ||
location: arg.loc!.start, | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
console.log(`Total queries found: ${queries.length}`); | ||
queries.forEach((q) => | ||
console.log( | ||
`Query: "${q.query}" at line ${q.location.line}, column ${q.location.column}`, | ||
), | ||
); | ||
return queries; | ||
} | ||
|
||
function traverseAST(node: any, callback: (node: any) => void) { | ||
function isGenerateAndSendCall( | ||
node: TSESTree.Node, | ||
): node is TSESTree.CallExpression { | ||
return ( | ||
node.type === AST_NODE_TYPES.CallExpression && | ||
node.callee.type === AST_NODE_TYPES.MemberExpression && | ||
node.callee.property.type === AST_NODE_TYPES.Identifier && | ||
node.callee.property.name === "generateAndSend" | ||
); | ||
} | ||
|
||
function traverseAST( | ||
node: TSESTree.Node, | ||
callback: (node: TSESTree.Node) => void, | ||
) { | ||
callback(node); | ||
for (const key in node) { | ||
if (typeof node[key] === "object" && node[key] !== null) { | ||
traverseAST(node[key], callback); | ||
} | ||
|
||
if (node.type === AST_NODE_TYPES.Program) { | ||
node.body.forEach((child) => traverseAST(child, callback)); | ||
} else if ("body" in node && Array.isArray(node.body)) { | ||
node.body.forEach((child) => traverseAST(child, callback)); | ||
} else { | ||
Object.keys(node).forEach((key) => { | ||
const child = (node as any)[key]; | ||
if (typeof child === "object" && child !== null) { | ||
if (Array.isArray(child)) { | ||
child.forEach((item) => { | ||
if (typeof item === "object" && item !== null) { | ||
traverseAST(item as TSESTree.Node, callback); | ||
} | ||
}); | ||
} else { | ||
traverseAST(child as TSESTree.Node, callback); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters