Skip to content
Merged
Show file tree
Hide file tree
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
37 changes: 19 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flast",
"version": "2.2.1",
"version": "2.2.2",
"description": "Flatten JS AST",
"main": "src/index.js",
"type": "module",
Expand Down Expand Up @@ -31,7 +31,7 @@
"espree": "^10.3.0"
},
"devDependencies": {
"eslint": "^9.16.0",
"eslint": "^9.18.0",
"husky": "^9.1.7"
}
}
31 changes: 4 additions & 27 deletions src/flast.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ function extractNodesFromRoot(rootNode, opts) {
const node = stack.shift();
if (node.nodeId) continue;
node.childNodes = node.childNodes || [];
const childrenLoc = {}; // Store the location of child nodes to sort them by order
const childrenLoc = {}; // Store the location of child nodes to sort them by order
node.parentKey = node.parentKey || ''; // Make sure parentKey exists
// Iterate over all keys of the node to find child nodes
const keys = Object.keys(node);
Expand All @@ -134,6 +134,7 @@ function extractNodesFromRoot(rootNode, opts) {
if (Array.isArray(content)) {
for (let j = 0; j < content.length; j++) {
const childNode = content[j];
if (!childNode) continue;
childNode.parentNode = node;
childNode.parentKey = key;
childrenLoc[childNode.start] = childNode;
Expand All @@ -154,7 +155,7 @@ function extractNodesFromRoot(rootNode, opts) {
typeMap[node.type] = typeMap[node.type] || [];
typeMap[node.type].push(node);
if (opts.detailed) {
node.scope = matchScopeToNode(node, scopes);
node.scope = scopes[node.scopeId] || node.parentNode?.scope;
node.lineage = [...node.parentNode?.lineage || []];
if (!node.lineage.includes(node.scope.scopeId)) {
node.lineage.push(node.scope.scopeId);
Expand Down Expand Up @@ -255,7 +256,7 @@ function getAllScopes(rootNode) {
const stack = [globalScope];
while (stack.length) {
const scope = stack.shift();
if (scope.type !== 'module') {
if (scope.type !== 'module' && !scope.type.includes('-name')) {
scope.scopeId = scopeId++;
scope.block.scopeId = scope.scopeId;
allScopes[scope.scopeId] = allScopes[scope.scopeId] || scope;
Expand All @@ -279,30 +280,6 @@ function getAllScopes(rootNode) {
return rootNode.allScopes = allScopes;
}

/**
* @param {ASTNode} node
* @param {{number: ASTScope}} allScopes
* @return {ASTScope}
*/
function matchScopeToNode(node, allScopes) {
let scope = node.scope;
if (!scope) {
let scopeBlock = node;
while (scopeBlock && scopeBlock.scopeId === undefined) {
scopeBlock = scopeBlock.parentNode;
}
if (scopeBlock) {
scope = allScopes[scopeBlock.scopeId];
}
}
if (scope) {
if (scope.type.includes('-name') && scope?.childScopes?.length === 1) scope = scope.childScopes[0];
if (node === scope.block && scope.upper) scope = scope.upper;
if (scope.type === 'module') scope = scope.upper;
} else scope = allScopes[0]; // Global scope - this should never be reached
return scope;
}

export {
extractNodesFromRoot,
generateCode,
Expand Down
5 changes: 5 additions & 0 deletions tests/parsing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ describe('Parsing tests', () => {
assert.deepEqual(n.lineage, extractedLineage);
});
});
it(`Verify null childNodes are correctly parsed`, () => {
const code = `[,,,].join('-');`;
const ast = generateFlatAST(code);
assert.notEqual(ast, [1]);
});
});
Loading