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): Fix ASI in expression for fast strip #9358

Merged
merged 2 commits into from
Jul 31, 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
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];
Loading