From d17e6e7452d343c3ff9cf351d0bb8294966d27a8 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 20 Aug 2024 15:25:19 +0800 Subject: [PATCH] refactor(parser)!: remove builder pattern from `Parser` struct part of #4455 use `with_options` API instead --- crates/oxc_language_server/src/linter.rs | 7 +++-- crates/oxc_linter/src/service.rs | 7 +++-- crates/oxc_parser/src/js/expression.rs | 2 +- crates/oxc_parser/src/lib.rs | 37 +++++++----------------- crates/oxc_prettier/examples/prettier.rs | 6 ++-- crates/oxc_wasm/src/lib.rs | 17 ++++++++--- napi/parser/src/lib.rs | 7 +++-- tasks/coverage/src/tools/prettier.rs | 7 +++-- tasks/prettier_conformance/src/lib.rs | 6 ++-- 9 files changed, 51 insertions(+), 45 deletions(-) diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index fcd8dcc8d3a810..f62e82a99be8ea 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -15,7 +15,7 @@ use oxc_linter::{ }, FixKind, Linter, }; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_semantic::SemanticBuilder; use oxc_span::{SourceType, VALID_EXTENSIONS}; use ropey::Rope; @@ -270,7 +270,10 @@ impl IsolatedLintHandler { source; let allocator = Allocator::default(); let ret = Parser::new(&allocator, javascript_source_text, source_type) - .allow_return_outside_function(true) + .with_options(ParseOptions { + allow_return_outside_function: true, + ..ParseOptions::default() + }) .parse(); if !ret.errors.is_empty() { diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 07078e4c09f1cf..14ea3c1b09ba06 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -10,7 +10,7 @@ use std::{ use dashmap::DashMap; use oxc_allocator::Allocator; use oxc_diagnostics::{DiagnosticSender, DiagnosticService, Error, OxcDiagnostic}; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_resolver::Resolver; use oxc_semantic::{ModuleRecord, SemanticBuilder}; use oxc_span::{SourceType, VALID_EXTENSIONS}; @@ -254,7 +254,10 @@ impl Runtime { tx_error: &DiagnosticSender, ) -> Vec> { let ret = Parser::new(allocator, source_text, source_type) - .allow_return_outside_function(true) + .with_options(ParseOptions { + allow_return_outside_function: true, + ..ParseOptions::default() + }) .parse(); if !ret.errors.is_empty() { diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index cc0e1a6db9350a..6a8b1872812c27 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -226,7 +226,7 @@ impl<'a> ParserImpl<'a> { ) }; - Ok(if self.preserve_parens { + Ok(if self.options.preserve_parens { self.ast.expression_parenthesized(paren_span, expression) } else { expression diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index 42c718b209ab61..614b989a9bdd87 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -103,10 +103,15 @@ pub struct ParserReturn<'a> { pub panicked: bool, } -/// Parser options -#[derive(Clone, Copy)] +/// Parse options +#[derive(Debug, Clone, Copy)] pub struct ParseOptions { + /// Allow return outside of function + /// + /// By default, a return statement at the top level raises an error. + /// Set this to true to accept such code. pub allow_return_outside_function: bool, + /// Emit `ParenthesizedExpression` in AST. /// /// If this option is true, parenthesized expressions are represented by @@ -145,26 +150,6 @@ impl<'a> Parser<'a> { self.options = options; self } - - /// Allow return outside of function - /// - /// By default, a return statement at the top level raises an error. - /// Set this to true to accept such code. - #[must_use] - pub fn allow_return_outside_function(mut self, allow: bool) -> Self { - self.options.allow_return_outside_function = allow; - 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.options.preserve_parens = allow; - self - } } mod parser_parse { @@ -243,6 +228,8 @@ use parser_parse::UniquePromise; /// Implementation of parser. /// `Parser` is just a public wrapper, the guts of the implementation is in this type. struct ParserImpl<'a> { + options: ParseOptions, + lexer: Lexer<'a>, /// SourceType: JavaScript or TypeScript, Script or Module, jsx support? @@ -269,10 +256,6 @@ struct ParserImpl<'a> { /// Ast builder for creating AST spans ast: AstBuilder<'a>, - - /// Emit `ParenthesizedExpression` in AST. - /// Default: `true` - preserve_parens: bool, } impl<'a> ParserImpl<'a> { @@ -289,6 +272,7 @@ impl<'a> ParserImpl<'a> { unique: UniquePromise, ) -> Self { Self { + options, lexer: Lexer::new(allocator, source_text, source_type, unique), source_type, source_text, @@ -298,7 +282,6 @@ impl<'a> ParserImpl<'a> { state: ParserState::default(), ctx: Self::default_context(source_type, options), ast: AstBuilder::new(allocator), - preserve_parens: options.preserve_parens, } } diff --git a/crates/oxc_prettier/examples/prettier.rs b/crates/oxc_prettier/examples/prettier.rs index ddd0e60edc3f3c..26b849d0756b0d 100644 --- a/crates/oxc_prettier/examples/prettier.rs +++ b/crates/oxc_prettier/examples/prettier.rs @@ -2,7 +2,7 @@ use std::path::Path; use oxc_allocator::Allocator; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_prettier::{Prettier, PrettierOptions, TrailingComma}; use oxc_span::SourceType; use pico_args::Arguments; @@ -22,7 +22,9 @@ fn main() -> std::io::Result<()> { let source_text = std::fs::read_to_string(path)?; let allocator = Allocator::default(); let source_type = SourceType::from_path(path).unwrap(); - let ret = Parser::new(&allocator, &source_text, source_type).preserve_parens(false).parse(); + let ret = Parser::new(&allocator, &source_text, source_type) + .with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() }) + .parse(); let output = Prettier::new( &allocator, &source_text, diff --git a/crates/oxc_wasm/src/lib.rs b/crates/oxc_wasm/src/lib.rs index 07cdf429230948..39af6f7c618100 100644 --- a/crates/oxc_wasm/src/lib.rs +++ b/crates/oxc_wasm/src/lib.rs @@ -11,7 +11,7 @@ use oxc::{ codegen::{CodeGenerator, WhitespaceRemover}, diagnostics::Error, minifier::{CompressOptions, Minifier, MinifierOptions}, - parser::Parser, + parser::{ParseOptions, Parser}, semantic::{ScopeId, Semantic, SemanticBuilder}, span::SourceType, transformer::{TransformOptions, Transformer}, @@ -169,7 +169,10 @@ impl Oxc { let source_type = SourceType::from_path(&path).unwrap_or_default(); let ret = Parser::new(&allocator, source_text, source_type) - .allow_return_outside_function(parser_options.allow_return_outside_function) + .with_options(ParseOptions { + allow_return_outside_function: parser_options.allow_return_outside_function, + ..ParseOptions::default() + }) .parse(); self.comments = self.map_comments(&ret.trivias); @@ -204,7 +207,10 @@ impl Oxc { if run_options.prettier_format() { let ret = Parser::new(&allocator, source_text, source_type) - .allow_return_outside_function(parser_options.allow_return_outside_function) + .with_options(ParseOptions { + allow_return_outside_function: parser_options.allow_return_outside_function, + ..ParseOptions::default() + }) .parse(); let printed = Prettier::new(&allocator, source_text, ret.trivias, PrettierOptions::default()) @@ -214,7 +220,10 @@ impl Oxc { if run_options.prettier_ir() { let ret = Parser::new(&allocator, source_text, source_type) - .allow_return_outside_function(parser_options.allow_return_outside_function) + .with_options(ParseOptions { + allow_return_outside_function: parser_options.allow_return_outside_function, + ..ParseOptions::default() + }) .parse(); let prettier_doc = Prettier::new( &allocator, diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index b7e3e7927812e5..a7348a4c7a6cc7 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -8,7 +8,7 @@ use oxc_allocator::Allocator; pub use oxc_ast::ast::Program; use oxc_ast::CommentKind; use oxc_diagnostics::{Error, NamedSource}; -use oxc_parser::{Parser, ParserReturn}; +use oxc_parser::{ParseOptions, Parser, ParserReturn}; use oxc_span::SourceType; pub use crate::module_lexer::*; @@ -64,7 +64,10 @@ fn parse<'a>( _ => source_type, }; Parser::new(allocator, source_text, source_type) - .preserve_parens(options.preserve_parens.unwrap_or(true)) + .with_options(ParseOptions { + preserve_parens: options.preserve_parens.unwrap_or(true), + ..ParseOptions::default() + }) .parse() } diff --git a/tasks/coverage/src/tools/prettier.rs b/tasks/coverage/src/tools/prettier.rs index c4db2f24ccc30f..e9d8869e776206 100644 --- a/tasks/coverage/src/tools/prettier.rs +++ b/tasks/coverage/src/tools/prettier.rs @@ -1,7 +1,7 @@ use std::path::{Path, PathBuf}; use oxc::allocator::Allocator; -use oxc::parser::{Parser, ParserReturn}; +use oxc::parser::{ParseOptions, Parser, ParserReturn}; use oxc::span::SourceType; use oxc_prettier::{Prettier, PrettierOptions}; @@ -18,13 +18,14 @@ fn get_result(source_text: &str, source_type: SourceType) -> TestResult { let options = PrettierOptions::default(); let allocator = Allocator::default(); + let parse_options = ParseOptions { preserve_parens: false, ..ParseOptions::default() }; let ParserReturn { program, trivias, .. } = - Parser::new(&allocator, source_text, source_type).preserve_parens(false).parse(); + Parser::new(&allocator, source_text, source_type).with_options(parse_options).parse(); let source_text1 = Prettier::new(&allocator, source_text, trivias, options).build(&program); let allocator = Allocator::default(); let ParserReturn { program, trivias, .. } = - Parser::new(&allocator, &source_text1, source_type).preserve_parens(false).parse(); + Parser::new(&allocator, &source_text1, source_type).with_options(parse_options).parse(); let source_text2 = Prettier::new(&allocator, &source_text1, trivias, options).build(&program); if source_text1 == source_text2 { diff --git a/tasks/prettier_conformance/src/lib.rs b/tasks/prettier_conformance/src/lib.rs index d5bc135dfc1808..decd8efe63b74e 100644 --- a/tasks/prettier_conformance/src/lib.rs +++ b/tasks/prettier_conformance/src/lib.rs @@ -9,7 +9,7 @@ use std::{ }; use oxc_allocator::Allocator; -use oxc_parser::Parser; +use oxc_parser::{ParseOptions, Parser}; use oxc_prettier::{Prettier, PrettierOptions}; use oxc_span::SourceType; use oxc_tasks_common::project_root; @@ -382,7 +382,9 @@ impl TestRunner { fn prettier(path: &Path, source_text: &str, prettier_options: PrettierOptions) -> String { let allocator = Allocator::default(); let source_type = SourceType::from_path(path).unwrap(); - let ret = Parser::new(&allocator, source_text, source_type).preserve_parens(false).parse(); + let ret = Parser::new(&allocator, source_text, source_type) + .with_options(ParseOptions { preserve_parens: false, ..ParseOptions::default() }) + .parse(); Prettier::new(&allocator, source_text, ret.trivias, prettier_options).build(&ret.program) } }