Skip to content

Commit

Permalink
ts this param
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jul 5, 2024
1 parent 60ec5ce commit f2c5d06
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use swc_common::{
BytePos, FileName, SourceMap, Span, Spanned,
};
use swc_ecma_ast::{
BindingIdent, Decorator, EsVersion, Program, TsAsExpr, TsConstAssertion, TsEnumDecl,
TsInstantiation, TsModuleDecl, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam,
BindingIdent, Decorator, EsVersion, Ident, Param, Pat, Program, TsAsExpr, TsConstAssertion,
TsEnumDecl, TsInstantiation, TsModuleDecl, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam,
TsSatisfiesExpr, TsTypeAliasDecl, TsTypeAnn,
};
use swc_ecma_parser::{
Expand Down Expand Up @@ -221,6 +221,27 @@ impl Visit for TsStrip {
self.add_replacement(span(n.id.span.hi, n.id.span.hi + BytePos(1)));
}
}

fn visit_params(&mut self, n: &[Param]) {
if let Some(p) = n.first().filter(|param| {
matches!(
&param.pat,
Pat::Ident(BindingIdent {
id: Ident { sym, .. },
..
}) if &**sym == "this"
)
}) {
let lo = p.span.lo;
let hi = n.get(1).map(|x| x.span.lo).unwrap_or(p.span.hi);
self.add_replacement(span(lo, hi));

n[1..].visit_children_with(self);
return;
}

n.visit_children_with(self);
}
}

fn span(lo: BytePos, hi: BytePos) -> Span {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/this-param.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function foo() {}
export function bar(x) {}
2 changes: 2 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/this-param.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function foo(this: number): void {}
export function bar(this: number, x: string): void {}

0 comments on commit f2c5d06

Please sign in to comment.