diff --git a/crates/oxc_parser/src/cursor.rs b/crates/oxc_parser/src/cursor.rs index f7b20a8cb..bd58f0a8a 100644 --- a/crates/oxc_parser/src/cursor.rs +++ b/crates/oxc_parser/src/cursor.rs @@ -1,6 +1,6 @@ //! Code related to navigating `Token`s from the lexer -use oxc_ast::ast::RegExpFlags; +use oxc_ast::ast::{Decorator, RegExpFlags}; use oxc_diagnostics::Result; use oxc_span::Span; @@ -306,4 +306,9 @@ impl<'a> ParserImpl<'a> { self.ctx = ctx; result } + + pub(crate) fn consume_decorators(&mut self) -> oxc_allocator::Vec<'a, Decorator<'a>> { + let decorators = std::mem::take(&mut self.state.decorators); + self.ast.new_vec_from_iter(decorators) + } } diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index 6db0acc99..a8628798c 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -57,7 +57,7 @@ impl<'a> ParserImpl<'a> { ) -> Result>> { self.bump_any(); // advance `class` - let decorators = self.state.consume_decorators(); + let decorators = self.consume_decorators(); let start_span = decorators.iter().next().map_or(start_span, |d| d.span); let id = if self.cur_kind().is_binding_identifier() && !self.at(Kind::Implements) { @@ -360,7 +360,7 @@ impl<'a> ParserImpl<'a> { kind }; - let decorators = self.state.consume_decorators(); + let decorators = self.consume_decorators(); let value = self.parse_method(r#async, generator)?; @@ -444,7 +444,7 @@ impl<'a> ParserImpl<'a> { accessibility, optional, definite, - decorators: self.state.consume_decorators(), + decorators: self.consume_decorators(), }; Ok(ClassElement::PropertyDefinition(self.ast.alloc(property_definition))) } @@ -474,6 +474,7 @@ impl<'a> ParserImpl<'a> { AccessorPropertyType::AccessorProperty }; + let decorators = self.consume_decorators(); Ok(self.ast.accessor_property( r#type, self.end_span(span), @@ -481,7 +482,7 @@ impl<'a> ParserImpl<'a> { value, computed, r#static, - self.state.consume_decorators(), + decorators, )) } } diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index 520196ee1..c2c1f1062 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -264,7 +264,7 @@ impl<'a> SeparatedList<'a> for FormalParameterList<'a> { } _ => { let pattern = p.parse_binding_pattern_with_initializer()?; - let decorators = p.state.consume_decorators(); + let decorators = p.consume_decorators(); let formal_parameter = p.ast.formal_parameter( p.end_span(span), pattern, diff --git a/crates/oxc_parser/src/lib.rs b/crates/oxc_parser/src/lib.rs index b6c156e49..3d23fa738 100644 --- a/crates/oxc_parser/src/lib.rs +++ b/crates/oxc_parser/src/lib.rs @@ -287,7 +287,7 @@ impl<'a> ParserImpl<'a> { errors: vec![], token: Token::default(), prev_token_end: 0, - state: ParserState::new(allocator), + state: ParserState::default(), ctx: Self::default_context(source_type, options), ast: AstBuilder::new(allocator), preserve_parens: options.preserve_parens, diff --git a/crates/oxc_parser/src/state.rs b/crates/oxc_parser/src/state.rs index 00dcbfbce..d8573c0bb 100644 --- a/crates/oxc_parser/src/state.rs +++ b/crates/oxc_parser/src/state.rs @@ -1,26 +1,10 @@ use rustc_hash::FxHashSet; -use oxc_allocator::{Allocator, Vec}; use oxc_ast::ast::Decorator; +#[derive(Default)] pub struct ParserState<'a> { - allocator: &'a Allocator, - pub not_parenthesized_arrow: FxHashSet, - pub decorators: Vec<'a, Decorator<'a>>, -} - -impl<'a> ParserState<'a> { - pub fn new(allocator: &'a Allocator) -> Self { - Self { - allocator, - not_parenthesized_arrow: FxHashSet::default(), - decorators: Vec::new_in(allocator), - } - } - - pub fn consume_decorators(&mut self) -> Vec<'a, Decorator<'a>> { - std::mem::replace(&mut self.decorators, Vec::new_in(self.allocator)) - } + pub decorators: Vec>, } diff --git a/crates/oxc_parser/src/ts/statement.rs b/crates/oxc_parser/src/ts/statement.rs index 7b1c59a43..5f17d1351 100644 --- a/crates/oxc_parser/src/ts/statement.rs +++ b/crates/oxc_parser/src/ts/statement.rs @@ -418,7 +418,7 @@ impl<'a> ParserImpl<'a> { return Ok(()); } - let mut decorators = self.ast.new_vec(); + let mut decorators = vec![]; while self.at(Kind::At) { let decorator = self.parse_decorator()?; decorators.push(decorator);