Skip to content

Conversation

ryanswanson
Copy link
Contributor

What:

Correct the createUnsafeSelectorsAlarm algorithm to successfully find the ignore comment even when the style source is parsed as multiple lines.

Ignore comment: /* emotion-disable-server-rendering-unsafe-selector-warning-please-do-not-use-this-the-warning-exists-for-a-reason */

Why:

Despite including an ignore comment for the use of unsafe selectors, a console warning (alarm) will still display under certain conditions:

  • The offending style statement is not the last statement within a CSS style block.
  • The style block is parsed into multiple lines.

Emotion previously detected the ignore comment successfully, but appears to have regressed with changes for Issue #2751.

I believe this is why Issue #2917 was submitted shortly thereafter (from Aug 22 to Oct 11) and this may be the proper fix for it.

Additional Details

The pseudo-selector warnings only appear in dev mode; however, we don't see the issue when running in dev mode for our emotion-based component library, presumably because source maps are enabled.

Through runtime debugging, it appears that the source is parsed as a large single line of text with newline characters (and with the source map detail at the end), in which case the detection of the 'ignore comment' will succeed.

We only found this issue when our emotion-based component library was used as a dependency in an application and the styles are parsed into multiple lines.

The issue also only presents when the offending pseudo-selector style with comment is NOT the last CSS statement within a block. This is simply because the algorithm uses a reverse traversal of the statements within the block.

How:

In order to properly find the ignore comment when styles are parsed into multiple lines, first check that element and node are on the same line before comparing column (character position).

Checklist:

  • Documentation N/A
  • Tests N/A
  • Code complete
  • Changeset

I didn't include a change to tests as it appears the createUnsafeSelectorsAlarm method is not currently tested.

…y fix issue emotion-js#2917.

This issue was introduced with changes for Issue emotion-js#2751.
Column (character position) comparison only applies within the same line.
Copy link

changeset-bot bot commented Jul 18, 2025

🦋 Changeset detected

Latest commit: 42ae0f1

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

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

Copy link

codesandbox-ci bot commented Jul 18, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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.

1 participant