-
-
Notifications
You must be signed in to change notification settings - Fork 986
Add rule-nesting-at-rule-required-list
#8680
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
ybiquitous
merged 23 commits into
stylelint:main
from
sw1tch3roo:feature/declaration-block-nesting-at-rule-required-list
Aug 12, 2025
Merged
Changes from 3 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e649efa
add `declaration-block-nesting-at-rule-required-list`
sw1tch3roo d51f557
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo 3c04e94
fix the type in index.d.ts
sw1tch3roo 3fe8df7
Refactor rule to check entire rules instead of individual declarations
sw1tch3roo 016b16e
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo cbade06
changes after review: change code style, delete unnecessary tests, up…
sw1tch3roo 52c7654
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo e048d14
suggestions from code review
sw1tch3roo 18b7ec7
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo 2bdb301
build cjs file
sw1tch3roo db6523f
use typeGuards utils
sw1tch3roo 892693a
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo d90f473
enhance the expected messages
sw1tch3roo fb24ded
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo e79ff39
change the examples in README.md
sw1tch3roo 9f765c3
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo fa64d61
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo dd2a7ff
fix expected message type in `d.ts` declaration
sw1tch3roo 554ecd6
reuse the type of args
sw1tch3roo 5dca318
change expected message type in `d.ts`
sw1tch3roo c0c5591
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo f4000e8
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo 6260d46
Merge branch 'main' into feature/declaration-block-nesting-at-rule-re…
sw1tch3roo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "stylelint": minor | ||
| --- | ||
|
|
||
| Added: `declaration-block-nesting-at-rule-required-list` rule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
lib/rules/declaration-block-nesting-at-rule-required-list/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # declaration-block-nesting-at-rule-required-list | ||
|
|
||
| Require specific at-rules to be present around CSS declarations. | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| a { color: red; } | ||
| /** ↑ | ||
| * This declaration */ | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| `array|string|regex`: `["layer"]` | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Given: | ||
|
|
||
| ```json | ||
| ["layer"] | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| The following patterns are considered problems: | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| a { color: red; } | ||
| ``` | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| @media all { | ||
| a { color: red; } | ||
| } | ||
| ``` | ||
|
|
||
| The following patterns are _not_ considered problems: | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| @layer { | ||
| a { color: red; } | ||
| } | ||
| ``` | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```css | ||
| a { | ||
| @layer { | ||
| color: red; | ||
| } | ||
| } | ||
| ``` | ||
158 changes: 158 additions & 0 deletions
158
lib/rules/declaration-block-nesting-at-rule-required-list/__tests__/index.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import rule from '../index.mjs'; | ||
| const { messages, ruleName } = rule; | ||
|
|
||
| testRule({ | ||
| ruleName, | ||
| config: ['layer', '/^s/'], | ||
|
|
||
| accept: [ | ||
| { | ||
| code: '@layer { a { color: red; } }', | ||
| description: 'declaration within @layer at-rule', | ||
| }, | ||
| { | ||
| code: 'a { @layer { color: red; } }', | ||
| description: 'declaration within nested @layer at-rule', | ||
| }, | ||
| { | ||
| code: '@supports (color: red) { a { color: red; } }', | ||
| description: 'declaration within @supports at-rule (matches regex)', | ||
| }, | ||
| { | ||
| code: '@scope (a) { a { color: red; } }', | ||
| description: 'declaration within @scope at-rule (matches regex)', | ||
| }, | ||
| { | ||
| code: '@media all {}', | ||
| description: 'empty at-rule', | ||
| }, | ||
| { | ||
| code: '@layer { @media all { a { color: red; } } }', | ||
| description: 'declaration within nested required at-rule', | ||
| }, | ||
| ], | ||
|
|
||
| reject: [ | ||
| { | ||
| code: 'a { color: red; }', | ||
| description: 'declaration without required at-rule', | ||
| message: messages.expected('layer, /^s/'), | ||
| line: 1, | ||
| column: 5, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| { | ||
| code: 'a { color: red; color: blue; }', | ||
| description: 'multiple declarations without required at-rule', | ||
| warnings: [ | ||
| { | ||
| message: messages.expected('layer, /^s/'), | ||
| line: 1, | ||
| column: 5, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| { | ||
| message: messages.expected('layer, /^s/'), | ||
| line: 1, | ||
| column: 17, | ||
| endLine: 1, | ||
| endColumn: 29, | ||
| }, | ||
| ], | ||
| }, | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| code: '@media all { a { color: red; } }', | ||
| description: 'declaration within non-required at-rule', | ||
| message: messages.expected('layer, /^s/'), | ||
| line: 1, | ||
| column: 18, | ||
| endLine: 1, | ||
| endColumn: 29, | ||
| }, | ||
| { | ||
| code: 'a { @media all { color: red; } }', | ||
| description: 'declaration within nested non-required at-rule', | ||
| message: messages.expected('layer, /^s/'), | ||
| line: 1, | ||
| column: 18, | ||
| endLine: 1, | ||
| endColumn: 29, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| testRule({ | ||
| ruleName, | ||
| config: ['layer'], | ||
|
|
||
| accept: [ | ||
| { | ||
| code: '@layer { a { color: red; } }', | ||
| description: 'declaration within @layer at-rule', | ||
| }, | ||
| { | ||
| code: 'a { @layer { color: red; } }', | ||
| description: 'declaration within nested @layer at-rule', | ||
| }, | ||
| ], | ||
|
|
||
| reject: [ | ||
| { | ||
| code: 'a { color: red; }', | ||
| description: 'declaration without required at-rule', | ||
| message: messages.expected('layer'), | ||
| line: 1, | ||
| column: 5, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| { | ||
| code: '@supports (color: red) { a { color: red; } }', | ||
| description: 'declaration within non-required at-rule', | ||
| message: messages.expected('layer'), | ||
| line: 1, | ||
| column: 30, | ||
| endLine: 1, | ||
| endColumn: 41, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| testRule({ | ||
| ruleName, | ||
| config: ['/^s/'], | ||
|
|
||
| accept: [ | ||
| { | ||
| code: '@supports (color: red) { a { color: red; } }', | ||
| description: 'declaration within @supports at-rule (matches regex)', | ||
| }, | ||
| { | ||
| code: '@scope (a) { a { color: red; } }', | ||
| description: 'declaration within @scope at-rule (matches regex)', | ||
| }, | ||
| ], | ||
|
|
||
| reject: [ | ||
| { | ||
| code: 'a { color: red; }', | ||
| description: 'declaration without required at-rule', | ||
| message: messages.expected('/^s/'), | ||
| line: 1, | ||
| column: 5, | ||
| endLine: 1, | ||
| endColumn: 16, | ||
| }, | ||
| { | ||
| code: '@layer { a { color: red; } }', | ||
| description: 'declaration within non-matching at-rule', | ||
| message: messages.expected('/^s/'), | ||
| line: 1, | ||
| column: 14, | ||
| endLine: 1, | ||
| endColumn: 25, | ||
| }, | ||
| ], | ||
| }); | ||
85 changes: 85 additions & 0 deletions
85
lib/rules/declaration-block-nesting-at-rule-required-list/index.cjs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
lib/rules/declaration-block-nesting-at-rule-required-list/index.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { isRegExp, isString } from '../../utils/validateTypes.mjs'; | ||
| import eachDeclarationBlock from '../../utils/eachDeclarationBlock.mjs'; | ||
| import isStandardSyntaxDeclaration from '../../utils/isStandardSyntaxDeclaration.mjs'; | ||
| import matchesStringOrRegExp from '../../utils/matchesStringOrRegExp.mjs'; | ||
| import report from '../../utils/report.mjs'; | ||
| import ruleMessages from '../../utils/ruleMessages.mjs'; | ||
| import validateOptions from '../../utils/validateOptions.mjs'; | ||
|
|
||
| const ruleName = 'declaration-block-nesting-at-rule-required-list'; | ||
|
|
||
| const messages = ruleMessages(ruleName, { | ||
| expected: (atRule) => `Expected nesting at-rule "${atRule}" for declaration`, | ||
| }); | ||
|
|
||
| const meta = { | ||
| url: 'https://stylelint.io/user-guide/rules/declaration-block-nesting-at-rule-required-list', | ||
| }; | ||
|
|
||
| /** @type {import('stylelint').CoreRules[ruleName]} */ | ||
| const rule = (primary) => { | ||
| return (root, result) => { | ||
| const validOptions = validateOptions(result, ruleName, { | ||
| actual: primary, | ||
| possible: [isString, isRegExp], | ||
| }); | ||
|
|
||
| if (!validOptions) return; | ||
|
|
||
| eachDeclarationBlock(root, (eachDecl) => { | ||
sw1tch3roo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| eachDecl((decl) => { | ||
| if (!isStandardSyntaxDeclaration(decl)) return; | ||
|
|
||
| // Skip if declaration already has a required at-rule ancestor | ||
| if (hasRequiredAtRuleAncestor(decl, primary)) return; | ||
|
|
||
| report({ | ||
| message: messages.expected, | ||
| messageArgs: [Array.isArray(primary) ? primary.join(', ') : primary], | ||
| node: decl, | ||
| result, | ||
| ruleName, | ||
| }); | ||
| }); | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {import('postcss').Declaration} decl | ||
| * @param {string | RegExp | Array<string | RegExp>} requiredAtRules | ||
| * @returns {boolean} | ||
| */ | ||
| function hasRequiredAtRuleAncestor(decl, requiredAtRules) { | ||
| let current = decl.parent; | ||
|
|
||
| while (current) { | ||
| if (current.type === 'atrule') { | ||
| const atRuleName = current.name; | ||
|
|
||
| // Check if at-rule name matches any of the required patterns | ||
| if (matchesStringOrRegExp(atRuleName, requiredAtRules)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| if (current.type === 'root') { | ||
| break; | ||
| } | ||
|
|
||
| current = current.parent; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| rule.primaryOptionArray = true; | ||
| rule.ruleName = ruleName; | ||
| rule.messages = messages; | ||
| rule.meta = meta; | ||
| export default rule; | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.