Skip to content

Commit 4301f6d

Browse files
authored
Merge pull request #7 from PerimeterX/improve/parentKey
Improve getParentKey to include parsing the name of grouped nodes
2 parents b6fc8ed + 8cfc946 commit 4301f6d

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ inside the arborist.
188188
To contribute to this project see our [contribution guide](CONTRIBUTING.md)
189189

190190
## Changes
191+
### v1.1.1
192+
- Improve getParentKey to include parsing the name of grouped nodes such as 'arguments' or 'body'.
191193
### v1.1.0
192194
- Added parentKey property.
193195
- Improved ASTNode definition (useful for intellisense).

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flast",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Flatten JS AST",
55
"main": "src/index.js",
66
"scripts": {

src/flast.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function getParentKey(parent, targetChildNodeId) {
4040
if (parent) {
4141
for (const key of Object.keys(parent)) {
4242
if (parent[key]?.nodeId === targetChildNodeId) return key;
43+
else if (Array.isArray(parent[key])) {
44+
for (const item of parent[key]) {
45+
if (item.nodeId === targetChildNodeId) return key;
46+
}
47+
}
4348
}
4449
}
4550
return null;

tests/testFlast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function testNodesStructureIntegrity() {
6464
const ast = generateFlatAST(code);
6565
const expectedBreakdown = [
6666
{nodeId: 0, type: 'Program', start: 0, end: 3, src: 'a=3', parentNode: null, parentKey: null},
67-
{nodeId: 1, type: 'ExpressionStatement', start: 0, end: 3, src: 'a=3', parentKey: null},
67+
{nodeId: 1, type: 'ExpressionStatement', start: 0, end: 3, src: 'a=3', parentKey: 'body'},
6868
{nodeId: 2, type: 'AssignmentExpression', start: 0, end: 3, src: 'a=3', operator: '=', parentKey: 'expression'},
6969
{nodeId: 3, type: 'Identifier', start: 0, end: 1, src: 'a', parentKey: 'left'},
7070
{nodeId: 4, type: 'Literal', start: 2, end: 3, src: '3', value: 3, raw: '3', parentKey: 'right'},

0 commit comments

Comments
 (0)