Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/rules/use-layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ When `allowUnnamedLayers` is set to `true`, the following code is **correct**:

#### `layerNamePattern`

The `layerNamePattern` is a regular expression string that allows you to validate the name of layers and prevent misspellings.
The `layerNamePattern` is a regular expression string that allows you to validate the name of layers and prevent misspellings. This option supports period-separated layer names (e.g., `foo.bar`) as defined in [CSS Cascade and Inheritance Level 5](https://drafts.csswg.org/css-cascade-5/#layer-names).

Here's an example of **incorrect** code:

Expand All @@ -106,8 +106,21 @@ Here's an example of **incorrect** code:
color: red;
}
}

/* unknown period-separated layer name */
@layer theme.custom {
a {
color: red;
}
}
```

Each part of a period-separated layer name is validated individually against the pattern. For example, with `layerNamePattern: "^(reset|theme|base)$"`:

- `theme.base` is valid (both parts match the pattern)
- `theme.custom` is invalid (`custom` doesn't match the pattern)
- `other.base` is invalid (`other` doesn't match the pattern)

#### `requireImportLayers: false`

When `requireImportLayers` is set to `false`, the following code is **correct**:
Expand Down
43 changes: 33 additions & 10 deletions src/rules/use-layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,39 @@ export default {
return;
}

if (!layerNameRegex.test(node.name)) {
context.report({
loc: node.loc,
messageId: "layerNameMismatch",
data: {
name: node.name,
pattern: options.layerNamePattern,
},
});
}
const parts = node.name.split(".");
let currentPos = 0;

parts.forEach((part, index) => {
if (!layerNameRegex.test(part)) {
const startColumn = node.loc.start.column + currentPos;
const endColumn = startColumn + part.length;

context.report({
loc: {
start: {
line: node.loc.start.line,
column: startColumn,
},
end: {
line: node.loc.start.line,
column: endColumn,
},
},
messageId: "layerNameMismatch",
data: {
name: part,
pattern: options.layerNamePattern,
},
});
}

currentPos += part.length;
// add 1 to account for the . symbol
if (index < parts.length - 1) {
currentPos += 1;
}
});
},

"Atrule[name=layer]"(node) {
Expand Down
145 changes: 145 additions & 0 deletions tests/rules/use-layers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ ruleTester.run("use-layers", rule, {
code: "@import 'foo.css';",
options: [{ requireImportLayers: false }],
},
{
code: "@layer foo.bar { a { color: red; } }",
options: [{ layerNamePattern: "^(foo|bar)$" }],
},
{
code: "@layer foo.bar.baz { a { color: red; } }",
options: [{ layerNamePattern: "^(foo|bar|baz)$" }],
},
{
code: "@import 'foo.css' layer(foo.bar);",
options: [{ layerNamePattern: "^(foo|bar)$" }],
},
{
code: "@layer foo, bar.baz, baz.qux;",
options: [{ layerNamePattern: "^(foo|bar|baz|qux)$" }],
},
],
invalid: [
{
Expand Down Expand Up @@ -263,5 +279,134 @@ ruleTester.run("use-layers", rule, {
},
],
},
{
code: "@layer foo.bar { a { color: red; } }",
options: [{ layerNamePattern: "bar" }],
errors: [
{
messageId: "layerNameMismatch",
data: {
name: "foo",
pattern: "bar",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 11,
},
],
},
{
code: "@layer foo.baz { a { color: red; } }",
options: [{ layerNamePattern: "bar" }],
errors: [
{
messageId: "layerNameMismatch",
data: {
name: "foo",
pattern: "bar",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 11,
},
{
messageId: "layerNameMismatch",
data: {
name: "baz",
pattern: "bar",
},
line: 1,
column: 12,
endLine: 1,
endColumn: 15,
},
],
},
{
code: "@layer foo.bar, baz.qux { a { color: red; } }",
options: [{ layerNamePattern: "bar" }],
errors: [
{
messageId: "layerNameMismatch",
data: {
name: "foo",
pattern: "bar",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 11,
},
{
messageId: "layerNameMismatch",
data: {
name: "baz",
pattern: "bar",
},
line: 1,
column: 17,
endLine: 1,
endColumn: 20,
},
{
messageId: "layerNameMismatch",
data: {
name: "qux",
pattern: "bar",
},
line: 1,
column: 21,
endLine: 1,
endColumn: 24,
},
],
},
{
code: "@import 'style.css' layer(foo.bar);",
options: [{ layerNamePattern: "bar" }],
errors: [
{
messageId: "layerNameMismatch",
data: {
name: "foo",
pattern: "bar",
},
line: 1,
column: 27,
endLine: 1,
endColumn: 30,
},
],
},
{
code: "@import 'style.css' layer(foo.baz);",
options: [{ layerNamePattern: "bar" }],
errors: [
{
messageId: "layerNameMismatch",
data: {
name: "foo",
pattern: "bar",
},
line: 1,
column: 27,
endLine: 1,
endColumn: 30,
},
{
messageId: "layerNameMismatch",
data: {
name: "baz",
pattern: "bar",
},
line: 1,
column: 31,
endLine: 1,
endColumn: 34,
},
],
},
],
});