Skip to content

Commit

Permalink
fix(es/typescript): Strip class modifiers (#9399)
Browse files Browse the repository at this point in the history
Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
  • Loading branch information
magic-akari and kdy1 committed Aug 9, 2024
1 parent ab356e9 commit 124e5ff
Show file tree
Hide file tree
Showing 69 changed files with 153 additions and 1,021 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-birds-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_fast_ts_strip: patch
swc_core: patch
---

fix(es/typescript): Strip class modifiers
162 changes: 114 additions & 48 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ use swc_common::{
BytePos, FileName, Mark, SourceMap, Span, Spanned,
};
use swc_ecma_ast::{
ArrowExpr, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, Decl, DoWhileStmt,
EsVersion, ExportAll, ExportDecl, ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt,
ForOfStmt, ForStmt, IfStmt, ImportDecl, ImportSpecifier, NamedExport, Param, Pat, Program,
Stmt, TsAsExpr, TsConstAssertion, TsEnumDecl, TsExportAssignment, TsImportEqualsDecl,
TsIndexSignature, TsInstantiation, TsModuleDecl, TsModuleName, TsNamespaceDecl, TsNonNullExpr,
TsParamPropParam, TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion,
TsTypeParamDecl, TsTypeParamInstantiation, WhileStmt,
ArrowExpr, AutoAccessor, BindingIdent, Class, ClassDecl, ClassMethod, ClassProp, Constructor,
Decl, DoWhileStmt, EsVersion, ExportAll, ExportDecl, ExportDefaultDecl, ExportSpecifier,
FnDecl, ForInStmt, ForOfStmt, ForStmt, IfStmt, ImportDecl, ImportSpecifier, NamedExport, Param,
Pat, PrivateMethod, PrivateProp, Program, Stmt, TsAsExpr, TsConstAssertion, TsEnumDecl,
TsExportAssignment, TsImportEqualsDecl, TsIndexSignature, TsInstantiation, TsModuleDecl,
TsModuleName, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr,
TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation,
WhileStmt,
};
use swc_ecma_parser::{
lexer::Lexer,
Expand Down Expand Up @@ -441,6 +442,59 @@ impl TsStrip {
self.add_overwrite(span.lo, b';');
}
}

fn strip_class_modifier(&mut self, mut start_pos: BytePos, key_pos: BytePos) {
let mut index = self.get_next_token_index(start_pos);

while start_pos < key_pos {
let TokenAndSpan { token, span, .. } = &self.tokens[index];
start_pos = span.hi;
index += 1;

let next = &self.tokens[index];

if next.had_line_break {
return;
}

// see ts_next_token_can_follow_modifier
// class { public public() {} }
if !matches!(
next.token,
Token::LBracket
| Token::LBrace
| Token::BinOp(BinOpToken::Mul)
| Token::DotDotDot
| Token::Hash
| Token::Word(_)
| Token::Str { .. }
| Token::Num { .. }
| Token::BigInt { .. }
) {
return;
}

match token {
Token::Word(Word::Ident(IdentLike::Known(KnownIdent::Static))) => {
continue;
}
Token::Word(Word::Ident(IdentLike::Known(
KnownIdent::Readonly
| KnownIdent::Public
| KnownIdent::Protected
| KnownIdent::Private,
))) => {
self.add_replacement(*span);
}
Token::Word(Word::Ident(IdentLike::Other(o))) if *o == "override" => {
self.add_replacement(*span);
}
_ => {
return;
}
}
}
}
}

impl Visit for TsStrip {
Expand Down Expand Up @@ -532,32 +586,31 @@ impl Visit for TsStrip {
n.visit_children_with(self);
}

fn visit_constructor(&mut self, n: &Constructor) {
if n.body.is_none() {
self.add_replacement(n.span);
return;
}

self.strip_class_modifier(n.span_lo(), n.key.span_lo());

n.visit_children_with(self);
}

fn visit_class_method(&mut self, n: &ClassMethod) {
if n.function.body.is_none() || n.is_abstract {
self.add_replacement(n.span);
return;
}

let key_pos = n.key.span_lo();
let mut pos = n.span_lo();
let mut index = self.get_next_token_index(pos);
// @foo public m(): void {}
let start_pos = n
.function
.decorators
.last()
.map_or(n.span_lo(), |d| d.span_hi());

while pos < key_pos {
let TokenAndSpan { token, span, .. } = &self.tokens[index];
pos = span.hi;
index += 1;
match token {
Token::Word(Word::Ident(IdentLike::Known(
KnownIdent::Public | KnownIdent::Protected | KnownIdent::Private,
))) => {
self.add_replacement(*span);
}
Token::Word(Word::Ident(IdentLike::Other(o))) if *o == "override" => {
self.add_replacement(*span);
}
_ => {}
}
}
self.strip_class_modifier(start_pos, n.key.span_lo());

n.visit_children_with(self);
}
Expand All @@ -568,29 +621,9 @@ impl Visit for TsStrip {
return;
}

let key_pos = n.key.span_lo();
let mut pos = n.span_lo();
let mut index = self.get_next_token_index(pos);
let start_pos = n.decorators.last().map_or(n.span_lo(), |d| d.span_hi());

while pos < key_pos {
let TokenAndSpan { token, span, .. } = &self.tokens[index];
pos = span.hi;
index += 1;
match token {
Token::Word(Word::Ident(IdentLike::Known(
KnownIdent::Readonly
| KnownIdent::Public
| KnownIdent::Protected
| KnownIdent::Private,
))) => {
self.add_replacement(*span);
}
Token::Word(Word::Ident(IdentLike::Other(o))) if *o == "override" => {
self.add_replacement(*span);
}
_ => {}
}
}
self.strip_class_modifier(start_pos, n.key.span_lo());

if n.is_optional {
let optional_mark = self.get_next_token(n.key.span_hi());
Expand All @@ -615,6 +648,39 @@ impl Visit for TsStrip {
n.visit_children_with(self);
}

fn visit_private_method(&mut self, n: &PrivateMethod) {
let start_pos = n
.function
.decorators
.last()
.map_or(n.span_lo(), |d| d.span_hi());

self.strip_class_modifier(start_pos, n.key.span_lo());

n.visit_children_with(self);
}

fn visit_private_prop(&mut self, n: &PrivateProp) {
let start_pos = n.decorators.last().map_or(n.span_lo(), |d| d.span_hi());

self.strip_class_modifier(start_pos, n.key.span_lo());

n.visit_children_with(self);
}

fn visit_auto_accessor(&mut self, n: &AutoAccessor) {
if n.is_abstract {
self.add_replacement(n.span);
return;
}

let start_pos = n.decorators.last().map_or(n.span_lo(), |d| d.span_hi());

self.strip_class_modifier(start_pos, n.key.span_lo());

n.visit_children_with(self);
}

fn visit_export_all(&mut self, n: &ExportAll) {
if n.type_only {
self.add_replacement(n.span);
Expand Down
7 changes: 0 additions & 7 deletions crates/swc_fast_ts_strip/tests/tsc/Protected3.strip.broken

This file was deleted.

3 changes: 0 additions & 3 deletions crates/swc_fast_ts_strip/tests/tsc/Protected3.strip.js

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions crates/swc_fast_ts_strip/tests/tsc/autoAccessor7.strip.broken

This file was deleted.

14 changes: 0 additions & 14 deletions crates/swc_fast_ts_strip/tests/tsc/autoAccessor7.strip.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
x Unexpected token `accessor`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/autoAccessorAllowedModifiers.strip.js:6:1]
5 | accessor a ;
6 | public accessor b ;
: ^^^^^^^^
7 | private accessor c ;
`----
x Unexpected token `!`. Expected * for generator, private key, identifier or async
,-[$DIR/tests/tsc/autoAccessorAllowedModifiers.strip.js:18:1]
17 | accessor ["m"] ;
18 | accessor n! ;
: ^
19 | }
`----
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

class C1 {
accessor a ;
public accessor b ;
private accessor c ;
protected accessor d ;
abstract accessor e ;
accessor b ;
accessor c ;
accessor d ;
static accessor f ;
public static accessor g ;
private static accessor h ;
protected static accessor i ;
static accessor g ;
static accessor h ;
static accessor i ;
accessor #j ;
accessor "k" ;
accessor 108 ;
Expand All @@ -19,8 +19,8 @@
}

class C2 extends C1 {
override accessor e ;
static override accessor i ;
accessor e ;
static accessor i ;
}


Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 124e5ff

Please sign in to comment.