Skip to content

Commit

Permalink
fix(es/typescript): Handle backtick in ASI issue
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Aug 1, 2024
1 parent 4f0fc6e commit 94a89f6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
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`

0 comments on commit 94a89f6

Please sign in to comment.