diff --git a/.changeset/mean-camels-sleep.md b/.changeset/mean-camels-sleep.md new file mode 100644 index 000000000000..026dc3ab7646 --- /dev/null +++ b/.changeset/mean-camels-sleep.md @@ -0,0 +1,5 @@ +--- +swc_ecma_ast: patch +--- + +fix(ast): span of binding ident should include type ann diff --git a/crates/swc/tests/tsc-references/for-inStatementsInvalid.1.normal.js b/crates/swc/tests/tsc-references/for-inStatementsInvalid.1.normal.js index 4e356b59903d..02ee1472c27a 100644 --- a/crates/swc/tests/tsc-references/for-inStatementsInvalid.1.normal.js +++ b/crates/swc/tests/tsc-references/for-inStatementsInvalid.1.normal.js @@ -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()) { } diff --git a/crates/swc/tests/tsc-references/for-inStatementsInvalid.2.minified.js b/crates/swc/tests/tsc-references/for-inStatementsInvalid.2.minified.js index 4e356b59903d..02ee1472c27a 100644 --- a/crates/swc/tests/tsc-references/for-inStatementsInvalid.2.minified.js +++ b/crates/swc/tests/tsc-references/for-inStatementsInvalid.2.minified.js @@ -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()) { } diff --git a/crates/swc/tests/tsc-references/parserES5ForOfStatement5.1.normal.js b/crates/swc/tests/tsc-references/parserES5ForOfStatement5.1.normal.js index 71de08a24a52..d2b0a19b6cff 100644 --- a/crates/swc/tests/tsc-references/parserES5ForOfStatement5.1.normal.js +++ b/crates/swc/tests/tsc-references/parserES5ForOfStatement5.1.normal.js @@ -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 | } //! `---- diff --git a/crates/swc/tests/tsc-references/parserES5ForOfStatement5.2.minified.js b/crates/swc/tests/tsc-references/parserES5ForOfStatement5.2.minified.js index 71de08a24a52..d2b0a19b6cff 100644 --- a/crates/swc/tests/tsc-references/parserES5ForOfStatement5.2.minified.js +++ b/crates/swc/tests/tsc-references/parserES5ForOfStatement5.2.minified.js @@ -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 | } //! `---- diff --git a/crates/swc/tests/tsc-references/parserForInStatement5.1.normal.js b/crates/swc/tests/tsc-references/parserForInStatement5.1.normal.js index 2e1400647fbf..162bd28980c4 100644 --- a/crates/swc/tests/tsc-references/parserForInStatement5.1.normal.js +++ b/crates/swc/tests/tsc-references/parserForInStatement5.1.normal.js @@ -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 | } //! `---- diff --git a/crates/swc/tests/tsc-references/parserForInStatement5.2.minified.js b/crates/swc/tests/tsc-references/parserForInStatement5.2.minified.js index 2e1400647fbf..162bd28980c4 100644 --- a/crates/swc/tests/tsc-references/parserForInStatement5.2.minified.js +++ b/crates/swc/tests/tsc-references/parserForInStatement5.2.minified.js @@ -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 | } //! `---- diff --git a/crates/swc/tests/tsc-references/parserForOfStatement5.1.normal.js b/crates/swc/tests/tsc-references/parserForOfStatement5.1.normal.js index e197bfbc33a1..889f740ac0cc 100644 --- a/crates/swc/tests/tsc-references/parserForOfStatement5.1.normal.js +++ b/crates/swc/tests/tsc-references/parserForOfStatement5.1.normal.js @@ -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 | } //! `---- diff --git a/crates/swc/tests/tsc-references/parserForOfStatement5.2.minified.js b/crates/swc/tests/tsc-references/parserForOfStatement5.2.minified.js index e197bfbc33a1..889f740ac0cc 100644 --- a/crates/swc/tests/tsc-references/parserForOfStatement5.2.minified.js +++ b/crates/swc/tests/tsc-references/parserForOfStatement5.2.minified.js @@ -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 | } //! `---- diff --git a/crates/swc_ecma_ast/src/ident.rs b/crates/swc_ecma_ast/src/ident.rs index fa67207b645a..26e631f69f35 100644 --- a/crates/swc_ecma_ast/src/ident.rs +++ b/crates/swc_ecma_ast/src/ident.rs @@ -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"), @@ -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, @@ -41,6 +40,15 @@ pub struct BindingIdent { pub type_ann: Option>, } +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; diff --git a/crates/swc_ecma_parser/tests/span/ts/decl/decorated-class.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/decl/decorated-class.ts.span.swc-stderr index 7ac7b48beb95..a10a07c6fc6b 100644 --- a/crates/swc_ecma_parser/tests/span/ts/decl/decorated-class.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/decl/decorated-class.ts.span.swc-stderr @@ -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] @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/crates/swc_ecma_parser/tests/span/ts/expr/arrow.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/expr/arrow.ts.span.swc-stderr index 0aeee9aa964a..ab4cb7561f0a 100644 --- a/crates/swc_ecma_parser/tests/span/ts/expr/arrow.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/expr/arrow.ts.span.swc-stderr @@ -37,7 +37,7 @@ x Pat ,-[$DIR/tests/span/ts/expr/arrow.ts:1:1] 1 | const a: () => any = () => { - : ^ + : ^^^^^^^^^^^^ 2 | return 'foo' `---- x Ident diff --git a/crates/swc_ecma_parser/tests/span/ts/issue-8856/param.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/issue-8856/param.ts.span.swc-stderr index 6ac98e7cda48..8c7bb2740ed2 100644 --- a/crates/swc_ecma_parser/tests/span/ts/issue-8856/param.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/issue-8856/param.ts.span.swc-stderr @@ -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] diff --git a/crates/swc_ecma_parser/tests/span/ts/issue-8856/var-decl.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/issue-8856/var-decl.ts.span.swc-stderr index e261a26ac90c..ff5d4cad7b26 100644 --- a/crates/swc_ecma_parser/tests/span/ts/issue-8856/var-decl.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/issue-8856/var-decl.ts.span.swc-stderr @@ -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] diff --git a/crates/swc_ecma_parser/tests/span/ts/stmt/try-catch-unknown.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/stmt/try-catch-unknown.ts.span.swc-stderr index df53b26260a5..121607c1d02c 100644 --- a/crates/swc_ecma_parser/tests/span/ts/stmt/try-catch-unknown.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/stmt/try-catch-unknown.ts.span.swc-stderr @@ -38,7 +38,7 @@ ,-[$DIR/tests/span/ts/stmt/try-catch-unknown.ts:2:1] 1 | try { 2 | } catch (e: unknown) { - : ^ + : ^^^^^^^^^^ 3 | } `---- x Ident diff --git a/crates/swc_ecma_parser/tests/span/ts/type/array.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/type/array.ts.span.swc-stderr index ab5ca5e7ec14..d37b2a4a7092 100644 --- a/crates/swc_ecma_parser/tests/span/ts/type/array.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/type/array.ts.span.swc-stderr @@ -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] diff --git a/crates/swc_ecma_parser/tests/span/ts/type/interface.ts.span.swc-stderr b/crates/swc_ecma_parser/tests/span/ts/type/interface.ts.span.swc-stderr index 1c47339dc7f1..da1a5c1ff8a8 100644 --- a/crates/swc_ecma_parser/tests/span/ts/type/interface.ts.span.swc-stderr +++ b/crates/swc_ecma_parser/tests/span/ts/type/interface.ts.span.swc-stderr @@ -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 diff --git a/crates/swc_ecma_parser/tests/typescript-errors/class/parameter-properties/input.ts.swc-stderr b/crates/swc_ecma_parser/tests/typescript-errors/class/parameter-properties/input.ts.swc-stderr index f0af01647b1a..0a632baa7d7d 100644 --- a/crates/swc_ecma_parser/tests/typescript-errors/class/parameter-properties/input.ts.swc-stderr +++ b/crates/swc_ecma_parser/tests/typescript-errors/class/parameter-properties/input.ts.swc-stderr @@ -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 | } `----