diff --git a/src/flast.js b/src/flast.js index dc16df2..45eca1d 100644 --- a/src/flast.js +++ b/src/flast.js @@ -233,10 +233,11 @@ function mapIdentifierRelations(node, scopeVarMaps) { let decls = []; if (variable) { decls = variable.identifiers || []; - } else if (scope && scope.references) { - for (let i = 0; i < scope.references.length; i++) { - if (scope.references[i].identifier.name === node.name) { - decls = scope.references[i].resolved?.identifiers || []; + } else if (scope && (scope.references.length || scope.variableScope?.references.length)) { + const references = scope.references?.length ? scope.references : scope.variableScope.references; + for (let i = 0; i < references.length; i++) { + if (references[i].identifier.name === node.name) { + decls = references[i].resolved?.identifiers || []; break; } } diff --git a/tests/parsing.test.js b/tests/parsing.test.js index 912cee9..30972d1 100644 --- a/tests/parsing.test.js +++ b/tests/parsing.test.js @@ -109,4 +109,11 @@ describe('Parsing tests', () => { const ast = generateFlatAST(code); assert.notEqual(ast, [1]); }); + it(`Verify all identifiers are referenced correctly`, () => { + const code = `let a = 1; switch(a) {}`; + const ast = generateFlatAST(code); + ast.filter(n => n.type === 'Identifier').forEach(n => { + assert.ok(n.references?.length || n.declNode, `Identifier '${n.name}' (#${n.nodeId}) is not referenced`); + }); + }); }); \ No newline at end of file