Skip to content

Commit 77daf21

Browse files
authored
fix: vardef parsing (#300)
1 parent 440faf7 commit 77daf21

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

ui/src/lib/parser.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export function analyzeCode(code: string): CodeAnalysisResult {
149149
const function_def = "(function_definition (identifier) @function)";
150150

151151
const vardef = `
152-
(expression_statement (assignment (identifier) @vardef))
153-
(expression_statement (assignment (pattern_list (identifier) @vardef)))
152+
(module (expression_statement (assignment (identifier) @vardef)))
153+
(module (expression_statement (assignment (pattern_list (identifier) @vardef))))
154154
`;
155155

156156
let query_func = parser.getLanguage().query(`
@@ -174,29 +174,23 @@ export function analyzeCode(code: string): CodeAnalysisResult {
174174
${vardef}
175175
]
176176
`);
177-
// Do not parse variable def/use inside a function definition.
178-
// FIXME there might be duplicate.
179-
// let index2annotation: Record<number, Annotation> = {};
180-
// annotations = annotations.concat(Object.values(index2annotation));
181-
tree.rootNode.children
182-
.filter(({ type }) => type !== "function_definition")
183-
.forEach((child) => {
184-
query_var.matches(child).forEach((match) => {
177+
178+
query_var.matches(tree.rootNode).forEach((match) => {
185179
let node = match.captures[0].node;
186180
annotations.push({
187181
name: node.text, // the name of the function or variable
188-
type: "function",
182+
type: "vardef",
189183
startIndex: node.startIndex,
190184
endIndex: node.endIndex,
191185
startPosition: node.startPosition,
192186
endPosition: node.endPosition,
193187
});
194188
});
195-
});
196189

190+
// Do the compilation: unbound variable analysis.
197191
let { unbound, errors } = compileModule(tree.rootNode);
198192
if (errors.length > 0) {
199-
console.log("ERROR in compileModule", errors);
193+
console.warn("ERROR in compileModule", errors);
200194
}
201195
unbound
202196
.filter((node) => !keywords.has(node.text))

0 commit comments

Comments
 (0)