Skip to content

Commit c5ccc0f

Browse files
authored
Fix childNode null Issue (#36)
* Fix issue where childNode is null * Split matchScopeToNode's functionality into getAllScopes and extractNodesFromRoot and remove redundant function * Add a test for null childNodes fix * Update eslint * 2.2.2
1 parent 4361553 commit c5ccc0f

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

package-lock.json

Lines changed: 19 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flast",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "Flatten JS AST",
55
"main": "src/index.js",
66
"type": "module",
@@ -31,7 +31,7 @@
3131
"espree": "^10.3.0"
3232
},
3333
"devDependencies": {
34-
"eslint": "^9.16.0",
34+
"eslint": "^9.18.0",
3535
"husky": "^9.1.7"
3636
}
3737
}

src/flast.js

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function extractNodesFromRoot(rootNode, opts) {
120120
const node = stack.shift();
121121
if (node.nodeId) continue;
122122
node.childNodes = node.childNodes || [];
123-
const childrenLoc = {}; // Store the location of child nodes to sort them by order
123+
const childrenLoc = {}; // Store the location of child nodes to sort them by order
124124
node.parentKey = node.parentKey || ''; // Make sure parentKey exists
125125
// Iterate over all keys of the node to find child nodes
126126
const keys = Object.keys(node);
@@ -134,6 +134,7 @@ function extractNodesFromRoot(rootNode, opts) {
134134
if (Array.isArray(content)) {
135135
for (let j = 0; j < content.length; j++) {
136136
const childNode = content[j];
137+
if (!childNode) continue;
137138
childNode.parentNode = node;
138139
childNode.parentKey = key;
139140
childrenLoc[childNode.start] = childNode;
@@ -154,7 +155,7 @@ function extractNodesFromRoot(rootNode, opts) {
154155
typeMap[node.type] = typeMap[node.type] || [];
155156
typeMap[node.type].push(node);
156157
if (opts.detailed) {
157-
node.scope = matchScopeToNode(node, scopes);
158+
node.scope = scopes[node.scopeId] || node.parentNode?.scope;
158159
node.lineage = [...node.parentNode?.lineage || []];
159160
if (!node.lineage.includes(node.scope.scopeId)) {
160161
node.lineage.push(node.scope.scopeId);
@@ -255,7 +256,7 @@ function getAllScopes(rootNode) {
255256
const stack = [globalScope];
256257
while (stack.length) {
257258
const scope = stack.shift();
258-
if (scope.type !== 'module') {
259+
if (scope.type !== 'module' && !scope.type.includes('-name')) {
259260
scope.scopeId = scopeId++;
260261
scope.block.scopeId = scope.scopeId;
261262
allScopes[scope.scopeId] = allScopes[scope.scopeId] || scope;
@@ -279,30 +280,6 @@ function getAllScopes(rootNode) {
279280
return rootNode.allScopes = allScopes;
280281
}
281282

282-
/**
283-
* @param {ASTNode} node
284-
* @param {{number: ASTScope}} allScopes
285-
* @return {ASTScope}
286-
*/
287-
function matchScopeToNode(node, allScopes) {
288-
let scope = node.scope;
289-
if (!scope) {
290-
let scopeBlock = node;
291-
while (scopeBlock && scopeBlock.scopeId === undefined) {
292-
scopeBlock = scopeBlock.parentNode;
293-
}
294-
if (scopeBlock) {
295-
scope = allScopes[scopeBlock.scopeId];
296-
}
297-
}
298-
if (scope) {
299-
if (scope.type.includes('-name') && scope?.childScopes?.length === 1) scope = scope.childScopes[0];
300-
if (node === scope.block && scope.upper) scope = scope.upper;
301-
if (scope.type === 'module') scope = scope.upper;
302-
} else scope = allScopes[0]; // Global scope - this should never be reached
303-
return scope;
304-
}
305-
306283
export {
307284
extractNodesFromRoot,
308285
generateCode,

tests/parsing.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,9 @@ describe('Parsing tests', () => {
104104
assert.deepEqual(n.lineage, extractedLineage);
105105
});
106106
});
107+
it(`Verify null childNodes are correctly parsed`, () => {
108+
const code = `[,,,].join('-');`;
109+
const ast = generateFlatAST(code);
110+
assert.notEqual(ast, [1]);
111+
});
107112
});

0 commit comments

Comments
 (0)