Skip to content

Commit 8bca849

Browse files
authored
feat: add css-baseline preset (#887)
1 parent 020bc2e commit 8bca849

File tree

8 files changed

+180
-2
lines changed

8 files changed

+180
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ module.exports = {
7676

7777
### `eslint.config.js` (Flat Config)
7878

79-
> [!NOTE]
80-
> Flat Config support is experimental. The applicable rules and scope may have changed.
8179

8280
Put it into your `eslint.config.js`
8381

@@ -115,6 +113,8 @@ module.exports = [
115113
- Including `@cybozu/eslint-config/presets/typescript` and `@cybozu/eslint-config/presets/react`
116114
- `@cybozu/eslint-config/presets/es5`
117115
- or `@cybozu/eslint-config/flat/presets/es5` for Flat Config
116+
- `@cybozu/eslint-config/flat/presets/css-baseline` for Flat Config
117+
- CSS baseline rules using `@eslint/css`
118118

119119
## Prettier Support
120120

flat/lib/css-baseline.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const css = require("@eslint/css");
2+
3+
/**
4+
* @return { import("eslint").Linter.FlatConfig[] }
5+
*/
6+
module.exports = function cssBaseline() {
7+
return [
8+
{
9+
plugins: {
10+
css: css.default,
11+
},
12+
language: "css/css",
13+
// the same error recovery algorithm as the browser
14+
// https://eslint.org/blog/2025/02/eslint-css-support/#tolerant-parsing-support
15+
languageOptions: {
16+
tolerant: true,
17+
},
18+
rules: {
19+
"css/no-duplicate-imports": "error",
20+
21+
// Lint CSS files to ensure they are using
22+
// only Baseline Widely available features:
23+
"css/use-baseline": [
24+
"warn",
25+
{
26+
available: "widely",
27+
},
28+
],
29+
},
30+
},
31+
];
32+
};

flat/presets/css-baseline.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const cssBaseline = require("../lib/css-baseline.js");
2+
3+
/**
4+
* @type { import("eslint").Linter.Config[] }
5+
*/
6+
module.exports = [{ files: ["**/*.css"] }, ...cssBaseline()];

package-lock.json

Lines changed: 87 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"author": "cybozu",
2929
"license": "MIT",
3030
"devDependencies": {
31+
"@eslint/css": "^0.9.0",
3132
"@types/react": "^18.3.23",
3233
"eslint": "^9.19.0",
3334
"mocha": "^11.7.1",

test/fixtures/css-baseline/error.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* CSS with duplicate imports and non-widely available features */
2+
3+
/* duplicate imports are subject to error */
4+
@import url("styles.css");
5+
@import url("styles.css");
6+
7+
.container {
8+
display: grid;
9+
grid-template-columns: 1fr 1fr;
10+
/* non-widely available warning */
11+
container-type: inline-size;
12+
/* non-widely available warning */
13+
backdrop-filter: blur(10px);
14+
aspect-ratio: 16/9;
15+
}
16+
17+
.modern-feature {
18+
/* non-widely available warning: oklch */
19+
color: oklch(0.7 0.2 180);
20+
/* non-widely available warning */
21+
accent-color: blue;
22+
}

test/fixtures/css-baseline/ok.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Valid CSS using widely available features */
2+
.container {
3+
display: flex;
4+
margin: 10px;
5+
padding: 20px;
6+
background-color: #ffffff;
7+
border-radius: 4px;
8+
}
9+
10+
.button {
11+
color: #000;
12+
font-size: 16px;
13+
text-align: center;
14+
}

test/flat/preset-css-baseline-test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require("assert");
2+
const runLintWithFixturesFlat = require("../lib/runLintWithFixturesFlat");
3+
const cssBaseline = require("../../flat/presets/css-baseline");
4+
5+
describe("flat preset css-baseline", () => {
6+
it("should get expected errors and warnings", async () => {
7+
const result = await runLintWithFixturesFlat("css-baseline", cssBaseline);
8+
assert.deepStrictEqual(result, {
9+
"ok.css": {},
10+
"error.css": {
11+
errors: ["css/no-duplicate-imports"],
12+
warnings: ["css/use-baseline", "css/use-baseline", "css/use-baseline", "css/use-baseline"],
13+
},
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)