Skip to content

fix: throw the unnamed stylis plugin error instead of discarding it - #5837

Merged
quantizor merged 1 commit into
styled-components:6.xfrom
mrityunjay-pandey:fix/throw-unnamed-plugin-error
Oct 19, 2026
Merged

quantizor merged 1 commit into
styled-components:6.xfrom
mrityunjay-pandey:fix/throw-unnamed-plugin-error

Conversation

@mrityunjay-pandey

Copy link
Copy Markdown

Fixes the 6.x part of #5828 ("Related on 6.x: a plugin with no name is supposed to throw error 15, but the error is created and never thrown").

Problem

createStylisInstance checks each plugin's name with

if (!plugins[i].name) throwStyledError(15);

but utils/error only builds and returns the Error; every other call site writes throw styledError(...). So on 6.x an anonymous function passed in stylisPlugins is accepted silently, contributes an empty string to the hash, and shares its class names with the same component rendered without plugins.

The existing test for this passed by accident: it sets name to undefined, which made the next line (phash(h, undefined)) throw Cannot read properties of undefined (reading 'length').

Change

  • Throw the error.
  • The existing test now asserts the error message, and a new test covers an anonymous arrow function, the case that was silently accepted.
  • Patch changeset.

This does not change the hashing for two different plugins that share a name, the main subject of #5828.

One thing to weigh: apps on 6.x that pass an anonymous plugin today work (with the class-name collision described above) and will start throwing error 15 after this. That is the documented behaviour and what main does, but it lands in a patch release. If you would rather have a development-only warning on 6.x, I can change it.

Testing

  • The two tests fail on current 6.x and pass with this change.
  • test:web (980 tests, 624 snapshots), test:native (45 tests), typecheck and test:types pass locally (Linux, Node 24).

createStylisInstance called the error helper for an unnamed plugin
without throwing its result, so anonymous plugins were accepted
silently and shared class names with plugin-less renders.

Refs styled-components#5828
@changeset-bot

changeset-bot Bot commented Oct 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e545904

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

@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 critical issues — two minor notes inline, plus one release-strategy question below.

Reviewed changes

  • Throw error #15 for unnamed plugins — createStylisInstance now throws the value returned by the error factory instead of discarding it, so an unnamed or anonymous plugin is rejected as documented. packages/styled-components/src/utils/stylis.ts:367
  • Regression coverage — the unnamed-plugin test now asserts the #15 message and a new test covers an anonymous arrow function, the case previously accepted silently. packages/styled-components/src/models/test/StyleSheetManager.test.tsx:360
  • Patch changeset — .changeset/throw-unnamed-plugin-error.md.

ℹ️ A hard throw now ships in a 6.x patch

The fix restores the documented contract and matches main's v7 compiler.ts (if (!name) throw throwStyledError(15)), so the behavior is correct. The open question is release mechanics rather than code: apps on 6.x that pass an anonymous plugin work today (with the silent class-name collision) and will start throwing at component creation after a patch upgrade. That is the trade-off the description already flags; the call between a hard throw and a dev-only warning belongs to the maintainers.

Technical details
# Release tier when previously-accepted input becomes a throw

## Affected sites
- `.changeset/throw-unnamed-plugin-error.md:2` — declares `patch`.
- `packages/styled-components/src/utils/stylis.ts:367` — throws error 15 during `createStylisInstance`, i.e. at `StyleSheetManager` render / first styled component that carries plugins.

## Required outcome
- Confirm whether restoring the documented throw (now identical to main) is acceptable on a 6.x patch, or whether 6.x should warn in development and defer the hard throw to 7.x.

## Open questions for the human
- Is there precedent on 6.x for promoting previously-silent input to a hard error in a patch release?

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

"styled-components": patch
---

A stylis plugin without a name passed to `<StyleSheetManager stylisPlugins={[...]}>` now throws the documented "not named" error (#15) again. The error was being created but never thrown, so an anonymous function was accepted silently and shared its class names with the same component rendered without plugins.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The changeset rules ask for what a user observes, without mechanism descriptions, and "The error was being created but never thrown" is an internal detail the reader hasn't seen. Consider keeping only the observable half — an anonymous plugin was previously accepted silently (and could share class names with the same component rendered without plugins) and now throws error #15.

</StyleSheetManager>
)
).toThrow();
).toThrow(/A stylis plugin has been supplied that is not named/);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This assertion now genuinely exercises the throw path, which exposes a fragility: stylisRTLPlugin.name is restored only on the following line, so whenever the assertion fails the restore is skipped and later tests in the file fail too (reverting only stylis.ts:367 cascaded into two unrelated failures). Wrapping the mutation and restore in try/finally, or building a one-off plugin instead of mutating the shared one, keeps a regression localized to this test.

@quantizor quantizor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you!

@quantizor
quantizor merged commit 2a285ac into styled-components:6.x Oct 19, 2026
8 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