Skip to content
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
47 changes: 16 additions & 31 deletions crates/oxc_transformer/src/plugins/styled_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,34 +920,24 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a

// Find end of comment
let start_index = if is_block_comment {
let Some(mut pos) = bytes.windows(2).position(|q| q == b"*/") else {
let Some(pos) = bytes.windows(2).position(|q| q == b"*/") else {
// Comment contains whole of this quasi
continue;
};

pos += 2;
if pos == bytes.len() {
// Comment ends at end of quasi
comment_type = None;
continue;
}

// Add a space when this is a own line block comment
if !bytes[pos].is_ascii_whitespace()
&& output.last().is_some_and(|&last| last != b' ')
{
output.push(b' ');
}

pos
pos + 2 // After `*/`
} else {
let Some(pos) = bytes.iter().position(|&b| matches!(b, b'\n' | b'\r')) else {
// Comment contains whole of this quasi
continue;
};
pos
pos + 1 // After `\n` or `\r`
};

// Comments behave like whitespace.
// Block comments: `padding: 10/* */0` is equivalent to `padding: 10 0`, not `padding: 100`.
// Line comments: End with a newline (whitespace).
insert_space_if_required(&mut output, quasi_index);

// Trim off to end of comment
bytes = &bytes[start_index..];

Expand Down Expand Up @@ -1018,19 +1008,11 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
let end_index =
bytes[i + 2..].windows(2).position(|q| q == b"*/");
if let Some(end_index) = end_index {
i += end_index + 4; // After `*/`
// Block comments behave like whitespace.
// `padding: 10/* */0` is equivalent to `padding: 10 0`, not `padding: 100`.
insert_space_if_required(&mut output, quasi_index);

if i == bytes.len() {
// Comment ends at end of quasi
break;
}

// Add a space when this is a own line block comment
if !bytes[i].is_ascii_whitespace()
&& output.last().is_some_and(|&last| last != b' ')
{
output.push(b' ');
}
i += end_index + 4; // After `*/`
continue;
}

Expand All @@ -1046,7 +1028,10 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
let end_index =
bytes[i + 2..].iter().position(|&b| matches!(b, b'\n' | b'\r'));
if let Some(end_index) = end_index {
i += end_index + 2; // On `\n` or `\r`
// Insert space in place of `\n` / `\r`
insert_space_if_required(&mut output, quasi_index);

i += end_index + 3; // After `\n` or `\r`
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const Bar = styled.div`
const Qux = styled.div`
color: /* blah */ red;
.a /* blah */ { color: blue; }
.b {/* blah */color: green; }
`;

const Bing = styled.div`
color: /* ${123} */ red;
.a /* ${123} */ { color: blue; }
.b {/* ${123} */color: green; }
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styled from 'styled-components';
const Button = styled.div`${x}${z}`;
const Button = styled.div`${x} ${z}`;
const Foo = styled.div`.a{color:red;}`;
const Bar = styled.div`.a{color:red;}`;
const Qux = styled.div`color:red;.a{color:blue;}`;
const Bing = styled.div`color:red;.a{color:blue;}`;
const Qux = styled.div`color:red;.a{color:blue;}.b{color:green;}`;
const Bing = styled.div`color:red;.a{color:blue;}.b{color:green;}`;
Loading