-
Notifications
You must be signed in to change notification settings - Fork 527
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
Add rule: class-name-format #327
Add rule: class-name-format #327
Conversation
I think this needs a lot more test examples, including parent selectors etc, I'll add some examples here later on. |
@benthemonkey Just a heads up that you need to add the rule to |
And needs to allow a leading underscore. They are valid first characters for class names (and something I personally use)
|
It looks like the regexs are also missing the escape sequences that the CSS Spec for identifiers defines.
You can take a look at the CSS Grammar for a regex-like interpretation of that rule. Mathias Bynens has a primer on CSS escapes with a test suite of pathologically absurd-but-still-valid class names. Long story short, this sort of thing is valid CSS class naming. |
In a total unrelated note, how many Bens are working on SassLint now? Three Bens, a Dan, and a Sam?
|
haha world domination soon 😛 |
OK I'll definitely have the class naming adhere to the spec. These regexp's were grabbed straight from scss-lint (as I did with previous rules). But now that you mention it I've wasted hours in the past figuring out why my CSS selector wasn't working, and the problem was an ID started with a number. However, do you think this rule should perform the role of class validity checking? The alternative is to differentiate rules which check for style from rules which check for syntax issues / validity (again, ESLint's model). For example, if I had |
I would say in the spirit of small single purpose rules we're aiming for we should probably stick with the validity of classnames being handled by a separate rule. |
I feel like the documentation for this rule is unnecessarily long. Does the user really need to see Good/Bad examples of every convention? Can't I just briefly describe the format of each convention (possibly in a separate documentation page to avoid duplication)? I feel like it would be more concise and consumable by the reader. I would still include examples for the other options. For instance: Convention Examples
|
@BPScott I don't think that escape sequences or number codes, while valid, fit within any of the conventions included in this rule. E.g. if a user chooses |
I refuse to believe my rocking up in the issues and muttering "you missed a bit" counts as working on sass-lint, everyone else contributing code is easily in a different league ;) @benthemonkey: Good point. The escape code stuff is very much a niche thing and should be avoided 99% of the time. Having this rule for naming conventions with the current regexes, and then a new lower-level rule for Then a user can leave the validity check on for a whole codebase, while occasionally explicitly ignoring the naming convention rule for when they do want a class using escape codes using #70. |
So for a start with BEM you'll want to check for the following which should all be valid .block
.block__element
.block__element--modifier
.block--modifier
.block {
&__element {
&--mofifier {
}
}
&--modifier {
}
} |
b07aa1b
to
95e9dc9
Compare
@DanPurdy So I tried out your example, utilizing suffix additions to selectors. Gonzales-PE doesn't consider those suffix additions to be part of a class name, and so they are ignored by this linter. Do you know if I basically have to wait for us to version bump Gonzales-PE so I can utilize the |
It should see them as parent selectors no? Looking at your code you're parsing by class, if you were to traverse by selectors as I had to in |
95e9dc9
to
95bf03f
Compare
Sorry for such a long delay. I played around with accomplishing this, and it was just way too complicated. Here's the location of a class selector, relative to a suffix addition to the selector: Class selector: That's crazy hard to dependably reason about, right? I didn't expect the block of a style definition to be a sibling to the selector, instead of a child of the selector. Not to mention the fact that this has to be recursive, so that multiple nested suffix additions can be processed. Maybe I'm just still getting used to tree traversals. Do you think this can reasonably be tested for? Edit: @DanPurdy |
Poking @sasstools/sass-lint-contributors to try to close up this PR. Are you all ok with not supporting suffix extensions to selectors in the initial version of this rule? If not, could you provide some insight into the best way to robustly accomplish the tree traversal I describe in my previous comment? |
@@ -0,0 +1,2 @@ | |||
options: | |||
merge-default-rules: 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.
Any reason for this to be there in this PR?
So we're saying it wouldnt support parent selectors? |
We're going to have to support suffix extensions to selectors when we write a bem-depth rule, so I'll give this another shot. I think I'll need to write some tree-traversal helper functions to accomplish this. Maybe a This is a more global issue than I realized. Any selector can be extended so a lot of our rules need fixing. |
I'm just trying to get at what we're classing as suffix extensions here. If it's all parent selectors then I've already sort of gone through that process with the |
By "suffix extensions to selectors," I was trying to mimic Sass' documentation for the best way to describe adding stuff to the end of a parent's class name. "& must appear at the beginning of a compound selector, but it can be followed by a suffix that will be added to the parent selector" source. For our conversations I thought it would be helpful to have a term to describe this specific use of parent selectors. I'll look more carefully at |
95bf03f
to
66c3b93
Compare
@DanPurdy Quick question. For BEM stuff, once we check suffix extensions to selectors, do we allow stuff like this? .block_ {
&_element-- {
&modifier {}
}
} Because the way I'm currently writing this rule, I don't know how I should check for that sort of thing... |
9f5201f
to
6ff1a42
Compare
yaml = require('js-yaml'), | ||
merge = require('merge'); | ||
|
||
var clone = function (obj) { |
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.
Can we include JSDoc style comments for each new function in helpers please.
3ba99ab
to
fd9235d
Compare
@DanPurdy I think I covered all your comments |
👍 |
Slightly different than the other
name-format
rules. It addsignore
, an array of whitelisted names.allow-leading-underscore
back inlib/config/sass-lint.yml
I welcome feedback on the documentation, as I'm not very familiar with BEM.
Will close #324
DCO 1.1 Signed-off-by: Ben Rothman bensrothman@gmail.com