Skip to content

How to replace a Identifier node with a Literal node? #8065

Closed Answered by overlookmotel
rattlecattle asked this question in Q&A
Discussion options

You must be logged in to vote

PI is an IdentifierReference, which is a variant of Expression enum. So you can use VisitMut::visit_expression and replace that Expression with a different kind of Expression.

use oxc_ast::{
    ast::{Expression, NumberBase},
    visit::VisitMut,
    AstBuilder,
};

struct PiReplacer<'a> {
    ast_builder: AstBuilder<'a>,
}

impl<'a> VisitMut<'a> for PiReplacer<'a> {
    fn visit_expression(&mut self, expr: &mut Expression<'a>) {
        if let Expression::Identifier(ident) = expr {
            if ident.name == "PI" {
                *expr = self.ast_builder.expression_numeric_literal(
                    ident.span,
                    3.14,
                    None,
                    N…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Boshen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants