Skip to content

Commit b20f2ab

Browse files
committed
pref(transformer/styled-components): remove quasis and expressions in batch
1 parent 8152fe7 commit b20f2ab

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ fn is_valid_styled_component_source(source: &str) -> bool {
868868
/// ```
869869
fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a>) {
870870
const NOT_IN_STRING: u8 = 0;
871+
const QUASI_DELETION_MARKER: Atom = Atom::new_const("__QUASI_DELETION_MARKER__");
871872

872873
let TemplateLiteral { quasis, expressions, .. } = lit;
873874

@@ -883,6 +884,8 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
883884
let mut string_quote = NOT_IN_STRING;
884885
// Parentheses depth. `((x)` -> 1, `(x))` -> -1.
885886
let mut paren_depth: isize = 0;
887+
// A state that indicates whether we should delete quasi and expression.
888+
let mut has_deletion_marker = false;
886889

887890
// Current minified quasi being built
888891
let mut output = Vec::new();
@@ -911,11 +914,10 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
911914
// // ^^^^^^^^^^^^^^^^^^^^^^
912915
// ```
913916
quasis[quasi_index - 1].span.end = quasi.span.end;
914-
915-
// Remove this quasi and the preceding expression.
916-
// TODO: Remove scopes, symbols, and references for removed `Expression`.
917-
quasis.remove(quasi_index);
918-
expressions.remove(quasi_index - 1);
917+
// Mark previous quasi for deletion, it will be deleted all of quasis are processed.
918+
quasis[quasi_index - 1].value.raw = QUASI_DELETION_MARKER;
919+
has_deletion_marker = true;
920+
quasi_index += 1;
919921

920922
// Find end of comment
921923
let start_index = if is_block_comment {
@@ -1087,6 +1089,14 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
10871089
// SAFETY: Output is all picked from the original `raw` values and is guaranteed to be valid UTF-8.
10881090
let output_str = unsafe { std::str::from_utf8_unchecked(&output) };
10891091
quasis.last_mut().unwrap().value.raw = ast.atom(output_str);
1092+
1093+
// Remove quasis that were marked for deletion as well as their preceding expressions.
1094+
if has_deletion_marker {
1095+
let mut quasis_iter = quasis.iter();
1096+
// TODO: Remove scopes, symbols, and references for removed `Expression`.
1097+
expressions.retain(|_| quasis_iter.next().unwrap().value.raw != QUASI_DELETION_MARKER);
1098+
quasis.retain(|quasi| quasi.value.raw != QUASI_DELETION_MARKER);
1099+
}
10901100
}
10911101

10921102
#[cfg(test)]

0 commit comments

Comments
 (0)