Skip to content

Commit 2a285ac

Browse files
fix: throw the unnamed stylis plugin error instead of discarding it (#5837)
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 #5828
1 parent adde94c commit 2a285ac

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
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.

‎packages/styled-components/src/models/test/StyleSheetManager.test.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,25 @@ describe('StyleSheetManager', () => {
357357
<Test>Foo</Test>
358358
</StyleSheetManager>
359359
)
360-
).toThrow();
360+
).toThrow(/A stylis plugin has been supplied that is not named/);
361361

362362
Object.defineProperty(stylisRTLPlugin, 'name', { value: cachedName });
363363
});
364364

365+
it('an error is emitted if an anonymous function is provided as a stylis plugin', () => {
366+
const Test = styled.div`
367+
padding-left: 5px;
368+
`;
369+
370+
expect(() =>
371+
render(
372+
<StyleSheetManager stylisPlugins={[() => undefined]}>
373+
<Test>Foo</Test>
374+
</StyleSheetManager>
375+
)
376+
).toThrow(/A stylis plugin has been supplied that is not named/);
377+
});
378+
365379
it('changing stylis plugins via StyleSheetManager works', () => {
366380
const Test = styled.div`
367381
padding-left: 5px;

‎packages/styled-components/src/utils/stylis.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export default function createStylisInstance(
364364
const o = options as ICreateStylisInstance['options'];
365365
let h = SEED;
366366
for (let i = 0; i < plugins.length; i++) {
367-
if (!plugins[i].name) throwStyledError(15);
367+
if (!plugins[i].name) throw throwStyledError(15);
368368
h = phash(h, plugins[i].name);
369369
}
370370
if (o?.namespace) h = phash(h, o.namespace);

0 commit comments

Comments
 (0)