From 442fb7b48715597d62f8d09327f93acc66f2d1b8 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 10 Aug 2024 02:12:45 +0800 Subject: [PATCH] fix(es/typescript): Strip `this` param in getter/setter (#9414) --- .changeset/friendly-onions-bathe.md | 5 + crates/swc_fast_ts_strip/src/lib.rs | 40 +++++- crates/swc_fast_ts_strip/tests/fixture.rs | 1 + .../tsc/errorSuperPropertyAccess.strip.broken | 7 - .../tsc/errorSuperPropertyAccess.strip.js | 127 ------------------ .../tsc/thisTypeInAccessors.strip.broken | 7 - .../tests/tsc/thisTypeInAccessors.strip.js | 38 ------ .../thisTypeInAccessorsNegative.strip.broken | 7 - .../tsc/thisTypeInAccessorsNegative.strip.js | 19 --- .../typeOfThisInStaticMembers9.strip.broken | 14 -- .../tsc/typeOfThisInStaticMembers9.strip.js | 22 --- 11 files changed, 40 insertions(+), 247 deletions(-) create mode 100644 .changeset/friendly-onions-bathe.md delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.broken delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.broken delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.broken delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.js delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.broken delete mode 100644 crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.js diff --git a/.changeset/friendly-onions-bathe.md b/.changeset/friendly-onions-bathe.md new file mode 100644 index 000000000000..3ed326df271a --- /dev/null +++ b/.changeset/friendly-onions-bathe.md @@ -0,0 +1,5 @@ +--- +swc_fast_ts_strip: patch +--- + +fix(es/typescript): Strip `this` param in getter/setter diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index d2a06d6c43b7..cf81f508c347 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -13,12 +13,12 @@ use swc_common::{ use swc_ecma_ast::{ ArrayPat, ArrowExpr, AutoAccessor, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, Constructor, Decl, DefaultDecl, DoWhileStmt, EsVersion, ExportAll, ExportDecl, - ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt, ForOfStmt, ForStmt, IfStmt, ImportDecl, - ImportSpecifier, NamedExport, ObjectPat, Param, Pat, PrivateMethod, PrivateProp, Program, Stmt, - TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, TsImportEqualsDecl, - TsIndexSignature, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr, - TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, - TsTypeParamDecl, TsTypeParamInstantiation, VarDeclarator, WhileStmt, + ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt, ForOfStmt, ForStmt, GetterProp, IfStmt, + ImportDecl, ImportSpecifier, NamedExport, ObjectPat, Param, Pat, PrivateMethod, PrivateProp, + Program, SetterProp, Stmt, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, + TsImportEqualsDecl, TsIndexSignature, TsInstantiation, TsModuleDecl, TsModuleName, + TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, + TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation, VarDeclarator, WhileStmt, }; use swc_ecma_parser::{ lexer::Lexer, @@ -1069,6 +1069,34 @@ impl Visit for TsStrip { self.add_overwrite(n.body.span_lo(), b';'); } } + + fn visit_getter_prop(&mut self, n: &GetterProp) { + let l_parern_index = self.get_next_token_index(n.key.span_hi()); + let l_parern = &self.tokens[l_parern_index]; + debug_assert_eq!(l_parern.token, Token::LParen); + + let r_parern_pos = n.type_ann.as_ref().map_or(n.body.span_lo(), |t| t.span.lo) - BytePos(1); + let r_parern = self.get_prev_token(r_parern_pos); + debug_assert_eq!(r_parern.token, Token::RParen); + + let span = span(l_parern.span.lo + BytePos(1), r_parern.span.hi - BytePos(1)); + self.add_replacement(span); + + n.visit_children_with(self); + } + + fn visit_setter_prop(&mut self, n: &SetterProp) { + if let Some(this_param) = &n.this_param { + self.add_replacement(this_param.span()); + + let comma = self.get_prev_token(n.param.span_lo() - BytePos(1)); + debug_assert_eq!(comma.token, Token::Comma); + + self.add_replacement(comma.span); + } + + n.visit_children_with(self); + } } trait IsTsDecl { diff --git a/crates/swc_fast_ts_strip/tests/fixture.rs b/crates/swc_fast_ts_strip/tests/fixture.rs index 254f8353e9b1..9c54bb131bbe 100644 --- a/crates/swc_fast_ts_strip/tests/fixture.rs +++ b/crates/swc_fast_ts_strip/tests/fixture.rs @@ -130,6 +130,7 @@ fn reparse(cm: &Lrc, handler: &Handler, filename: &PathBuf, input: St let fm = cm.new_source_file(filename.into(), input); let syntax = Syntax::Es(EsSyntax { + allow_super_outside_method: true, auto_accessors: true, decorators: true, decorators_before_export: true, diff --git a/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.broken b/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.broken deleted file mode 100644 index c530df833f7b..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.broken +++ /dev/null @@ -1,7 +0,0 @@ - x Invalid access to super - ,-[$DIR/tests/tsc/errorSuperPropertyAccess.strip.js:73:1] - 72 | function inner() { - 73 | super.publicFunc(); - : ^^^^^^^^^^ - 74 | } - `---- diff --git a/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js b/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js deleted file mode 100644 index 883d0b4a4b83..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js +++ /dev/null @@ -1,127 +0,0 @@ -//super property access in constructor of class with no base type -//super property access in instance member function of class with no base type -//super property access in instance member accessor(get and set) of class with no base type -class NoBase { - constructor() { - var a = super.prototype; - var b = super.hasOwnProperty(''); - } - - fn() { - var a = super.prototype; - var b = super.hasOwnProperty(''); - } - - m = super.prototype; - n = super.hasOwnProperty(''); - - //super static property access in static member function of class with no base type - //super static property access in static member accessor(get and set) of class with no base type - static static1() { - super.hasOwnProperty(''); - } - - static get static2() { - super.hasOwnProperty(''); - return ''; - } - - static set static2(n) { - super.hasOwnProperty(''); - } -} - -class SomeBase { - privateFunc() { } - privateMember = 0; - - publicFunc() { } - publicMember = 0; - - static privateStaticFunc() { } - static privateStaticMember = 0; - - static publicStaticFunc() { } - static publicStaticMember = 0; - -} - - -//super.publicInstanceMemberNotFunction in constructor of derived class -//super.publicInstanceMemberNotFunction in instance member function of derived class -//super.publicInstanceMemberNotFunction in instance member accessor(get and set) of derived class -//super property access only available with typed this -class SomeDerived1 extends SomeBase { - constructor() { - super(); - super.publicMember = 1; - } - - fn() { - var x = super.publicMember; - } - - get a() { - var x = super.publicMember; - return undefined; - } - set a(n) { - n = super.publicMember; - } - fn2() { - function inner() { - super.publicFunc(); - } - var x = { - test: function () { return super.publicFunc(); } - } - } -} - -//super.privateProperty in constructor of derived class -//super.privateProperty in instance member function of derived class -//super.privateProperty in instance member accessor(get and set) of derived class -class SomeDerived2 extends SomeBase { - constructor() { - super(); - super.privateMember = 1; - } - - fn() { - var x = super.privateMember; - } - - get a() { - var x = super.privateMember; - return undefined; - } - set a(n) { - n = super.privateMember; - } -} - -//super.publicStaticMemberNotFunction in static member function of derived class -//super.publicStaticMemberNotFunction in static member accessor(get and set) of derived class -//super.privateStaticProperty in static member function of derived class -//super.privateStaticProperty in static member accessor(get and set) of derived class -class SomeDerived3 extends SomeBase { - static fn() { - super.publicStaticMember = 3; - super.privateStaticMember = 3; - super.privateStaticFunc(); - } - static get a() { - super.publicStaticMember = 3; - super.privateStaticMember = 3; - super.privateStaticFunc(); - return ''; - } - static set a(n) { - super.publicStaticMember = 3; - super.privateStaticMember = 3; - super.privateStaticFunc(); - } -} - -// In object literal -var obj = { n: super.wat, p: super.foo() }; diff --git a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.broken b/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.broken deleted file mode 100644 index 2335516ef9b0..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.broken +++ /dev/null @@ -1,7 +0,0 @@ - x Expected ident - ,-[$DIR/tests/tsc/thisTypeInAccessors.strip.js:11:1] - 10 | n: 12, - 11 | get x(this: Foo) { return this.n; }, - : ^^^^ - 12 | set x(this , n ) { this.n = n; } - `---- diff --git a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js b/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js deleted file mode 100644 index 8558467123ed..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js +++ /dev/null @@ -1,38 +0,0 @@ -// @noImplicitAny: true -// @noImplicitThis: true -// @target: es5 - - - - - -const explicit = { - n: 12, - get x(this: Foo) { return this.n; }, - set x(this , n ) { this.n = n; } -} -const copiedFromGetter = { - n: 14, - get x(this: Foo) { return this.n; }, - set x(n) { this.n = n; } -} -const copiedFromSetter = { - n: 15, - get x() { return this.n }, - set x(this , n ) { this.n = n; } -} -const copiedFromGetterUnannotated = { - n: 16, - get x(this: Foo) { return this.n }, - set x(this, n) { this.n = n; } -} - -class Explicit { - n = 17; - get x( ) { return this.n; } - set x( n ) { this.n = n; } -} -class Contextual { - n = 21; - get x() { return this.n } // inside a class, so already correct -} diff --git a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.broken b/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.broken deleted file mode 100644 index a8674e1ca618..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.broken +++ /dev/null @@ -1,7 +0,0 @@ - x Expected ident - ,-[$DIR/tests/tsc/thisTypeInAccessorsNegative.strip.js:13:1] - 12 | n: 13, - 13 | get x(this: Foo) { return this.n; }, - : ^^^^ - 14 | set x(this , n) { this.wrong = "method"; } - `---- diff --git a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.js b/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.js deleted file mode 100644 index 3ee08a14d55b..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.js +++ /dev/null @@ -1,19 +0,0 @@ -// @noImplicitAny: true -// @noImplicitThis: true -// @target: es5 - - - - - - - -const mismatch = { - n: 13, - get x(this: Foo) { return this.n; }, - set x(this , n) { this.wrong = "method"; } -} -const contextual = { - n: 16, - get x() { return this.n; } -} diff --git a/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.broken b/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.broken deleted file mode 100644 index ba26229a1d56..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.broken +++ /dev/null @@ -1,14 +0,0 @@ - x Expression expected - ,-[$DIR/tests/tsc/typeOfThisInStaticMembers9.strip.js:9:1] - 8 | static arrowFunctionBoundary = () => super.f + 1; - 9 | static functionExprBoundary = function () { return super.f + 2 }; - : ^ - 10 | static classExprBoundary = class { a = super.f + 3 }; - `---- - x Expression expected - ,-[$DIR/tests/tsc/typeOfThisInStaticMembers9.strip.js:13:1] - 12 | function foo () { - 13 | return super.f + 4 - : ^ - 14 | } - `---- diff --git a/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.js b/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.js deleted file mode 100644 index 8c5fc9308b1e..000000000000 --- a/crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.js +++ /dev/null @@ -1,22 +0,0 @@ -// @target: esnext, es2022, es6, es5 - -class C { - static f = 1 -} - -class D extends C { - static arrowFunctionBoundary = () => super.f + 1; - static functionExprBoundary = function () { return super.f + 2 }; - static classExprBoundary = class { a = super.f + 3 }; - static functionAndClassDeclBoundary = (() => { - function foo () { - return super.f + 4 - } - class C { - a = super.f + 5 - method () { - return super.f +6 - } - } - })(); -}