Skip to content

Commit 60ae4c7

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

File tree

15 files changed

+383
-3
lines changed

15 files changed

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

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)