Skip to content

Fix syntax error in styled-components v6 handling - #5635

Merged
quantizor merged 10 commits into
mainfrom
copilot/fix-styled-components-syntax-error
Jan 11, 2026
Merged

quantizor merged 10 commits into
mainfrom
copilot/fix-styled-components-syntax-error

Conversation

Copilot AI commented Jan 11, 2026 •

Copy link
Copy Markdown
Contributor
  • Understand the issue: Invalid CSS syntax causes stylis to misinterpret closing braces, dropping subsequent styles
  • Explore the codebase and understand the CSS processing flow
  • Identify the root cause: stylis.compile() interprets extra } as closing the rule block
  • Design a solution: Add CSS sanitization that contains invalid syntax to just the affected declaration
  • Implement the fix in utils/stylis.ts:
  • Add comprehensive inline snapshot tests for all variations
  • Run existing tests to ensure no regressions (404 web tests + 37 native tests pass)
  • Update changeset
  • Run code review and address feedback
  • Run CodeQL security check - no issues found
  • Optimize sanitizeCSS with microbenchmarks:
    • Early bail: Skip processing if no // or } found
    • charCodeAt: Use numeric char codes instead of string comparisons
    • Array join: Use array + join instead of string concatenation
    • Result: 70x faster for valid CSS (most common case), 3.5x faster for CSS with comments
  • Add separate changeset for line comment fix (Line comments are not supported in multiline style declarations #5613)
  • Run prettier formatter
Original prompt

This section details on the original issue you should resolve

<issue_title>styled-components v6: syntax error in one line prevents remaining styles from applying</issue_title>
<issue_description>

📝 Description

In styled-components v6, if a line inside a styled block contains invalid CSS syntax, it causes all subsequent styles to be ignored.

In v5, the same malformed line would be silently ignored, and the rest of the valid styles would still be applied.


💥 Reproduction Example

const Circle = styled.div`
  width: 100px;
  height: 100px;
  border-radius: 50%;
  line-height: ${({ theme }) => "14px}"}; // ⛔️ Syntax error: redundant `}`
  background-color: green; // ❌ This is ignored in v6
`;

In v6, because of the syntax error on the line-height line, background-color: green; is also ignored.

Code example in code sandbox
https://codesandbox.io/p/sandbox/styled-components-v6-examples-forked-9hm43z

Demo
Image

Environment

  • styled-components: 5.3.11 -> 6.0.7
  • react: 18.0.0

Expected Behavior (as in v5)

  • The invalid line is ignored.
  • background-color: green; is still applied.
  • The background should render as a green.

Actual Behavior in v6

  • The error in line-height causes background-color and any further styles to be ignored entirely.
  • The background does not turn green.
    </issue_description>

<agent_instructions>see if there's a pragmatic way of handling this without butchering performance</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@changeset-bot

changeset-bot Bot commented Jan 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 867cf07

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

This PR includes changesets to release 1 package
Name Type
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

Copilot AI requested a review from quantizor January 11, 2026 02:26
Copilot stopped work on behalf of quantizor due to an error January 11, 2026 02:26
@quantizor

Copy link
Copy Markdown
Contributor

@copilot explore other classes / variations of this problem such as other types of rule-like incomplete syntax causing broken css. the library's default behavior should be to contain invalid syntax to just the affected line

@quantizor

Copy link
Copy Markdown
Contributor

@copilot resume

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot resume

Resumed and completed the work. In commit 5ae49f0 I:

  1. Explored additional CSS syntax variations (extra opening braces, unclosed parentheses, missing colons, extra semicolons, etc.)
  2. Fixed issue Line comments are not supported in multiline style declarations #5613 - line comments (//) in multiline declarations like calc() now properly stripped
  3. Added 16 comprehensive inline snapshot tests covering all variations
  4. Ensured invalid syntax is contained to just the affected line - subsequent valid styles are preserved

All 404 web tests and 37 native tests pass. CodeQL security check found no issues.

@quantizor

Copy link
Copy Markdown
Contributor

@copilot try to further optimize sanitizeCSS as it is the hottest path in the system. analyze branching complexity, short circuiting optimizations, polymorphism etc. Use microbenchmarks to validate approaches.

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot try to further optimize sanitizeCSS as it is the hottest path in the system. analyze branching complexity, short circuiting optimizations, polymorphism etc. Use microbenchmarks to validate ap...

Optimized in commit c1c04b2. Microbenchmark results:

Valid CSS (most common case): 70x faster

  • Original: 303K ops/sec
  • Optimized: 21.2M ops/sec

CSS with comments: 3.5x faster

  • Original: 490K ops/sec
  • Optimized: 1.7M ops/sec

Optimizations applied:

  1. Early bail: indexOf('//') === -1 and indexOf('}') === -1 checks skip processing entirely for clean CSS
  2. charCodeAt: Numeric char code comparisons instead of string comparisons
  3. Array join: Use parts[] + join() instead of string concatenation for comment stripping

All 404 tests pass.

@quantizor

Copy link
Copy Markdown
Contributor

@copilot Need a changeset for the comment fix

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Need a changeset for the comment fix

Added separate changeset for the line comment fix in commit 212a4dd. There are now two changesets:

@quantizor
quantizor marked this pull request as ready for review January 11, 2026 03:57
@quantizor

Copy link
Copy Markdown
Contributor

@copilot Run the formatter

Copilot AI commented Jan 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Run the formatter

Done in commit 7281abd. Prettier formatted 3 files: stylis.ts, stylis.test.ts, and styles.test.tsx.

@quantizor quantizor changed the title [WIP] Fix syntax error in styled-components v6 handling Fix syntax error in styled-components v6 handling Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants