Skip to content

fix(minify): strip line comments after the closing line of a multi-line value - #427

Merged
quantizor merged 2 commits into
styled-components:mainfrom
seongwon030:fix/line-comment-multiline-value
Oct 22, 2026
Merged

quantizor merged 2 commits into
styled-components:mainfrom
seongwon030:fix/line-comment-multiline-value

Conversation

@seongwon030

Copy link
Copy Markdown
Contributor

Summary

Refs styled-components/styled-components#5613.

With the default minify: true, a // comment on the closing line of a multi-line value survives minification, and styled-components then drops everything after it in the template:

const Styles = styled.div`
  grid-template-rows: auto auto minmax(
    0,
    1fr
  ); // long comment

  ${AnotherComponent} {
    color: red;
  }
`
Generated CSS (styled-components 6.6.0)
runtime only, no plugin .x{grid-template-rows:...;} .x .sc-y{color:red;}
plugin 2.3.0 .x{grid-template-rows:auto auto minmax( 0,1fr );} (nested block gone)
this PR .x{grid-template-rows:auto auto minmax( 0,1fr );} .x .sc-y{color:red;}

Cause

stripLineComment only treats // as a comment when the text before it on that line has as many ( as ). On ); // long comment the ) closes a paren opened on an earlier line, so the line looks unbalanced and the comment is kept. minify then joins lines with ' ', and the runtime strips from // to the next newline, which is now the end of the template.

Fix

Count only parens left open within the line, ignoring a ) that has no match on that line (countOpenParens(str) === 0). The check stays per line on purpose: tracking depth across lines would keep comments inside a multi-line value (calc(\n 10px // c\n + 20px\n)), and after the join they would swallow the rest of the template. That case has a test now.

The same change stops truncating url(//...) after an unmatched ). For example, linear-gradient(\n red, blue\n), url(//cdn.example.com/x.png); // c previously minified to ...),url(.

One behavior change: ), repeat( // c (closing one paren and opening another on the same line) used to be stripped and is now kept, the same as the existing repeat( // c case. Only that declaration is affected, since the runtime leaves // inside parens alone; following rules survive.

Tests

  • 7 new tests in test/minify/index.test.js, each checked through both minifyRaw and minifyCooked. 4 fail on main and pass with this change; 3 are regression guards (comments inside multi-line values, url(//...), url(http://...), // in strings).
  • pnpm install --frozen-lockfile && pnpm test: 70/70 on Node 22 and 24.
  • Changeset added (patch).

…ne value

stripLineComment required balanced parentheses before `//` on the same
line, so `); // comment` closing a value opened on an earlier line kept
the comment. After lines were joined, styled-components stripped from
`//` to the end of the template, silently dropping the declarations and
nested blocks that followed.

Ignore closing parentheses without a match on the line when deciding
whether `//` is inside a value.
@changeset-bot

changeset-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1bcfdd5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
babel-plugin-styled-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@quantizor

Copy link
Copy Markdown
Collaborator

@pullfrog review

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • countOpenParens helper (src/minify/index.js:31): walks a line counting parens left open, ignoring a ) with no match inside the line so an unmatched close from a value opened on an earlier line no longer looks unbalanced.
  • stripLineComment predicate (src/minify/index.js:75): replaced countOccurences(str, '(') === countOccurences(str, ')') with countOpenParens(str) === 0, so ); // comment after a multi-line value is treated as a comment and stripped before lines are joined.
  • Tests (test/minify/index.test.js): seven new cases covering the closing-paren fix, interpolations in the multi-line value, comments inside multi-line values, url(//...) after an unmatched ), url(http://...), and // inside strings.
  • Changeset: patch entry describing the fix and the truncation of url(//...) it also resolves.

The predicate change is sound: a stripped // is now only kept when an open paren actually precedes it on the line, and a url(/function ( opened before the marker still leaves countOpenParens > 0, so genuine // value content is preserved. I confirmed all 70 tests pass locally on this branch.

ℹ️ The documented behavior change is safe on released styled-components

The PR notes one intentional flip: ), repeat( // c (which closes a paren and opens a new function before the marker) now keeps the comment instead of stripping it, matching the pre-existing repeat( // c handling. I verified the load-bearing assumption behind this — that the runtime leaves // inside parentheses untouched — against first-party sources for the released v6 line, so a kept comment cannot run to the end of the joined template.

Technical details
# `//` inside parens is preserved by the runtime

The minifier joins lines with a single space, so a comment that survives minification would run to the end of the template if the runtime stripped `//` unconditionally. It does not, for the pattern this PR now keeps:

## Affected site
- `src/minify/index.js:75` — `countOpenParens(str) === 0`; a kept `//` implies an unmatched `(` precedes it on the same line.

## Evidence
- styled-components v6 pins `[email protected]`, whose parser consumes a balanced `(...)` group opaquely via `delimit(...)`; the `//` comment handler (`case 47`) is only reached at top level.
- The pre-stylis `normalize` step in the released 6.6.0 bundle skips comment/brace detection while `parenDepth > 0`, so `url(https://...)` and friends are preserved verbatim.
- Net effect: `url(//...)` is preserved (the case this PR fixes), and the contrived `), repeat( // c` yields an invalid single declaration the browser drops rather than swallowing following rules.

## Open questions for the human
- None blocking. The `), func( //` flip only changes that one declaration and is already documented in the PR body.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@quantizor

Copy link
Copy Markdown
Collaborator

@seongwon030 thank you!

@quantizor
quantizor merged commit f4b2476 into styled-components:main Oct 22, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants