Fix Issue #2917 - Update createUnsafeSelectorsAlarm to handle multi-line parsed styles #3337
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Correct the
createUnsafeSelectorsAlarm
algorithm to successfully find theignore 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: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:
I didn't include a change to tests as it appears the
createUnsafeSelectorsAlarm
method is not currently tested.