Skip to content

Commit fb76e3c

Browse files
committed
fix(transform/styled-components): fix spans of quasis when some quasis removed (#12240)
In CSS minification: Input: ```js styled.div`color: /* ${c} */ blue` // ^^^^^^^^^^ ^^^^^^^^ spans of `quasis[0]` and `quasis[1]` ``` Minified: ```js styled.div`color:blue` ``` The one remaining quasi needs to have span which covers both original quasis: ```js styled.div`color: /* ${c} */ blue` // ^^^^^^^^^^^^^^^^^^^^^^ ```
1 parent 8e13e2a commit fb76e3c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,27 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
891891
// TODO: What about `cooked`? Shouldn't we alter that too?
892892
let mut quasi_index = 0;
893893
while quasi_index < quasis.len() {
894-
let mut bytes = quasis[quasi_index].value.raw.as_str().as_bytes();
894+
let quasi = &quasis[quasi_index];
895+
let mut bytes = quasi.value.raw.as_str().as_bytes();
895896

896897
if quasi_index > 0 {
897898
if let Some(is_block_comment) = comment_type {
899+
// This quasi is being merged into previous.
900+
// Extend span end of previous quasi to this quasi's span end, as previous quasi now covers both.
901+
//
902+
// ```
903+
// // Input:
904+
// styled.div`color: /* ${c} */ blue`
905+
// // ^^^^^^^^^^ ^^^^^^^^ spans of `quasis[0]` and `quasis[1]`
906+
//
907+
// // Transformed:
908+
// styled.div`color:blue`
909+
// // `quasis[0]` in transformed `TemplateLiteral` has span of:
910+
// styled.div`color: /* ${c} */ blue`
911+
// // ^^^^^^^^^^^^^^^^^^^^^^
912+
// ```
913+
quasis[quasi_index - 1].span.end = quasi.span.end;
914+
898915
// Remove this quasi and the preceding expression.
899916
// TODO: Remove scopes, symbols, and references for removed `Expression`.
900917
quasis.remove(quasi_index);

0 commit comments

Comments
 (0)