Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ts/fast-strip): More robust generic arrow handling #9913

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/grumpy-clocks-boil.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/ts_strip): More robust generic arrow handling
97 changes: 69 additions & 28 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use swc_ecma_ast::{
Constructor, Decl, DefaultDecl, DoWhileStmt, EsVersion, ExportAll, ExportDecl,
ExportDefaultDecl, ExportSpecifier, FnDecl, ForInStmt, ForOfStmt, ForStmt, GetterProp, IfStmt,
ImportDecl, ImportSpecifier, NamedExport, ObjectPat, Param, Pat, PrivateMethod, PrivateProp,
Program, ReturnStmt, SetterProp, Stmt, TsAsExpr, TsConstAssertion, TsEnumDecl,
Program, ReturnStmt, SetterProp, Stmt, ThrowStmt, TsAsExpr, TsConstAssertion, TsEnumDecl,
TsExportAssignment, TsImportEqualsDecl, TsIndexSignature, TsInstantiation, TsModuleDecl,
TsModuleName, TsNamespaceDecl, TsNonNullExpr, TsParamPropParam, TsSatisfiesExpr,
TsTypeAliasDecl, TsTypeAnn, TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation,
VarDeclarator, WhileStmt,
VarDeclarator, WhileStmt, YieldExpr,
};
use swc_ecma_parser::{
lexer::Lexer,
Expand Down Expand Up @@ -596,6 +596,34 @@ impl TsStrip {

self.add_replacement(*span);
}

// ```TypeScript
// return/yield/throw <T>
// (v: T) => v;
// ```
//
// ```TypeScript
// return/yield/throw (
// v ) => v;
// ```
fn fix_asi_in_arrow_expr(&mut self, arrow_expr: &ArrowExpr) {
if let Some(tp) = &arrow_expr.type_params {
let l_paren = self.get_next_token(tp.span.hi);
debug_assert_eq!(l_paren.token, Token::LParen);

let slice = self.get_src_slice(tp.span.with_hi(l_paren.span.lo));

if !slice.chars().any(is_new_line) {
return;
}

let l_paren_pos = l_paren.span.lo;
let l_lt_pos = tp.span.lo;

self.add_overwrite(l_paren_pos, b' ');
self.add_overwrite(l_lt_pos, b'(');
}
}
}

impl Visit for TsStrip {
Expand Down Expand Up @@ -695,39 +723,52 @@ impl Visit for TsStrip {

arg.visit_with(self);

if let Some(arrow_expr) = arg.as_arrow() {
if arrow_expr.is_async {
// We have already handled type parameters in `visit_arrow_expr`.
return;
}
let Some(arrow_expr) = arg.as_arrow() else {
return;
};

// ```TypeScript
// return <T>
// (v: T) => v;
// ```
//
// ```TypeScript
// return (
// v ) => v;
// ```
if arrow_expr.is_async {
// We have already handled type parameters in `visit_arrow_expr`.
return;
}

if let Some(tp) = &arrow_expr.type_params {
let l_paren = self.get_next_token(tp.span.hi);
debug_assert_eq!(l_paren.token, Token::LParen);
self.fix_asi_in_arrow_expr(arrow_expr);
}

let slice = self.get_src_slice(tp.span.with_hi(l_paren.span.lo));
fn visit_yield_expr(&mut self, n: &YieldExpr) {
let Some(arg) = &n.arg else {
return;
};

if !slice.chars().any(is_new_line) {
return;
}
arg.visit_with(self);

let l_paren_pos = l_paren.span.lo;
let l_lt_pos = tp.span.lo;
let Some(arrow_expr) = arg.as_arrow() else {
return;
};

self.add_overwrite(l_paren_pos, b' ');
self.add_overwrite(l_lt_pos, b'(');
}
if arrow_expr.is_async {
// We have already handled type parameters in `visit_arrow_expr`.
return;
}

self.fix_asi_in_arrow_expr(arrow_expr);
}

fn visit_throw_stmt(&mut self, n: &ThrowStmt) {
let arg = &n.arg;

arg.visit_with(self);

let Some(arrow_expr) = arg.as_arrow() else {
return;
};

if arrow_expr.is_async {
// We have already handled type parameters in `visit_arrow_expr`.
return;
}

self.fix_asi_in_arrow_expr(arrow_expr);
}

fn visit_binding_ident(&mut self, n: &BindingIdent) {
Expand Down
21 changes: 20 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/issue-9878.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@ function f3() {

x
)=>x;
}
}

(function () {
return(
v ) => v
});
(function () {
return/**/(

/**/ v /**/
)/**/=> v
});
(function* () {
yield(
v )=>v;
});
(function* () {
throw(
v )=>v;
});
12 changes: 12 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9878.transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,15 @@ function f2() {
function f3() {
return (x)=>x;
}
(function() {
return (v)=>v;
});
(function() {
return (v)=>v;
});
(function*() {
yield (v)=>v;
});
(function*() {
throw (v)=>v;
});
21 changes: 20 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/issue-9878.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,23 @@ function f3() {
>
(x: T): Promise<
T>=>x;
}
}

(function () {
return<T>
(v: T) => v
});
(function () {
return/**/<
T
>/**/(v: T)/**/:
T/**/=> v
});
(function* () {
yield<T>
(v: T)=>v;
});
(function* () {
throw<T>
(v: T)=>v;
});
Loading