@css-modules-kit/stylelint-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) 2024 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,28 @@
1
+ # `@css-modules-kit/stylelint-plugin`
2
+
3
+ A stylelint plugin for CSS Modules
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm i -D @css-modules-kit/stylelint-plugin
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ // stylelint.config.js
15
+ /** @type {import('stylelint').Config} */
16
+ export default {
17
+ plugins: ['@css-modules-kit/stylelint-plugin'],
18
+ rules: {
19
+ 'css-modules-kit/no-unused-class-names': true,
20
+ 'css-modules-kit/no-missing-component-file': true,
21
+ },
22
+ };
23
+ ```
24
+
25
+ ## Rules
26
+
27
+ - `css-modules-kit/no-unused-class-names`
28
+ - `css-modules-kit/no-missing-component-file`
@@ -0,0 +1,3 @@
1
+ declare const _default: import("stylelint").Plugin[];
2
+ export = _default;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAGA,kBAAsD"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ const no_missing_component_file_js_1 = require("./rules/no-missing-component-file.js");
3
+ const no_unused_class_names_js_1 = require("./rules/no-unused-class-names.js");
4
+ module.exports = [no_unused_class_names_js_1.noUnusedClassNames, no_missing_component_file_js_1.noMissingComponentFile];
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,uFAA8E;AAC9E,+EAAsE;AAEtE,iBAAS,CAAC,6CAAkB,EAAE,qDAAsB,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import stylelint from 'stylelint';
2
+ export declare const noMissingComponentFile: stylelint.Plugin;
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":"AAEA,OAAO,SAAS,MAAM,WAAW,CAAC;AAwClC,eAAO,MAAM,sBAAsB,kBAAuC,CAAC"}
@@ -0,0 +1,41 @@
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.noMissingComponentFile = void 0;
7
+ const core_1 = require("@css-modules-kit/core");
8
+ const stylelint_1 = __importDefault(require("stylelint"));
9
+ const util_js_1 = require("../util.js");
10
+ // TODO: Report cjs-module-lexer compatibility problem to stylelint
11
+ const { createPlugin, utils } = stylelint_1.default;
12
+ const ruleName = 'css-modules-kit/no-missing-component-file';
13
+ const messages = utils.ruleMessages(ruleName, {
14
+ disallow: () => `The corresponding component file is not found.`,
15
+ });
16
+ const meta = {
17
+ url: 'https://github.com/mizdra/css-modules-kit/blob/main/packages/stylelint-plugin/docs/rules/no-missing-component-file.md',
18
+ };
19
+ const ruleFunction = (_primaryOptions, _secondaryOptions, _context) => {
20
+ return async (root, result) => {
21
+ const fileName = root.source?.input.file;
22
+ if (fileName === undefined || !(0, core_1.isCSSModuleFile)(fileName))
23
+ return;
24
+ const componentFile = await (0, core_1.findComponentFile)(fileName, util_js_1.readFile);
25
+ if (componentFile === undefined) {
26
+ utils.report({
27
+ result,
28
+ ruleName,
29
+ message: messages.disallow(),
30
+ node: root,
31
+ index: 0,
32
+ endIndex: 0,
33
+ });
34
+ }
35
+ };
36
+ };
37
+ ruleFunction.ruleName = ruleName;
38
+ ruleFunction.messages = messages;
39
+ ruleFunction.meta = meta;
40
+ exports.noMissingComponentFile = createPlugin(ruleName, ruleFunction);
41
+ //# 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,gDAA2E;AAE3E,0DAAkC;AAClC,wCAAsC;AAEtC,mEAAmE;AACnE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,GAAG,mBAAS,CAAC;AAE1C,MAAM,QAAQ,GAAG,2CAA2C,CAAC;AAE7D,MAAM,QAAQ,GAAG,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE;IAC5C,QAAQ,EAAE,GAAG,EAAE,CAAC,gDAAgD;CACjE,CAAC,CAAC;AAEH,MAAM,IAAI,GAAG;IACX,GAAG,EAAE,uHAAuH;CAC7H,CAAC;AAEF,MAAM,YAAY,GAAS,CAAC,eAAe,EAAE,iBAAiB,EAAE,QAAQ,EAAE,EAAE;IAC1E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC5B,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC;QACzC,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,IAAA,sBAAe,EAAC,QAAQ,CAAC;YAAE,OAAO;QAEjE,MAAM,aAAa,GAAG,MAAM,IAAA,wBAAiB,EAAC,QAAQ,EAAE,kBAAQ,CAAC,CAAC;QAElE,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,KAAK,CAAC,MAAM,CAAC;gBACX,MAAM;gBACN,QAAQ;gBACR,OAAO,EAAE,QAAQ,CAAC,QAAQ,EAAE;gBAC5B,IAAI,EAAE,IAAI;gBACV,KAAK,EAAE,CAAC;gBACR,QAAQ,EAAE,CAAC;aACZ,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC;AAEZ,QAAA,sBAAsB,GAAG,YAAY,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ import stylelint from 'stylelint';
2
+ export declare const noMissingTsFile: stylelint.Plugin;
3
+ //# sourceMappingURL=no-missing-ts-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-missing-ts-file.d.ts","sourceRoot":"","sources":["../../src/rules/no-missing-ts-file.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,WAAW,CAAC;AA0ClC,eAAO,MAAM,eAAe,kBAAuC,CAAC"}
@@ -0,0 +1,42 @@
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.noMissingTsFile = void 0;
7
+ const stylelint_1 = __importDefault(require("stylelint"));
8
+ const util_js_1 = require("../util.js");
9
+ // TODO: Report cjs-module-lexer compatibility problem to stylelint
10
+ const { createPlugin, utils } = stylelint_1.default;
11
+ const ruleName = 'honey-css-modules/no-missing-ts-file';
12
+ const messages = utils.ruleMessages(ruleName, {
13
+ disallow: () => `The corresponding TypeScript file is not found.`,
14
+ });
15
+ const meta = {
16
+ url: 'https://github.com/mizdra/honey-css-modules/blob/main/packages/stylelint-plugin-honey-css-modules/docs/rules/no-missing-ts-file.md',
17
+ };
18
+ const ruleFunction = (_primaryOptions, _secondaryOptions, _context) => {
19
+ return async (root, result) => {
20
+ if (root.source?.input.file === undefined)
21
+ return;
22
+ const cssModuleFileName = root.source.input.file;
23
+ if (!cssModuleFileName.endsWith('.module.css'))
24
+ return;
25
+ const tsFile = await (0, util_js_1.findComponentFile)(cssModuleFileName);
26
+ if (tsFile === undefined) {
27
+ utils.report({
28
+ result,
29
+ ruleName,
30
+ message: messages.disallow(),
31
+ node: root,
32
+ index: 0,
33
+ endIndex: 0,
34
+ });
35
+ }
36
+ };
37
+ };
38
+ ruleFunction.ruleName = ruleName;
39
+ ruleFunction.messages = messages;
40
+ ruleFunction.meta = meta;
41
+ exports.noMissingTsFile = createPlugin(ruleName, ruleFunction);
42
+ //# sourceMappingURL=no-missing-ts-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-missing-ts-file.js","sourceRoot":"","sources":["../../src/rules/no-missing-ts-file.ts"],"names":[],"mappings":";;;;;;AACA,0DAAkC;AAClC,wCAA+C;AAE/C,mEAAmE;AACnE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,GAAG,mBAAS,CAAC;AAE1C,MAAM,QAAQ,GAAG,sCAAsC,CAAC;AAExD,MAAM,QAAQ,GAAG,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE;IAC5C,QAAQ,EAAE,GAAG,EAAE,CAAC,iDAAiD;CAClE,CAAC,CAAC;AAEH,MAAM,IAAI,GAAG;IACX,GAAG,EAAE,oIAAoI;CAC1I,CAAC;AAEF,MAAM,YAAY,GAAS,CAAC,eAAe,EAAE,iBAAiB,EAAE,QAAQ,EAAE,EAAE;IAC1E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC5B,IAAI,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,KAAK,SAAS;YAAE,OAAO;QAClD,MAAM,iBAAiB,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QAEjD,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,aAAa,CAAC;YAAE,OAAO;QAEvD,MAAM,MAAM,GAAG,MAAM,IAAA,2BAAiB,EAAC,iBAAiB,CAAC,CAAC;QAE1D,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,KAAK,CAAC,MAAM,CAAC;gBACX,MAAM;gBACN,QAAQ;gBACR,OAAO,EAAE,QAAQ,CAAC,QAAQ,EAAE;gBAC5B,IAAI,EAAE,IAAI;gBACV,KAAK,EAAE,CAAC;gBACR,QAAQ,EAAE,CAAC;aACZ,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC;AAEZ,QAAA,eAAe,GAAG,YAAY,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import stylelint from 'stylelint';
2
+ declare function findUsedTokenNames(componentText: string): Set<string>;
3
+ export declare const noUnusedClassNames: stylelint.Plugin;
4
+ export { findUsedTokenNames as findUsedTokenNamesForTest };
5
+ //# 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":"AAEA,OAAO,SAAS,MAAM,WAAW,CAAC;AA4DlC,iBAAS,kBAAkB,CAAC,aAAa,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAO9D;AAMD,eAAO,MAAM,kBAAkB,kBAAuC,CAAC;AACvE,OAAO,EAAE,kBAAkB,IAAI,yBAAyB,EAAE,CAAC"}
@@ -0,0 +1,70 @@
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
+ exports.findUsedTokenNamesForTest = findUsedTokenNames;
8
+ const core_1 = require("@css-modules-kit/core");
9
+ const stylelint_1 = __importDefault(require("stylelint"));
10
+ const util_js_1 = require("../util.js");
11
+ // TODO: Report cjs-module-lexer compatibility problem to stylelint
12
+ const { createPlugin, utils } = stylelint_1.default;
13
+ const ruleName = 'css-modules-kit/no-unused-class-names';
14
+ const messages = utils.ruleMessages(ruleName, {
15
+ disallow: (className, componentFileName) => `"${className}" is defined but never used in "${(0, core_1.basename)(componentFileName)}"`,
16
+ });
17
+ const meta = {
18
+ url: 'https://github.com/mizdra/css-modules-kit/blob/main/packages/stylelint-plugin/docs/rules/no-unused-class-names.md',
19
+ };
20
+ const ruleFunction = (_primaryOptions, _secondaryOptions, _context) => {
21
+ return async (root, result) => {
22
+ const fileName = root.source?.input.file;
23
+ if (fileName === undefined || !(0, core_1.isCSSModuleFile)(fileName))
24
+ return;
25
+ const componentFile = await (0, core_1.findComponentFile)(fileName, util_js_1.readFile);
26
+ // If the corresponding component file is not found, it is treated as a CSS Module file shared by the entire project.
27
+ // It is difficult to determine where class names in a shared CSS Module file are used. Therefore, it is
28
+ // assumed that all class names are used.
29
+ if (componentFile === undefined)
30
+ return;
31
+ const usedTokenNames = findUsedTokenNames(componentFile.text);
32
+ root.walkRules((rule) => {
33
+ const { classSelectors } = (0, core_1.parseRule)(rule);
34
+ for (const classSelector of classSelectors) {
35
+ if (!usedTokenNames.has(classSelector.name)) {
36
+ utils.report({
37
+ result,
38
+ ruleName,
39
+ message: messages.disallow(classSelector.name, componentFile.fileName),
40
+ node: rule,
41
+ index: classSelector.loc.start.offset,
42
+ endIndex: classSelector.loc.end.offset,
43
+ word: classSelector.name,
44
+ });
45
+ }
46
+ }
47
+ });
48
+ };
49
+ };
50
+ /**
51
+ * The syntax pattern for consuming tokens imported from CSS Module.
52
+ * @example `styles.foo`
53
+ */
54
+ // TODO: Support `styles['foo']` and `styles["foo"]`
55
+ // MEMO: The `xxxStyles.foo` format is not supported, because the css module file for current component file is usually imported with `styles`.
56
+ // It is sufficient to support only the `styles.foo` format.
57
+ const TOKEN_CONSUMER_PATTERN = /styles\.([$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*)/gu;
58
+ function findUsedTokenNames(componentText) {
59
+ const usedClassNames = new Set();
60
+ let match;
61
+ while ((match = TOKEN_CONSUMER_PATTERN.exec(componentText)) !== null) {
62
+ usedClassNames.add(match[1]);
63
+ }
64
+ return usedClassNames;
65
+ }
66
+ ruleFunction.ruleName = ruleName;
67
+ ruleFunction.messages = messages;
68
+ ruleFunction.meta = meta;
69
+ exports.noUnusedClassNames = createPlugin(ruleName, ruleFunction);
70
+ //# 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":";;;;;;AA4E+B,uDAAyB;AA5ExD,gDAAgG;AAEhG,0DAAkC;AAClC,wCAAsC;AAEtC,mEAAmE;AACnE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,GAAG,mBAAS,CAAC;AAE1C,MAAM,QAAQ,GAAG,uCAAuC,CAAC;AAEzD,MAAM,QAAQ,GAAG,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE;IAC5C,QAAQ,EAAE,CAAC,SAAiB,EAAE,iBAAyB,EAAE,EAAE,CACzD,IAAI,SAAS,mCAAmC,IAAA,eAAQ,EAAC,iBAAiB,CAAC,GAAG;CACjF,CAAC,CAAC;AAEH,MAAM,IAAI,GAAG;IACX,GAAG,EAAE,mHAAmH;CACzH,CAAC;AAEF,MAAM,YAAY,GAAS,CAAC,eAAe,EAAE,iBAAiB,EAAE,QAAQ,EAAE,EAAE;IAC1E,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC5B,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC;QACzC,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,IAAA,sBAAe,EAAC,QAAQ,CAAC;YAAE,OAAO;QAEjE,MAAM,aAAa,GAAG,MAAM,IAAA,wBAAiB,EAAC,QAAQ,EAAE,kBAAQ,CAAC,CAAC;QAElE,qHAAqH;QACrH,wGAAwG;QACxG,yCAAyC;QACzC,IAAI,aAAa,KAAK,SAAS;YAAE,OAAO;QAExC,MAAM,cAAc,GAAG,kBAAkB,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;QAE9D,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,KAAK,CAAC,MAAM,CAAC;wBACX,MAAM;wBACN,QAAQ;wBACR,OAAO,EAAE,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,IAAI,EAAE,aAAa,CAAC,QAAQ,CAAC;wBACtE,IAAI,EAAE,IAAI;wBACV,KAAK,EAAE,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM;wBACrC,QAAQ,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM;wBACtC,IAAI,EAAE,aAAa,CAAC,IAAI;qBACzB,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF;;;GAGG;AACH,oDAAoD;AACpD,+IAA+I;AAC/I,kEAAkE;AAClE,MAAM,sBAAsB,GAAG,6DAA6D,CAAC;AAE7F,SAAS,kBAAkB,CAAC,aAAqB;IAC/C,MAAM,cAAc,GAAG,IAAI,GAAG,EAAU,CAAC;IACzC,IAAI,KAAK,CAAC;IACV,OAAO,CAAC,KAAK,GAAG,sBAAsB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;QACrE,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACjC,YAAY,CAAC,IAAI,GAAG,IAAI,CAAC;AAEZ,QAAA,kBAAkB,GAAG,YAAY,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC"}
package/dist/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function readFile(path: string): Promise<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,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAE5D"}
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 promises_1 = __importDefault(require("node:fs/promises"));
8
+ async function readFile(path) {
9
+ return promises_1.default.readFile(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,gEAAkC;AAE3B,KAAK,UAAU,QAAQ,CAAC,IAAY;IACzC,OAAO,kBAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACpC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@css-modules-kit/stylelint-plugin",
3
+ "description": "A stylelint 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/stylelint-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
+ "stylelint",
29
+ "stylelint-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.0.1"
41
+ },
42
+ "peerDependencies": {
43
+ "stylelint": "^16.0.0"
44
+ }
45
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { noMissingComponentFile } from './rules/no-missing-component-file.js';
2
+ import { noUnusedClassNames } from './rules/no-unused-class-names.js';
3
+
4
+ export = [noUnusedClassNames, noMissingComponentFile];
@@ -0,0 +1,43 @@
1
+ import { findComponentFile, isCSSModuleFile } from '@css-modules-kit/core';
2
+ import type { Rule } from 'stylelint';
3
+ import stylelint from 'stylelint';
4
+ import { readFile } from '../util.js';
5
+
6
+ // TODO: Report cjs-module-lexer compatibility problem to stylelint
7
+ const { createPlugin, utils } = stylelint;
8
+
9
+ const ruleName = 'css-modules-kit/no-missing-component-file';
10
+
11
+ const messages = utils.ruleMessages(ruleName, {
12
+ disallow: () => `The corresponding component file is not found.`,
13
+ });
14
+
15
+ const meta = {
16
+ url: 'https://github.com/mizdra/css-modules-kit/blob/main/packages/stylelint-plugin/docs/rules/no-missing-component-file.md',
17
+ };
18
+
19
+ const ruleFunction: Rule = (_primaryOptions, _secondaryOptions, _context) => {
20
+ return async (root, result) => {
21
+ const fileName = root.source?.input.file;
22
+ if (fileName === undefined || !isCSSModuleFile(fileName)) return;
23
+
24
+ const componentFile = await findComponentFile(fileName, readFile);
25
+
26
+ if (componentFile === undefined) {
27
+ utils.report({
28
+ result,
29
+ ruleName,
30
+ message: messages.disallow(),
31
+ node: root,
32
+ index: 0,
33
+ endIndex: 0,
34
+ });
35
+ }
36
+ };
37
+ };
38
+
39
+ ruleFunction.ruleName = ruleName;
40
+ ruleFunction.messages = messages;
41
+ ruleFunction.meta = meta;
42
+
43
+ export const noMissingComponentFile = createPlugin(ruleName, ruleFunction);
@@ -0,0 +1,77 @@
1
+ import { basename, findComponentFile, isCSSModuleFile, parseRule } from '@css-modules-kit/core';
2
+ import type { Rule } from 'stylelint';
3
+ import stylelint from 'stylelint';
4
+ import { readFile } from '../util.js';
5
+
6
+ // TODO: Report cjs-module-lexer compatibility problem to stylelint
7
+ const { createPlugin, utils } = stylelint;
8
+
9
+ const ruleName = 'css-modules-kit/no-unused-class-names';
10
+
11
+ const messages = utils.ruleMessages(ruleName, {
12
+ disallow: (className: string, componentFileName: string) =>
13
+ `"${className}" is defined but never used in "${basename(componentFileName)}"`,
14
+ });
15
+
16
+ const meta = {
17
+ url: 'https://github.com/mizdra/css-modules-kit/blob/main/packages/stylelint-plugin/docs/rules/no-unused-class-names.md',
18
+ };
19
+
20
+ const ruleFunction: Rule = (_primaryOptions, _secondaryOptions, _context) => {
21
+ return async (root, result) => {
22
+ const fileName = root.source?.input.file;
23
+ if (fileName === undefined || !isCSSModuleFile(fileName)) return;
24
+
25
+ const componentFile = await findComponentFile(fileName, readFile);
26
+
27
+ // If the corresponding component file is not found, it is treated as a CSS Module file shared by the entire project.
28
+ // It is difficult to determine where class names in a shared CSS Module file are used. Therefore, it is
29
+ // assumed that all class names are used.
30
+ if (componentFile === undefined) return;
31
+
32
+ const usedTokenNames = findUsedTokenNames(componentFile.text);
33
+
34
+ root.walkRules((rule) => {
35
+ const { classSelectors } = parseRule(rule);
36
+
37
+ for (const classSelector of classSelectors) {
38
+ if (!usedTokenNames.has(classSelector.name)) {
39
+ utils.report({
40
+ result,
41
+ ruleName,
42
+ message: messages.disallow(classSelector.name, componentFile.fileName),
43
+ node: rule,
44
+ index: classSelector.loc.start.offset,
45
+ endIndex: classSelector.loc.end.offset,
46
+ word: classSelector.name,
47
+ });
48
+ }
49
+ }
50
+ });
51
+ };
52
+ };
53
+
54
+ /**
55
+ * The syntax pattern for consuming tokens imported from CSS Module.
56
+ * @example `styles.foo`
57
+ */
58
+ // TODO: Support `styles['foo']` and `styles["foo"]`
59
+ // MEMO: The `xxxStyles.foo` format is not supported, because the css module file for current component file is usually imported with `styles`.
60
+ // It is sufficient to support only the `styles.foo` format.
61
+ const TOKEN_CONSUMER_PATTERN = /styles\.([$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*)/gu;
62
+
63
+ function findUsedTokenNames(componentText: string): Set<string> {
64
+ const usedClassNames = new Set<string>();
65
+ let match;
66
+ while ((match = TOKEN_CONSUMER_PATTERN.exec(componentText)) !== null) {
67
+ usedClassNames.add(match[1]!);
68
+ }
69
+ return usedClassNames;
70
+ }
71
+
72
+ ruleFunction.ruleName = ruleName;
73
+ ruleFunction.messages = messages;
74
+ ruleFunction.meta = meta;
75
+
76
+ export const noUnusedClassNames = createPlugin(ruleName, ruleFunction);
77
+ export { findUsedTokenNames as findUsedTokenNamesForTest };
package/src/util.ts ADDED
@@ -0,0 +1,5 @@
1
+ import fs from 'node:fs/promises';
2
+
3
+ export async function readFile(path: string): Promise<string> {
4
+ return fs.readFile(path, 'utf-8');
5
+ }