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
Fix selector-attribute-quotes
false positives for "never"
#6571
Fix selector-attribute-quotes
false positives for "never"
#6571
Conversation
🦋 Changeset detectedLatest commit: e6d6781 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ended up being a bit more of a deep dive into the CSS spec than I thought! I think this PR is now ready for review. I've left some comments where I'm unsure of how I'm doing.
@@ -0,0 +1,41 @@ | |||
'use strict'; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be its own file? And hopefully, I haven't missed an existing version of this util!
lib/utils/isValidIdentifier.js
Outdated
// trims, removes ISO 10646 characters, and singly-escaped characters | ||
const trimmedIdent = ident | ||
.trim() | ||
.replace(/\\[0-9a-f]{1,6}(\\r\\n|[ \t\r\n\f])?/gi, '') | ||
.replace(/\\./g, ''); | ||
|
||
// characters that are not alphanumeric, hyphen or underscore | ||
if (trimmedIdent.match(/[^\w-]/)) { | ||
return false; | ||
} | ||
|
||
// ident has a leading digit | ||
if (hasDigitAt(0)) { | ||
return false; | ||
} | ||
|
||
// ident has a leading dash followed by a digit or a dash | ||
if (trimmedIdent.charAt(0) === '-' && (hasDigitAt(1) || trimmedIdent.charAt(1) === '-')) { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we prefer for me to collapse this into one expression? I did it this way since I felt that it was more naturally readable.
Alt: see my note on the full FLEX-based regex option.
* @param {string} ident | ||
* @returns {boolean} | ||
*/ | ||
module.exports = function isValidIdentifier(ident) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question
I dunno the stylelint conventions, but did you check if an npm package already existed?
Even if it does maybe we recommend not relying on dependencies whenever possible.
Iv done an id assert package for HTML/XML so I wouldn't be surprised if something similar existed for your case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, to be honest I didn't look too in-depth; I did a quick site:npmjs.com valid css identifier
and found nothing on the first page or so of Google (much more support for valid JS identifiers or CSS selectors). If you're aware of one and we think we should use it though, happy to refactor!
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
Thanks for the quick review @ybiquitous! Think I've addressed each of the comments, let me know what you think. |
selector-attribute-quotes
selector-attribute-quotes
false positives for "never"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. LGTM 👍🏼
Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com>
Closes #4300.
First - should I add some documentation to explain this new behaviour? Or do we think the current setup is enough.
There's a bit of discussion in #4300. To reiterate @Mouvedia's point:
As such, I've adjusted the relevant test cases. The latter two are moved from being rejected to accepted; let me know if that was the desired change (or if instead we should still report, but not fix).
Regex alternative
I realized that the CSS grammar actually has a FLEX tokenizer, so I tried to directly copy the rule as a Regex:
Unfortunately, this doesn't work! It misses some "basic" cases, including strings with spaces in them. If anybody has any ideas on what I've done wrong, that would be great - I feel like using the CSS spec when possible seems like a good idea.