From d615b342e3b42c9c564e6fc0ef6cd917681b6e87 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Wed, 19 Feb 2025 09:00:03 +0000 Subject: [PATCH] refactor(parser): add `ArrowFunctionHead` struct (#9222) --- crates/oxc_parser/src/js/arrow.rs | 48 ++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/crates/oxc_parser/src/js/arrow.rs b/crates/oxc_parser/src/js/arrow.rs index a93048733c376..494b350d7c660 100644 --- a/crates/oxc_parser/src/js/arrow.rs +++ b/crates/oxc_parser/src/js/arrow.rs @@ -7,13 +7,13 @@ use oxc_syntax::precedence::Precedence; use super::Tristate; use crate::{diagnostics, lexer::Kind, ParserImpl}; -type ArrowFunctionHead<'a> = ( - Option>>, - Box<'a, FormalParameters<'a>>, - Option>>, - bool, - Span, -); +struct ArrowFunctionHead<'a> { + type_parameters: Option>>, + params: Box<'a, FormalParameters<'a>>, + return_type: Option>>, + r#async: bool, + span: Span, +} impl<'a> ParserImpl<'a> { pub(super) fn try_parse_parenthesized_arrow_function_expression( @@ -236,9 +236,13 @@ impl<'a> ParserImpl<'a> { self.expect(Kind::Arrow)?; - self.parse_arrow_function_body( - span, /* type_parameters */ None, params, /* return_type */ None, r#async, - ) + self.parse_arrow_function_body(ArrowFunctionHead { + type_parameters: None, + params, + return_type: None, + r#async, + span, + }) } fn parse_parenthesized_arrow_function_head(&mut self) -> Result> { @@ -268,7 +272,7 @@ impl<'a> ParserImpl<'a> { self.expect(Kind::Arrow)?; - Ok((type_parameters, params, return_type, r#async, span)) + Ok(ArrowFunctionHead { type_parameters, params, return_type, r#async, span }) } /// [ConciseBody](https://tc39.es/ecma262/#prod-ConciseBody) @@ -278,12 +282,10 @@ impl<'a> ParserImpl<'a> { /// `AssignmentExpression`[?In, ~Yield, ?Await] fn parse_arrow_function_body( &mut self, - span: Span, - type_parameters: Option>>, - params: Box<'a, FormalParameters<'a>>, - return_type: Option>>, - r#async: bool, + arrow_function_head: ArrowFunctionHead<'a>, ) -> Result> { + let ArrowFunctionHead { type_parameters, params, return_type, r#async, span } = + arrow_function_head; let has_await = self.ctx.has_await(); let has_yield = self.ctx.has_yield(); self.ctx = self.ctx.and_await(r#async).and_yield(false); @@ -315,10 +317,8 @@ impl<'a> ParserImpl<'a> { /// `ArrowFunction`[In, Yield, Await] : /// `ArrowParameters`[?Yield, ?Await] [no `LineTerminator` here] => `ConciseBody`[?In] fn parse_parenthesized_arrow_function(&mut self) -> Result>> { - let (type_parameters, params, return_type, r#async, span) = - self.parse_parenthesized_arrow_function_head()?; - self.parse_arrow_function_body(span, type_parameters, params, return_type, r#async) - .map(Some) + let head = self.parse_parenthesized_arrow_function_head()?; + self.parse_arrow_function_body(head).map(Some) } fn parse_possible_parenthesized_arrow_function_expression( @@ -328,12 +328,8 @@ impl<'a> ParserImpl<'a> { if self.state.not_parenthesized_arrow.contains(&pos) { return Ok(None); } - if let Some((type_parameters, params, return_type, r#async, span)) = - self.try_parse(ParserImpl::parse_parenthesized_arrow_function_head) - { - return self - .parse_arrow_function_body(span, type_parameters, params, return_type, r#async) - .map(Some); + if let Some(head) = self.try_parse(ParserImpl::parse_parenthesized_arrow_function_head) { + return self.parse_arrow_function_body(head).map(Some); } self.state.not_parenthesized_arrow.insert(pos); Ok(None)