Skip to content

Commit

Permalink
feat(parser): add preserve_parens option (default: true) (oxc-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Nov 21, 2023
1 parent 0f7d6a5 commit 07b0109
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ impl<'a> Parser<'a> {
)
};

Ok(self.ast.parenthesized_expression(paren_span, expression))
Ok(if self.preserve_parens {
self.ast.parenthesized_expression(paren_span, expression)
} else {
expression
})
}

/// Section 13.2.2 This Expression
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a> Lexer<'a> {
/// See Section 12:
/// The `InputElementRegExp` goal symbol is used in all syntactic grammar contexts
/// where a `RegularExpressionLiteral` is permitted
/// Which meams the parser needs to re-tokenize on `PrimaryExpression`,
/// Which means the parser needs to re-tokenize on `PrimaryExpression`,
/// `RegularExpressionLiteral` only appear on the right hand side of `PrimaryExpression`
pub fn next_regex(&mut self, kind: Kind) -> Token<'a> {
self.current.token.start = self.offset()
Expand Down
15 changes: 15 additions & 0 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ pub struct Parser<'a> {

/// Ast builder for creating AST spans
ast: AstBuilder<'a>,

/// Emit `ParenthesizedExpression` in AST.
/// Default: `true`
preserve_parens: bool,
}

impl<'a> Parser<'a> {
Expand All @@ -141,6 +145,7 @@ impl<'a> Parser<'a> {
state: ParserState::new(allocator),
ctx: Self::default_context(source_type),
ast: AstBuilder::new(allocator),
preserve_parens: true,
}
}

Expand All @@ -154,6 +159,16 @@ impl<'a> Parser<'a> {
self
}

/// Emit `ParenthesizedExpression` in AST.
///
/// If this option is true, parenthesized expressions are represented by (non-standard)
/// `ParenthesizedExpression` nodes that have a single expression property containing the expression inside parentheses.
#[must_use]
pub fn preserve_parens(mut self, allow: bool) -> Self {
self.preserve_parens = allow;
self
}

/// Main entry point
///
/// Returns an empty `Program` on unrecoverable error,
Expand Down

0 comments on commit 07b0109

Please sign in to comment.