From cfdcfdb17cdded5cd0b5757ae2dec6aaf750c3f0 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:29:35 +0000 Subject: [PATCH] fix(parser): fix end span for optional binding pattern without type annotation (#9652) closes #9636 ``` function foo(bar?) {} ^^^^ span for `Identifier` ``` --- crates/oxc_parser/src/js/binding.rs | 8 +++++--- crates/oxc_parser/src/js/declaration.rs | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/oxc_parser/src/js/binding.rs b/crates/oxc_parser/src/js/binding.rs index a627bc9196995..312ccea5d70d8 100644 --- a/crates/oxc_parser/src/js/binding.rs +++ b/crates/oxc_parser/src/js/binding.rs @@ -22,7 +22,9 @@ impl<'a> ParserImpl<'a> { let optional = if allow_question && self.is_ts { self.eat(Kind::Question) } else { false }; let type_annotation = self.parse_ts_type_annotation()?; if let Some(type_annotation) = &type_annotation { - Self::extend_binding_pattern_span_end(type_annotation.span, &mut kind); + Self::extend_binding_pattern_span_end(type_annotation.span.end, &mut kind); + } else if optional { + Self::extend_binding_pattern_span_end(self.prev_token_end, &mut kind); } Ok(self.ast.binding_pattern(kind, type_annotation, optional)) } @@ -178,13 +180,13 @@ impl<'a> ParserImpl<'a> { } } - pub(super) fn extend_binding_pattern_span_end(span: Span, kind: &mut BindingPatternKind<'a>) { + pub(super) fn extend_binding_pattern_span_end(end: u32, kind: &mut BindingPatternKind<'a>) { let pat_span = match kind { BindingPatternKind::BindingIdentifier(pat) => &mut pat.span, BindingPatternKind::ObjectPattern(pat) => &mut pat.span, BindingPatternKind::ArrayPattern(pat) => &mut pat.span, BindingPatternKind::AssignmentPattern(pat) => &mut pat.span, }; - pat_span.end = span.end; + pat_span.end = end; } } diff --git a/crates/oxc_parser/src/js/declaration.rs b/crates/oxc_parser/src/js/declaration.rs index 4ee23597155e3..a5dff1569619c 100644 --- a/crates/oxc_parser/src/js/declaration.rs +++ b/crates/oxc_parser/src/js/declaration.rs @@ -104,7 +104,7 @@ impl<'a> ParserImpl<'a> { let optional = self.eat(Kind::Question); // not allowed, but checked in checker/typescript.rs let type_annotation = self.parse_ts_type_annotation()?; if let Some(type_annotation) = &type_annotation { - Self::extend_binding_pattern_span_end(type_annotation.span, &mut binding_kind); + Self::extend_binding_pattern_span_end(type_annotation.span.end, &mut binding_kind); } (self.ast.binding_pattern(binding_kind, type_annotation, optional), definite) } else {