Skip to content

Commit

Permalink
fix(es/ast): Make span of binding ident include type ann (#9293)
Browse files Browse the repository at this point in the history
**Description:**

The binding ident includes a type ann and should have the type ann in it.

**Related issue:**

 - Closes #9290
  • Loading branch information
dsherret committed Jul 19, 2024
1 parent ae2ac05 commit 2b32481
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-camels-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_ast: patch
---

fix(ast): span of binding ident should include type ann
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! 8 | for (aRegExp in {}) { }
//! 9 |
//! 10 | for (var idx : number in {}) { }
//! : ^^^
//! : ^^^^^^^^^^^^
//! 11 |
//! 12 | function fn(): void { }
//! 13 | for (var x in fn()) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! 8 | for (aRegExp in {}) { }
//! 9 |
//! 10 | for (var idx : number in {}) { }
//! : ^^^
//! : ^^^^^^^^^^^^
//! 11 |
//! 12 | function fn(): void { }
//! 13 | for (var x in fn()) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number of X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number of X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number in X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number in X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number of X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
//! x The left-hand side of a 'for...of' statement cannot use a type annotation
//! ,-[1:1]
//! 1 | for (var a: number of X) {
//! : ^
//! : ^^^^^^^^^
//! 2 | }
//! `----
12 changes: 10 additions & 2 deletions crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use swc_common::{
use crate::{typescript::TsTypeAnn, Expr};

/// Identifier used as a pattern.
#[derive(Spanned, Clone, Debug, PartialEq, Eq, Hash, EqIgnoreSpan, Default)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(
any(feature = "rkyv-impl"),
Expand All @@ -31,7 +31,6 @@ use crate::{typescript::TsTypeAnn, Expr};
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(C)))]
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
pub struct BindingIdent {
#[span]
#[cfg_attr(feature = "serde-impl", serde(flatten))]
#[cfg_attr(feature = "__rkyv", omit_bounds)]
pub id: Ident,
Expand All @@ -41,6 +40,15 @@ pub struct BindingIdent {
pub type_ann: Option<Box<TsTypeAnn>>,
}

impl Spanned for BindingIdent {
fn span(&self) -> Span {
match &self.type_ann {
Some(ann) => Span::new(self.id.span.lo(), ann.span().hi()),
None => self.id.span,
}
}
}

impl Deref for BindingIdent {
type Target = Ident;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:5:1]
4 | export class CommentController {
5 | constructor(private commentService: CommentService) { }
: ^^^^^^^^^^^^^^
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----
x Ident
,-[$DIR/tests/span/ts/decl/decorated-class.ts:5:1]
Expand Down Expand Up @@ -541,7 +541,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:10:1]
9 | public updateComment(
10 | @Param('id') id: string,
: ^^
: ^^^^^^^^^^
11 | @Body() updateCommentDto: UpdateCommentDto,
`----
x Ident
Expand Down Expand Up @@ -625,7 +625,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:11:1]
10 | @Param('id') id: string,
11 | @Body() updateCommentDto: UpdateCommentDto,
: ^^^^^^^^^^^^^^^^
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12 | @User() user: UserType,
`----
x Ident
Expand Down Expand Up @@ -723,7 +723,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:12:1]
11 | @Body() updateCommentDto: UpdateCommentDto,
12 | @User() user: UserType,
: ^^^^
: ^^^^^^^^^^^^^^
13 | ) {
`----
x Ident
Expand Down Expand Up @@ -1173,7 +1173,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:19:1]
18 | @Delete(COMMENT_DELETE_ENDPOINT)
19 | public deleteComment(@Param('id') id: string, @User() user: UserType) {
: ^^
: ^^^^^^^^^^
20 | return this.commentService.delete(id, user.id);
`----
x Ident
Expand Down Expand Up @@ -1257,7 +1257,7 @@
,-[$DIR/tests/span/ts/decl/decorated-class.ts:19:1]
18 | @Delete(COMMENT_DELETE_ENDPOINT)
19 | public deleteComment(@Param('id') id: string, @User() user: UserType) {
: ^^^^
: ^^^^^^^^^^^^^^
20 | return this.commentService.delete(id, user.id);
`----
x Ident
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
x Pat
,-[$DIR/tests/span/ts/expr/arrow.ts:1:1]
1 | const a: () => any = () => {
: ^
: ^^^^^^^^^^^^
2 | return 'foo'
`----
x Ident
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
x Pat
,-[$DIR/tests/span/ts/issue-8856/param.ts:1:1]
1 | function b(a: number = 1) { }
: ^
: ^^^^^^^^^
`----
x Ident
,-[$DIR/tests/span/ts/issue-8856/param.ts:1:1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
x Pat
,-[$DIR/tests/span/ts/issue-8856/var-decl.ts:1:1]
1 | const a: number
: ^
: ^^^^^^^^^
`----
x Ident
,-[$DIR/tests/span/ts/issue-8856/var-decl.ts:1:1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
,-[$DIR/tests/span/ts/stmt/try-catch-unknown.ts:2:1]
1 | try {
2 | } catch (e: unknown) {
: ^
: ^^^^^^^^^^
3 | }
`----
x Ident
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
x Pat
,-[$DIR/tests/span/ts/type/array.ts:1:1]
1 | let a: string[] = foo
: ^
: ^^^^^^^^^^^
`----
x Ident
,-[$DIR/tests/span/ts/type/array.ts:1:1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
,-[$DIR/tests/span/ts/type/interface.ts:3:1]
2 | new();
3 | (foo: string): void;
: ^^^
: ^^^^^^^^^^^
4 | [foo: string]: void;
`----
x Ident
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
,-[$DIR/tests/typescript-errors/class/parameter-properties/input.ts:10:1]
9 | readonly x = 0,
10 | public y?: number = 0) {}
: ^
: ^^^^^^^^^^
11 | }
`----

0 comments on commit 2b32481

Please sign in to comment.