Skip to content

fix: vardef parsing #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions ui/src/lib/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ export function analyzeCode(code: string): CodeAnalysisResult {
const function_def = "(function_definition (identifier) @function)";

const vardef = `
(expression_statement (assignment (identifier) @vardef))
(expression_statement (assignment (pattern_list (identifier) @vardef)))
(module (expression_statement (assignment (identifier) @vardef)))
(module (expression_statement (assignment (pattern_list (identifier) @vardef))))
`;

let query_func = parser.getLanguage().query(`
Expand All @@ -174,29 +174,23 @@ export function analyzeCode(code: string): CodeAnalysisResult {
${vardef}
]
`);
// Do not parse variable def/use inside a function definition.
// FIXME there might be duplicate.
// let index2annotation: Record<number, Annotation> = {};
// annotations = annotations.concat(Object.values(index2annotation));
tree.rootNode.children
.filter(({ type }) => type !== "function_definition")
.forEach((child) => {
query_var.matches(child).forEach((match) => {

query_var.matches(tree.rootNode).forEach((match) => {
let node = match.captures[0].node;
annotations.push({
name: node.text, // the name of the function or variable
type: "function",
type: "vardef",
startIndex: node.startIndex,
endIndex: node.endIndex,
startPosition: node.startPosition,
endPosition: node.endPosition,
});
});
});

// Do the compilation: unbound variable analysis.
let { unbound, errors } = compileModule(tree.rootNode);
if (errors.length > 0) {
console.log("ERROR in compileModule", errors);
console.warn("ERROR in compileModule", errors);
}
unbound
.filter((node) => !keywords.has(node.text))
Expand Down