-
-
Notifications
You must be signed in to change notification settings - Fork 973
Add layer-name-pattern
#8474
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 layer-name-pattern
#8474
Conversation
🦋 Changeset detectedLatest commit: 58bb335 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 |
Try the Instant Preview in Online DemoInstall the Instant Preview to Your Local
|
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.
@ryo-manba Thanks for the PR!
I've suggested a couple of changes.
lib/utils/regexes.mjs
Outdated
@@ -9,8 +9,13 @@ export const atRuleRegexes = { | |||
`^((?!${[...nestingSupportedAtKeywords.values()].join('|')}).)*$`, | |||
'i', | |||
), | |||
layer: /^layer$/i, |
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.
layer: /^layer$/i, | |
layerName: /^layer$/i, |
}); | ||
|
||
root.walkAtRules(atRuleRegexes.importName, (atRule) => { | ||
const layerMatch = atRule.params.match(functionRegexes.layer); |
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.
Let's use postcss-value-parser
to find the layer
function and get the comma-separated names.
Co-authored-by: Richard Hallows <jeddy3@users.noreply.github.com>
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 for making the changes. The rule code using the value parser is nice, thanks.
I've made a couple more suggestions.
Given the string: | ||
|
||
```json | ||
"^[a-z][a-z0-9-]*$" |
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.
Let's update the regex to be more permissive in our example and allow segmented layer names as that's likely what we'll turn on in our standard config, e.g.
@layer foo.bar {}
i.e. lowercase with .
allowed.
}); | ||
|
||
root.walkAtRules(atRuleRegexes.importName, (atRule) => { | ||
const parsedParams = valueParser(atRule.params); |
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.
const parsedParams = valueParser(atRule.params); | |
const { params } = atRule; | |
if (!LAYER_FUNC_REGEX.test(params)) return | |
const parsedParams = valueParser(params); |
// Near top of file (or in regex utilities):
const LAYER_FUNC_REGEX = /layer\(/i;
When possible, we typically do a cheap check before parsing values.
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.
Yes, good point. 👍🏼
We, especially Romain, have worked on improving rule performance. Avoiding extra parsing is worthwhile. See #6869
Co-authored-by: Richard Hallows <jeddy3@users.noreply.github.com>
This reverts commit 4f2bebd.
Thank you for the feedback and review! |
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, it's taking shape nicely.
I've left one refactoring suggestion and a minor docs one.
if (child.type !== 'word') continue; | ||
|
||
if (pattern.test(child.value)) continue; | ||
|
||
const index = atRuleParamIndex(atRule) + child.sourceIndex; | ||
const endIndex = index + child.value.length; | ||
|
||
report({ | ||
message: messages.expected(child.value, primary), | ||
node: atRule, | ||
index, | ||
endIndex, | ||
ruleName, | ||
result, | ||
}); |
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.
Shall we pull all or some of this repetition into a check()
function, like we do in other rules?
And, if appropriate, we can destructure the type
, value
and sourceIndex
consts as it helps with readability to see what properties are going to be used.
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.
Thanks for the detailed feedback!
I've applied the changes.
<!-- prettier-ignore --> | ||
```css | ||
@layer foo, bar {} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
@layer foo.bar {} | ||
``` |
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.
Let's switch these two examples around so they match the order above.
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.
Fixed it.
Adjust order in example
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 for making all the changes.
LGTM. @ybiquitous may have further suggestions, as he's very good with our code structure.
| datasource | package | from | to | | ---------- | --------- | ------- | ------- | | npm | stylelint | 16.16.0 | 16.17.0 | ## [v16.17.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16170---2025-03-26) It adds 1 new rule, support for `languageOptions` to 2 rules, 1 option to a rule, the `--compute-edit-info` CLI flag (along with support for `EditInfo` in 3 rules), and fixes 1 bug. `EditInfo` is useful for automated fixing tools and editor integrations. - Added: `layer-name-pattern` rule ([#8474](stylelint/stylelint#8474)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `--compute-edit-info` CLI flag ([#8473](stylelint/stylelint#8473)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `ignorePreludeOfAtRules: []` to `length-zero-no-unit` ([#8472](stylelint/stylelint#8472)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `at-rule-no-unknown` support for `languageOptions` ([#8475](stylelint/stylelint#8475)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `property-no-unknown` support for `languageOptions` ([#8476](stylelint/stylelint#8476)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `declaration-block-no-redundant-longhand-properties` support for computing `EditInfo` ([#8482](stylelint/stylelint#8482)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Added: `function-url-quotes` support for computing `EditInfo` ([#8483](stylelint/stylelint#8483)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Added: `selector-attribute-quotes` support for computing `EditInfo` ([#8484](stylelint/stylelint#8484)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Fixed: `custom-property-pattern` false negatives for `@property` preludes ([#8468](stylelint/stylelint#8468)) ([@rohitgs28](https://github.com/rohitgs28)).
| datasource | package | from | to | | ---------- | --------- | ------- | ------- | | npm | stylelint | 16.16.0 | 16.17.0 | ## [v16.17.0](https://github.com/stylelint/stylelint/blob/HEAD/CHANGELOG.md#16170---2025-03-26) It adds 1 new rule, support for `languageOptions` to 2 rules, 1 option to a rule, the `--compute-edit-info` CLI flag (along with support for `EditInfo` in 3 rules), and fixes 1 bug. `EditInfo` is useful for automated fixing tools and editor integrations. - Added: `layer-name-pattern` rule ([#8474](stylelint/stylelint#8474)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `--compute-edit-info` CLI flag ([#8473](stylelint/stylelint#8473)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `ignorePreludeOfAtRules: []` to `length-zero-no-unit` ([#8472](stylelint/stylelint#8472)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `at-rule-no-unknown` support for `languageOptions` ([#8475](stylelint/stylelint#8475)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `property-no-unknown` support for `languageOptions` ([#8476](stylelint/stylelint#8476)) ([@ryo-manba](https://github.com/ryo-manba)). - Added: `declaration-block-no-redundant-longhand-properties` support for computing `EditInfo` ([#8482](stylelint/stylelint#8482)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Added: `function-url-quotes` support for computing `EditInfo` ([#8483](stylelint/stylelint#8483)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Added: `selector-attribute-quotes` support for computing `EditInfo` ([#8484](stylelint/stylelint#8484)) ([@pamelalozano16](https://github.com/pamelalozano16)). - Fixed: `custom-property-pattern` false negatives for `@property` preludes ([#8468](stylelint/stylelint#8468)) ([@rohitgs28](https://github.com/rohitgs28)).
Closes #8431
This PR adds support for enforcing layer name patterns, including handling period-separated segments like@layer foo.bar
.See the CSS Cascade 5 specification.This functionality was decided not to be supported. For more details: #8474 (comment)