From 9479865d9b2a2baeee46bd979887b5bdfea3bb88 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sun, 3 Mar 2024 15:18:47 +0800 Subject: [PATCH] feat(napi/parser): expose `preserveParans` option (#2582) closes #2576 --- crates/oxc_parser/src/lib.rs | 7 +++++++ napi/parser/src/lib.rs | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index 75cd065e4..6e92ae3f4 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -122,6 +122,13 @@ pub struct ParserReturn<'a> { #[derive(Clone, Copy)] struct ParserOptions { pub allow_return_outside_function: bool, + /// 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. + /// + /// Default: true pub preserve_parens: bool, } diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index f4c5d15cc..3db2b8e9f 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -23,6 +23,14 @@ pub struct ParserOptions { #[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")] pub source_type: Option, pub source_filename: Option, + /// 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. + /// + /// Default: true + pub preserve_parens: Option, } #[napi(object)] @@ -56,7 +64,9 @@ fn parse<'a>( Some("module") => source_type.with_module(true), _ => source_type, }; - Parser::new(allocator, source_text, source_type).parse() + Parser::new(allocator, source_text, source_type) + .preserve_parens(options.preserve_parens.unwrap_or(true)) + .parse() } /// Parse without returning anything.