Skip to content

Commit 442fb7b

Browse files
authoredAug 9, 2024··
fix(es/typescript): Strip this param in getter/setter (#9414)
1 parent b395f48 commit 442fb7b

11 files changed

+40
-247
lines changed
 

‎.changeset/friendly-onions-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_fast_ts_strip: patch
3+
---
4+
5+
fix(es/typescript): Strip `this` param in getter/setter

‎crates/swc_fast_ts_strip/src/lib.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use swc_common::{
1313
use swc_ecma_ast::{
1414
ArrayPat, ArrowExpr, AutoAccessor, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp,
1515
Constructor, Decl, DefaultDecl, DoWhileStmt, EsVersion, ExportAll, ExportDecl,
16-
ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt, ForOfStmt, ForStmt, IfStmt, ImportDecl,
17-
ImportSpecifier, NamedExport, ObjectPat, Param, Pat, PrivateMethod, PrivateProp, Program, Stmt,
18-
TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, TsImportEqualsDecl,
19-
TsIndexSignature, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr,
20-
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion,
21-
TsTypeParamDecl, TsTypeParamInstantiation, VarDeclarator, WhileStmt,
16+
ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt, ForOfStmt, ForStmt, GetterProp, IfStmt,
17+
ImportDecl, ImportSpecifier, NamedExport, ObjectPat, Param, Pat, PrivateMethod, PrivateProp,
18+
Program, SetterProp, Stmt, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment,
19+
TsImportEqualsDecl, TsIndexSignature, TsInstantiation, TsModuleDecl, TsModuleName,
20+
TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
21+
TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation, VarDeclarator, WhileStmt,
2222
};
2323
use swc_ecma_parser::{
2424
lexer::Lexer,
@@ -1069,6 +1069,34 @@ impl Visit for TsStrip {
10691069
self.add_overwrite(n.body.span_lo(), b';');
10701070
}
10711071
}
1072+
1073+
fn visit_getter_prop(&mut self, n: &GetterProp) {
1074+
let l_parern_index = self.get_next_token_index(n.key.span_hi());
1075+
let l_parern = &self.tokens[l_parern_index];
1076+
debug_assert_eq!(l_parern.token, Token::LParen);
1077+
1078+
let r_parern_pos = n.type_ann.as_ref().map_or(n.body.span_lo(), |t| t.span.lo) - BytePos(1);
1079+
let r_parern = self.get_prev_token(r_parern_pos);
1080+
debug_assert_eq!(r_parern.token, Token::RParen);
1081+
1082+
let span = span(l_parern.span.lo + BytePos(1), r_parern.span.hi - BytePos(1));
1083+
self.add_replacement(span);
1084+
1085+
n.visit_children_with(self);
1086+
}
1087+
1088+
fn visit_setter_prop(&mut self, n: &SetterProp) {
1089+
if let Some(this_param) = &n.this_param {
1090+
self.add_replacement(this_param.span());
1091+
1092+
let comma = self.get_prev_token(n.param.span_lo() - BytePos(1));
1093+
debug_assert_eq!(comma.token, Token::Comma);
1094+
1095+
self.add_replacement(comma.span);
1096+
}
1097+
1098+
n.visit_children_with(self);
1099+
}
10721100
}
10731101

10741102
trait IsTsDecl {

‎crates/swc_fast_ts_strip/tests/fixture.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ fn reparse(cm: &Lrc<SourceMap>, handler: &Handler, filename: &PathBuf, input: St
130130
let fm = cm.new_source_file(filename.into(), input);
131131

132132
let syntax = Syntax::Es(EsSyntax {
133+
allow_super_outside_method: true,
133134
auto_accessors: true,
134135
decorators: true,
135136
decorators_before_export: true,

‎crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.broken

-7
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js

-127
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.broken

-7
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js

-38
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.broken

-7
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessorsNegative.strip.js

-19
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.broken

-14
This file was deleted.

‎crates/swc_fast_ts_strip/tests/tsc/typeOfThisInStaticMembers9.strip.js

-22
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.