mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 09:38:16 +03:00
fix(es/typescript): Fix ASI in expression for fast strip (#9358)
- Closes #9355
This commit is contained in:
parent
24e87985d4
commit
3ee82e223f
5
.changeset/polite-tools-lie.md
Normal file
5
.changeset/polite-tools-lie.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
swc_fast_ts_strip: patch
|
||||
---
|
||||
|
||||
fix(es/typescript): Fix ASI in expression for TypeScript strip
|
@ -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 {
|
||||
@ -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 {
|
||||
@ -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);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
20
crates/swc_fast_ts_strip/tests/fixture/issue-9355.js
Normal file
20
crates/swc_fast_ts_strip/tests/fixture/issue-9355.js
Normal file
@ -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];
|
@ -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
crates/swc_fast_ts_strip/tests/fixture/issue-9355.ts
Normal file
20
crates/swc_fast_ts_strip/tests/fixture/issue-9355.ts
Normal file
@ -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…
Reference in New Issue
Block a user