Skip to content
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

TypeMap #32

Merged
merged 4 commits into from
Nov 12, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ npm install
- Tracks scope and connects each declaration to its references.
See [eslint-scope](https://github.com/eslint/eslint-scope) for more info on the scopes used.
- Adds a unique id to each node to simplify tracking and understanding relations between nodes.
- Maps the types to the nodes for easier access.
- <u>Arborist</u> - marks nodes for replacement or deletion and applies all changes in a single iteration over the tree.

### flAST Data Structure
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flast",
"version": "2.0.3",
"version": "2.1.0",
"description": "Flatten JS AST",
"main": "src/index.js",
"type": "module",
Expand All @@ -27,12 +27,12 @@
"homepage": "https://github.com/PerimeterX/flast#readme",
"dependencies": {
"escodegen": "npm:@javascript-obfuscator/escodegen",
"eslint-scope": "^8.1.0",
"eslint-scope": "^8.2.0",
"espree": "^10.3.0",
"estraverse": "^5.3.0"
},
"devDependencies": {
"eslint": "^9.12.0",
"eslint": "^9.14.0",
"husky": "^9.1.6"
}
}
6 changes: 5 additions & 1 deletion src/flast.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function parseCode(inputCode, opts = {}) {

const excludedParentKeys = [
'type', 'start', 'end', 'range', 'sourceType', 'comments', 'srcClosure', 'nodeId',
'childNodes', 'parentNode', 'parentKey', 'scope',
'childNodes', 'parentNode', 'parentKey', 'scope', 'typeMap', 'lineage', 'allScopes',
];

/**
Expand Down Expand Up @@ -123,6 +123,7 @@ function extractNodesFromRoot(rootNode, opts) {
opts = { ...generateFlatASTDefaultOptions, ...opts };
const tree = [];
let nodeId = 0;
const typeMap = {};

// noinspection JSUnusedGlobalSymbols
estraverse.traverse(rootNode, {
Expand All @@ -133,6 +134,8 @@ function extractNodesFromRoot(rootNode, opts) {
enter(node, parentNode) {
tree.push(node);
node.nodeId = nodeId++;
if (!typeMap[node.type]) typeMap[node.type] = [node];
else typeMap[node.type].push(node);
node.childNodes = [];
node.parentNode = parentNode;
node.parentKey = parentNode ? getParentKey(node) : '';
Expand All @@ -146,6 +149,7 @@ function extractNodesFromRoot(rootNode, opts) {
});
}
});
if (tree?.length) tree[0].typeMap = typeMap;
return tree;
}

Expand Down
1 change: 1 addition & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import {Scope} from 'eslint-scope';
* @property {ASTNode} [test]
* @property {ASTNode} [tokens]
* @property {Object[]} [trailingComments]
* @property {Object} [typeMap]
* @property {ASTNode} [update]
* @property {ASTNode|string|number|boolean} [value]
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/parsing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ describe('Parsing tests', () => {
const result = generateCode(ast[0]);
assert.strictEqual(result, expected);
});
it(`Verify the type map is generated accurately`, () => {
const code = `class a {
static b = 1;
#c = 2;
}`;
const ast = generateFlatAST(code);
const expected = {
Program: [ast[0]],
ClassDeclaration: [ast[1]],
Identifier: [ast[2], ast[5]],
ClassBody: [ast[3]],
PropertyDefinition: [ast[4], ast[7]],
Literal: [ast[6], ast[9]],
PrivateIdentifier: [ast[8]],
};
const result = ast[0].typeMap;
assert.deepEqual(result, expected);
});
});
Loading