stylelint-plugin-rhythmguard 3.19.0 → 3.20.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/CHANGELOG.md +43 -0
- package/README.md +3 -3
- package/SECURITY.md +2 -1
- package/agents/claude-code/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/audit/args.js +30 -8
- package/src/audit/baseline.js +38 -4
- package/src/audit/codemod.js +4 -2
- package/src/audit/config.js +7 -0
- package/src/audit/contract.js +124 -14
- package/src/audit/next-steps.js +3 -1
- package/src/audit/non-authored.js +1 -0
- package/src/audit/render-github.js +16 -11
- package/src/audit/render-markdown.js +8 -2
- package/src/audit/render-text.js +41 -2
- package/src/audit/render-utils.js +14 -0
- package/src/audit/report.js +230 -10
- package/src/audit/scan/files.js +14 -5
- package/src/audit/scan/stylesheets.js +48 -53
- package/src/audit/scan/templates.js +2 -0
- package/src/audit/shared.js +7 -1
- package/src/audit/token-chains.js +1 -0
- package/src/cli/audit.js +2 -2
- package/src/cli/doctor.js +853 -237
- package/src/cli/first-run.js +210 -0
- package/src/cli/index.js +25 -1
- package/src/cli/index.mjs +3 -0
- package/src/cli/init.js +42 -13
- package/src/cli/quickstart.js +53 -102
- package/src/cli/scale.js +223 -0
- package/src/configs/recommended.js +3 -1
- package/src/configs/strict.js +3 -1
- package/src/configs/tailwind.js +15 -0
- package/src/core/decisions.js +18 -5
- package/src/core/fs-cache.js +1 -0
- package/src/core/options.js +2 -1
- package/src/core/scale-inference.js +72 -52
- package/src/core/tailwind-class-analysis.js +56 -13
- package/src/core/token-index.js +55 -14
- package/src/core/token-map.js +138 -29
- package/src/core/token-sources.js +461 -40
- package/src/core/value-nodes.js +4 -2
- package/src/eslint/class-context.js +330 -0
- package/src/eslint/literal-spans.js +5 -4
- package/src/eslint/rules/tailwind-class-use-scale.js +30 -9
- package/src/presets/default-scale.js +42 -0
- package/src/presets/scales.js +6 -1
- package/src/rules/no-offscale-transform/index.js +17 -5
- package/src/rules/prefer-token/index.js +51 -15
- package/src/rules/use-scale/index.js +7 -4
- package/types/__checks__/consumer.ts +17 -0
- package/types/__checks__/eslint-config.ts +5 -0
- package/types/audit.d.ts +40 -3
- package/types/cli.d.ts +77 -1
- package/types/shared.d.ts +10 -0
package/src/cli/doctor.js
CHANGED
|
@@ -1,85 +1,76 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* `rhythmguard doctor`: check that the setup in a directory loads and lints
|
|
5
|
+
* the way the user expects, and say which spacing scale it checks against.
|
|
6
|
+
*
|
|
7
|
+
* `runDoctor({ cwd })` does the work and returns typed checks; it keeps no
|
|
8
|
+
* module state, writes nothing and never exits the process, so quickstart, an
|
|
9
|
+
* agent or a test can call it as often as it likes (cq-11). The Stylelint
|
|
10
|
+
* config is loaded through the project's own Stylelint (`resolveConfig`), so
|
|
11
|
+
* every `extends` and plugin is resolved as `npx stylelint` resolves it, and
|
|
12
|
+
* one stylesheet is linted for real (dx-07). The ESLint side is loaded through
|
|
13
|
+
* the project's own ESLint, and an ESLint-only project is checked without
|
|
14
|
+
* Stylelint instead of being told to install it (ai-02).
|
|
15
|
+
*/
|
|
3
16
|
const fs = require('node:fs');
|
|
4
17
|
const path = require('node:path');
|
|
18
|
+
const { pathToFileURL } = require('node:url');
|
|
19
|
+
|
|
20
|
+
const postcss = require('postcss');
|
|
21
|
+
|
|
5
22
|
const { applyAuditConfig, loadAuditConfig, resolveIgnorePatterns } = require('../audit/config');
|
|
6
|
-
const {
|
|
23
|
+
const { eslintInstallLine, stylelintInstallLine } = require('../audit/next-steps');
|
|
24
|
+
const { isNonAuthoredPath } = require('../audit/non-authored');
|
|
25
|
+
const { walkFiles } = require('../audit/scan/files');
|
|
26
|
+
const { DEFAULT_IGNORE_PATH, createDefaultAuditOptions } = require('../audit/shared');
|
|
7
27
|
const { normalizeDecisions } = require('../core/decisions');
|
|
8
28
|
const { readJsonFile } = require('../core/json');
|
|
29
|
+
const { normalizeScale } = require('../core/length');
|
|
30
|
+
const { buildScaleOptions } = require('../core/options');
|
|
31
|
+
const { DEFAULT_AUTO_TOKEN_PATTERN, resolveAutoScale } = require('../core/scale-inference');
|
|
32
|
+
const { createTailwindClassAnalyzer } = require('../core/tailwind-class-analysis');
|
|
9
33
|
const { normalizeTokenSourceEntry } = require('../core/token-source-entries');
|
|
10
34
|
const { normalizeTokenSourceFormat, parseTokenSources } = require('../core/token-sources');
|
|
35
|
+
const { DEFAULT_SCALE, TAILWIND_SCALE_NAME } = require('../presets/default-scale');
|
|
36
|
+
const ownPackage = require('../../package.json');
|
|
11
37
|
|
|
12
38
|
const HELP = `Usage: rhythmguard doctor
|
|
13
39
|
|
|
14
|
-
Validate the Rhythmguard setup in the current directory
|
|
15
|
-
the
|
|
16
|
-
|
|
17
|
-
|
|
40
|
+
Validate the Rhythmguard setup in the current directory. Loads the Stylelint
|
|
41
|
+
config through the project's own Stylelint, with every extends and plugin
|
|
42
|
+
resolved, checks Stylelint against the plugin's peer range, lints one
|
|
43
|
+
stylesheet, and prints the spacing scale the rules check and where it comes
|
|
44
|
+
from. Loads the ESLint config through the project's own ESLint and compares
|
|
45
|
+
the scale tailwind-class-use-scale checks with it. In a project that uses
|
|
46
|
+
Rhythmguard through ESLint only (no Stylelint), checks ESLint and skips the
|
|
47
|
+
Stylelint checks. Also checks .rhythmguardrc.json, token sources and the
|
|
48
|
+
baseline. Exits 1 when a check fails; warnings do not change the exit code.
|
|
18
49
|
|
|
19
50
|
Options:
|
|
20
51
|
--help Show this help message
|
|
21
52
|
`;
|
|
22
53
|
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
issues++;
|
|
32
|
-
process.stdout.write(`✗ ${msg}\n`);
|
|
33
|
-
if (fix) {
|
|
34
|
-
process.stdout.write(` → ${fix}\n`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function skip(msg) {
|
|
39
|
-
process.stdout.write(`- ${msg}\n`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let warnings = 0;
|
|
43
|
-
|
|
44
|
-
// Worth knowing, but not a failure: the exit code does not change.
|
|
45
|
-
function warn(msg) {
|
|
46
|
-
warnings++;
|
|
47
|
-
process.stdout.write(`! ${msg}\n`);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function installedVersion(name) {
|
|
51
|
-
try {
|
|
52
|
-
return require(require.resolve(`${name}/package.json`, { paths: [cwd] })).version;
|
|
53
|
-
} catch {
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Check 1: Stylelint installed
|
|
59
|
-
function checkStylelint() {
|
|
60
|
-
const version = installedVersion('stylelint');
|
|
61
|
-
if (version) {
|
|
62
|
-
pass(`stylelint installed (v${version})`);
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
fail(
|
|
66
|
-
'stylelint not installed',
|
|
67
|
-
'Run: npm install --save-dev stylelint',
|
|
68
|
-
);
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
54
|
+
const PLUGIN_NAME = 'stylelint-plugin-rhythmguard';
|
|
55
|
+
const ESLINT_PLUGIN_NAME = 'eslint-plugin-rhythmguard';
|
|
56
|
+
const ESLINT_RULE = 'tailwind-class-use-scale';
|
|
57
|
+
// The Stylelint rules whose options hold the spacing scale, in the order the scale is read from.
|
|
58
|
+
const SCALE_RULES = ['rhythmguard/use-scale', 'rhythmguard/no-offscale-transform'];
|
|
59
|
+
const TOOL_CONFIG_FILE = /^(?:\..*|.*\.config\.[cm]?[jt]s)$/;
|
|
60
|
+
// Stylesheets tried for the real lint, so one ignored file does not hide the check.
|
|
61
|
+
const MAX_LINT_ATTEMPTS = 5;
|
|
71
62
|
|
|
72
63
|
const STYLELINT_CONFIG_FILES = [
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
64
|
+
'.stylelintrc',
|
|
65
|
+
'.stylelintrc.json',
|
|
66
|
+
'.stylelintrc.js',
|
|
67
|
+
'.stylelintrc.cjs',
|
|
68
|
+
'.stylelintrc.mjs',
|
|
69
|
+
'.stylelintrc.yml',
|
|
70
|
+
'.stylelintrc.yaml',
|
|
71
|
+
'stylelint.config.js',
|
|
72
|
+
'stylelint.config.cjs',
|
|
73
|
+
'stylelint.config.mjs',
|
|
83
74
|
];
|
|
84
75
|
|
|
85
76
|
const ESLINT_CONFIG_FILES = [
|
|
@@ -97,173 +88,723 @@ const ESLINT_CONFIG_FILES = [
|
|
|
97
88
|
'.eslintrc.yaml',
|
|
98
89
|
];
|
|
99
90
|
|
|
100
|
-
|
|
91
|
+
/** The checks of one run, and the helpers that add to them. */
|
|
92
|
+
function createChecks() {
|
|
93
|
+
const checks = [];
|
|
94
|
+
const add = (status) => (id, message, fix) => {
|
|
95
|
+
checks.push({ id, status, message, ...(fix ? { fix } : {}) });
|
|
96
|
+
};
|
|
97
|
+
return { checks, fail: add('fail'), pass: add('pass'), skip: add('skip'), warn: add('warn') };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// --- Packages, resolved from the project rather than from this package ------
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* A package installed for `cwd`: its directory and manifest, found the way
|
|
104
|
+
* Node finds it (node_modules in cwd, then each parent). Read from disk, not
|
|
105
|
+
* through `require.resolve`, so a package without a main entry or without a
|
|
106
|
+
* `./package.json` export is still found. Null when absent.
|
|
107
|
+
*/
|
|
108
|
+
function findPackage(name, cwd) {
|
|
109
|
+
let dir = path.resolve(cwd);
|
|
110
|
+
for (;;) {
|
|
111
|
+
const manifestPath = path.join(dir, 'node_modules', name, 'package.json');
|
|
112
|
+
if (fs.existsSync(manifestPath)) {
|
|
113
|
+
try {
|
|
114
|
+
return { dir: path.dirname(manifestPath), manifest: readJsonFile(manifestPath) };
|
|
115
|
+
} catch {
|
|
116
|
+
// Unreadable: treated as an absent manifest; the install check reports it.
|
|
117
|
+
return { dir: path.dirname(manifestPath), manifest: {} };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const parent = path.dirname(dir);
|
|
121
|
+
if (parent === dir) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
dir = parent;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The entry `import()` would load: the `import` or `default` export of ".", else `main`. */
|
|
129
|
+
function moduleEntry(manifest) {
|
|
130
|
+
const exportsField = manifest.exports;
|
|
131
|
+
const root = exportsField && typeof exportsField === 'object' && !Array.isArray(exportsField) && '.' in exportsField
|
|
132
|
+
? exportsField['.']
|
|
133
|
+
: exportsField;
|
|
134
|
+
const pick = (value) => {
|
|
135
|
+
if (typeof value === 'string') {
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
139
|
+
return pick(value.import) || pick(value.node) || pick(value.default) || pick(value.require);
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
143
|
+
return pick(root) || manifest.main || 'index.js';
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Load an installed package through its ESM or CJS entry; the default export when it has one. */
|
|
147
|
+
async function importPackage(pkg) {
|
|
148
|
+
const mod = await import(pathToFileURL(path.join(pkg.dir, moduleEntry(pkg.manifest))).href);
|
|
149
|
+
return mod && mod.default ? mod.default : mod;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// --- Version ranges ---------------------------------------------------------
|
|
153
|
+
|
|
154
|
+
function parseVersion(version) {
|
|
155
|
+
const match = /^v?(\d+)\.(\d+)\.(\d+)/.exec(String(version || '').trim());
|
|
156
|
+
return match ? match.slice(1, 4).map(Number) : null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function compareVersions(a, b) {
|
|
160
|
+
for (let index = 0; index < 3; index++) {
|
|
161
|
+
if (a[index] !== b[index]) {
|
|
162
|
+
return a[index] < b[index] ? -1 : 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function satisfiesComparator(version, comparator) {
|
|
169
|
+
const match = /^(\^|~|>=|<=|>|<|=)?\s*v?(\d+)(?:\.(\d+))?(?:\.(\d+))?/.exec(comparator);
|
|
170
|
+
if (!match) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const operator = match[1] || '=';
|
|
174
|
+
const base = [Number(match[2]), Number(match[3] || 0), Number(match[4] || 0)];
|
|
175
|
+
const cmp = compareVersions(version, base);
|
|
176
|
+
switch (operator) {
|
|
177
|
+
case '^': {
|
|
178
|
+
// ^1.2.3 is >=1.2.3 <2.0.0; ^0.2.3 is >=0.2.3 <0.3.0.
|
|
179
|
+
const upper = base[0] > 0 ? [base[0] + 1, 0, 0] : [0, base[1] + 1, 0];
|
|
180
|
+
return cmp >= 0 && compareVersions(version, upper) < 0;
|
|
181
|
+
}
|
|
182
|
+
case '~':
|
|
183
|
+
return cmp >= 0 && compareVersions(version, [base[0], base[1] + 1, 0]) < 0;
|
|
184
|
+
case '>=':
|
|
185
|
+
return cmp >= 0;
|
|
186
|
+
case '<=':
|
|
187
|
+
return cmp <= 0;
|
|
188
|
+
case '>':
|
|
189
|
+
return cmp > 0;
|
|
190
|
+
case '<':
|
|
191
|
+
return cmp < 0;
|
|
192
|
+
default:
|
|
193
|
+
return cmp === 0;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Whether a version satisfies an npm range of the forms package.json peer
|
|
199
|
+
* ranges use: `^`, `~`, comparisons, sets joined by spaces and alternatives
|
|
200
|
+
* joined by `||`. Null when the version does not parse.
|
|
201
|
+
*/
|
|
202
|
+
function satisfiesRange(version, range) {
|
|
203
|
+
const parsed = parseVersion(version);
|
|
204
|
+
if (!parsed) {
|
|
205
|
+
return null;
|
|
206
|
+
}
|
|
207
|
+
return String(range).split('||').some((set) => {
|
|
208
|
+
const comparators = set.trim().replace(/(\^|~|>=|<=|>|<|=)\s+/g, '$1').split(/\s+/).filter(Boolean);
|
|
209
|
+
return comparators.length > 0 && comparators.every((comparator) => satisfiesComparator(parsed, comparator));
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// --- Project files -----------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
function readPackageJson(cwd) {
|
|
101
216
|
try {
|
|
102
|
-
return
|
|
217
|
+
return readJsonFile(path.join(cwd, 'package.json'));
|
|
103
218
|
} catch {
|
|
219
|
+
// No readable package.json: the callers read that as no dependencies.
|
|
104
220
|
return null;
|
|
105
221
|
}
|
|
106
222
|
}
|
|
107
223
|
|
|
108
|
-
|
|
109
|
-
function locateConfig(files, packageKey) {
|
|
224
|
+
/** The first config file present in cwd, or the package.json key; `{ label, path, content }` or null. */
|
|
225
|
+
function locateConfig(cwd, files, packageKey) {
|
|
110
226
|
for (const file of files) {
|
|
111
227
|
const fullPath = path.join(cwd, file);
|
|
112
228
|
if (fs.existsSync(fullPath)) {
|
|
113
|
-
|
|
229
|
+
let content = '';
|
|
230
|
+
try {
|
|
231
|
+
content = fs.readFileSync(fullPath, 'utf8');
|
|
232
|
+
} catch {
|
|
233
|
+
// Unreadable: the loader reports it with its own message.
|
|
234
|
+
}
|
|
235
|
+
return { content, label: file, path: fullPath };
|
|
114
236
|
}
|
|
115
237
|
}
|
|
116
|
-
const pkg = readPackageJson();
|
|
238
|
+
const pkg = readPackageJson(cwd);
|
|
117
239
|
if (pkg && pkg[packageKey]) {
|
|
118
|
-
return {
|
|
119
|
-
content: JSON.stringify(pkg[packageKey]),
|
|
120
|
-
label: `package.json ${packageKey} key`,
|
|
121
|
-
path: path.join(cwd, 'package.json'),
|
|
122
|
-
};
|
|
240
|
+
return { content: JSON.stringify(pkg[packageKey]), label: `package.json ${packageKey} key`, path: path.join(cwd, 'package.json') };
|
|
123
241
|
}
|
|
124
242
|
return null;
|
|
125
243
|
}
|
|
126
244
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
245
|
+
function relative(cwd, file) {
|
|
246
|
+
const rel = path.relative(cwd, path.resolve(cwd, file)).replace(/\\/g, '/');
|
|
247
|
+
return rel || '.';
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Stylesheets and class-string files, authored ones first, as the audit scans them. */
|
|
251
|
+
function scanProject(cwd) {
|
|
252
|
+
let found = { cssFiles: [], templateFiles: [] };
|
|
253
|
+
try {
|
|
254
|
+
found = walkFiles(cwd, []);
|
|
255
|
+
} catch {
|
|
256
|
+
// An unreadable directory: the checks that need files say none were found.
|
|
133
257
|
}
|
|
258
|
+
const authored = (files) => files.filter((file) => !isNonAuthoredPath(relative(cwd, file)));
|
|
259
|
+
const cssFiles = authored(found.cssFiles);
|
|
260
|
+
// Tool configs (eslint.config.mjs, .prettierrc.js) hold no class strings.
|
|
261
|
+
const templateFiles = authored(found.templateFiles).filter((file) => !TOOL_CONFIG_FILE.test(path.basename(file)));
|
|
262
|
+
// Markup with class attributes first, then plain scripts.
|
|
263
|
+
const markup = templateFiles.filter((file) => /\.(?:jsx|tsx|vue|svelte|astro)$/.test(file));
|
|
264
|
+
const scripts = templateFiles.filter((file) => !markup.includes(file));
|
|
265
|
+
return {
|
|
266
|
+
classFiles: [...markup, ...scripts],
|
|
267
|
+
cssFiles: [...cssFiles.filter((file) => file.endsWith('.css')), ...cssFiles.filter((file) => !file.endsWith('.css'))],
|
|
268
|
+
scssFiles: cssFiles.filter((file) => file.endsWith('.scss')),
|
|
269
|
+
typescript: templateFiles.some((file) => /\.tsx?$/.test(file)),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
134
272
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
);
|
|
139
|
-
return null;
|
|
273
|
+
// --- Formatting ---------------------------------------------------------------
|
|
274
|
+
|
|
275
|
+
function formatScale(values) {
|
|
276
|
+
return values.length > 0 ? `${values.join(', ')}px` : '(empty)';
|
|
140
277
|
}
|
|
141
278
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
279
|
+
function listDifference(values) {
|
|
280
|
+
const shown = values.slice(0, 8).join(', ');
|
|
281
|
+
return values.length > 8 ? `${shown} and ${values.length - 8} more` : shown;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function firstLine(error) {
|
|
285
|
+
return String((error && error.message) || error).split('\n')[0];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function sameValues(a, b) {
|
|
289
|
+
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// --- Stylelint ----------------------------------------------------------------
|
|
293
|
+
|
|
294
|
+
/** The secondary options of an enabled rule in a resolved config, or null when the rule is off. */
|
|
295
|
+
function enabledRuleOptions(rules, name) {
|
|
296
|
+
if (!rules || !(name in rules)) {
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
const value = rules[name];
|
|
300
|
+
const primary = Array.isArray(value) ? value[0] : value;
|
|
301
|
+
if (primary === null || primary === false || primary === undefined) {
|
|
146
302
|
return null;
|
|
147
303
|
}
|
|
304
|
+
const secondary = Array.isArray(value) ? value[1] : undefined;
|
|
305
|
+
return secondary && typeof secondary === 'object' ? secondary : {};
|
|
306
|
+
}
|
|
148
307
|
|
|
149
|
-
|
|
150
|
-
|
|
308
|
+
/** The install command for what a Stylelint configuration error names. */
|
|
309
|
+
function fixForConfigError(message, project) {
|
|
310
|
+
const missing = /Could not find "([^"]+)"|Cannot find (?:module|package) '([^']+)'/.exec(message);
|
|
311
|
+
const name = missing ? (missing[1] || missing[2]) : '';
|
|
312
|
+
if (name.startsWith(PLUGIN_NAME) || name === 'stylelint-config-tailwindcss' || name === 'postcss-scss') {
|
|
313
|
+
return `Run: ${stylelintInstallLine({ scss: project.scssFiles.length > 0 || name === 'postcss-scss', tailwindConfig: name === 'stylelint-config-tailwindcss' })}`;
|
|
314
|
+
}
|
|
315
|
+
if (name) {
|
|
316
|
+
return `Run: npm install --save-dev ${name.startsWith('@') ? name.split('/').slice(0, 2).join('/') : name.split('/')[0]}`;
|
|
317
|
+
}
|
|
318
|
+
return 'Fix the Stylelint config; npx stylelint stops on the same error';
|
|
319
|
+
}
|
|
151
320
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
321
|
+
/**
|
|
322
|
+
* The scale a rule's options check, and where it comes from: the same
|
|
323
|
+
* resolution the rule makes (core/options, then core/scale-inference for
|
|
324
|
+
* `scale: "auto"`). The sample stylesheet stands in for the linted file when
|
|
325
|
+
* auto inference reaches the stylesheet tier.
|
|
326
|
+
*/
|
|
327
|
+
function resolveRuleScale({ cwd, ruleName, secondary, sample }) {
|
|
328
|
+
const options = buildScaleOptions(secondary);
|
|
329
|
+
if (options.invalidPreset) {
|
|
330
|
+
return { error: `Unknown scale preset "${options.invalidPreset}" on ${ruleName}. Available presets: ${options.presetNames.join(', ')}.` };
|
|
331
|
+
}
|
|
332
|
+
const baseFontSize = options.baseFontSize;
|
|
333
|
+
if (!options.scaleAuto) {
|
|
334
|
+
const values = normalizeScale(options.scale, baseFontSize);
|
|
335
|
+
let label;
|
|
336
|
+
if (Array.isArray(secondary.customScale)) {
|
|
337
|
+
label = `the "customScale" option of ${ruleName}`;
|
|
338
|
+
} else if (Array.isArray(secondary.scale)) {
|
|
339
|
+
label = `the "scale" option of ${ruleName}`;
|
|
340
|
+
} else if (options.preset) {
|
|
341
|
+
label = `preset "${options.preset}" on ${ruleName}`;
|
|
342
|
+
} else {
|
|
343
|
+
label = `the default scale (${ruleName} sets no scale)`;
|
|
155
344
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const pkg = JSON.parse(content);
|
|
160
|
-
const stylelintConfig = JSON.stringify(pkg.stylelint || {});
|
|
161
|
-
if (stylelintConfig.includes('rhythmguard')) {
|
|
162
|
-
pass('config references rhythmguard');
|
|
163
|
-
return stylelintConfig;
|
|
164
|
-
}
|
|
345
|
+
const isDefault = sameValues(values, [...DEFAULT_SCALE]);
|
|
346
|
+
if (isDefault && Array.isArray(secondary.scale)) {
|
|
347
|
+
label += ', the default scale';
|
|
165
348
|
}
|
|
349
|
+
return { scale: { files: [], isDefault, label, source: Array.isArray(secondary.scale) || Array.isArray(secondary.customScale) ? 'explicit' : options.preset ? 'preset' : 'default', values, warnings: [] } };
|
|
350
|
+
}
|
|
166
351
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
352
|
+
const inference = resolveAutoScale({
|
|
353
|
+
baseFontSize,
|
|
354
|
+
cwd,
|
|
355
|
+
fallbackPreset: options.preset || undefined,
|
|
356
|
+
root: sample ? sample.root : undefined,
|
|
357
|
+
scaleSources: options.scaleSources,
|
|
358
|
+
sourceTokenPattern: options.tokenPatternExplicit ? options.tokenPattern : null,
|
|
359
|
+
tailwindConfigPath: options.tailwindConfigPath ? path.resolve(cwd, options.tailwindConfigPath) : null,
|
|
360
|
+
tokenPattern: options.tokenPatternExplicit ? options.tokenPattern : DEFAULT_AUTO_TOKEN_PATTERN,
|
|
361
|
+
});
|
|
362
|
+
const values = normalizeScale(inference.scale || [], baseFontSize);
|
|
363
|
+
const files = (inference.files || []).map((file) => relative(cwd, path.resolve(process.cwd(), file)));
|
|
364
|
+
const fileList = files.length > 0 ? `: ${files.join(', ')}` : '';
|
|
365
|
+
const labels = {
|
|
366
|
+
fallback: `scale "auto" on ${ruleName} found no spacing tokens, so it falls back to preset "${inference.preset}"`,
|
|
367
|
+
rhythmguardrc: `scale "auto" on ${ruleName}, from the .rhythmguardrc.json token sources${fileList}`,
|
|
368
|
+
scaleSources: `scale "auto" on ${ruleName}, from scaleSources${fileList}`,
|
|
369
|
+
stylesheet: `scale "auto" on ${ruleName}, from the spacing tokens in ${sample ? sample.relative : 'the linted stylesheet'} (each stylesheet's own tokens)`,
|
|
370
|
+
tailwind: `scale "auto" on ${ruleName}, from the Tailwind config${fileList}`,
|
|
371
|
+
'token-package': `scale "auto" on ${ruleName}, from an installed token package${fileList}`,
|
|
372
|
+
};
|
|
373
|
+
let label = labels[inference.source] || `scale "auto" on ${ruleName} (${inference.source})`;
|
|
374
|
+
if (inference.source === 'fallback' && inference.rejected) {
|
|
375
|
+
label = `scale "auto" on ${ruleName}: the spacing tokens found do not form a spacing scale (${inference.rejected.reasons.join(', ')}), so it falls back to preset "${inference.preset}"`;
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
scale: {
|
|
379
|
+
files,
|
|
380
|
+
isDefault: inference.source === 'fallback' && sameValues(values, [...DEFAULT_SCALE]),
|
|
381
|
+
label,
|
|
382
|
+
source: inference.source,
|
|
383
|
+
values,
|
|
384
|
+
warnings: Array.from(new Set(inference.warnings || [])),
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** The sample stylesheet's PostCSS root, parsed as the lint would parse it. */
|
|
390
|
+
function parseSample(cwd, file, customSyntax) {
|
|
391
|
+
let source;
|
|
392
|
+
try {
|
|
393
|
+
source = fs.readFileSync(file, 'utf8');
|
|
172
394
|
} catch {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
395
|
+
// Unreadable: no sample to lint; the caller reports that.
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
let parser = postcss.parse;
|
|
399
|
+
const syntaxName = customSyntax || (file.endsWith('.scss') ? 'postcss-scss' : null);
|
|
400
|
+
if (syntaxName) {
|
|
401
|
+
try {
|
|
402
|
+
// The project's own syntax package, as Stylelint loads it.
|
|
403
|
+
const syntax = require(require.resolve(syntaxName, { paths: [cwd, __dirname] }));
|
|
404
|
+
parser = (syntax && (syntax.parse || (syntax.default && syntax.default.parse))) || parser;
|
|
405
|
+
} catch {
|
|
406
|
+
// Not installed: the custom syntax check reports it.
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
return { relative: relative(cwd, file), root: parser(source, { from: file }) };
|
|
411
|
+
} catch {
|
|
412
|
+
// Not parseable: no sample to lint; the caller reports that.
|
|
177
413
|
return null;
|
|
178
414
|
}
|
|
179
415
|
}
|
|
180
416
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
417
|
+
async function checkStylelint(ctx) {
|
|
418
|
+
const { cwd, fail, pass, project, skip, warn } = ctx;
|
|
419
|
+
const stylelintPkg = findPackage('stylelint', cwd);
|
|
420
|
+
const pluginPkg = findPackage(PLUGIN_NAME, cwd);
|
|
421
|
+
const isPluginItself = (readPackageJson(cwd) || {}).name === PLUGIN_NAME;
|
|
422
|
+
const installLine = stylelintInstallLine({ scss: project.scssFiles.length > 0 });
|
|
423
|
+
|
|
424
|
+
if (!stylelintPkg) {
|
|
425
|
+
fail('stylelint-installed', 'stylelint not installed', `Run: ${installLine}`);
|
|
426
|
+
} else {
|
|
427
|
+
pass('stylelint-installed', `stylelint installed (v${stylelintPkg.manifest.version})`);
|
|
186
428
|
}
|
|
187
429
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
430
|
+
if (stylelintPkg) {
|
|
431
|
+
// The installed plugin's own range, which npm checks; this package's when it is not installed.
|
|
432
|
+
const peerRange = ((pluginPkg && pluginPkg.manifest.peerDependencies) || ownPackage.peerDependencies).stylelint;
|
|
433
|
+
const inRange = satisfiesRange(stylelintPkg.manifest.version, peerRange);
|
|
434
|
+
if (inRange) {
|
|
435
|
+
pass('stylelint-peer-range', `stylelint ${stylelintPkg.manifest.version} is in the plugin's peer range (${peerRange})`);
|
|
436
|
+
} else {
|
|
437
|
+
fail(
|
|
438
|
+
'stylelint-peer-range',
|
|
439
|
+
`stylelint ${stylelintPkg.manifest.version} is outside the plugin's peer range (${peerRange})`,
|
|
440
|
+
`Run: ${installLine}`,
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
191
444
|
|
|
192
|
-
if (
|
|
193
|
-
|
|
445
|
+
if (isPluginItself) {
|
|
446
|
+
pass('plugin-installed', `${PLUGIN_NAME} is this project`);
|
|
447
|
+
} else if (!pluginPkg) {
|
|
448
|
+
fail('plugin-installed', `${PLUGIN_NAME} not installed`, `Run: ${installLine}`);
|
|
449
|
+
} else {
|
|
450
|
+
pass('plugin-installed', `${PLUGIN_NAME} installed (v${pluginPkg.manifest.version})`);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (!stylelintPkg) {
|
|
454
|
+
skip('stylelint-config', 'Stylelint config not loaded (Stylelint is not installed)');
|
|
194
455
|
return;
|
|
195
456
|
}
|
|
196
457
|
|
|
458
|
+
let stylelint;
|
|
197
459
|
try {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
460
|
+
stylelint = await importPackage(stylelintPkg);
|
|
461
|
+
} catch (error) {
|
|
462
|
+
fail('stylelint-config', `stylelint could not be loaded: ${firstLine(error)}`, `Run: ${installLine}`);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
if (!stylelint || typeof stylelint.resolveConfig !== 'function' || typeof stylelint.lint !== 'function') {
|
|
466
|
+
fail('stylelint-config', `stylelint ${stylelintPkg.manifest.version} has no resolveConfig API`, `Run: ${installLine}`);
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const located = locateConfig(cwd, STYLELINT_CONFIG_FILES, 'stylelint');
|
|
471
|
+
const sampleFile = project.cssFiles[0] || path.join(cwd, 'index.css');
|
|
472
|
+
let config;
|
|
473
|
+
try {
|
|
474
|
+
config = await stylelint.resolveConfig(sampleFile, { cwd });
|
|
475
|
+
} catch (error) {
|
|
476
|
+
fail('stylelint-config', `Stylelint config does not load: ${firstLine(error)}`, fixForConfigError(firstLine(error), project));
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
if (!config) {
|
|
480
|
+
fail('stylelint-config', 'no Stylelint config found', 'Run: npx rhythmguard init');
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
pass('stylelint-config', `Stylelint config loads${located ? ` (${located.label})` : ''}, with every extends and plugin resolved`);
|
|
484
|
+
|
|
485
|
+
const rhythmguardRules = Object.keys(config.rules || {}).filter((name) => name.startsWith('rhythmguard/') && enabledRuleOptions(config.rules, name));
|
|
486
|
+
if (rhythmguardRules.length === 0) {
|
|
201
487
|
fail(
|
|
202
|
-
|
|
203
|
-
'
|
|
488
|
+
'stylelint-rules',
|
|
489
|
+
'Stylelint config enables no rhythmguard rule',
|
|
490
|
+
'Add: "extends": ["stylelint-plugin-rhythmguard/configs/recommended"]',
|
|
204
491
|
);
|
|
492
|
+
} else {
|
|
493
|
+
pass('stylelint-rules', `Stylelint config enables ${rhythmguardRules.join(', ')}`);
|
|
205
494
|
}
|
|
206
|
-
}
|
|
207
495
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
496
|
+
checkCustomSyntax(ctx, config);
|
|
497
|
+
await checkScssSyntax(ctx, stylelint);
|
|
498
|
+
checkRuleOptions(ctx, config, rhythmguardRules);
|
|
499
|
+
|
|
500
|
+
const sample = project.cssFiles.length > 0 ? parseSample(cwd, project.cssFiles[0], config.customSyntax) : null;
|
|
501
|
+
const scaleRule = SCALE_RULES.find((name) => enabledRuleOptions(config.rules, name));
|
|
502
|
+
if (scaleRule) {
|
|
503
|
+
const resolved = resolveRuleScale({ cwd, ruleName: scaleRule, sample, secondary: enabledRuleOptions(config.rules, scaleRule) });
|
|
504
|
+
if (resolved.error) {
|
|
505
|
+
fail('scale', resolved.error, `Fix the "preset" option of ${scaleRule}`);
|
|
506
|
+
} else {
|
|
507
|
+
ctx.scale = resolved.scale;
|
|
508
|
+
const message = `spacing scale ${formatScale(resolved.scale.values)}, from ${resolved.scale.label}`;
|
|
509
|
+
if (resolved.scale.warnings.length > 0) {
|
|
510
|
+
warn('scale', `${message}. ${resolved.scale.warnings.join(' ')}`, 'Fix the token sources named above');
|
|
511
|
+
} else {
|
|
512
|
+
pass('scale', message);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
} else {
|
|
516
|
+
skip('scale', 'spacing scale not resolved (no use-scale or no-offscale-transform rule enabled)');
|
|
213
517
|
}
|
|
214
518
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
);
|
|
519
|
+
await lintOneStylesheet(ctx, stylelint);
|
|
520
|
+
}
|
|
218
521
|
|
|
219
|
-
|
|
220
|
-
|
|
522
|
+
function checkCustomSyntax({ cwd, fail, pass }, config) {
|
|
523
|
+
if (typeof config.customSyntax !== 'string' || config.customSyntax.length === 0) {
|
|
221
524
|
return;
|
|
222
525
|
}
|
|
526
|
+
const name = config.customSyntax;
|
|
527
|
+
try {
|
|
528
|
+
require.resolve(name, { paths: [cwd] });
|
|
529
|
+
pass('custom-syntax', `custom syntax installed (${name})`);
|
|
530
|
+
} catch {
|
|
531
|
+
// Not resolvable from the project: reported just below.
|
|
532
|
+
if (path.isAbsolute(name) && fs.existsSync(name)) {
|
|
533
|
+
pass('custom-syntax', `custom syntax found (${name})`);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
fail('custom-syntax', `custom syntax package not installed: ${name}`, `Run: npm install --save-dev ${name}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
223
539
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
540
|
+
/** SCSS files linted as plain CSS fail to parse: warn when a .scss file has no customSyntax. */
|
|
541
|
+
async function checkScssSyntax({ cwd, project, warn }, stylelint) {
|
|
542
|
+
const scss = project.scssFiles[0];
|
|
543
|
+
if (!scss) {
|
|
544
|
+
return;
|
|
545
|
+
}
|
|
546
|
+
let config;
|
|
547
|
+
try {
|
|
548
|
+
config = await stylelint.resolveConfig(scss, { cwd });
|
|
549
|
+
} catch {
|
|
550
|
+
// A config Stylelint cannot resolve is reported by the config check.
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
if (config && !config.customSyntax) {
|
|
554
|
+
warn(
|
|
555
|
+
'scss-syntax',
|
|
556
|
+
`${project.scssFiles.length} .scss file${project.scssFiles.length === 1 ? '' : 's'} and no customSyntax for ${relative(cwd, scss)}; Stylelint parses SCSS as CSS`,
|
|
557
|
+
'Add "customSyntax": "postcss-scss" to the Stylelint config and run: npm install --save-dev postcss-scss',
|
|
231
558
|
);
|
|
232
559
|
}
|
|
233
560
|
}
|
|
234
561
|
|
|
235
|
-
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
562
|
+
/** Options that name a regex or a file, read from the resolved config rather than its text. */
|
|
563
|
+
function checkRuleOptions({ cwd, fail, pass }, config, ruleNames) {
|
|
564
|
+
for (const ruleName of ruleNames) {
|
|
565
|
+
const secondary = enabledRuleOptions(config.rules, ruleName);
|
|
566
|
+
if (typeof secondary.tokenPattern === 'string') {
|
|
567
|
+
try {
|
|
568
|
+
new RegExp(secondary.tokenPattern);
|
|
569
|
+
pass('token-pattern', `token pattern valid on ${ruleName} (${secondary.tokenPattern})`);
|
|
570
|
+
} catch {
|
|
571
|
+
// Reported by fail() below.
|
|
572
|
+
fail('token-pattern', `token pattern invalid on ${ruleName}: ${secondary.tokenPattern}`, 'Fix the regex in the tokenPattern option');
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (typeof secondary.tailwindConfigPath === 'string' && secondary.tailwindConfigPath.length > 0) {
|
|
576
|
+
if (fs.existsSync(path.resolve(cwd, secondary.tailwindConfigPath))) {
|
|
577
|
+
pass('tailwind-config', `tailwind config found for ${ruleName} (${secondary.tailwindConfigPath})`);
|
|
578
|
+
} else {
|
|
579
|
+
fail(
|
|
580
|
+
'tailwind-config',
|
|
581
|
+
`tailwind config not found at ${secondary.tailwindConfigPath} (${ruleName})`,
|
|
582
|
+
'Update tailwindConfigPath or remove tokenMapFromTailwindSpacing',
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const entry of Array.isArray(secondary.scaleSources) ? secondary.scaleSources : []) {
|
|
587
|
+
const { error, source } = normalizeTokenSourceEntry(entry, cwd);
|
|
588
|
+
if (error) {
|
|
589
|
+
fail('scale-sources', `scaleSources entry invalid on ${ruleName}: ${error}`, 'Use paths or { "path": "...", "format": "..." } entries');
|
|
590
|
+
} else if (!fs.existsSync(path.resolve(source.baseDir, source.path))) {
|
|
591
|
+
fail('scale-sources', `scaleSources file not found on ${ruleName}: ${source.path}`, 'Update scaleSources or create the token file');
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
async function lintOneStylesheet({ cwd, fail, pass, project, skip, warn }, stylelint) {
|
|
598
|
+
if (project.cssFiles.length === 0) {
|
|
599
|
+
skip('stylelint-lint', 'Stylelint lint skipped (no stylesheet found)');
|
|
239
600
|
return;
|
|
240
601
|
}
|
|
602
|
+
for (const file of project.cssFiles.slice(0, MAX_LINT_ATTEMPTS)) {
|
|
603
|
+
let result;
|
|
604
|
+
try {
|
|
605
|
+
// allowEmptyInput: an ignored file is a result to move past, not an error.
|
|
606
|
+
result = (await stylelint.lint({ allowEmptyInput: true, cwd, files: [file] })).results[0];
|
|
607
|
+
} catch (error) {
|
|
608
|
+
fail('stylelint-lint', `Stylelint could not lint ${relative(cwd, file)}: ${firstLine(error)}`, fixForConfigError(firstLine(error), project));
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (!result || result.ignored) {
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
const name = relative(cwd, file);
|
|
615
|
+
const invalid = (result.invalidOptionWarnings || []).map((warning) => warning.text);
|
|
616
|
+
if (invalid.length > 0) {
|
|
617
|
+
fail('stylelint-lint', `Stylelint rejects rule options: ${invalid.join(' ')}`, 'Fix the options it names; the rules do not run until then');
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
const syntaxError = (result.warnings || []).find((warning) => warning.rule === 'CssSyntaxError');
|
|
621
|
+
if (syntaxError) {
|
|
622
|
+
warn(
|
|
623
|
+
'stylelint-lint',
|
|
624
|
+
`Stylelint could not parse ${name}: ${syntaxError.text}`,
|
|
625
|
+
file.endsWith('.scss') ? 'Add "customSyntax": "postcss-scss" to the Stylelint config' : 'Fix the syntax error, or set the customSyntax this file needs',
|
|
626
|
+
);
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const count = (result.warnings || []).filter((warning) => String(warning.rule || '').startsWith('rhythmguard/')).length;
|
|
630
|
+
pass('stylelint-lint', `Stylelint linted ${name} with this config (${count} rhythmguard finding${count === 1 ? '' : 's'})`);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
skip('stylelint-lint', 'Stylelint lint skipped (every stylesheet tried is ignored by the config)');
|
|
634
|
+
}
|
|
241
635
|
|
|
242
|
-
|
|
243
|
-
/customSyntax['":\s]+['"]([^'"]+)['"]/,
|
|
244
|
-
);
|
|
636
|
+
// --- ESLint -------------------------------------------------------------------
|
|
245
637
|
|
|
246
|
-
|
|
247
|
-
|
|
638
|
+
/** The options of `tailwind-class-use-scale` in a computed config, under whatever prefix the plugin is registered. */
|
|
639
|
+
function eslintRuleEntry(config) {
|
|
640
|
+
for (const [name, value] of Object.entries((config && config.rules) || {})) {
|
|
641
|
+
if (name !== ESLINT_RULE && !name.endsWith(`/${ESLINT_RULE}`)) {
|
|
642
|
+
continue;
|
|
643
|
+
}
|
|
644
|
+
const severity = Array.isArray(value) ? value[0] : value;
|
|
645
|
+
if (severity === 0 || severity === 'off') {
|
|
646
|
+
continue;
|
|
647
|
+
}
|
|
648
|
+
const option = Array.isArray(value) && value[1] && typeof value[1] === 'object' ? value[1] : {};
|
|
649
|
+
return { name, option };
|
|
650
|
+
}
|
|
651
|
+
return null;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
async function checkEslint(ctx, { required }) {
|
|
655
|
+
const { cwd, fail, pass, project, skip, warn } = ctx;
|
|
656
|
+
const located = locateConfig(cwd, ESLINT_CONFIG_FILES, 'eslintConfig');
|
|
657
|
+
const referencesRhythmguard = Boolean(located && located.content.includes('rhythmguard'));
|
|
658
|
+
// Outside an ESLint-only setup, a broken ESLint config is ours to report only when it uses Rhythmguard.
|
|
659
|
+
const ours = required || referencesRhythmguard;
|
|
660
|
+
const eslintPkg = findPackage('eslint', cwd);
|
|
661
|
+
const installLine = eslintInstallLine({ typescript: project.typescript });
|
|
662
|
+
|
|
663
|
+
if (!eslintPkg) {
|
|
664
|
+
if (required) {
|
|
665
|
+
fail('eslint-installed', 'eslint not installed', `Run: ${installLine}`);
|
|
666
|
+
} else {
|
|
667
|
+
skip('eslint-installed', `ESLint checks skipped (${located ? 'ESLint is not installed' : 'no ESLint config'})`);
|
|
668
|
+
}
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (!located && !required) {
|
|
672
|
+
skip('eslint-installed', 'ESLint checks skipped (no ESLint config)');
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
pass('eslint-installed', `eslint installed (v${eslintPkg.manifest.version})`);
|
|
676
|
+
if (required && !findPackage(ESLINT_PLUGIN_NAME, cwd) && !referencesRhythmguard) {
|
|
677
|
+
fail('eslint-plugin', `${ESLINT_PLUGIN_NAME} not installed`, `Run: ${installLine}`);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
let linter;
|
|
681
|
+
try {
|
|
682
|
+
const mod = await importPackage(eslintPkg);
|
|
683
|
+
const ESLint = typeof mod.loadESLint === 'function' ? await mod.loadESLint({ cwd }) : mod.ESLint;
|
|
684
|
+
linter = new ESLint({ cwd });
|
|
685
|
+
} catch (error) {
|
|
686
|
+
(ours ? fail : warn)('eslint-config', `ESLint could not be loaded: ${firstLine(error)}`, `Run: ${installLine}`);
|
|
248
687
|
return;
|
|
249
688
|
}
|
|
250
689
|
|
|
251
|
-
const
|
|
690
|
+
const candidates = [
|
|
691
|
+
...project.classFiles.slice(0, 1),
|
|
692
|
+
path.join(cwd, 'src', 'index.tsx'),
|
|
693
|
+
path.join(cwd, 'src', 'index.jsx'),
|
|
694
|
+
path.join(cwd, 'src', 'index.js'),
|
|
695
|
+
];
|
|
696
|
+
let found = null;
|
|
697
|
+
let anyConfig = false;
|
|
252
698
|
try {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
699
|
+
for (const file of candidates) {
|
|
700
|
+
const config = await linter.calculateConfigForFile(file);
|
|
701
|
+
anyConfig = anyConfig || Boolean(config);
|
|
702
|
+
const entry = eslintRuleEntry(config);
|
|
703
|
+
if (entry) {
|
|
704
|
+
found = { ...entry, file };
|
|
705
|
+
break;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
} catch (error) {
|
|
709
|
+
const message = firstLine(error);
|
|
710
|
+
const missingPlugin = message.includes(ESLINT_PLUGIN_NAME) || message.includes(PLUGIN_NAME);
|
|
711
|
+
(ours ? fail : warn)(
|
|
712
|
+
'eslint-config',
|
|
713
|
+
`ESLint config does not load${located ? ` (${located.label})` : ''}: ${message}`,
|
|
714
|
+
missingPlugin ? `Run: ${installLine}` : 'Fix the ESLint config; npx eslint stops on the same error',
|
|
259
715
|
);
|
|
716
|
+
return;
|
|
260
717
|
}
|
|
718
|
+
|
|
719
|
+
if (!found) {
|
|
720
|
+
const message = anyConfig
|
|
721
|
+
? `ESLint config${located ? ` (${located.label})` : ''} does not enable ${ESLINT_RULE}`
|
|
722
|
+
: 'no ESLint config applies to class-string files';
|
|
723
|
+
if (required) {
|
|
724
|
+
fail('eslint-rule', message, 'Add rhythmguard.configs.recommended from eslint-plugin-rhythmguard to the ESLint config');
|
|
725
|
+
} else {
|
|
726
|
+
skip('eslint-rule', `${message}; class strings are not checked by ESLint`);
|
|
727
|
+
}
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
pass('eslint-config', `ESLint config loads${located ? ` (${located.label})` : ''} and enables ${found.name} for ${relative(cwd, found.file)}`);
|
|
731
|
+
|
|
732
|
+
await lintOneClassFile(ctx, linter, found.name);
|
|
733
|
+
compareEslintScale(ctx, found);
|
|
261
734
|
}
|
|
262
735
|
|
|
263
|
-
|
|
736
|
+
/** Run ESLint on one class-string file, as `npx eslint .` would, to prove the rule runs. */
|
|
737
|
+
async function lintOneClassFile({ cwd, fail, pass, project, skip, warn }, linter, ruleName) {
|
|
738
|
+
for (const file of project.classFiles.slice(0, MAX_LINT_ATTEMPTS)) {
|
|
739
|
+
try {
|
|
740
|
+
if (await linter.isPathIgnored(file) || !eslintRuleEntry(await linter.calculateConfigForFile(file))) {
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
const [result] = await linter.lintFiles([file]);
|
|
744
|
+
const name = relative(cwd, file);
|
|
745
|
+
const fatal = ((result && result.messages) || []).find((message) => message.fatal);
|
|
746
|
+
if (fatal) {
|
|
747
|
+
warn('eslint-lint', `ESLint could not parse ${name}: ${fatal.message}`, 'Configure a parser for this file type in the ESLint config');
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const count = ((result && result.messages) || []).filter((message) => message.ruleId === ruleName).length;
|
|
751
|
+
pass('eslint-lint', `ESLint linted ${name} with this config (${count} ${ESLINT_RULE} finding${count === 1 ? '' : 's'})`);
|
|
752
|
+
return;
|
|
753
|
+
} catch (error) {
|
|
754
|
+
fail('eslint-lint', `ESLint could not lint ${relative(cwd, file)}: ${firstLine(error)}`, 'Fix the ESLint config; npx eslint stops on the same error');
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
skip('eslint-lint', `ESLint lint skipped (no class-string file found that ${ESLINT_RULE} applies to)`);
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* The scale the ESLint rule checks, compared with the stylesheet scale. With
|
|
763
|
+
* no `scale`, the rule checks Tailwind's default ladder, which is what the
|
|
764
|
+
* audit pairs with the default stylesheet scale; anything else should match.
|
|
765
|
+
*/
|
|
766
|
+
function compareEslintScale(ctx, found) {
|
|
767
|
+
const { pass, warn } = ctx;
|
|
768
|
+
const analyzer = createTailwindClassAnalyzer(found.option);
|
|
769
|
+
const values = [...analyzer.scalePx];
|
|
770
|
+
const defaultLadder = !Array.isArray(found.option.scale);
|
|
771
|
+
const label = defaultLadder ? TAILWIND_SCALE_NAME : `the "scale" option of ${found.name}`;
|
|
772
|
+
ctx.eslintScale = { files: [], isDefault: defaultLadder, label, source: defaultLadder ? 'tailwind-default' : 'explicit', values, warnings: [] };
|
|
773
|
+
const message = `class-string scale ${defaultLadder && values.length > 12 ? `${values.slice(0, 12).join(', ')}, ... ${values[values.length - 1]}px` : formatScale(values)}, from ${label}`;
|
|
774
|
+
|
|
775
|
+
const stylesheet = ctx.scale;
|
|
776
|
+
if (!stylesheet) {
|
|
777
|
+
pass('eslint-scale', message);
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
if (sameValues(values, stylesheet.values)) {
|
|
781
|
+
pass('eslint-scale', `${message}, the same scale Stylelint checks`);
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (defaultLadder && (stylesheet.isDefault || stylesheet.source === 'fallback')) {
|
|
785
|
+
pass('eslint-scale', `${message}; the stylesheet scale is the default, so class strings use Tailwind's ladder, as the audit does`);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
const onlyStylelint = stylesheet.values.filter((value) => !values.includes(value));
|
|
789
|
+
const onlyEslint = values.filter((value) => !stylesheet.values.includes(value));
|
|
790
|
+
const differences = [
|
|
791
|
+
onlyStylelint.length > 0 ? `only in Stylelint: ${listDifference(onlyStylelint)}` : '',
|
|
792
|
+
onlyEslint.length > 0 ? `only in ESLint: ${listDifference(onlyEslint)}` : '',
|
|
793
|
+
].filter(Boolean).join('; ');
|
|
794
|
+
warn(
|
|
795
|
+
'eslint-scale',
|
|
796
|
+
`${message}, which is not the stylesheet scale (${differences})`,
|
|
797
|
+
`Set "scale": [${stylesheet.values.join(', ')}] on ${found.name} so class strings and stylesheets are checked against one scale`,
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// --- .rhythmguardrc.json --------------------------------------------------------
|
|
802
|
+
|
|
803
|
+
function checkRhythmguardConfig(ctx) {
|
|
804
|
+
const { cwd, fail, pass, skip } = ctx;
|
|
264
805
|
const configPath = path.join(cwd, '.rhythmguardrc.json');
|
|
265
806
|
if (!fs.existsSync(configPath)) {
|
|
266
|
-
skip('rhythmguard config check skipped (.rhythmguardrc.json not found)');
|
|
807
|
+
skip('rc', 'rhythmguard config check skipped (.rhythmguardrc.json not found)');
|
|
267
808
|
return;
|
|
268
809
|
}
|
|
269
810
|
|
|
@@ -271,7 +812,8 @@ function checkRhythmguardConfig() {
|
|
|
271
812
|
try {
|
|
272
813
|
parsed = readJsonFile(configPath);
|
|
273
814
|
} catch {
|
|
274
|
-
|
|
815
|
+
// Reported by fail() below.
|
|
816
|
+
fail('rc', '.rhythmguardrc.json is not valid JSON', 'Fix or remove .rhythmguardrc.json');
|
|
275
817
|
return;
|
|
276
818
|
}
|
|
277
819
|
|
|
@@ -282,57 +824,69 @@ function checkRhythmguardConfig() {
|
|
|
282
824
|
let configResult = null;
|
|
283
825
|
let options = null;
|
|
284
826
|
try {
|
|
285
|
-
configResult = loadAuditConfig({});
|
|
827
|
+
configResult = loadAuditConfig({ configPath });
|
|
286
828
|
const applied = applyAuditConfig(
|
|
287
829
|
{ ...createDefaultAuditOptions(), cliOptions: new Set() },
|
|
288
830
|
{ ...configResult, decisions: undefined },
|
|
289
831
|
);
|
|
290
|
-
resolveIgnorePatterns(applied);
|
|
832
|
+
resolveIgnorePatterns({ ...applied, ignorePath: ignorePathFor(cwd, applied.ignorePath) });
|
|
291
833
|
options = applied;
|
|
292
|
-
pass('.rhythmguardrc.json audit config valid');
|
|
834
|
+
pass('rc-audit', '.rhythmguardrc.json audit config valid');
|
|
293
835
|
} catch (err) {
|
|
294
836
|
fail(
|
|
837
|
+
'rc-audit',
|
|
295
838
|
`.rhythmguardrc.json audit config invalid: ${err.message}`,
|
|
296
839
|
'Fix the "audit" section; rhythmguard audit stops on the same error',
|
|
297
840
|
);
|
|
298
841
|
}
|
|
299
842
|
|
|
300
|
-
checkRhythmguardDecisions(parsed, options ? options.baseFontSize : undefined);
|
|
843
|
+
checkRhythmguardDecisions(ctx, parsed, options ? options.baseFontSize : undefined);
|
|
301
844
|
|
|
302
845
|
const audit = configResult ? configResult.audit : {};
|
|
303
846
|
if (options) {
|
|
304
|
-
checkRhythmguardTokenSources(audit, path.dirname(configPath));
|
|
305
|
-
checkRhythmguardMotionConfig(audit);
|
|
847
|
+
checkRhythmguardTokenSources(ctx, audit, path.dirname(configPath));
|
|
848
|
+
checkRhythmguardMotionConfig(ctx, audit);
|
|
306
849
|
}
|
|
307
|
-
checkRhythmguardBaseline(audit);
|
|
850
|
+
checkRhythmguardBaseline(ctx, audit);
|
|
308
851
|
}
|
|
309
852
|
|
|
310
|
-
|
|
853
|
+
// The ignore file the audit would read from cwd; the default one only when present.
|
|
854
|
+
function ignorePathFor(cwd, ignorePath) {
|
|
855
|
+
if (!ignorePath) {
|
|
856
|
+
return ignorePath;
|
|
857
|
+
}
|
|
858
|
+
const resolved = path.resolve(cwd, ignorePath);
|
|
859
|
+
return ignorePath === DEFAULT_IGNORE_PATH && !fs.existsSync(resolved) ? null : resolved;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function checkRhythmguardDecisions({ fail, pass, skip }, config, baseFontSize) {
|
|
311
863
|
const raw = config && typeof config === 'object' && !Array.isArray(config) ? config.decisions : undefined;
|
|
312
864
|
if (raw === undefined || raw === null) {
|
|
313
|
-
skip('decisions check skipped (not configured)');
|
|
865
|
+
skip('rc-decisions', 'decisions check skipped (not configured)');
|
|
314
866
|
return;
|
|
315
867
|
}
|
|
316
868
|
|
|
317
869
|
try {
|
|
318
870
|
const decisions = normalizeDecisions(raw, baseFontSize === undefined ? {} : { baseFontSize });
|
|
319
|
-
pass(`.rhythmguardrc.json decisions valid (${decisions.length} decision${decisions.length === 1 ? '' : 's'})`);
|
|
871
|
+
pass('rc-decisions', `.rhythmguardrc.json decisions valid (${decisions.length} decision${decisions.length === 1 ? '' : 's'})`);
|
|
320
872
|
} catch (err) {
|
|
321
873
|
fail(
|
|
874
|
+
'rc-decisions',
|
|
322
875
|
`.rhythmguardrc.json decisions invalid: ${err.message}`,
|
|
323
876
|
'Fix the "decisions" section; rhythmguard audit and the Stylelint rules read the same entries',
|
|
324
877
|
);
|
|
325
878
|
}
|
|
326
879
|
}
|
|
327
880
|
|
|
328
|
-
function checkRhythmguardTokenSources(audit, baseDir) {
|
|
881
|
+
function checkRhythmguardTokenSources(ctx, audit, baseDir) {
|
|
882
|
+
const { fail, pass, skip } = ctx;
|
|
329
883
|
if (audit.tokenSources === undefined) {
|
|
330
|
-
skip('token source config check skipped (not configured)');
|
|
884
|
+
skip('rc-token-sources', 'token source config check skipped (not configured)');
|
|
331
885
|
return;
|
|
332
886
|
}
|
|
333
887
|
|
|
334
888
|
if (!Array.isArray(audit.tokenSources)) {
|
|
335
|
-
fail('audit.tokenSources must be an array', 'Use strings or { "path": "...", "format": "..." } entries');
|
|
889
|
+
fail('rc-token-sources', 'audit.tokenSources must be an array', 'Use strings or { "path": "...", "format": "..." } entries');
|
|
336
890
|
return;
|
|
337
891
|
}
|
|
338
892
|
|
|
@@ -341,21 +895,22 @@ function checkRhythmguardTokenSources(audit, baseDir) {
|
|
|
341
895
|
const format = typeof source === 'string' ? 'auto' : source && source.format;
|
|
342
896
|
|
|
343
897
|
if (typeof sourcePath !== 'string' || sourcePath.trim().length === 0) {
|
|
344
|
-
fail('token source entry missing path', 'Use strings or objects with a non-empty path');
|
|
898
|
+
fail('rc-token-sources', 'token source entry missing path', 'Use strings or objects with a non-empty path');
|
|
345
899
|
continue;
|
|
346
900
|
}
|
|
347
901
|
|
|
348
902
|
try {
|
|
349
903
|
normalizeTokenSourceFormat(format || 'auto');
|
|
350
904
|
} catch {
|
|
351
|
-
|
|
905
|
+
// Reported by fail() below.
|
|
906
|
+
fail('rc-token-sources', `token source format invalid for ${sourcePath}`, 'Use auto, css, flat-json, style-dictionary, or dtcg');
|
|
352
907
|
}
|
|
353
908
|
|
|
354
909
|
if (fs.existsSync(path.resolve(baseDir, sourcePath))) {
|
|
355
|
-
pass(`token source found (${sourcePath})`);
|
|
356
|
-
warnAboutTokenSource(source, baseDir);
|
|
910
|
+
pass('rc-token-sources', `token source found (${sourcePath})`);
|
|
911
|
+
warnAboutTokenSource(ctx, source, baseDir);
|
|
357
912
|
} else {
|
|
358
|
-
fail(`token source not found (${sourcePath})`, 'Update audit.tokenSources or create the token file');
|
|
913
|
+
fail('rc-token-sources', `token source not found (${sourcePath})`, 'Update audit.tokenSources or create the token file');
|
|
359
914
|
}
|
|
360
915
|
}
|
|
361
916
|
}
|
|
@@ -365,111 +920,172 @@ function checkRhythmguardTokenSources(audit, baseDir) {
|
|
|
365
920
|
* pass on what the audit would warn about: a file that does not parse, skipped
|
|
366
921
|
* tokens, or no spacing tokens at all (issue #163). Warnings, not failures.
|
|
367
922
|
*/
|
|
368
|
-
function warnAboutTokenSource(entry, baseDir) {
|
|
923
|
+
function warnAboutTokenSource({ warn }, entry, baseDir) {
|
|
369
924
|
const { source } = normalizeTokenSourceEntry(entry, baseDir);
|
|
370
925
|
if (!source) {
|
|
371
926
|
return;
|
|
372
927
|
}
|
|
373
928
|
const parsed = parseTokenSources({ sources: [source], tokenKind: 'spacing', warnWhenEmpty: true });
|
|
374
929
|
for (const message of parsed.warnings) {
|
|
375
|
-
warn(message);
|
|
930
|
+
warn('rc-token-sources', message);
|
|
376
931
|
}
|
|
377
932
|
}
|
|
378
933
|
|
|
379
|
-
function checkRhythmguardMotionConfig(audit) {
|
|
934
|
+
function checkRhythmguardMotionConfig({ fail, pass, skip }, audit) {
|
|
380
935
|
if (audit.includeMotion === undefined) {
|
|
381
|
-
skip('motion audit check skipped (not configured)');
|
|
936
|
+
skip('rc-motion', 'motion audit check skipped (not configured)');
|
|
382
937
|
return;
|
|
383
938
|
}
|
|
384
939
|
|
|
385
940
|
if (typeof audit.includeMotion === 'boolean') {
|
|
386
|
-
pass(`motion audit config valid (${audit.includeMotion})`);
|
|
941
|
+
pass('rc-motion', `motion audit config valid (${audit.includeMotion})`);
|
|
387
942
|
} else {
|
|
388
|
-
fail('audit.includeMotion must be a boolean', 'Use true or false');
|
|
943
|
+
fail('rc-motion', 'audit.includeMotion must be a boolean', 'Use true or false');
|
|
389
944
|
}
|
|
390
945
|
}
|
|
391
946
|
|
|
392
|
-
function checkRhythmguardBaseline(audit) {
|
|
947
|
+
function checkRhythmguardBaseline({ cwd, fail, pass, skip }, audit) {
|
|
393
948
|
const baselinePath = path.resolve(cwd, audit.baseline || '.rhythmguard-baseline.json');
|
|
394
949
|
if (!fs.existsSync(baselinePath)) {
|
|
395
|
-
skip('baseline freshness check skipped (baseline not found)');
|
|
950
|
+
skip('rc-baseline', 'baseline freshness check skipped (baseline not found)');
|
|
396
951
|
return;
|
|
397
952
|
}
|
|
398
953
|
|
|
399
954
|
try {
|
|
400
955
|
const baseline = readJsonFile(baselinePath);
|
|
401
956
|
if (!Array.isArray(baseline.findings)) {
|
|
402
|
-
fail('baseline file does not include findings array', 'Regenerate it with rhythmguard audit --write-baseline');
|
|
957
|
+
fail('rc-baseline', 'baseline file does not include findings array', 'Regenerate it with rhythmguard audit --write-baseline');
|
|
403
958
|
return;
|
|
404
959
|
}
|
|
405
|
-
pass(`baseline file readable (${
|
|
960
|
+
pass('rc-baseline', `baseline file readable (${relative(cwd, baselinePath)})`);
|
|
406
961
|
} catch {
|
|
407
|
-
|
|
962
|
+
// Reported by fail() below.
|
|
963
|
+
fail('rc-baseline', 'baseline file is not valid JSON', 'Regenerate it with rhythmguard audit --write-baseline');
|
|
408
964
|
}
|
|
409
965
|
}
|
|
410
966
|
|
|
411
|
-
//
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
967
|
+
// --- Entry points -----------------------------------------------------------------
|
|
968
|
+
|
|
969
|
+
/**
|
|
970
|
+
* An ESLint-only setup: no Stylelint installed or configured, and Rhythmguard
|
|
971
|
+
* reached through ESLint (the ESLint package installed, or named in the
|
|
972
|
+
* ESLint config).
|
|
973
|
+
*/
|
|
974
|
+
function isEslintOnly(cwd) {
|
|
975
|
+
if (findPackage('stylelint', cwd) || locateConfig(cwd, STYLELINT_CONFIG_FILES, 'stylelint')) {
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
if (findPackage(ESLINT_PLUGIN_NAME, cwd)) {
|
|
979
|
+
return true;
|
|
415
980
|
}
|
|
416
|
-
const
|
|
417
|
-
return
|
|
981
|
+
const eslintConfig = locateConfig(cwd, ESLINT_CONFIG_FILES, 'eslintConfig');
|
|
982
|
+
return Boolean(eslintConfig && eslintConfig.content.includes('rhythmguard'));
|
|
418
983
|
}
|
|
419
984
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
985
|
+
/**
|
|
986
|
+
* Check the Rhythmguard setup in `cwd` (default: the process's working
|
|
987
|
+
* directory). Resolves to `{ ok, setup, issues, warnings, checks, scale,
|
|
988
|
+
* eslintScale }`; `ok` is false when any check failed. Loads the project's own
|
|
989
|
+
* Stylelint and ESLint, so it runs their configs as their CLIs would; it makes
|
|
990
|
+
* no network request.
|
|
991
|
+
*/
|
|
992
|
+
async function runDoctor({ cwd = process.cwd() } = {}) {
|
|
993
|
+
const root = path.resolve(cwd);
|
|
994
|
+
const { checks, fail, pass, skip, warn } = createChecks();
|
|
995
|
+
const ctx = { cwd: root, eslintScale: null, fail, pass, project: scanProject(root), scale: null, skip, warn };
|
|
996
|
+
|
|
997
|
+
const setup = isEslintOnly(root) ? 'eslint-only' : 'stylelint';
|
|
998
|
+
if (setup === 'eslint-only') {
|
|
999
|
+
await checkEslint(ctx, { required: true });
|
|
1000
|
+
skip('stylelint-skipped', 'Stylelint checks skipped (ESLint-only setup: no Stylelint installed or configured)');
|
|
424
1001
|
} else {
|
|
425
|
-
|
|
1002
|
+
await checkStylelint(ctx);
|
|
1003
|
+
await checkEslint(ctx, { required: false });
|
|
426
1004
|
}
|
|
427
|
-
|
|
428
|
-
|
|
1005
|
+
checkRhythmguardConfig(ctx);
|
|
1006
|
+
|
|
1007
|
+
const issues = checks.filter((check) => check.status === 'fail').length;
|
|
1008
|
+
const warnings = checks.filter((check) => check.status === 'warn').length;
|
|
1009
|
+
return {
|
|
1010
|
+
checks,
|
|
1011
|
+
cwd: root,
|
|
1012
|
+
eslintScale: ctx.eslintScale,
|
|
1013
|
+
issues,
|
|
1014
|
+
ok: issues === 0,
|
|
1015
|
+
scale: ctx.scale,
|
|
1016
|
+
setup,
|
|
1017
|
+
warnings,
|
|
1018
|
+
};
|
|
429
1019
|
}
|
|
430
1020
|
|
|
431
|
-
|
|
1021
|
+
const SYMBOLS = { fail: '✗', pass: '✓', skip: '-', warn: '!' };
|
|
1022
|
+
|
|
1023
|
+
function checkLines(check) {
|
|
1024
|
+
const lines = [`${SYMBOLS[check.status] || '-'} ${check.message}`];
|
|
1025
|
+
if (check.fix && (check.status === 'fail' || check.status === 'warn')) {
|
|
1026
|
+
lines.push(` → ${check.fix}`);
|
|
1027
|
+
}
|
|
1028
|
+
return lines;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
function countsLine(issues, warnings) {
|
|
1032
|
+
const warningNote = warnings > 0 ? ` (${warnings} warning${warnings === 1 ? '' : 's'})` : '';
|
|
1033
|
+
return issues === 0 ? `All checks passed${warningNote}.` : `${issues} issue${issues === 1 ? '' : 's'} found${warningNote}.`;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/** The text `rhythmguard doctor` prints for a result. */
|
|
1037
|
+
function formatDoctorReport(result) {
|
|
1038
|
+
return [
|
|
1039
|
+
'',
|
|
1040
|
+
'Rhythmguard Doctor',
|
|
1041
|
+
'',
|
|
1042
|
+
...result.checks.flatMap(checkLines),
|
|
1043
|
+
'',
|
|
1044
|
+
countsLine(result.issues, result.warnings),
|
|
1045
|
+
'',
|
|
1046
|
+
'',
|
|
1047
|
+
].join('\n');
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* A short summary for another command to print (quickstart): the count line,
|
|
1052
|
+
* then each failed check with its fix. Takes a runDoctor result or its checks.
|
|
1053
|
+
*/
|
|
1054
|
+
function doctorSummary(resultOrChecks) {
|
|
1055
|
+
const checks = Array.isArray(resultOrChecks) ? resultOrChecks : (resultOrChecks && resultOrChecks.checks) || [];
|
|
1056
|
+
const failed = checks.filter((check) => check.status === 'fail');
|
|
1057
|
+
const warnings = checks.filter((check) => check.status === 'warn').length;
|
|
1058
|
+
const scale = checks.find((check) => check.id === 'scale' && check.status !== 'fail');
|
|
1059
|
+
return [
|
|
1060
|
+
`Setup check: ${countsLine(failed.length, warnings)}`,
|
|
1061
|
+
...(scale && failed.length === 0 ? [` ${scale.message}`] : []),
|
|
1062
|
+
...failed.flatMap((check) => checkLines(check).map((line) => ` ${line}`)),
|
|
1063
|
+
].join('\n');
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
async function run(argv = process.argv.slice(3)) {
|
|
432
1067
|
// Doctor takes no arguments; --help prints the usage, anything else is refused (#286).
|
|
433
|
-
for (const arg of
|
|
1068
|
+
for (const arg of argv) {
|
|
434
1069
|
if (arg === '--help' || arg === '-h') {
|
|
435
1070
|
process.stdout.write(HELP);
|
|
436
1071
|
return;
|
|
437
1072
|
}
|
|
438
1073
|
process.stderr.write(`Unknown option: ${arg}\n\n${HELP}`);
|
|
439
|
-
process.
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
process.stdout.write('\nRhythmguard Doctor\n\n');
|
|
443
|
-
|
|
444
|
-
const eslintConfig = eslintOnlyConfig();
|
|
445
|
-
let stylelintOk = true;
|
|
446
|
-
if (eslintConfig) {
|
|
447
|
-
checkEslintSetup(eslintConfig);
|
|
448
|
-
} else {
|
|
449
|
-
stylelintOk = checkStylelint();
|
|
450
|
-
const configPath = findConfig();
|
|
451
|
-
const configContent = checkConfigReferences(configPath);
|
|
452
|
-
checkTokenPattern(configContent);
|
|
453
|
-
checkTailwindConfig(configContent);
|
|
454
|
-
checkCustomSyntax(configContent);
|
|
1074
|
+
process.exitCode = 1;
|
|
1075
|
+
return;
|
|
455
1076
|
}
|
|
456
|
-
checkRhythmguardConfig();
|
|
457
1077
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
process.
|
|
465
|
-
`${issues} issue${issues === 1 ? '' : 's'} found${warningNote}.\n\n`,
|
|
466
|
-
);
|
|
1078
|
+
try {
|
|
1079
|
+
const result = await runDoctor({ cwd: process.cwd() });
|
|
1080
|
+
process.stdout.write(formatDoctorReport(result));
|
|
1081
|
+
process.exitCode = result.ok ? 0 : 1;
|
|
1082
|
+
} catch (error) {
|
|
1083
|
+
process.stderr.write(`Doctor could not finish: ${error && error.stack ? error.stack : error}\n`);
|
|
1084
|
+
process.exitCode = 1;
|
|
467
1085
|
}
|
|
468
|
-
|
|
469
|
-
process.exit(stylelintOk && issues === 0 ? 0 : 1);
|
|
470
1086
|
}
|
|
471
1087
|
|
|
472
|
-
module.exports = { HELP, run };
|
|
1088
|
+
module.exports = { HELP, doctorSummary, formatDoctorReport, run, runDoctor, satisfiesRange };
|
|
473
1089
|
|
|
474
1090
|
if (require.main === module) {
|
|
475
1091
|
run();
|