impeccable 3.6.1 → 4.0.0

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.
@@ -1,355 +0,0 @@
1
- import path from 'node:path';
2
-
3
- import {
4
- getConfigPath,
5
- getLocalConfigPath,
6
- normalizeIgnoreValue,
7
- readDetectionConfig,
8
- readRawDetectionConfig,
9
- writeDetectionConfig,
10
- extractFindingIgnoreValue,
11
- } from '../../lib/impeccable-config.mjs';
12
-
13
- const ACTION_ALIASES = new Map([
14
- ['status', 'list'],
15
- ['ls', 'list'],
16
- ['list', 'list'],
17
- ['add-rule', 'add-rule'],
18
- ['ignore-rule', 'add-rule'],
19
- ['add-file', 'add-file'],
20
- ['ignore-file', 'add-file'],
21
- ['add-value', 'add-value'],
22
- ['ignore-value', 'add-value'],
23
- ['update-value', 'add-value'],
24
- ['remove-rule', 'remove-rule'],
25
- ['rm-rule', 'remove-rule'],
26
- ['remove-file', 'remove-file'],
27
- ['rm-file', 'remove-file'],
28
- ['remove-value', 'remove-value'],
29
- ['rm-value', 'remove-value'],
30
- ['clear', 'clear'],
31
- ]);
32
-
33
- function printUsage() {
34
- console.log(`Usage: impeccable ignores <action> [options]
35
-
36
- Manage detector ignores in .impeccable config.
37
-
38
- Actions:
39
- list Show merged, shared, and local ignores
40
- add-rule <rule> [--all-values] Ignore a rule
41
- add-file <glob> Ignore files by glob
42
- add-value <rule> <value> Ignore one rule/value pair
43
- remove-rule <rule> Remove a rule ignore
44
- remove-file <glob> Remove a file ignore
45
- remove-value <rule> <value> Remove a rule/value ignore
46
- clear Clear detector ignores in the selected scope
47
-
48
- Scope:
49
- --shared Write .impeccable/config.json (default)
50
- --local Write .impeccable/config.local.json
51
- --all For remove/clear, apply to shared and local
52
-
53
- Value options:
54
- --file <glob> Scope add-value/remove-value to a file glob
55
- --reason <text> Store or update a reason on add-value
56
-
57
- Examples:
58
- impeccable ignores add-file "src/legacy/**"
59
- impeccable ignores add-value overused-font Inter --reason "Brand font"
60
- impeccable ignores add-value design-system-color "*" --file "src/demo.css"
61
- impeccable ignores remove-value overused-font Inter`);
62
- }
63
-
64
- function parseScope(args, { allowAll = false } = {}) {
65
- const rest = [];
66
- let local = false;
67
- let shared = false;
68
- let all = false;
69
- for (const arg of args) {
70
- if (arg === '--local') local = true;
71
- else if (arg === '--shared') shared = true;
72
- else if (arg === '--all') all = true;
73
- else rest.push(arg);
74
- }
75
- if ([local, shared, all].filter(Boolean).length > 1) {
76
- throw new Error(`Pass only one scope flag: --shared${allowAll ? ', --local, or --all' : ' or --local'}`);
77
- }
78
- if (all && !allowAll) throw new Error('--all is only supported for remove and clear actions');
79
- return { local, all, rest };
80
- }
81
-
82
- // An empty glob used to be dropped by filter(Boolean), so `--file=` reported
83
- // success and wrote an entry with no files: the user asked to scope a rule to one
84
- // file and silently got the project-wide suppression instead. Refuse it.
85
- function requireGlob(raw, flag) {
86
- const glob = String(raw ?? '').trim();
87
- if (!glob) throw new Error(`${flag} requires a non-empty glob`);
88
- // A following flag is not a glob. `--file --reason "why"` consumed `--reason`
89
- // as the scope and left the reason text to fold into the value, storing
90
- // value="* why" files=["--reason"] and reporting success. Same silent-no-op
91
- // class as an unknown flag folding into the value; refuse it the same way.
92
- if (glob.startsWith('--')) throw new Error(`${flag} requires a glob, got the flag ${glob}`);
93
- return glob;
94
- }
95
-
96
- function parseValueArgs(args, { allowUnscopedWildcard = false } = {}) {
97
- const positionals = [];
98
- const files = [];
99
- let reason = '';
100
-
101
- for (let i = 0; i < args.length; i++) {
102
- const arg = String(args[i] || '');
103
- if (arg === '--reason') {
104
- const chunks = [];
105
- while (i + 1 < args.length && !String(args[i + 1]).startsWith('--')) chunks.push(args[++i]);
106
- reason = chunks.join(' ').trim();
107
- } else if (arg.startsWith('--reason=')) {
108
- reason = arg.slice('--reason='.length).trim();
109
- } else if (arg === '--file' || arg === '--files') {
110
- if (i + 1 >= args.length) throw new Error(`${arg} requires a glob`);
111
- files.push(requireGlob(args[++i], arg));
112
- } else if (arg.startsWith('--file=')) {
113
- files.push(requireGlob(arg.slice('--file='.length), '--file'));
114
- } else if (arg.startsWith('--files=')) {
115
- files.push(requireGlob(arg.slice('--files='.length), '--files'));
116
- } else if (arg.startsWith('--')) {
117
- throw new Error(`Unknown add-value flag: ${arg}`);
118
- } else {
119
- positionals.push(arg);
120
- }
121
- }
122
-
123
- const [rule, ...valueParts] = positionals;
124
- const value = normalizeIgnoreValue(valueParts.join(' '));
125
- if (!rule || !value) throw new Error('Pass a rule id and value, e.g. impeccable ignores add-value overused-font Inter');
126
- // Sorted: the dedup key compares the files array, so an unsorted scope made
127
- // `--file b.css --file a.css` a different entry from `--file a.css --file b.css`.
128
- const scopedFiles = Array.from(new Set(files.filter(Boolean))).sort();
129
- if (value === '*' && scopedFiles.length === 0 && !allowUnscopedWildcard) {
130
- throw new Error('Wildcard value ignores must be scoped with --file <glob>.');
131
- }
132
- return {
133
- rule: String(rule).trim().toLowerCase(),
134
- value,
135
- files: scopedFiles,
136
- reason,
137
- };
138
- }
139
-
140
- function formatValues(values) {
141
- if (!values.length) return '(none)';
142
- return values
143
- .map((entry) => {
144
- const fileSuffix = Array.isArray(entry.files) && entry.files.length
145
- ? ` [${entry.files.join(', ')}]`
146
- : '';
147
- const reasonSuffix = entry.reason ? ` - ${entry.reason}` : '';
148
- return `${entry.rule}=${entry.value}${fileSuffix}${reasonSuffix}`;
149
- })
150
- .join(', ');
151
- }
152
-
153
- function formatConfig(label, config) {
154
- return [
155
- `${label}:`,
156
- ` ignoreRules: ${config.ignoreRules.length ? config.ignoreRules.join(', ') : '(none)'}`,
157
- ` ignoreFiles: ${config.ignoreFiles.length ? config.ignoreFiles.join(', ') : '(none)'}`,
158
- ` ignoreValues: ${formatValues(config.ignoreValues)}`,
159
- ` designSystem: ${config.designSystem?.enabled === false ? 'disabled' : 'enabled'}`,
160
- ].join('\n');
161
- }
162
-
163
- function list(cwd) {
164
- const merged = readDetectionConfig(cwd);
165
- const shared = readRawDetectionConfig(cwd);
166
- const local = readRawDetectionConfig(cwd, { local: true });
167
- return [
168
- 'Impeccable detector ignores',
169
- ` shared file: ${path.relative(cwd, getConfigPath(cwd)) || getConfigPath(cwd)}`,
170
- ` local file: ${path.relative(cwd, getLocalConfigPath(cwd)) || getLocalConfigPath(cwd)}`,
171
- '',
172
- formatConfig('Merged', merged),
173
- '',
174
- formatConfig('Shared', shared),
175
- '',
176
- formatConfig('Local', local),
177
- ].join('\n');
178
- }
179
-
180
- function readScopeConfig(cwd, local) {
181
- return readRawDetectionConfig(cwd, { local });
182
- }
183
-
184
- function writeScopeConfig(cwd, config, local) {
185
- return writeDetectionConfig(cwd, config, { local });
186
- }
187
-
188
- function parseRuleArgs(args) {
189
- const positionals = [];
190
- let allValues = false;
191
-
192
- for (let i = 0; i < args.length; i++) {
193
- const arg = String(args[i] || '');
194
- if (arg === '--all-values') {
195
- allValues = true;
196
- } else if (arg === '--reason') {
197
- while (i + 1 < args.length && !String(args[i + 1]).startsWith('--')) i++;
198
- } else if (arg.startsWith('--reason=')) {
199
- // Accepted for symmetry with add-value; ignoreRules stores ids only.
200
- } else if (arg.startsWith('--')) {
201
- throw new Error(`Unknown add-rule flag: ${arg}`);
202
- } else {
203
- positionals.push(arg);
204
- }
205
- }
206
-
207
- return {
208
- rule: String(positionals[0] || '').trim().toLowerCase(),
209
- allValues,
210
- };
211
- }
212
-
213
- function addRule(cwd, args) {
214
- const { local, rest } = parseScope(args);
215
- const { rule, allValues } = parseRuleArgs(rest);
216
- if (!rule) throw new Error('Pass a rule id, e.g. impeccable ignores add-rule side-tab');
217
- if (rule === 'overused-font' && !allValues) {
218
- throw new Error('overused-font is value-specific by default. Use add-value overused-font <font>, or add-rule overused-font --all-values for broad suppression.');
219
- }
220
- const config = readScopeConfig(cwd, local);
221
- if (!config.ignoreRules.includes(rule)) config.ignoreRules.push(rule);
222
- const target = writeScopeConfig(cwd, config, local);
223
- return `Added ${rule} to ${local ? 'local' : 'shared'} detector ignoreRules (${path.relative(cwd, target) || target}).`;
224
- }
225
-
226
- function addFile(cwd, args) {
227
- const { local, rest } = parseScope(args);
228
- const glob = String(rest[0] || '').trim();
229
- if (!glob) throw new Error('Pass a glob, e.g. impeccable ignores add-file "src/legacy/**"');
230
- const config = readScopeConfig(cwd, local);
231
- if (!config.ignoreFiles.includes(glob)) config.ignoreFiles.push(glob);
232
- const target = writeScopeConfig(cwd, config, local);
233
- return `Added ${glob} to ${local ? 'local' : 'shared'} detector ignoreFiles (${path.relative(cwd, target) || target}).`;
234
- }
235
-
236
- function addValue(cwd, args) {
237
- const { local, rest } = parseScope(args);
238
- const parsed = parseValueArgs(rest);
239
- if (parsed.value !== '*' && !extractFindingIgnoreValue({ antipattern: parsed.rule, ignoreValue: parsed.value })) {
240
- throw new Error(`${parsed.rule} has no extractable ignore value. Use impeccable ignores add-value ${parsed.rule} "*" --file <glob> to suppress it in matching files.`);
241
- }
242
- const config = readScopeConfig(cwd, local);
243
- const key = ignoreValueKey(parsed);
244
- const existing = config.ignoreValues.find((entry) => ignoreValueKey(entry) === key);
245
- if (existing) {
246
- if (parsed.reason) existing.reason = parsed.reason;
247
- if (parsed.files.length) existing.files = parsed.files;
248
- } else {
249
- // rule, value, files, createdAt, reason — the same order the normalizers emit,
250
- // so a fresh entry survives the next write untouched.
251
- const entry = {
252
- rule: parsed.rule,
253
- value: parsed.value,
254
- };
255
- if (parsed.files.length) entry.files = parsed.files;
256
- entry.createdAt = new Date().toISOString();
257
- if (parsed.reason) entry.reason = parsed.reason;
258
- config.ignoreValues.push(entry);
259
- }
260
- const target = writeScopeConfig(cwd, config, local);
261
- return `Added ${parsed.rule}=${parsed.value} to ${local ? 'local' : 'shared'} detector ignoreValues (${path.relative(cwd, target) || target}).`;
262
- }
263
-
264
- function removeFromScopes(cwd, args, remover) {
265
- const { local, all, rest } = parseScope(args, { allowAll: true });
266
- const scopes = all ? [false, true] : [local];
267
- const removed = [];
268
- for (const isLocal of scopes) {
269
- const config = readScopeConfig(cwd, isLocal);
270
- const count = remover(config, rest);
271
- if (count > 0) {
272
- const target = writeScopeConfig(cwd, config, isLocal);
273
- removed.push(`${count} from ${isLocal ? 'local' : 'shared'} (${path.relative(cwd, target) || target})`);
274
- }
275
- }
276
- return removed.length ? `Removed ${removed.join(', ')}.` : 'No matching detector ignore found.';
277
- }
278
-
279
- function removeRule(cwd, args) {
280
- return removeFromScopes(cwd, args, (config, rest) => {
281
- const rule = String(rest[0] || '').trim().toLowerCase();
282
- if (!rule) throw new Error('Pass a rule id, e.g. impeccable ignores remove-rule side-tab');
283
- const before = config.ignoreRules.length;
284
- config.ignoreRules = config.ignoreRules.filter((entry) => entry !== rule);
285
- return before - config.ignoreRules.length;
286
- });
287
- }
288
-
289
- function removeFile(cwd, args) {
290
- return removeFromScopes(cwd, args, (config, rest) => {
291
- const glob = String(rest[0] || '').trim();
292
- if (!glob) throw new Error('Pass a glob, e.g. impeccable ignores remove-file "src/legacy/**"');
293
- const before = config.ignoreFiles.length;
294
- config.ignoreFiles = config.ignoreFiles.filter((entry) => entry !== glob);
295
- return before - config.ignoreFiles.length;
296
- });
297
- }
298
-
299
- function removeValue(cwd, args) {
300
- return removeFromScopes(cwd, args, (config, rest) => {
301
- const parsed = parseValueArgs(rest, { allowUnscopedWildcard: true });
302
- const key = ignoreValueKey(parsed);
303
- const before = config.ignoreValues.length;
304
- config.ignoreValues = config.ignoreValues.filter((entry) => ignoreValueKey(entry) !== key);
305
- return before - config.ignoreValues.length;
306
- });
307
- }
308
-
309
- function clear(cwd, args) {
310
- const { local, all, rest } = parseScope(args, { allowAll: true });
311
- if (rest.length > 0) throw new Error('clear does not take positional arguments');
312
- const scopes = all ? [false, true] : [local];
313
- for (const isLocal of scopes) {
314
- const config = readScopeConfig(cwd, isLocal);
315
- config.ignoreRules = [];
316
- config.ignoreFiles = [];
317
- config.ignoreValues = [];
318
- writeScopeConfig(cwd, config, isLocal);
319
- }
320
- return `Cleared detector ignores in ${all ? 'shared and local config' : local ? 'local config' : 'shared config'}.`;
321
- }
322
-
323
- function ignoreValueKey(entry) {
324
- // Sorted: a file scope is a set. Comparing stored order made an on-disk scope
325
- // miss the sorted argv form, so a re-add duplicated the entry and a remove
326
- // silently failed. Every key that hashes `files` must sort — there are four.
327
- const files = Array.isArray(entry.files) && entry.files.length ? [...entry.files].sort().join('\x1f') : '';
328
- return `${String(entry.rule || '').trim().toLowerCase()}\0${normalizeIgnoreValue(entry.value)}\0${files}`;
329
- }
330
-
331
- export async function run(args = [], opts = {}) {
332
- const cwd = opts.cwd || process.cwd();
333
- const actionArg = args[0] || 'list';
334
- if (actionArg === '--help' || actionArg === '-h') {
335
- printUsage();
336
- return;
337
- }
338
- const action = ACTION_ALIASES.get(String(actionArg).toLowerCase());
339
- if (!action) {
340
- throw new Error(`Unknown ignores action: ${actionArg}. Run "impeccable ignores --help".`);
341
- }
342
- const rest = args.slice(1);
343
- let out;
344
- switch (action) {
345
- case 'list': out = list(cwd); break;
346
- case 'add-rule': out = addRule(cwd, rest); break;
347
- case 'add-file': out = addFile(cwd, rest); break;
348
- case 'add-value': out = addValue(cwd, rest); break;
349
- case 'remove-rule': out = removeRule(cwd, rest); break;
350
- case 'remove-file': out = removeFile(cwd, rest); break;
351
- case 'remove-value': out = removeValue(cwd, rest); break;
352
- case 'clear': out = clear(cwd, rest); break;
353
- }
354
- if (out) console.log(out);
355
- }