Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/generated/ast_changes_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ src:
- 'napi/parser/generated/lazy/types.mjs'
- 'napi/parser/generated/lazy/walk.mjs'
- 'napi/parser/generated/visit/keys.mjs'
- 'napi/parser/generated/visit/types.mjs'
- 'napi/parser/generated/visit/visitor.d.ts'
- 'napi/parser/generated/visit/walk.mjs'
- 'napi/parser/src/generated/assert_layouts.rs'
- 'napi/parser/src/generated/derive_estree.rs'
- 'napi/parser/src/generated/raw_transfer_constants.rs'
Expand Down
26 changes: 23 additions & 3 deletions napi/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import { Statement } from '@oxc-project/types';

### Visitor

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

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

```js
import { visitorKeys } from 'oxc-parser';
Expand Down Expand Up @@ -99,7 +99,7 @@ export interface EcmaScriptModule {
## API

```javascript
import { parseSync } from 'oxc-parser';
import { parseSync, Visitor } from 'oxc-parser';

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

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

// ESM information - imports, exports, `import.meta`s.
console.log(result.module);

// Visit the AST
const visitations = [];

const visitor = new Visitor({
VariableDeclaration(decl) {
visitations.push(`enter ${decl.kind}`);
},
'VariableDeclaration:exit'(decl) {
visitations.push(`exit ${decl.kind}`);
},
Identifier(ident) {
visitations.push(ident.name);
},
});

visitor.visit(result.program);

// Logs: [ 'enter const', 'url', 'String', 'import', 'meta', 'url', 'exit const' ]
console.log(visitations);
```

### Options
Expand Down
176 changes: 176 additions & 0 deletions napi/parser/generated/visit/types.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// Auto-generated code, DO NOT EDIT DIRECTLY!
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/estree_visit.rs`.

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

export const NODE_TYPES_COUNT = 165;
export const LEAF_NODE_TYPES_COUNT = 27;
Loading
Loading