Skip to content

Commit 488d5b2

Browse files
committed
feat(napi/parser): add AST walker
1 parent fe80787 commit 488d5b2

File tree

15 files changed

+3946
-28
lines changed

15 files changed

+3946
-28
lines changed

.github/generated/ast_changes_watch_list.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ src:
7373
- 'napi/parser/generated/lazy/types.mjs'
7474
- 'napi/parser/generated/lazy/walk.mjs'
7575
- 'napi/parser/generated/visit/keys.mjs'
76+
- 'napi/parser/generated/visit/types.mjs'
77+
- 'napi/parser/generated/visit/visitor.d.ts'
78+
- 'napi/parser/generated/visit/walk.mjs'
7679
- 'napi/parser/src/generated/assert_layouts.rs'
7780
- 'napi/parser/src/generated/derive_estree.rs'
7881
- 'napi/parser/src/generated/raw_transfer_constants.rs'

napi/parser/README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import { Statement } from '@oxc-project/types';
3939

4040
### Visitor
4141

42-
[oxc-walker](https://www.npmjs.com/package/oxc-walker) or [estree-walker](https://www.npmjs.com/package/estree-walker) can be used.
42+
An AST visitor is provided. See example below.
4343

44-
This package exports visitor keys which can be used with any ESTree walker.
44+
This package also exports visitor keys which can be used with any other ESTree walker.
4545

4646
```js
4747
import { visitorKeys } from 'oxc-parser';
@@ -99,7 +99,7 @@ export interface EcmaScriptModule {
9999
## API
100100

101101
```javascript
102-
import { parseSync } from 'oxc-parser';
102+
import { parseSync, Visitor } from 'oxc-parser';
103103

104104
const code = 'const url: String = /* 🤨 */ import.meta.url;';
105105

@@ -117,6 +117,26 @@ console.log(result.program, result.comments);
117117

118118
// ESM information - imports, exports, `import.meta`s.
119119
console.log(result.module);
120+
121+
// Visit the AST
122+
const visitations = [];
123+
124+
const visitor = new Visitor({
125+
VariableDeclaration(decl) {
126+
visitations.push(`enter ${decl.kind}`);
127+
},
128+
'VariableDeclaration:exit'(decl) {
129+
visitations.push(`exit ${decl.kind}`);
130+
},
131+
Identifier(ident) {
132+
visitations.push(ident.name);
133+
},
134+
});
135+
136+
visitor.visit(result.program);
137+
138+
// Logs: [ 'enter const', 'url', 'String', 'import', 'meta', 'url', 'exit const' ]
139+
console.log(visitations);
120140
```
121141

122142
### Options
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Auto-generated code, DO NOT EDIT DIRECTLY!
2+
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/estree_visit.rs`.
3+
4+
// Mapping from node type name to node type ID
5+
export const NODE_TYPE_IDS_MAP = new Map([
6+
// Leaf nodes
7+
['DebuggerStatement', 0],
8+
['EmptyStatement', 1],
9+
['Literal', 2],
10+
['PrivateIdentifier', 3],
11+
['Super', 4],
12+
['TemplateElement', 5],
13+
['ThisExpression', 6],
14+
['JSXClosingFragment', 7],
15+
['JSXEmptyExpression', 8],
16+
['JSXIdentifier', 9],
17+
['JSXOpeningFragment', 10],
18+
['JSXText', 11],
19+
['TSAnyKeyword', 12],
20+
['TSBigIntKeyword', 13],
21+
['TSBooleanKeyword', 14],
22+
['TSIntrinsicKeyword', 15],
23+
['TSJSDocUnknownType', 16],
24+
['TSNeverKeyword', 17],
25+
['TSNullKeyword', 18],
26+
['TSNumberKeyword', 19],
27+
['TSObjectKeyword', 20],
28+
['TSStringKeyword', 21],
29+
['TSSymbolKeyword', 22],
30+
['TSThisType', 23],
31+
['TSUndefinedKeyword', 24],
32+
['TSUnknownKeyword', 25],
33+
['TSVoidKeyword', 26],
34+
// Non-leaf nodes
35+
['AccessorProperty', 27],
36+
['ArrayExpression', 28],
37+
['ArrayPattern', 29],
38+
['ArrowFunctionExpression', 30],
39+
['AssignmentExpression', 31],
40+
['AssignmentPattern', 32],
41+
['AwaitExpression', 33],
42+
['BinaryExpression', 34],
43+
['BlockStatement', 35],
44+
['BreakStatement', 36],
45+
['CallExpression', 37],
46+
['CatchClause', 38],
47+
['ChainExpression', 39],
48+
['ClassBody', 40],
49+
['ClassDeclaration', 41],
50+
['ClassExpression', 42],
51+
['ConditionalExpression', 43],
52+
['ContinueStatement', 44],
53+
['Decorator', 45],
54+
['DoWhileStatement', 46],
55+
['ExportAllDeclaration', 47],
56+
['ExportDefaultDeclaration', 48],
57+
['ExportNamedDeclaration', 49],
58+
['ExportSpecifier', 50],
59+
['ExpressionStatement', 51],
60+
['ForInStatement', 52],
61+
['ForOfStatement', 53],
62+
['ForStatement', 54],
63+
['FunctionDeclaration', 55],
64+
['FunctionExpression', 56],
65+
['Identifier', 57],
66+
['IfStatement', 58],
67+
['ImportAttribute', 59],
68+
['ImportDeclaration', 60],
69+
['ImportDefaultSpecifier', 61],
70+
['ImportExpression', 62],
71+
['ImportNamespaceSpecifier', 63],
72+
['ImportSpecifier', 64],
73+
['LabeledStatement', 65],
74+
['LogicalExpression', 66],
75+
['MemberExpression', 67],
76+
['MetaProperty', 68],
77+
['MethodDefinition', 69],
78+
['NewExpression', 70],
79+
['ObjectExpression', 71],
80+
['ObjectPattern', 72],
81+
['ParenthesizedExpression', 73],
82+
['Program', 74],
83+
['Property', 75],
84+
['PropertyDefinition', 76],
85+
['RestElement', 77],
86+
['ReturnStatement', 78],
87+
['SequenceExpression', 79],
88+
['SpreadElement', 80],
89+
['StaticBlock', 81],
90+
['SwitchCase', 82],
91+
['SwitchStatement', 83],
92+
['TaggedTemplateExpression', 84],
93+
['TemplateLiteral', 85],
94+
['ThrowStatement', 86],
95+
['TryStatement', 87],
96+
['UnaryExpression', 88],
97+
['UpdateExpression', 89],
98+
['V8IntrinsicExpression', 90],
99+
['VariableDeclaration', 91],
100+
['VariableDeclarator', 92],
101+
['WhileStatement', 93],
102+
['WithStatement', 94],
103+
['YieldExpression', 95],
104+
['JSXAttribute', 96],
105+
['JSXClosingElement', 97],
106+
['JSXElement', 98],
107+
['JSXExpressionContainer', 99],
108+
['JSXFragment', 100],
109+
['JSXMemberExpression', 101],
110+
['JSXNamespacedName', 102],
111+
['JSXOpeningElement', 103],
112+
['JSXSpreadAttribute', 104],
113+
['JSXSpreadChild', 105],
114+
['TSAbstractAccessorProperty', 106],
115+
['TSAbstractMethodDefinition', 107],
116+
['TSAbstractPropertyDefinition', 108],
117+
['TSArrayType', 109],
118+
['TSAsExpression', 110],
119+
['TSCallSignatureDeclaration', 111],
120+
['TSClassImplements', 112],
121+
['TSConditionalType', 113],
122+
['TSConstructSignatureDeclaration', 114],
123+
['TSConstructorType', 115],
124+
['TSDeclareFunction', 116],
125+
['TSEmptyBodyFunctionExpression', 117],
126+
['TSEnumBody', 118],
127+
['TSEnumDeclaration', 119],
128+
['TSEnumMember', 120],
129+
['TSExportAssignment', 121],
130+
['TSExternalModuleReference', 122],
131+
['TSFunctionType', 123],
132+
['TSImportEqualsDeclaration', 124],
133+
['TSImportType', 125],
134+
['TSIndexSignature', 126],
135+
['TSIndexedAccessType', 127],
136+
['TSInferType', 128],
137+
['TSInstantiationExpression', 129],
138+
['TSInterfaceBody', 130],
139+
['TSInterfaceDeclaration', 131],
140+
['TSInterfaceHeritage', 132],
141+
['TSIntersectionType', 133],
142+
['TSJSDocNonNullableType', 134],
143+
['TSJSDocNullableType', 135],
144+
['TSLiteralType', 136],
145+
['TSMappedType', 137],
146+
['TSMethodSignature', 138],
147+
['TSModuleBlock', 139],
148+
['TSModuleDeclaration', 140],
149+
['TSNamedTupleMember', 141],
150+
['TSNamespaceExportDeclaration', 142],
151+
['TSNonNullExpression', 143],
152+
['TSOptionalType', 144],
153+
['TSParameterProperty', 145],
154+
['TSParenthesizedType', 146],
155+
['TSPropertySignature', 147],
156+
['TSQualifiedName', 148],
157+
['TSRestType', 149],
158+
['TSSatisfiesExpression', 150],
159+
['TSTemplateLiteralType', 151],
160+
['TSTupleType', 152],
161+
['TSTypeAliasDeclaration', 153],
162+
['TSTypeAnnotation', 154],
163+
['TSTypeAssertion', 155],
164+
['TSTypeLiteral', 156],
165+
['TSTypeOperator', 157],
166+
['TSTypeParameter', 158],
167+
['TSTypeParameterDeclaration', 159],
168+
['TSTypeParameterInstantiation', 160],
169+
['TSTypePredicate', 161],
170+
['TSTypeQuery', 162],
171+
['TSTypeReference', 163],
172+
['TSUnionType', 164],
173+
]);
174+
175+
export const NODE_TYPES_COUNT = 165;
176+
export const LEAF_NODE_TYPES_COUNT = 27;

0 commit comments

Comments
 (0)