Skip to content

Commit

Permalink
fix(styled-jsx): Fix comment stripping (#342)
Browse files Browse the repository at this point in the history
Closes #317
  • Loading branch information
kdy1 committed Aug 28, 2024
1 parent c07cb40 commit 4e485c6
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/styled-jsx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-styled-jsx

## 3.0.1

### Patch Changes

- fdd2dda: Fix comment stripping

## 3.0.0

### Major Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/styled-jsx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# @swc/plugin-styled-jsx

## 3.0.1

### Patch Changes

- fdd2dda: Fix comment stripping

## 3.0.0

### Major Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-styled-jsx",
"version": "3.0.0",
"version": "3.0.1",
"description": "SWC plugin for styled-jsx",
"main": "swc_plugin_styled_jsx.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/styled-jsx/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = { workspace = true }
name = "styled_jsx"
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.73.31"
version = "0.73.32"


[features]
Expand Down
69 changes: 35 additions & 34 deletions packages/styled-jsx/transform/src/transform_css_lightningcss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,46 +208,47 @@ fn convert_browsers(browsers: &Versions) -> Browsers {
}
}

pub(super) fn strip_comments(s: &str) -> Cow<str> {
if !s.contains("//") {
return Cow::Borrowed(s);
}

let mut buf = String::with_capacity(s.len());

for line in s.lines() {
let line = line.trim();

buf.push_str(strip_comment_from_line(line));

buf.push('\n');
}

Cow::Owned(buf)
/// Counts occurrences of a character inside string
fn count_occurrences(s: impl AsRef<str>, c: char) -> usize {
s.as_ref().split(c).count() - 1
}

fn strip_comment_from_line(s: &str) -> &str {
// Check for ' or "
// if there's one, it's a string literal and we should not strip it.
// After then, we should check for two `/`s

let s = s.trim();

let mut in_string = false;
let mut last = '\0';
for (i, c) in s.char_indices() {
if c == '\'' || c == '"' {
in_string = !(in_string && last != '\\');
/// Joins substrings until predicate returns true
fn reduce_substr(
substrs: impl IntoIterator<Item = impl AsRef<str>>,
join: &str,
predicate: impl Fn(&str) -> bool,
) -> String {
let mut res = "".to_string();

for (i, substr) in substrs.into_iter().enumerate() {
if i == 0 {
res.push_str(substr.as_ref());
continue;
}

if !in_string && last == '/' && c == '/' {
return &s[..i - 1];
if predicate(&res) {
break;
}

last = c;
res.push_str(join.as_ref());
res.push_str(substr.as_ref());
}

s
res
}

pub(crate) fn strip_comments(s: &str) -> String {
s.lines().map(strip_line_comment).collect()
}

/// Joins at comment starts when it's inside a string or parentheses
/// effectively removing line comments
fn strip_line_comment(line: &str) -> String {
reduce_substr(line.split("//"), "//", |s| {
!s.ends_with(':') // NOTE: This is another guard against urls, if they're not inside strings or parantheses.
&& count_occurrences(s, '\'') % 2 == 0
&& count_occurrences(s, '"') % 2 == 0
&& count_occurrences(s, '(') == count_occurrences(s, ')')
})
}

/// Returns `(length, expression_index)`
Expand Down
10 changes: 10 additions & 0 deletions packages/styled-jsx/transform/tests/fixture/issue--317/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const MyComponent = () => (
<>
<div />
<style jsx>{`
div {
background-image: url("data:image/svg+xml,%3Csvg stroke='currentColor' fill='currentColor' stroke-width='0' viewBox='0 0 24 24' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'%3E%3Cg%3E%3Cpath fill='none' d='M0 0h24v24H0z'%3E%3C/path%3E%3Cpath d='M17 3h4a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h4V1h2v2h6V1h2v2zm3 6V5h-3v2h-2V5H9v2H7V5H4v4h16zm0 2H4v8h16v-8zM6 13h5v4H6v-4z'%3E%3C/path%3E%3C/g%3E%3C/svg%3E%0A");
}
`}</style>
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _JSXStyle from "styled-jsx/style";
export const MyComponent = ()=><>
<div className={"jsx-98442910aac83b4a"}/>
<_JSXStyle id={"98442910aac83b4a"}>{"div.jsx-98442910aac83b4a{background-image:url(\"data:image/svg+xml,%3Csvg stroke='currentColor' fill='currentColor' stroke-width='0' viewBox='0 0 24 24' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'%3E%3Cg%3E%3Cpath fill='none' d='M0 0h24v24H0z'%3E%3C/path%3E%3Cpath d='M17 3h4a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h4V1h2v2h6V1h2v2zm3 6V5h-3v2h-2V5H9v2H7V5H4v4h16zm0 2H4v8h16v-8zM6 13h5v4H6v-4z'%3E%3C/path%3E%3C/g%3E%3C/svg%3E%0A\")}"}</_JSXStyle>
</>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import _JSXStyle from "styled-jsx/style";
export const MyComponent = ()=><>
<div className={"jsx-98442910aac83b4a"}/>
<_JSXStyle id={"98442910aac83b4a"}>{"div.jsx-98442910aac83b4a{background-image:url(\"data:image/svg+xml,%3Csvg stroke='currentColor' fill='currentColor' stroke-width='0' viewBox='0 0 24 24' height='1em' width='1em' xmlns='http://www.w3.org/2000/svg'%3E%3Cg%3E%3Cpath fill='none' d='M0 0h24v24H0z'%3E%3C/path%3E%3Cpath d='M17 3h4a1 1 0 0 1 1 1v16a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h4V1h2v2h6V1h2v2zm3 6V5h-3v2h-2V5H9v2H7V5H4v4h16zm0 2H4v8h16v-8zM6 13h5v4H6v-4z'%3E%3C/path%3E%3C/g%3E%3C/svg%3E%0A\")}"}</_JSXStyle>
</>;
6 changes: 6 additions & 0 deletions packages/transform-imports/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-transform-imports

## 3.0.1

### Patch Changes

- fb6cf58: Preserve side-effect imports

## 3.0.0

### Major Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/transform-imports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@

# @swc/plugin-transform-imports

## 3.0.1

### Patch Changes

- fb6cf58: Preserve side-effect imports

## 3.0.0

### Major Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/plugin-transform-imports",
"version": "3.0.0",
"version": "3.0.1",
"description": "SWC plugin for https://www.npmjs.com/package/babel-plugin-transform-imports",
"main": "swc_plugin_transform_imports.wasm",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/transform-imports/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ homepage = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = "0.68.23"
version = "0.68.24"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down

0 comments on commit 4e485c6

Please sign in to comment.