diff --git a/Cargo.lock b/Cargo.lock index a9b25face..888f9e08b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1162,7 +1162,7 @@ checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] name = "modularize_imports" -version = "0.68.23" +version = "0.68.24" dependencies = [ "convert_case", "handlebars", @@ -2121,7 +2121,7 @@ dependencies = [ [[package]] name = "styled_jsx" -version = "0.73.31" +version = "0.73.32" dependencies = [ "anyhow", "lightningcss", diff --git a/packages/styled-jsx/CHANGELOG.md b/packages/styled-jsx/CHANGELOG.md index 2f684c5fa..824fce406 100644 --- a/packages/styled-jsx/CHANGELOG.md +++ b/packages/styled-jsx/CHANGELOG.md @@ -1,5 +1,11 @@ # @swc/plugin-styled-jsx +## 3.0.1 + +### Patch Changes + +- fdd2dda: Fix comment stripping + ## 3.0.0 ### Major Changes diff --git a/packages/styled-jsx/README.md b/packages/styled-jsx/README.md index 78677336f..06b205459 100644 --- a/packages/styled-jsx/README.md +++ b/packages/styled-jsx/README.md @@ -2,6 +2,12 @@ # @swc/plugin-styled-jsx +## 3.0.1 + +### Patch Changes + +- fdd2dda: Fix comment stripping + ## 3.0.0 ### Major Changes diff --git a/packages/styled-jsx/package.json b/packages/styled-jsx/package.json index d98cfd468..0530c8898 100644 --- a/packages/styled-jsx/package.json +++ b/packages/styled-jsx/package.json @@ -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": { diff --git a/packages/styled-jsx/transform/Cargo.toml b/packages/styled-jsx/transform/Cargo.toml index 26c5c85aa..4ad3dd61e 100644 --- a/packages/styled-jsx/transform/Cargo.toml +++ b/packages/styled-jsx/transform/Cargo.toml @@ -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] diff --git a/packages/styled-jsx/transform/src/transform_css_lightningcss.rs b/packages/styled-jsx/transform/src/transform_css_lightningcss.rs index f27b72d44..c09fa69e5 100644 --- a/packages/styled-jsx/transform/src/transform_css_lightningcss.rs +++ b/packages/styled-jsx/transform/src/transform_css_lightningcss.rs @@ -208,46 +208,47 @@ fn convert_browsers(browsers: &Versions) -> Browsers { } } -pub(super) fn strip_comments(s: &str) -> Cow { - 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, 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>, + 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)` diff --git a/packages/styled-jsx/transform/tests/fixture/issue--317/input.js b/packages/styled-jsx/transform/tests/fixture/issue--317/input.js new file mode 100644 index 000000000..ee5a67117 --- /dev/null +++ b/packages/styled-jsx/transform/tests/fixture/issue--317/input.js @@ -0,0 +1,10 @@ +export const MyComponent = () => ( + <> +
+ + +); \ No newline at end of file diff --git a/packages/styled-jsx/transform/tests/fixture/issue--317/output.lightningcss.js b/packages/styled-jsx/transform/tests/fixture/issue--317/output.lightningcss.js new file mode 100644 index 000000000..a52e368a6 --- /dev/null +++ b/packages/styled-jsx/transform/tests/fixture/issue--317/output.lightningcss.js @@ -0,0 +1,5 @@ +import _JSXStyle from "styled-jsx/style"; +export const MyComponent = ()=><> +
+ <_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\")}"} + ; diff --git a/packages/styled-jsx/transform/tests/fixture/issue--317/output.swc.js b/packages/styled-jsx/transform/tests/fixture/issue--317/output.swc.js new file mode 100644 index 000000000..a52e368a6 --- /dev/null +++ b/packages/styled-jsx/transform/tests/fixture/issue--317/output.swc.js @@ -0,0 +1,5 @@ +import _JSXStyle from "styled-jsx/style"; +export const MyComponent = ()=><> +
+ <_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\")}"} + ; diff --git a/packages/transform-imports/CHANGELOG.md b/packages/transform-imports/CHANGELOG.md index 887d0ce4a..6fafb39fd 100644 --- a/packages/transform-imports/CHANGELOG.md +++ b/packages/transform-imports/CHANGELOG.md @@ -1,5 +1,11 @@ # @swc/plugin-transform-imports +## 3.0.1 + +### Patch Changes + +- fb6cf58: Preserve side-effect imports + ## 3.0.0 ### Major Changes diff --git a/packages/transform-imports/README.md b/packages/transform-imports/README.md index 6cc2e054e..a09a468ab 100644 --- a/packages/transform-imports/README.md +++ b/packages/transform-imports/README.md @@ -18,6 +18,12 @@ # @swc/plugin-transform-imports +## 3.0.1 + +### Patch Changes + +- fb6cf58: Preserve side-effect imports + ## 3.0.0 ### Major Changes diff --git a/packages/transform-imports/package.json b/packages/transform-imports/package.json index 8f3b06316..aa6491d90 100644 --- a/packages/transform-imports/package.json +++ b/packages/transform-imports/package.json @@ -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": { diff --git a/packages/transform-imports/transform/Cargo.toml b/packages/transform-imports/transform/Cargo.toml index 5129f4e77..7b864d04a 100644 --- a/packages/transform-imports/transform/Cargo.toml +++ b/packages/transform-imports/transform/Cargo.toml @@ -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