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(es/typescript): Handle backtick in ASI issue #9367

Merged
merged 2 commits into from
Aug 1, 2024
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-ads-fix.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): Handle backtick in ASI issue
8 changes: 5 additions & 3 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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';');
Expand All @@ -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]
Expand Down
10 changes: 9 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/issue-9331.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ class foo {

2
;
+ 3
+ 3


f
;
`1`

f ;
`1`
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ class foo {
}
2;
+3;
f;
`1`;
f;
`1`;
export { };
10 changes: 9 additions & 1 deletion crates/swc_fast_ts_strip/tests/fixture/issue-9331.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ class foo {

2
export type { }
+ 3
+ 3


f
declare namespace N { }
`1`

f as any
`1`
Loading