mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 07:02:28 +03:00
feat(es/typescript): Improve fast TS strip (#9167)
This commit is contained in:
parent
18080f79d2
commit
98af5890da
@ -2,9 +2,18 @@
|
||||
|
||||
exports[`transform in strip-only mode should remove declare enum 1`] = `" "`;
|
||||
|
||||
exports[`transform in strip-only mode should remove declare enum 2`] = `" "`;
|
||||
exports[`transform in strip-only mode should remove declare enum 2`] = `
|
||||
"
|
||||
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`transform in strip-only mode should remove declare enum 3`] = `" "`;
|
||||
exports[`transform in strip-only mode should remove declare enum 3`] = `
|
||||
"
|
||||
|
||||
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`transform in strip-only mode should strip complex expressions 1`] = `
|
||||
"const foo = {
|
||||
|
@ -168,22 +168,28 @@ impl TsStrip {
|
||||
&self.src[(span.lo.0 - 1) as usize..(span.hi.0 - 1) as usize]
|
||||
}
|
||||
|
||||
fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan {
|
||||
fn get_next_token_index(&self, pos: BytePos) -> usize {
|
||||
let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo);
|
||||
let index = match index {
|
||||
match index {
|
||||
Ok(index) => index,
|
||||
Err(index) => index,
|
||||
};
|
||||
&self.tokens[index]
|
||||
}
|
||||
}
|
||||
|
||||
fn get_next_token(&self, pos: BytePos) -> &TokenAndSpan {
|
||||
&self.tokens[self.get_next_token_index(pos)]
|
||||
}
|
||||
|
||||
fn get_prev_token_index(&self, pos: BytePos) -> usize {
|
||||
let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo);
|
||||
match index {
|
||||
Ok(index) => index,
|
||||
Err(index) => index - 1,
|
||||
}
|
||||
}
|
||||
|
||||
fn get_prev_token(&self, pos: BytePos) -> &TokenAndSpan {
|
||||
let index = self.tokens.binary_search_by_key(&pos, |t| t.span.lo);
|
||||
let index = match index {
|
||||
Ok(index) => index,
|
||||
Err(index) => index - 1,
|
||||
};
|
||||
&self.tokens[index]
|
||||
&self.tokens[self.get_prev_token_index(pos)]
|
||||
}
|
||||
}
|
||||
|
||||
@ -218,7 +224,7 @@ impl Visit for TsStrip {
|
||||
// 1;
|
||||
// ```
|
||||
|
||||
self.add_overwrite(ret.span_hi(), b')');
|
||||
self.add_overwrite(ret.span_hi() - BytePos(1), b')');
|
||||
}
|
||||
}
|
||||
|
||||
@ -264,6 +270,10 @@ impl Visit for TsStrip {
|
||||
if !n.implements.is_empty() {
|
||||
let implements =
|
||||
self.get_prev_token(n.implements.first().unwrap().span_lo() - BytePos(1));
|
||||
debug_assert_eq!(
|
||||
implements.token,
|
||||
Token::Word(Word::Ident(IdentLike::Known(KnownIdent::Implements)))
|
||||
);
|
||||
|
||||
let last = n.implements.last().unwrap();
|
||||
let span = span(implements.span.lo, last.span.hi);
|
||||
@ -279,13 +289,14 @@ impl Visit for TsStrip {
|
||||
return;
|
||||
}
|
||||
|
||||
let key_span = n.key.span_hi();
|
||||
let method_pos = n.function.span_lo();
|
||||
let key_pos = n.key.span_lo();
|
||||
let mut pos = n.span_lo();
|
||||
let mut index = self.get_next_token_index(pos);
|
||||
|
||||
let mut pos = method_pos;
|
||||
while pos < key_span {
|
||||
let TokenAndSpan { token, span, .. } = self.get_next_token(pos);
|
||||
while pos < key_pos {
|
||||
let TokenAndSpan { token, span, .. } = &self.tokens[index];
|
||||
pos = span.hi;
|
||||
index += 1;
|
||||
match token {
|
||||
Token::Word(Word::Ident(IdentLike::Known(
|
||||
KnownIdent::Public | KnownIdent::Protected | KnownIdent::Private,
|
||||
@ -308,13 +319,14 @@ impl Visit for TsStrip {
|
||||
return;
|
||||
}
|
||||
|
||||
let key_span = n.key.span_hi();
|
||||
let method_pos = n.span_lo();
|
||||
let key_pos = n.key.span_lo();
|
||||
let mut pos = n.span_lo();
|
||||
let mut index = self.get_next_token_index(pos);
|
||||
|
||||
let mut pos = method_pos;
|
||||
while pos < key_span {
|
||||
let TokenAndSpan { token, span, .. } = self.get_next_token(pos);
|
||||
while pos < key_pos {
|
||||
let TokenAndSpan { token, span, .. } = &self.tokens[index];
|
||||
pos = span.hi;
|
||||
index += 1;
|
||||
match token {
|
||||
Token::Word(Word::Ident(IdentLike::Known(
|
||||
KnownIdent::Readonly
|
||||
|
@ -142,36 +142,41 @@ void 0;
|
||||
// `=>` spanning line cases:
|
||||
{
|
||||
(
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
{
|
||||
(
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
{
|
||||
(
|
||||
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
{
|
||||
(
|
||||
|
||||
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
{
|
||||
(
|
||||
|
||||
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
{
|
||||
(a, b, c = [] /*comment-1*/ /*comment-2*/
|
||||
)=>
|
||||
) =>
|
||||
1
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
(
|
||||
)=>
|
||||
1;
|
||||
|
@ -174,4 +174,9 @@ void 0;
|
||||
(a, b, c: D = [] as any/*comment-1*/)/*comment-2*/:
|
||||
any =>
|
||||
1
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
():
|
||||
any=>
|
||||
1;
|
||||
|
Loading…
Reference in New Issue
Block a user