Skip to content

Commit f68cffb

Browse files
committed
feat(napi/parser): export visitor keys
1 parent ad14a41 commit f68cffb

File tree

15 files changed

+385
-3
lines changed

15 files changed

+385
-3
lines changed

.github/generated/ast_changes_watch_list.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ src:
7272
- 'napi/parser/generated/lazy/constructors.mjs'
7373
- 'napi/parser/generated/lazy/types.mjs'
7474
- 'napi/parser/generated/lazy/walk.mjs'
75+
- 'napi/parser/generated/visit/keys.mjs'
7576
- 'napi/parser/src/generated/assert_layouts.rs'
7677
- 'napi/parser/src/generated/derive_estree.rs'
7778
- 'napi/parser/src/generated/raw_transfer_constants.rs'

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ jobs:
353353
cache-key: ast_changes
354354
save-cache: ${{ github.ref_name == 'main' }}
355355

356+
- uses: oxc-project/setup-node@fdbf0dfd334c4e6d56ceeb77d91c76339c2a0885 # v1.0.4
357+
if: steps.filter.outputs.src == 'true'
358+
356359
- name: Restore dprint plugin cache
357360
if: steps.filter.outputs.src == 'true'
358361
uses: actions/cache/restore@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4

Cargo.lock

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

napi/parser/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ import { Statement } from '@oxc-project/types';
4141

4242
[oxc-walker](https://www.npmjs.com/package/oxc-walker) or [estree-walker](https://www.npmjs.com/package/estree-walker) can be used.
4343

44+
This package exports visitor keys which can be used with any ESTree walker.
45+
46+
```js
47+
import { visitorKeys } from 'oxc-parser';
48+
```
49+
4450
### Fast Mode
4551

4652
By default, Oxc parser does not produce semantic errors where symbols and scopes are needed.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
export default {
5+
// Leaf nodes
6+
DebuggerStatement: [],
7+
EmptyStatement: [],
8+
Literal: [],
9+
PrivateIdentifier: [],
10+
Super: [],
11+
TemplateElement: [],
12+
ThisExpression: [],
13+
JSXClosingFragment: [],
14+
JSXEmptyExpression: [],
15+
JSXIdentifier: [],
16+
JSXOpeningFragment: [],
17+
JSXText: [],
18+
TSAnyKeyword: [],
19+
TSBigIntKeyword: [],
20+
TSBooleanKeyword: [],
21+
TSIntrinsicKeyword: [],
22+
TSJSDocUnknownType: [],
23+
TSNeverKeyword: [],
24+
TSNullKeyword: [],
25+
TSNumberKeyword: [],
26+
TSObjectKeyword: [],
27+
TSStringKeyword: [],
28+
TSSymbolKeyword: [],
29+
TSThisType: [],
30+
TSUndefinedKeyword: [],
31+
TSUnknownKeyword: [],
32+
TSVoidKeyword: [],
33+
// Non-leaf nodes
34+
AccessorProperty: ['decorators', 'key', 'typeAnnotation', 'value'],
35+
ArrayExpression: ['elements'],
36+
ArrayPattern: ['decorators', 'elements', 'typeAnnotation'],
37+
ArrowFunctionExpression: ['typeParameters', 'params', 'returnType', 'body'],
38+
AssignmentExpression: ['left', 'right'],
39+
AssignmentPattern: ['decorators', 'left', 'right', 'typeAnnotation'],
40+
AwaitExpression: ['argument'],
41+
BinaryExpression: ['left', 'right'],
42+
BlockStatement: ['body'],
43+
BreakStatement: ['label'],
44+
CallExpression: ['callee', 'typeArguments', 'arguments'],
45+
CatchClause: ['param', 'body'],
46+
ChainExpression: ['expression'],
47+
ClassBody: ['body'],
48+
ClassDeclaration: ['decorators', 'id', 'typeParameters', 'superClass', 'superTypeArguments', 'implements', 'body'],
49+
ClassExpression: ['decorators', 'id', 'typeParameters', 'superClass', 'superTypeArguments', 'implements', 'body'],
50+
ConditionalExpression: ['test', 'consequent', 'alternate'],
51+
ContinueStatement: ['label'],
52+
Decorator: ['expression'],
53+
DoWhileStatement: ['body', 'test'],
54+
ExportAllDeclaration: ['exported', 'source', 'attributes'],
55+
ExportDefaultDeclaration: ['declaration'],
56+
ExportNamedDeclaration: ['declaration', 'specifiers', 'source', 'attributes'],
57+
ExportSpecifier: ['local', 'exported'],
58+
ExpressionStatement: ['expression'],
59+
ForInStatement: ['left', 'right', 'body'],
60+
ForOfStatement: ['left', 'right', 'body'],
61+
ForStatement: ['init', 'test', 'update', 'body'],
62+
FunctionDeclaration: ['id', 'typeParameters', 'params', 'returnType', 'body'],
63+
FunctionExpression: ['id', 'typeParameters', 'params', 'returnType', 'body'],
64+
Identifier: ['decorators', 'typeAnnotation'],
65+
IfStatement: ['test', 'consequent', 'alternate'],
66+
ImportAttribute: ['key', 'value'],
67+
ImportDeclaration: ['specifiers', 'source', 'attributes'],
68+
ImportDefaultSpecifier: ['local'],
69+
ImportExpression: ['source', 'options'],
70+
ImportNamespaceSpecifier: ['local'],
71+
ImportSpecifier: ['imported', 'local'],
72+
LabeledStatement: ['label', 'body'],
73+
LogicalExpression: ['left', 'right'],
74+
MemberExpression: ['object', 'property'],
75+
MetaProperty: ['meta', 'property'],
76+
MethodDefinition: ['decorators', 'key', 'value'],
77+
NewExpression: ['callee', 'typeArguments', 'arguments'],
78+
ObjectExpression: ['properties'],
79+
ObjectPattern: ['decorators', 'properties', 'typeAnnotation'],
80+
ParenthesizedExpression: ['expression'],
81+
Program: ['body'],
82+
Property: ['key', 'value'],
83+
PropertyDefinition: ['decorators', 'key', 'typeAnnotation', 'value'],
84+
RestElement: ['decorators', 'argument', 'typeAnnotation'],
85+
ReturnStatement: ['argument'],
86+
SequenceExpression: ['expressions'],
87+
SpreadElement: ['argument'],
88+
StaticBlock: ['body'],
89+
SwitchCase: ['test', 'consequent'],
90+
SwitchStatement: ['discriminant', 'cases'],
91+
TaggedTemplateExpression: ['tag', 'typeArguments', 'quasi'],
92+
TemplateLiteral: ['quasis', 'expressions'],
93+
ThrowStatement: ['argument'],
94+
TryStatement: ['block', 'handler', 'finalizer'],
95+
UnaryExpression: ['argument'],
96+
UpdateExpression: ['argument'],
97+
V8IntrinsicExpression: ['name', 'arguments'],
98+
VariableDeclaration: ['declarations'],
99+
VariableDeclarator: ['id', 'init'],
100+
WhileStatement: ['test', 'body'],
101+
WithStatement: ['object', 'body'],
102+
YieldExpression: ['argument'],
103+
JSXAttribute: ['name', 'value'],
104+
JSXClosingElement: ['name'],
105+
JSXElement: ['openingElement', 'children', 'closingElement'],
106+
JSXExpressionContainer: ['expression'],
107+
JSXFragment: ['openingFragment', 'children', 'closingFragment'],
108+
JSXMemberExpression: ['object', 'property'],
109+
JSXNamespacedName: ['namespace', 'name'],
110+
JSXOpeningElement: ['name', 'typeArguments', 'attributes'],
111+
JSXSpreadAttribute: ['argument'],
112+
JSXSpreadChild: ['expression'],
113+
TSAbstractAccessorProperty: ['decorators', 'key', 'typeAnnotation'],
114+
TSAbstractMethodDefinition: ['key', 'value'],
115+
TSAbstractPropertyDefinition: ['decorators', 'key', 'typeAnnotation'],
116+
TSArrayType: ['elementType'],
117+
TSAsExpression: ['expression', 'typeAnnotation'],
118+
TSCallSignatureDeclaration: ['typeParameters', 'params', 'returnType'],
119+
TSClassImplements: ['expression', 'typeArguments'],
120+
TSConditionalType: ['checkType', 'extendsType', 'trueType', 'falseType'],
121+
TSConstructSignatureDeclaration: ['typeParameters', 'params', 'returnType'],
122+
TSConstructorType: ['typeParameters', 'params', 'returnType'],
123+
TSDeclareFunction: ['id', 'typeParameters', 'params', 'returnType', 'body'],
124+
TSEmptyBodyFunctionExpression: ['id', 'typeParameters', 'params', 'returnType'],
125+
TSEnumBody: ['members'],
126+
TSEnumDeclaration: ['id', 'body'],
127+
TSEnumMember: ['id', 'initializer'],
128+
TSExportAssignment: ['expression'],
129+
TSExternalModuleReference: ['expression'],
130+
TSFunctionType: ['typeParameters', 'params', 'returnType'],
131+
TSImportEqualsDeclaration: ['id', 'moduleReference'],
132+
TSImportType: ['argument', 'options', 'qualifier', 'typeArguments'],
133+
TSIndexSignature: ['parameters', 'typeAnnotation'],
134+
TSIndexedAccessType: ['objectType', 'indexType'],
135+
TSInferType: ['typeParameter'],
136+
TSInstantiationExpression: ['expression', 'typeArguments'],
137+
TSInterfaceBody: ['body'],
138+
TSInterfaceDeclaration: ['id', 'typeParameters', 'extends', 'body'],
139+
TSInterfaceHeritage: ['expression', 'typeArguments'],
140+
TSIntersectionType: ['types'],
141+
TSJSDocNonNullableType: ['typeAnnotation'],
142+
TSJSDocNullableType: ['typeAnnotation'],
143+
TSLiteralType: ['literal'],
144+
TSMappedType: ['key', 'constraint', 'nameType', 'typeAnnotation'],
145+
TSMethodSignature: ['key', 'typeParameters', 'params', 'returnType'],
146+
TSModuleBlock: ['body'],
147+
TSModuleDeclaration: ['id', 'body'],
148+
TSNamedTupleMember: ['label', 'elementType'],
149+
TSNamespaceExportDeclaration: ['id'],
150+
TSNonNullExpression: ['expression'],
151+
TSOptionalType: ['typeAnnotation'],
152+
TSParameterProperty: ['decorators', 'parameter'],
153+
TSParenthesizedType: ['typeAnnotation'],
154+
TSPropertySignature: ['key', 'typeAnnotation'],
155+
TSQualifiedName: ['left', 'right'],
156+
TSRestType: ['typeAnnotation'],
157+
TSSatisfiesExpression: ['expression', 'typeAnnotation'],
158+
TSTemplateLiteralType: ['quasis', 'types'],
159+
TSTupleType: ['elementTypes'],
160+
TSTypeAliasDeclaration: ['id', 'typeParameters', 'typeAnnotation'],
161+
TSTypeAnnotation: ['typeAnnotation'],
162+
TSTypeAssertion: ['typeAnnotation', 'expression'],
163+
TSTypeLiteral: ['members'],
164+
TSTypeOperator: ['typeAnnotation'],
165+
TSTypeParameter: ['name', 'constraint', 'default'],
166+
TSTypeParameterDeclaration: ['params'],
167+
TSTypeParameterInstantiation: ['params'],
168+
TSTypePredicate: ['parameterName', 'typeAnnotation'],
169+
TSTypeQuery: ['exprName', 'typeArguments'],
170+
TSTypeReference: ['typeName', 'typeArguments'],
171+
TSUnionType: ['types'],
172+
};

napi/parser/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"devDependencies": {
5252
"@codspeed/vitest-plugin": "^4.0.0",
5353
"@napi-rs/wasm-runtime": "catalog:",
54+
"@typescript-eslint/visitor-keys": "^8.44.0",
5455
"@vitest/browser": "3.2.4",
5556
"esbuild": "^0.25.0",
5657
"playwright": "^1.51.0",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { visitorKeys } from '@typescript-eslint/visitor-keys';
2+
3+
const keys = Object.entries(visitorKeys).map(([name, keys]) => ({ name, keys }));
4+
console.log(JSON.stringify(keys));

napi/parser/src-js/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { createRequire } from 'node:module';
22
import { parseAsync as parseAsyncBinding, parseSync as parseSyncBinding } from './bindings.mjs';
33
import { wrap } from './wrap.mjs';
44

5+
export { default as visitorKeys } from '../generated/visit/keys.mjs';
6+
57
export {
68
ExportExportNameKind,
79
ExportImportNameKind,

napi/parser/test/parse.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Worker } from 'node:worker_threads';
22
import { describe, expect, it, test } from 'vitest';
33

4-
import { parseAsync, parseSync } from '../src-js/index.mjs';
4+
import { parseAsync, parseSync, visitorKeys } from '../src-js/index.mjs';
55
import type {
66
ExpressionStatement,
77
ParserOptions,
@@ -874,6 +874,14 @@ describe('error', () => {
874874
});
875875
});
876876

877+
it('visitor keys', () => {
878+
expect(visitorKeys.Literal).toEqual([]);
879+
expect(visitorKeys.VariableDeclaration).toEqual(['declarations']);
880+
expect(visitorKeys.ObjectPattern).toEqual(['decorators', 'properties', 'typeAnnotation']);
881+
expect(visitorKeys.ParenthesizedExpression).toEqual(['expression']);
882+
expect(visitorKeys.V8IntrinsicExpression).toEqual(['name', 'arguments']);
883+
});
884+
877885
describe('worker', () => {
878886
it('should run', async () => {
879887
const code = await new Promise((resolve, reject) => {

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)