Fix function-url-quotes false positives for SCSS variable and @ character #7416
Merged
ybiquitous merged 4 commits intomainfrom Dec 24, 2023
Merged
Fix function-url-quotes false positives for SCSS variable and @ character #7416ybiquitous merged 4 commits intomainfrom
function-url-quotes false positives for SCSS variable and @ character #7416ybiquitous merged 4 commits intomainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #7404 (though, see discussion below).
tl;dr: this PR (right now) contains a bandaid fix, but there may be a bigger problem of
isStandardSyntaxUrl's heuristic regex not doing what it should be. Would love to get some opinions since I don't have all the context for the util!long explanation / context for problem
First, to
brieflyexplain why #7404 is happening:function-url-quotesusesisStandardSyntaxUrlto bail out for nonstandard urls withinurl():stylelint/lib/utils/isStandardSyntaxUrl.mjs
Lines 41 to 46 in b34a184
The regex
IS_SCSS_VARIABLE_IN_URLis:stylelint/lib/utils/isStandardSyntaxUrl.mjs
Line 7 in b34a184
Based off of the name and the comment in the condition, it seems like the purpose of this regex is to capture an SCSS variable within
url. Makes sense! In fact, we have tests for SCSS variables + interpolation (which left me very confused as to why adding an@sign caused an error):stylelint/lib/utils/__tests__/isStandardSyntaxUrl.test.mjs
Lines 137 to 139 in b34a184
However, I want to point out that the regex is anchored. It only matches if the entire string matches that regex. So, in
$sass-variable + 'foo',IS_SCSS_VARIABLE_IN_URLis matching all of$sass-variable + 'foo', instead of just$sass-variable. So, we're not "really" dealing with concatenation; we're just pretending that there is one SCSS variable called$sass-variable + 'foo'. Here's a regex101 explaining this.This then explains why #7404 occurred: because
@is not part of the character set,IS_SCSS_VARIABLE_IN_URLdoesn't match the entire string$asset-prefix + 'assets/images/fa_calculator_blue@2x.png', even though it probably "should" match$asset-prefix. Here is an accompanying regex101.I've introduced some failing tests in 48485ef to showcase this.
solutions?
The minimum solution to fix this problem is to just add
@to the regex. I did this in 90422c2, which resolves the failing tests in 48485ef. In terms of the above explanation, this now just lets us treat$asset-prefix + 'assets/images/fa_calculator_blue@2x.png'as "one-giant SCSS variable".However, I'm not super satisfied with this, since (in my eyes) it punts the problem down the line. For example, the similar declaration still has an error (see accompanying regex101):
'https://' + $asset-prefix + 'assets/images/fa_calculator_blue@2x.png'Unanchoring itself doesn't work, since it will then incorrectly flag strings with
$within them, like this current test case:So, a "robust"/full solution probably needs to actually parse the value. I'd be happy to spend some time exploring this if we think it's worthwhile, but we could also stick with the current heuristic and add
@to resolve the immediate issue. I'm also cognizant that I may be missing context (e.g. I didn't write the original util). What are our thoughts?