feat(napi/parser): expose preserveParans option (#2582)

closes #2576
This commit is contained in:
Boshen 2024-03-03 15:18:47 +08:00 committed by GitHub
parent 637cd1dea4
commit 9479865d9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View file

@ -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,
}

View file

@ -23,6 +23,14 @@ pub struct ParserOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,
pub source_filename: Option<String>,
/// 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<bool>,
}
#[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.