Skip to content

Commit

Permalink
Replace nodeIds with actual nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBaryoPX committed Nov 12, 2024
1 parent 30aa363 commit 9d2e9d4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +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 node ids for easier access.
- 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
4 changes: 2 additions & 2 deletions src/flast.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ function extractNodesFromRoot(rootNode, opts) {
enter(node, parentNode) {
tree.push(node);
node.nodeId = nodeId++;
if (!typeMap[node.type]) typeMap[node.type] = [node.nodeId];
else typeMap[node.type].push(node.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 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
16 changes: 8 additions & 8 deletions tests/parsing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ describe('Parsing tests', () => {
static b = 1;
#c = 2;
}`;
const ast = generateFlatAST(code);
const expected = {
Program: [0],
ClassDeclaration: [1],
Identifier: [2, 5],
ClassBody: [3],
PropertyDefinition: [4, 7],
Literal: [6, 9],
PrivateIdentifier: [8],
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 ast = generateFlatAST(code);
const result = ast[0].typeMap;
assert.deepEqual(result, expected);
});
Expand Down

0 comments on commit 9d2e9d4

Please sign in to comment.