@css-modules-kit/eslint-plugin 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 mizdra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # `@css-modules-kit/eslint-plugin`
2
+
3
+ A eslint plugin for CSS Modules
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i -D @css-modules-kit/eslint-plugin
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ // eslint.config.js
15
+ import { defineConfig } from 'eslint/config';
16
+ import css from '@eslint/css';
17
+ import cssModulesKit from '@css-modules-kit/eslint-plugin';
18
+
19
+ export default defineConfig([
20
+ {
21
+ files: ['**/*.css'],
22
+ language: 'css/css',
23
+ languageOptions: {
24
+ tolerant: true, // Required if you use `@value` rule or `composes` property
25
+ },
26
+ extends: [css.configs.recommended, cssModulesKit.configs.recommended],
27
+ },
28
+ ]);
29
+ ```
30
+
31
+ For vscode-eslint users, you need to add the following configuration to your `settings.json`:
32
+
33
+ ```jsonc
34
+ // .vscode/settings.json
35
+ {
36
+ "eslint.validate": ["css"],
37
+ }
38
+ ```
39
+
40
+ ## Rules
41
+
42
+ - `css-modules-kit/no-unused-class-names`
43
+ - `css-modules-kit/no-missing-component-file`
@@ -0,0 +1,37 @@
1
+ declare const plugin: {
2
+ meta: {
3
+ name: string;
4
+ version: string;
5
+ };
6
+ rules: {
7
+ 'no-missing-component-file': import("eslint").Rule.RuleModule;
8
+ 'no-unused-class-names': import("eslint").Rule.RuleModule;
9
+ };
10
+ configs: {
11
+ recommended: {
12
+ languageOptions: {
13
+ customSyntax: {
14
+ atrules: {
15
+ value: {
16
+ prelude: string;
17
+ };
18
+ };
19
+ properties: {
20
+ composes: {
21
+ syntax: string;
22
+ };
23
+ };
24
+ };
25
+ };
26
+ plugins: {
27
+ 'css-modules-kit': {};
28
+ };
29
+ rules: {
30
+ 'css-modules-kit/no-missing-component-file': "error";
31
+ 'css-modules-kit/no-unused-class-names': "error";
32
+ };
33
+ };
34
+ };
35
+ };
36
+ export = plugin;
37
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwDa,CAAC;AAG1B,SAAS,MAAM,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const package_json_1 = __importDefault(require("../package.json"));
6
+ const no_missing_component_file_js_1 = require("./rules/no-missing-component-file.js");
7
+ const no_unused_class_names_js_1 = require("./rules/no-unused-class-names.js");
8
+ const plugin = {
9
+ meta: {
10
+ name: package_json_1.default.name,
11
+ version: package_json_1.default.version,
12
+ },
13
+ rules: {
14
+ 'no-missing-component-file': no_missing_component_file_js_1.noMissingComponentFile,
15
+ 'no-unused-class-names': no_unused_class_names_js_1.noUnusedClassNames,
16
+ },
17
+ configs: {
18
+ recommended: {
19
+ languageOptions: {
20
+ customSyntax: {
21
+ atrules: {
22
+ value: {
23
+ // Example:
24
+ // - `@value a: #123;`
25
+ // - `@value empty:;`
26
+ // - `@value withoutSemicolon #123;`
27
+ // - `@value a from './test.module.css';`
28
+ // - `@value a, b from './test.module.css';`
29
+ // - `@value a as aliased_a from './test.module.css';`
30
+ //
31
+ // CSS Modules Kit does not support the following for implementation simplicity:
32
+ // - `@value (a, b) from '...';`
33
+ // - `@value a from moduleName;`
34
+ //
35
+ // ref: https://github.com/css-modules/postcss-icss-values/blob/acdf34a62cc2537a9507b1e9fd34db486e5cb0f8/test/test.js
36
+ prelude: '<custom-ident> :? <declaration-value>? | [ [ <custom-ident> [ as <custom-ident> ]? ]# from <string> ]',
37
+ },
38
+ },
39
+ properties: {
40
+ composes: {
41
+ // Example:
42
+ // - `composes: a;`
43
+ // - `composes: a from './test.module.css';`
44
+ // - `composes: a, b from './test.module.css';`
45
+ // - `composes: a b from './test.module.css';`
46
+ // - `composes: global(a) from './test.module.css';`
47
+ //
48
+ // ref: https://github.com/css-modules/postcss-modules-extract-imports/blob/16f9c570e517cf3558b88cf96dcadf794230965a/src/index.js
49
+ syntax: '[ [ <custom-ident> | global(<custom-ident>) ] ,? ]+ [ from <string> ]?',
50
+ },
51
+ },
52
+ },
53
+ },
54
+ plugins: {
55
+ 'css-modules-kit': {},
56
+ },
57
+ rules: {
58
+ 'css-modules-kit/no-missing-component-file': 'error',
59
+ 'css-modules-kit/no-unused-class-names': 'error',
60
+ },
61
+ },
62
+ },
63
+ };
64
+ plugin.configs.recommended.plugins['css-modules-kit'] = plugin;
65
+ module.exports = plugin;
66
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AACA,mEAA0C;AAC1C,uFAA8E;AAC9E,+EAAsE;AAEtE,MAAM,MAAM,GAAG;IACb,IAAI,EAAE;QACJ,IAAI,EAAE,sBAAW,CAAC,IAAI;QACtB,OAAO,EAAE,sBAAW,CAAC,OAAO;KAC7B;IACD,KAAK,EAAE;QACL,2BAA2B,EAAE,qDAAsB;QACnD,uBAAuB,EAAE,6CAAkB;KAC5C;IACD,OAAO,EAAE;QACP,WAAW,EAAE;YACX,eAAe,EAAE;gBACf,YAAY,EAAE;oBACZ,OAAO,EAAE;wBACP,KAAK,EAAE;4BACL,WAAW;4BACX,sBAAsB;4BACtB,qBAAqB;4BACrB,oCAAoC;4BACpC,yCAAyC;4BACzC,4CAA4C;4BAC5C,sDAAsD;4BACtD,EAAE;4BACF,gFAAgF;4BAChF,gCAAgC;4BAChC,gCAAgC;4BAChC,EAAE;4BACF,qHAAqH;4BACrH,OAAO,EACL,uGAAuG;yBAC1G;qBACF;oBACD,UAAU,EAAE;wBACV,QAAQ,EAAE;4BACR,WAAW;4BACX,mBAAmB;4BACnB,4CAA4C;4BAC5C,+CAA+C;4BAC/C,8CAA8C;4BAC9C,oDAAoD;4BACpD,EAAE;4BACF,iIAAiI;4BACjI,MAAM,EAAE,wEAAwE;yBACjF;qBACF;iBACF;aACF;YACD,OAAO,EAAE;gBACP,iBAAiB,EAAE,EAAE;aACtB;YACD,KAAK,EAAE;gBACL,2CAA2C,EAAE,OAAO;gBACpD,uCAAuC,EAAE,OAAO;aACjD;SACF;KACF;CACsB,CAAC;AAC1B,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,iBAAiB,CAAC,GAAG,MAAM,CAAC;AAE/D,iBAAS,MAAM,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const noMissingComponentFile: Rule.RuleModule;
3
+ //# sourceMappingURL=no-missing-component-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-missing-component-file.d.ts","sourceRoot":"","sources":["../../src/rules/no-missing-component-file.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAGnC,eAAO,MAAM,sBAAsB,EAAE,IAAI,CAAC,UAsBzC,CAAC"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.noMissingComponentFile = void 0;
4
+ const core_1 = require("@css-modules-kit/core");
5
+ const util_js_1 = require("../util.js");
6
+ exports.noMissingComponentFile = {
7
+ meta: {
8
+ type: 'problem',
9
+ language: 'css/css',
10
+ messages: {
11
+ disallow: 'The corresponding component file is not found.',
12
+ },
13
+ },
14
+ create(context) {
15
+ const fileName = context.filename;
16
+ if (fileName === undefined || !(0, core_1.isCSSModuleFile)(fileName))
17
+ return {};
18
+ const componentFile = (0, core_1.findComponentFileSync)(fileName, util_js_1.readFile);
19
+ if (componentFile === undefined) {
20
+ context.report({
21
+ loc: { line: 1, column: 0 },
22
+ messageId: 'disallow',
23
+ });
24
+ }
25
+ return {};
26
+ },
27
+ };
28
+ //# sourceMappingURL=no-missing-component-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-missing-component-file.js","sourceRoot":"","sources":["../../src/rules/no-missing-component-file.ts"],"names":[],"mappings":";;;AAAA,gDAA+E;AAE/E,wCAAsC;AAEzB,QAAA,sBAAsB,GAAoB;IACrD,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,QAAQ,EAAE;YACR,QAAQ,EAAE,gDAAgD;SAC3D;KACF;IACD,MAAM,CAAC,OAAO;QACZ,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QAClC,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,IAAA,sBAAe,EAAC,QAAQ,CAAC;YAAE,OAAO,EAAE,CAAC;QAEpE,MAAM,aAAa,GAAG,IAAA,4BAAqB,EAAC,QAAQ,EAAE,kBAAQ,CAAC,CAAC;QAEhE,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,OAAO,CAAC,MAAM,CAAC;gBACb,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE;gBAC3B,SAAS,EAAE,UAAU;aACtB,CAAC,CAAC;QACL,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;CACF,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Rule } from 'eslint';
2
+ export declare const noUnusedClassNames: Rule.RuleModule;
3
+ //# sourceMappingURL=no-unused-class-names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-unused-class-names.d.ts","sourceRoot":"","sources":["../../src/rules/no-unused-class-names.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAInC,eAAO,MAAM,kBAAkB,EAAE,IAAI,CAAC,UAiDrC,CAAC"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.noUnusedClassNames = void 0;
7
+ const core_1 = require("@css-modules-kit/core");
8
+ const postcss_safe_parser_1 = __importDefault(require("postcss-safe-parser"));
9
+ const util_js_1 = require("../util.js");
10
+ exports.noUnusedClassNames = {
11
+ meta: {
12
+ type: 'problem',
13
+ language: 'css/css',
14
+ messages: {
15
+ disallow: '"{{className}}" is defined but never used in "{{componentFileName}}"',
16
+ },
17
+ },
18
+ create(context) {
19
+ const fileName = context.filename;
20
+ if (fileName === undefined || !(0, core_1.isCSSModuleFile)(fileName))
21
+ return {};
22
+ const componentFile = (0, core_1.findComponentFileSync)(fileName, util_js_1.readFile);
23
+ // If the corresponding component file is not found, it is treated as a CSS Module file shared by the entire project.
24
+ // It is difficult to determine where class names in a shared CSS Module file are used. Therefore, it is
25
+ // assumed that all class names are used.
26
+ if (componentFile === undefined)
27
+ return {};
28
+ const usedTokenNames = (0, core_1.findUsedTokenNames)(componentFile.text);
29
+ const root = (0, postcss_safe_parser_1.default)(context.sourceCode.text, { from: fileName });
30
+ root.walkRules((rule) => {
31
+ const { classSelectors } = (0, core_1.parseRule)(rule);
32
+ for (const classSelector of classSelectors) {
33
+ if (!usedTokenNames.has(classSelector.name)) {
34
+ context.report({
35
+ loc: {
36
+ start: {
37
+ line: classSelector.loc.start.line,
38
+ column: classSelector.loc.start.column,
39
+ },
40
+ end: {
41
+ line: classSelector.loc.end.line,
42
+ column: classSelector.loc.end.column,
43
+ },
44
+ },
45
+ messageId: 'disallow',
46
+ data: {
47
+ className: classSelector.name,
48
+ componentFileName: (0, core_1.basename)(componentFile.fileName),
49
+ },
50
+ });
51
+ }
52
+ }
53
+ });
54
+ return {};
55
+ },
56
+ };
57
+ //# sourceMappingURL=no-unused-class-names.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-unused-class-names.js","sourceRoot":"","sources":["../../src/rules/no-unused-class-names.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwH;AAExH,8EAA6C;AAC7C,wCAAsC;AAEzB,QAAA,kBAAkB,GAAoB;IACjD,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,QAAQ,EAAE;YACR,QAAQ,EAAE,sEAAsE;SACjF;KACF;IACD,MAAM,CAAC,OAAO;QACZ,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QAClC,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,IAAA,sBAAe,EAAC,QAAQ,CAAC;YAAE,OAAO,EAAE,CAAC;QAEpE,MAAM,aAAa,GAAG,IAAA,4BAAqB,EAAC,QAAQ,EAAE,kBAAQ,CAAC,CAAC;QAEhE,qHAAqH;QACrH,wGAAwG;QACxG,yCAAyC;QACzC,IAAI,aAAa,KAAK,SAAS;YAAE,OAAO,EAAE,CAAC;QAE3C,MAAM,cAAc,GAAG,IAAA,yBAAkB,EAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAE9D,MAAM,IAAI,GAAG,IAAA,6BAAU,EAAC,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;QACrE,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE;YACtB,MAAM,EAAE,cAAc,EAAE,GAAG,IAAA,gBAAS,EAAC,IAAI,CAAC,CAAC;YAE3C,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;gBAC3C,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC;oBAC5C,OAAO,CAAC,MAAM,CAAC;wBACb,GAAG,EAAE;4BACH,KAAK,EAAE;gCACL,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI;gCAClC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM;6BACvC;4BACD,GAAG,EAAE;gCACH,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI;gCAChC,MAAM,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM;6BACrC;yBACF;wBACD,SAAS,EAAE,UAAU;wBACrB,IAAI,EAAE;4BACJ,SAAS,EAAE,aAAa,CAAC,IAAI;4BAC7B,iBAAiB,EAAE,IAAA,eAAQ,EAAC,aAAa,CAAC,QAAQ,CAAC;yBACpD;qBACF,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;QACH,OAAO,EAAE,CAAC;IACZ,CAAC;CACF,CAAC"}
package/dist/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function readFile(path: string): string;
2
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAEA,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE7C"}
package/dist/util.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.readFile = readFile;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ function readFile(path) {
9
+ return node_fs_1.default.readFileSync(path, 'utf-8');
10
+ }
11
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":";;;;;AAEA,4BAEC;AAJD,sDAAyB;AAEzB,SAAgB,QAAQ,CAAC,IAAY;IACnC,OAAO,iBAAE,CAAC,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACxC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@css-modules-kit/eslint-plugin",
3
+ "description": "A eslint plugin for CSS Modules",
4
+ "version": "0.0.1",
5
+ "type": "commonjs",
6
+ "sideEffects": false,
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/mizdra/css-modules-kit.git",
10
+ "directory": "packages/eslint-plugin"
11
+ },
12
+ "author": "mizdra <pp.mizdra@gmail.com>",
13
+ "license": "MIT",
14
+ "private": false,
15
+ "main": "./dist/index.js",
16
+ "scripts": {
17
+ "build": "tsc -b tsconfig.build.json"
18
+ },
19
+ "engines": {
20
+ "node": ">=22.0.0"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "registry": "https://registry.npmjs.org/"
25
+ },
26
+ "keywords": [
27
+ "css-modules",
28
+ "eslint",
29
+ "eslint-plugin"
30
+ ],
31
+ "files": [
32
+ "bin",
33
+ "src",
34
+ "!src/**/*.test.ts",
35
+ "!src/**/__snapshots__",
36
+ "!src/test",
37
+ "dist"
38
+ ],
39
+ "dependencies": {
40
+ "@css-modules-kit/core": "^0.1.0",
41
+ "postcss-safe-parser": "^7.0.1"
42
+ },
43
+ "peerDependencies": {
44
+ "@eslint/css": ">=0.4.0",
45
+ "eslint": ">=9.0.0"
46
+ }
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,65 @@
1
+ import type { ESLint } from 'eslint';
2
+ import packageJson from '../package.json';
3
+ import { noMissingComponentFile } from './rules/no-missing-component-file.js';
4
+ import { noUnusedClassNames } from './rules/no-unused-class-names.js';
5
+
6
+ const plugin = {
7
+ meta: {
8
+ name: packageJson.name,
9
+ version: packageJson.version,
10
+ },
11
+ rules: {
12
+ 'no-missing-component-file': noMissingComponentFile,
13
+ 'no-unused-class-names': noUnusedClassNames,
14
+ },
15
+ configs: {
16
+ recommended: {
17
+ languageOptions: {
18
+ customSyntax: {
19
+ atrules: {
20
+ value: {
21
+ // Example:
22
+ // - `@value a: #123;`
23
+ // - `@value empty:;`
24
+ // - `@value withoutSemicolon #123;`
25
+ // - `@value a from './test.module.css';`
26
+ // - `@value a, b from './test.module.css';`
27
+ // - `@value a as aliased_a from './test.module.css';`
28
+ //
29
+ // CSS Modules Kit does not support the following for implementation simplicity:
30
+ // - `@value (a, b) from '...';`
31
+ // - `@value a from moduleName;`
32
+ //
33
+ // ref: https://github.com/css-modules/postcss-icss-values/blob/acdf34a62cc2537a9507b1e9fd34db486e5cb0f8/test/test.js
34
+ prelude:
35
+ '<custom-ident> :? <declaration-value>? | [ [ <custom-ident> [ as <custom-ident> ]? ]# from <string> ]',
36
+ },
37
+ },
38
+ properties: {
39
+ composes: {
40
+ // Example:
41
+ // - `composes: a;`
42
+ // - `composes: a from './test.module.css';`
43
+ // - `composes: a, b from './test.module.css';`
44
+ // - `composes: a b from './test.module.css';`
45
+ // - `composes: global(a) from './test.module.css';`
46
+ //
47
+ // ref: https://github.com/css-modules/postcss-modules-extract-imports/blob/16f9c570e517cf3558b88cf96dcadf794230965a/src/index.js
48
+ syntax: '[ [ <custom-ident> | global(<custom-ident>) ] ,? ]+ [ from <string> ]?',
49
+ },
50
+ },
51
+ },
52
+ },
53
+ plugins: {
54
+ 'css-modules-kit': {},
55
+ },
56
+ rules: {
57
+ 'css-modules-kit/no-missing-component-file': 'error',
58
+ 'css-modules-kit/no-unused-class-names': 'error',
59
+ },
60
+ },
61
+ },
62
+ } satisfies ESLint.Plugin;
63
+ plugin.configs.recommended.plugins['css-modules-kit'] = plugin;
64
+
65
+ export = plugin;
@@ -0,0 +1,27 @@
1
+ import { findComponentFileSync, isCSSModuleFile } from '@css-modules-kit/core';
2
+ import type { Rule } from 'eslint';
3
+ import { readFile } from '../util.js';
4
+
5
+ export const noMissingComponentFile: Rule.RuleModule = {
6
+ meta: {
7
+ type: 'problem',
8
+ language: 'css/css',
9
+ messages: {
10
+ disallow: 'The corresponding component file is not found.',
11
+ },
12
+ },
13
+ create(context) {
14
+ const fileName = context.filename;
15
+ if (fileName === undefined || !isCSSModuleFile(fileName)) return {};
16
+
17
+ const componentFile = findComponentFileSync(fileName, readFile);
18
+
19
+ if (componentFile === undefined) {
20
+ context.report({
21
+ loc: { line: 1, column: 0 },
22
+ messageId: 'disallow',
23
+ });
24
+ }
25
+ return {};
26
+ },
27
+ };
@@ -0,0 +1,55 @@
1
+ import { basename, findComponentFileSync, findUsedTokenNames, isCSSModuleFile, parseRule } from '@css-modules-kit/core';
2
+ import type { Rule } from 'eslint';
3
+ import safeParser from 'postcss-safe-parser';
4
+ import { readFile } from '../util.js';
5
+
6
+ export const noUnusedClassNames: Rule.RuleModule = {
7
+ meta: {
8
+ type: 'problem',
9
+ language: 'css/css',
10
+ messages: {
11
+ disallow: '"{{className}}" is defined but never used in "{{componentFileName}}"',
12
+ },
13
+ },
14
+ create(context) {
15
+ const fileName = context.filename;
16
+ if (fileName === undefined || !isCSSModuleFile(fileName)) return {};
17
+
18
+ const componentFile = findComponentFileSync(fileName, readFile);
19
+
20
+ // If the corresponding component file is not found, it is treated as a CSS Module file shared by the entire project.
21
+ // It is difficult to determine where class names in a shared CSS Module file are used. Therefore, it is
22
+ // assumed that all class names are used.
23
+ if (componentFile === undefined) return {};
24
+
25
+ const usedTokenNames = findUsedTokenNames(componentFile.text);
26
+
27
+ const root = safeParser(context.sourceCode.text, { from: fileName });
28
+ root.walkRules((rule) => {
29
+ const { classSelectors } = parseRule(rule);
30
+
31
+ for (const classSelector of classSelectors) {
32
+ if (!usedTokenNames.has(classSelector.name)) {
33
+ context.report({
34
+ loc: {
35
+ start: {
36
+ line: classSelector.loc.start.line,
37
+ column: classSelector.loc.start.column,
38
+ },
39
+ end: {
40
+ line: classSelector.loc.end.line,
41
+ column: classSelector.loc.end.column,
42
+ },
43
+ },
44
+ messageId: 'disallow',
45
+ data: {
46
+ className: classSelector.name,
47
+ componentFileName: basename(componentFile.fileName),
48
+ },
49
+ });
50
+ }
51
+ }
52
+ });
53
+ return {};
54
+ },
55
+ };
package/src/util.ts ADDED
@@ -0,0 +1,5 @@
1
+ import fs from 'node:fs';
2
+
3
+ export function readFile(path: string): string {
4
+ return fs.readFileSync(path, 'utf-8');
5
+ }