Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parser)!: remove builder pattern from Parser struct #5000

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 5 additions & 2 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -254,7 +254,10 @@ impl Runtime {
tx_error: &DiagnosticSender,
) -> Vec<Message<'a>> {
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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 10 additions & 27 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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?
Expand All @@ -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> {
Expand All @@ -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,
Expand All @@ -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,
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_prettier/examples/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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())
Expand All @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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()
}

Expand Down
7 changes: 4 additions & 3 deletions tasks/coverage/src/tools/prettier.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions tasks/prettier_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
}
}
Expand Down