Skip to content

Commit b2f7e1b

Browse files
authored
TypeMap (#32)
* Update dependencies * Add the typemap feature * 2.1.0
1 parent 09a1bf8 commit b2f7e1b

File tree

6 files changed

+38
-14
lines changed

6 files changed

+38
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ npm install
4141
- Tracks scope and connects each declaration to its references.
4242
See [eslint-scope](https://github.com/eslint/eslint-scope) for more info on the scopes used.
4343
- Adds a unique id to each node to simplify tracking and understanding relations between nodes.
44+
- Maps the types to the nodes for easier access.
4445
- <u>Arborist</u> - marks nodes for replacement or deletion and applies all changes in a single iteration over the tree.
4546

4647
### flAST Data Structure

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flast",
3-
"version": "2.0.3",
3+
"version": "2.1.0",
44
"description": "Flatten JS AST",
55
"main": "src/index.js",
66
"type": "module",
@@ -27,12 +27,12 @@
2727
"homepage": "https://github.com/PerimeterX/flast#readme",
2828
"dependencies": {
2929
"escodegen": "npm:@javascript-obfuscator/escodegen",
30-
"eslint-scope": "^8.1.0",
30+
"eslint-scope": "^8.2.0",
3131
"espree": "^10.3.0",
3232
"estraverse": "^5.3.0"
3333
},
3434
"devDependencies": {
35-
"eslint": "^9.12.0",
35+
"eslint": "^9.14.0",
3636
"husky": "^9.1.6"
3737
}
3838
}

src/flast.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function parseCode(inputCode, opts = {}) {
1919

2020
const excludedParentKeys = [
2121
'type', 'start', 'end', 'range', 'sourceType', 'comments', 'srcClosure', 'nodeId',
22-
'childNodes', 'parentNode', 'parentKey', 'scope',
22+
'childNodes', 'parentNode', 'parentKey', 'scope', 'typeMap', 'lineage', 'allScopes',
2323
];
2424

2525
/**
@@ -123,6 +123,7 @@ function extractNodesFromRoot(rootNode, opts) {
123123
opts = { ...generateFlatASTDefaultOptions, ...opts };
124124
const tree = [];
125125
let nodeId = 0;
126+
const typeMap = {};
126127

127128
// noinspection JSUnusedGlobalSymbols
128129
estraverse.traverse(rootNode, {
@@ -133,6 +134,8 @@ function extractNodesFromRoot(rootNode, opts) {
133134
enter(node, parentNode) {
134135
tree.push(node);
135136
node.nodeId = nodeId++;
137+
if (!typeMap[node.type]) typeMap[node.type] = [node];
138+
else typeMap[node.type].push(node);
136139
node.childNodes = [];
137140
node.parentNode = parentNode;
138141
node.parentKey = parentNode ? getParentKey(node) : '';
@@ -146,6 +149,7 @@ function extractNodesFromRoot(rootNode, opts) {
146149
});
147150
}
148151
});
152+
if (tree?.length) tree[0].typeMap = typeMap;
149153
return tree;
150154
}
151155

src/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {Scope} from 'eslint-scope';
7373
* @property {ASTNode} [test]
7474
* @property {ASTNode} [tokens]
7575
* @property {Object[]} [trailingComments]
76+
* @property {Object} [typeMap]
7677
* @property {ASTNode} [update]
7778
* @property {ASTNode|string|number|boolean} [value]
7879
*/

tests/parsing.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@ describe('Parsing tests', () => {
5555
const result = generateCode(ast[0]);
5656
assert.strictEqual(result, expected);
5757
});
58+
it(`Verify the type map is generated accurately`, () => {
59+
const code = `class a {
60+
static b = 1;
61+
#c = 2;
62+
}`;
63+
const ast = generateFlatAST(code);
64+
const expected = {
65+
Program: [ast[0]],
66+
ClassDeclaration: [ast[1]],
67+
Identifier: [ast[2], ast[5]],
68+
ClassBody: [ast[3]],
69+
PropertyDefinition: [ast[4], ast[7]],
70+
Literal: [ast[6], ast[9]],
71+
PrivateIdentifier: [ast[8]],
72+
};
73+
const result = ast[0].typeMap;
74+
assert.deepEqual(result, expected);
75+
});
5876
});

0 commit comments

Comments
 (0)