Skip to content

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

Merged
merged 12 commits into from
Mar 25, 2025
Merged

Add layer-name-pattern #8474

merged 12 commits into from
Mar 25, 2025

Conversation

ryo-manba
Copy link
Member

@ryo-manba ryo-manba commented Mar 15, 2025

Which issue, if any, is this issue related to?

Closes #8431

Is there anything in the PR that needs further explanation?

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)

Copy link

changeset-bot bot commented Mar 15, 2025

🦋 Changeset detected

Latest commit: 58bb335

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
stylelint Minor

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

Copy link
Contributor

github-actions bot commented Mar 15, 2025

Try the Instant Preview in Online Demo

Stylelint Online Demo

Install the Instant Preview to Your Local

npm i -D https://pkg.pr.new/stylelint@8474

View Commit

Copy link
Member

@jeddy3 jeddy3 left a 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.

@@ -9,8 +9,13 @@ export const atRuleRegexes = {
`^((?!${[...nestingSupportedAtKeywords.values()].join('|')}).)*$`,
'i',
),
layer: /^layer$/i,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
layer: /^layer$/i,
layerName: /^layer$/i,

});

root.walkAtRules(atRuleRegexes.importName, (atRule) => {
const layerMatch = atRule.params.match(functionRegexes.layer);
Copy link
Member

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.

@ryo-manba ryo-manba requested review from ybiquitous and jeddy3 March 16, 2025 13:03
Copy link
Member

@jeddy3 jeddy3 left a 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-]*$"
Copy link
Member

@jeddy3 jeddy3 Mar 16, 2025

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);
Copy link
Member

@jeddy3 jeddy3 Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Member

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

@ryo-manba ryo-manba requested a review from jeddy3 March 16, 2025 14:46
@ryo-manba
Copy link
Member Author

Thank you for the feedback and review!
I've addressed the comments.

Copy link
Member

@jeddy3 jeddy3 left a 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.

Comment on lines 69 to 83
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,
});
Copy link
Member

@jeddy3 jeddy3 Mar 16, 2025

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.

Copy link
Member Author

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.

Refactor to unify check logic

Comment on lines 55 to 63
<!-- prettier-ignore -->
```css
@layer foo, bar {}
```

<!-- prettier-ignore -->
```css
@layer foo.bar {}
```
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@jeddy3 jeddy3 left a 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.

@jeddy3 jeddy3 merged commit 7dabd1a into main Mar 25, 2025
19 checks passed
@jeddy3 jeddy3 deleted the issue-8431 branch March 25, 2025 13:22
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Mar 28, 2025
| 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)).
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Mar 28, 2025
| 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)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add layer-name-pattern
3 participants