Skip to content

Commit

Permalink
fix(es/typescript): Strip this param in getter/setter (#9414)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Aug 9, 2024
1 parent b395f48 commit 442fb7b
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 247 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-onions-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_fast_ts_strip: patch
---

fix(es/typescript): Strip `this` param in getter/setter
40 changes: 34 additions & 6 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_fast_ts_strip/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn reparse(cm: &Lrc<SourceMap>, 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,
Expand Down

This file was deleted.

127 changes: 0 additions & 127 deletions crates/swc_fast_ts_strip/tests/tsc/errorSuperPropertyAccess.strip.js

This file was deleted.

This file was deleted.

38 changes: 0 additions & 38 deletions crates/swc_fast_ts_strip/tests/tsc/thisTypeInAccessors.strip.js

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 442fb7b

Please sign in to comment.