-
-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Add use-layers rule #27
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
90c27f9
feat: Add use-layers rule
nzakas 398edec
Update README
nzakas 6c097a0
@import should require layers by default
nzakas 59de581
Fix types error
nzakas a50e6fe
use-layers should not be recommended
nzakas cbb6594
Upgrade CSSTree to fix layer location error
nzakas 2985cb7
Add valid test
nzakas 253e0be
Update src/rules/use-layers.js
nzakas 2d537f2
Update docs/rules/use-layers.md
nzakas d0a99cf
Update src/rules/use-layers.js
nzakas 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
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,124 @@ | ||
| # use-layers | ||
|
|
||
| Require use of layers. | ||
|
|
||
| ## Background | ||
|
|
||
| Layers are a way to organize the cascading of rules outside of their source code order. By defining named layers and describing their order, you can ensure that rules are applied in the order that best matches your use case. Here's an example: | ||
|
|
||
| ```css | ||
| /* establish the cascade order */ | ||
| @layer reset, base, theme; | ||
|
|
||
| /* import styles into the reset layer */ | ||
| @import url("reset.css") layer(reset); | ||
|
|
||
| /* Theme styles */ | ||
| @layer theme { | ||
| body { | ||
| background-color: #f0f0f0; | ||
| color: #333; | ||
| } | ||
| } | ||
|
|
||
| /* Base styles */ | ||
| @layer base { | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| line-height: 1.6; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| In general, you don't want to mix rules inside of layers with rules outside of layers because you're then dealing with two different cascade behaviors. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule enforces the use of layers and warns when: | ||
|
|
||
| 1. Any rule appears outside of a `@layer` block. | ||
| 1. Any `@import` doesn't specify a layer. | ||
| 1. If any layer doesn't have a name. | ||
|
|
||
| Examples of incorrect code: | ||
|
|
||
| ```css | ||
| /* no layer name */ | ||
| @import url(foo.css) layer; | ||
|
|
||
| /* no layer */ | ||
| @import url(bar.css); | ||
|
|
||
| /* outside of layer */ | ||
| .my-style { | ||
| color: red; | ||
| } | ||
|
|
||
| /* no layer name */ | ||
| @layer { | ||
| a { | ||
| color: red; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| There are also additional options to customize the behavior of this rule. | ||
|
|
||
| ### Options | ||
|
|
||
| This rule accepts an options object with the following properties: | ||
|
|
||
| - `allowUnnamedLayers` (default: `false`) - Set to `true` to allow layers without names. | ||
| - `layerNamePattern` (default: `""`) - Set to a regular expression string to validate all layer names. | ||
| - `requireImportLayers` (default: `true`) - Set to `false` to allow `@import` rules without a layer. | ||
|
|
||
| #### `allowUnnamedLayers: true` | ||
|
|
||
| When `allowUnnamedLayers` is set to `true`, the following code is **correct**: | ||
|
|
||
| ```css | ||
| /* eslint css/use-layers: ["error", { allowUnnamedLayers: true }] */ | ||
| /* no layer name */ | ||
| @import url(foo.css) layer; | ||
|
|
||
| /* no layer name */ | ||
| @layer { | ||
| a { | ||
| color: red; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### `layerNamePattern` | ||
|
|
||
| The `layerNamePattern` is a regular expression string that allows you to validate the name of layers and prevent misspellings. | ||
|
|
||
| Here's an example of **incorrect** code: | ||
|
|
||
| ```css | ||
| /* eslint css/use-layers: ["error", { layerNamePattern: "^(reset|theme|base)$" }] */ | ||
| /* possible typo */ | ||
| @import url(foo.css) layer(resett); | ||
|
|
||
| /* unknown layer name */ | ||
| @layer defaults { | ||
| a { | ||
| color: red; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### `requireImportLayers: false` | ||
|
|
||
| When `requireImportLayers` is set to `false`, the following code is **correct**: | ||
|
|
||
| ```css | ||
| /* eslint css/use-layers: ["error", { requireImportLayers: false }] */ | ||
| @import url(foo.css); | ||
| @import url(foo.css) layer; | ||
| @import url(bar.css) layer(reset); | ||
| ``` | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| If you are defining rules without layers in a file (for example, `reset.css`) and then importing that file into a layer in another file (such as, `@import url(reset.css) layer(reset)`), then you should disable this rule in the imported file (in this example, `reset.css`). This rule is only needed in the file(s) that require layers. |
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
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
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,131 @@ | ||
| /** | ||
| * @fileoverview Rule to require layers in CSS. | ||
| * @author Nicholas C. Zakas | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Rule Definition | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| export default { | ||
| meta: { | ||
| type: /** @type {const} */ ("problem"), | ||
|
|
||
| docs: { | ||
| description: "Require use of layers", | ||
| url: "https://github.com/eslint/css/blob/main/docs/rules/use-layers.md", | ||
| }, | ||
|
|
||
| schema: [ | ||
| { | ||
| type: "object", | ||
| properties: { | ||
| allowUnnamedLayers: { | ||
| type: "boolean", | ||
| }, | ||
| requireImportLayers: { | ||
| type: "boolean", | ||
| }, | ||
| layerNamePattern: { | ||
| type: "string", | ||
| }, | ||
| }, | ||
| additionalProperties: false, | ||
| }, | ||
| ], | ||
|
|
||
| defaultOptions: [ | ||
| { | ||
| allowUnnamedLayers: false, | ||
| requireImportLayers: true, | ||
| layerNamePattern: "", | ||
| }, | ||
| ], | ||
|
|
||
| messages: { | ||
| missingLayer: "Expected rule to be within a layer.", | ||
| missingLayerName: "Expected layer to have a name.", | ||
| missingImportLayer: "Expected import to be within a layer.", | ||
| layerNameMismatch: | ||
| "Expected layer name '{{ name }}' to match pattern '{{pattern}}'.", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| let layerDepth = 0; | ||
| const options = context.options[0]; | ||
| const layerNameRegex = options.layerNamePattern | ||
| ? new RegExp(options.layerNamePattern, "u") | ||
| : null; | ||
|
|
||
| return { | ||
| "Atrule[name=import]"(node) { | ||
| // layer, if present, must always be the second child of the prelude | ||
| const secondChild = node.prelude.children[1]; | ||
| const layerNode = | ||
| secondChild?.name === "layer" ? secondChild : null; | ||
|
|
||
| if (options.requireImportLayers && !layerNode) { | ||
| context.report({ | ||
| loc: node.loc, | ||
| messageId: "missingImportLayer", | ||
| }); | ||
| } | ||
|
|
||
| if (layerNode) { | ||
| const isLayerFunction = layerNode.type === "Function"; | ||
|
|
||
| if (!options.allowUnnamedLayers && !isLayerFunction) { | ||
| context.report({ | ||
| loc: layerNode.loc, | ||
| messageId: "missingLayerName", | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| Layer(node) { | ||
| if (!layerNameRegex) { | ||
| return; | ||
| } | ||
|
|
||
| if (!layerNameRegex.test(node.name)) { | ||
| context.report({ | ||
| loc: node.loc, | ||
| messageId: "layerNameMismatch", | ||
| data: { | ||
| name: node.name, | ||
| pattern: options.layerNamePattern, | ||
| }, | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| "Atrule[name=layer]"(node) { | ||
| layerDepth++; | ||
|
|
||
| if (!options.allowUnnamedLayers && !node.prelude) { | ||
| context.report({ | ||
| loc: node.loc, | ||
| messageId: "missingLayerName", | ||
| }); | ||
| } | ||
| }, | ||
|
|
||
| "Atrule[name=layer]:exit"() { | ||
| layerDepth--; | ||
| }, | ||
|
|
||
| Rule(node) { | ||
| if (layerDepth > 0) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| loc: node.loc, | ||
| messageId: "missingLayer", | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
Oops, something went wrong.
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.