Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ignore flag not disabling the warning for rules defined inside other rules #3019

Merged
merged 1 commit into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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