Skip to content

Commit

Permalink
Fixed ignore flag not disabling the warning for rules defined inside …
Browse files Browse the repository at this point in the history
…other rules
  • Loading branch information
Andarist committed Apr 5, 2023
1 parent 314a5fb commit 293f21a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-maps-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/cache': patch
---

Fixed `/* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */` not disabling the warning for rules defined inside other rules.
6 changes: 3 additions & 3 deletions packages/cache/src/stylis-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export let createUnsafeSelectorsAlarm = cache => (element, index, children) => {
)

if (unsafePseudoClasses) {
const isNested = element.parent === children[0]
// in nested rules comments become children of the "auto-inserted" rule
const isNested = !!element.parent
// in nested rules comments become children of the "auto-inserted" rule and that's always the `element.parent`
//
// considering this input:
// .a {
Expand All @@ -182,7 +182,7 @@ export let createUnsafeSelectorsAlarm = cache => (element, index, children) => {
// .b {}
// }
const commentContainer = isNested
? children[0].children
? element.parent.children
: // global rule at the root level
children

Expand Down
20 changes: 20 additions & 0 deletions packages/react/__tests__/__snapshots__/warnings.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ exports[`global with css prop 1`] = `null`;

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does not warn when using the flag on a global rule 1`] = `null`;

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does not warn when using the flag on a rule that is defined in another one 1`] = `
.emotion-0 div span:first-child {
border-bottom-left-radius: 0;
}
<div
className="emotion-0"
/>
`;

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does not warn when using the flag on the rule that follows a declaration 1`] = `
.emotion-0 {
color: hotpink;
Expand Down Expand Up @@ -93,6 +103,16 @@ exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-di

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does warn when not using the flag on a global rule 1`] = `null`;

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does warn when not using the flag on a rule that is defined in another one 1`] = `
.emotion-0 div span:first-child {
border-bottom-left-radius: 0;
}
<div
className="emotion-0"
/>
`;

exports[`unsafe pseudo classes does not warn when using with flag: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */ does warn when not using the flag on the rule that follows a declaration 1`] = `
.emotion-0 {
color: hotpink;
Expand Down
44 changes: 44 additions & 0 deletions packages/react/__tests__/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,50 @@ describe('unsafe pseudo classes', () => {
).toMatchSnapshot()
expect(console.error).not.toBeCalled()
})

test('does warn when not using the flag on a rule that is defined in another one', () => {
expect(
renderer
.create(
<div
css={css`
div {
span:first-child {
border-bottom-left-radius: 0;
}
}
`}
/>
)
.toJSON()
).toMatchSnapshot()
expect((console.error: any).mock.calls).toMatchInlineSnapshot(`
[
[
"The pseudo class ":first-child" is potentially unsafe when doing server-side rendering. Try changing it to ":first-of-type".",
],
]
`)
})

test('does not warn when using the flag on a rule that is defined in another one', () => {
expect(
renderer
.create(
<div
css={css`
div {
span:first-child${ignoreSsrFlag} {
border-bottom-left-radius: 0;
}
}
`}
/>
)
.toJSON()
).toMatchSnapshot()
expect(console.error).not.toBeCalled()
})
})
})

Expand Down

0 comments on commit 293f21a

Please sign in to comment.