Skip to content

Conversation

overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Sep 19, 2025

Add an AST visitor to oxc-parser.

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

const { program } = parseSync('test.js', 'let x = { y: z };');

const idents = [];
const visitor = new Visitor({
  Identifier(node) {
    idents.push(node.name);
  }
});

visitor.visit(program);

// Logs [ 'x', 'y', 'z' ]
console.log(idents);

Primarily we need a visitor for Oxlint plugins, but it's easier to develop and test in oxc-parser, so I thought I'd add it there first. Oxlint build process will copy the generated files for tree-walking from napi/parser dir, as it does with other files.

Notes:

  • The walk.mjs file is large, so it's lazy-loaded only when user first constructs a Visitor. Adding Visitor to the package will not affect start-up time where user only uses parseSync etc.
  • TS type definition for Visitor and the visitor object it takes are provided.
  • Visitor supports enter and exit visitors e.g. Program and Program:exit.
  • src-js/visit/visitor.mjs is copied from apps/oxlint with only minor modifications, and conversion from TS to JS. It'd be better if we only had this impl in one place, but that would require converting oxc-parser to TS and building with TSDown, like oxlint is. Leaving that for later - Convert oxc-parser to TypeScript #13935.

@github-actions github-actions bot added A-parser Area - Parser A-ast-tools Area - AST tools C-enhancement Category - New feature or request labels Sep 19, 2025
Copy link
Member Author

overlookmotel commented Sep 19, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from d07fa71 to 1c50c58 Compare September 19, 2025 17:36
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch 2 times, most recently from a44ba52 to f68cffb Compare September 19, 2025 18:29
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from 1c50c58 to 098ac21 Compare September 19, 2025 18:29
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch from f68cffb to f7f9e3a Compare September 19, 2025 21:01
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from 098ac21 to c2b060a Compare September 19, 2025 21:01
@overlookmotel overlookmotel marked this pull request as ready for review September 19, 2025 21:14
@overlookmotel overlookmotel self-assigned this Sep 19, 2025
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from c2b060a to 4eb80fe Compare September 19, 2025 21:16
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from 4eb80fe to c8131c1 Compare September 20, 2025 09:43
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch from f7f9e3a to b0abdfd Compare September 20, 2025 09:43
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from c8131c1 to da2c6f6 Compare September 21, 2025 09:58
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch from b0abdfd to 7091e82 Compare September 21, 2025 09:58
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from da2c6f6 to 488d5b2 Compare September 21, 2025 10:31
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch from 7091e82 to fe80787 Compare September 21, 2025 10:31
@overlookmotel overlookmotel force-pushed the 09-17-feat_napi_parser_export_visitor_keys branch from fe80787 to 2395e3a Compare September 21, 2025 15:00
@overlookmotel overlookmotel force-pushed the 09-19-feat_napi_parser_add_ast_walker branch from 488d5b2 to 0800b58 Compare September 21, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ast-tools Area - AST tools A-parser Area - Parser C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant