Skip to content

Add missing parts for setting up AST transformation examples#1081

Open
kant2002 wants to merge 2 commits intooxc-project:mainfrom
kant2002:kant/patch-1
Open

Add missing parts for setting up AST transformation examples#1081
kant2002 wants to merge 2 commits intooxc-project:mainfrom
kant2002:kant/patch-1

Conversation

@kant2002
Copy link
Copy Markdown

@kant2002 kant2002 commented May 3, 2026

Added examples for using AstBuilder in MyTransformer.

Added examples for using AstBuilder in MyTransformer.

Signed-off-by: Andrii Kurdiumov <kant2002@gmail.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 3, 2026

Deploy Preview for oxc-project ready!

Name Link
🔨 Latest commit 6feee0d
🔍 Latest deploy log https://app.netlify.com/projects/oxc-project/deploys/69fb0a9238f0850008bd3fd4
😎 Deploy Preview https://deploy-preview-1081--oxc-project.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Author

@kant2002 kant2002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make recursive simplification, since that's usually what you want initially.

use oxc_ast::visit::{VisitMut, walk_mut};
use oxc_ast::AstBuilder;

struct MyTransformer<'a> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets add some rust doc comments explaining what this transformation is doing?

inline comments like

`x + y` -> `"''"`

explaining the before/after of what's being changed can also be helpful.

Comment on lines +116 to +126
fn visit_expression(&mut self, expr: &mut BinaryExpression<'a>) {
// Detect the expression type which you want to modify
match it {
Expression::BinaryExpression(expr)
if expr.operator == BinaryOperator::Addition => {
let new_expr = self.builder.expression_string_literal(SPAN, "", Some(Str::new_const("''")));
*it = new_expr;
},
_ => walk_mut::walk_expression(self, it),
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say "When changing from one enum varient to another. in this example a BinaryExpression, it is necessary to match on the enum variant rather than on the expression or..."

or sometihig like that

Copy link
Copy Markdown
Author

@kant2002 kant2002 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants