diff --git a/.changeset/grumpy-ads-fix.md b/.changeset/grumpy-ads-fix.md new file mode 100644 index 000000000000..c150edaebe18 --- /dev/null +++ b/.changeset/grumpy-ads-fix.md @@ -0,0 +1,6 @@ +--- +swc_fast_ts_strip: patch +swc_core: patch +--- + +fix(es/typescript): Handle backtick in ASI issue diff --git a/crates/swc_fast_ts_strip/src/lib.rs b/crates/swc_fast_ts_strip/src/lib.rs index 52774f924e93..fdc303235ba7 100644 --- a/crates/swc_fast_ts_strip/src/lib.rs +++ b/crates/swc_fast_ts_strip/src/lib.rs @@ -406,10 +406,12 @@ impl TsStrip { return; } - // Add a semicolon if the next token is `[`, `(`, `/`, `+`, or `-` + // https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-asi-interesting-cases-in-statement-lists + // Add a semicolon if the next token is `[`, `(`, `/`, `+`, `-` or backtick. match token { Token::LParen | Token::LBracket + | Token::BackQuote | Token::BinOp(BinOpToken::Add | BinOpToken::Sub | BinOpToken::Div) => { if prev_token == &Token::Semi { self.add_overwrite(prev_span.lo, b';'); @@ -430,8 +432,8 @@ impl TsStrip { } if let TokenAndSpan { - // Only `([` affect ASI. - token: Token::LParen | Token::LBracket, + // Only `(`, `[` and backtick affect ASI. + token: Token::LParen | Token::LBracket | Token::BackQuote, had_line_break: true, .. } = &self.tokens[index + 1] diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.js b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.js index 8cc1bd1ef7f7..a879d3d011f9 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.js +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.js @@ -42,4 +42,12 @@ class foo { 2 ; -+ 3 \ No newline at end of file ++ 3 + + +f +; +`1` + +f ; +`1` \ No newline at end of file diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.transform.js b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.transform.js index 983ca73e73c1..377fae926aaa 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.transform.js +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.transform.js @@ -21,4 +21,8 @@ class foo { } 2; +3; +f; +`1`; +f; +`1`; export { }; diff --git a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.ts b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.ts index 7cfb24babfea..13dfd74645ee 100644 --- a/crates/swc_fast_ts_strip/tests/fixture/issue-9331.ts +++ b/crates/swc_fast_ts_strip/tests/fixture/issue-9331.ts @@ -42,4 +42,12 @@ class foo { 2 export type { } -+ 3 \ No newline at end of file ++ 3 + + +f +declare namespace N { } +`1` + +f as any +`1` \ No newline at end of file