Skip to content

Commit

Permalink
fix(es/typescript): Fix ASI in expression for fast strip (#9358)
Browse files Browse the repository at this point in the history
- Closes #9355
  • Loading branch information
magic-akari committed Jul 31, 2024
1 parent 24e8798 commit 3ee82e2
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-tools-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_fast_ts_strip: patch
---

fix(es/typescript): Fix ASI in expression for TypeScript strip
50 changes: 48 additions & 2 deletions crates/swc_fast_ts_strip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,21 @@ impl TsStrip {
..
} = &self.tokens[index - 1];

let index = self.get_prev_token_index(span.hi);
let index = self.get_prev_token_index(span.hi - BytePos(1));
if index == self.tokens.len() - 1 {
// Skip if the token is the last token.
return;
}

let TokenAndSpan { token, .. } = &self.tokens[index + 1];
let TokenAndSpan {
token,
had_line_break,
..
} = &self.tokens[index + 1];

if !*had_line_break {
return;
}

// Add a semicolon if the next token is `[`, `(`, `/`, `+`, or `-`
match token {
Expand All @@ -413,6 +421,23 @@ impl TsStrip {
_ => {}
}
}

fn fix_asi_in_expr(&mut self, span: Span) {
let index = self.get_prev_token_index(span.hi - BytePos(1));
if index == self.tokens.len() - 1 {
return;
}

if let TokenAndSpan {
// Only `([` affect ASI.
token: Token::LParen | Token::LBracket,
had_line_break: true,
..
} = &self.tokens[index + 1]
{
self.add_overwrite(span.lo, b';');
}
}
}

impl Visit for TsStrip {
Expand Down Expand Up @@ -708,6 +733,16 @@ impl Visit for TsStrip {

fn visit_ts_as_expr(&mut self, n: &TsAsExpr) {
self.add_replacement(span(n.expr.span().hi, n.span.hi));
let TokenAndSpan {
token,
span: as_span,
..
} = self.get_next_token(n.expr.span_hi());
debug_assert_eq!(
token,
&Token::Word(Word::Ident(IdentLike::Known(KnownIdent::As)))
);
self.fix_asi_in_expr(span(as_span.lo, n.span.hi));

n.expr.visit_children_with(self);
}
Expand Down Expand Up @@ -820,6 +855,17 @@ impl Visit for TsStrip {
fn visit_ts_satisfies_expr(&mut self, n: &TsSatisfiesExpr) {
self.add_replacement(span(n.expr.span().hi, n.span.hi));

let TokenAndSpan {
token,
span: as_span,
..
} = self.get_next_token(n.expr.span_hi());
debug_assert_eq!(
token,
&Token::Word(Word::Ident(IdentLike::Known(KnownIdent::Satisfies)))
);
self.fix_asi_in_expr(span(as_span.lo, n.span.hi));

n.expr.visit_children_with(self);
}

Expand Down
20 changes: 20 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9355.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const x1 = 10 ;
(1)

const x2 = (10);
(1)

const x3 = 10 ;
(1)

const x4 = (10);
(1)

const y = 10
+ 1

const z = 10
/ 1

const w = 10 ;
[1];
14 changes: 14 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9355.transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const x1 = 10;
1;
const x2 = 10;
1;
const x3 = 10;
1;
const x4 = 10;
1;
const y = 10 + 1;
const z = 10 / 1;
const w = 10;
[
1
];
20 changes: 20 additions & 0 deletions crates/swc_fast_ts_strip/tests/fixture/issue-9355.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const x1 = 10 as any
(1)

const x2 = (10)as any
(1)

const x3 = 10 satisfies any
(1)

const x4 = (10)satisfies any
(1)

const y = 10 as any
+ 1

const z = 10 as any
/ 1

const w = 10 as any
[1];

0 comments on commit 3ee82e2

Please sign in to comment.