stylelint-plugin-rhythmguard 3.19.0 → 3.20.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.
- package/CHANGELOG.md +37 -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 +37 -3
- package/src/audit/codemod.js +2 -0
- 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 +5 -0
- 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 +39 -11
- 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 +136 -28
- 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
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Decides whether a string the ESLint rule visits is a class string (cs-04,
|
|
5
|
+
* fix-09). The JS and TS path used to check every Literal and TemplateElement,
|
|
6
|
+
* so `eslint --fix` rewrote type unions, enum members, `aria-label`, `title`,
|
|
7
|
+
* `id` and `data-*` values and prose. Now a string is checked when it sits in
|
|
8
|
+
* a class context, or, anywhere else, when it holds two or more
|
|
9
|
+
* utility-shaped tokens.
|
|
10
|
+
*
|
|
11
|
+
* The walk only reads `node.parent`, so it runs under any host that sets
|
|
12
|
+
* parents (ESLint, vue-eslint-parser and svelte-eslint-parser templates,
|
|
13
|
+
* Oxlint JS plugins); it needs no scope analysis.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// Callee and tag names whose string arguments are class strings. `tw` covers
|
|
17
|
+
// `tw\`...\`` and `tw.div\`...\`` tagged templates as well as `tw(...)` calls.
|
|
18
|
+
const DEFAULT_CLASS_FUNCTIONS = ['cn', 'clsx', 'classnames', 'cx', 'cva', 'tv', 'twMerge', 'twJoin', 'tw'];
|
|
19
|
+
|
|
20
|
+
// JSX attribute (and object property) names whose values are class strings.
|
|
21
|
+
// An entry starting with `*` matches by suffix: `*ClassName` covers
|
|
22
|
+
// `contentClassName`, `wrapperClassName` and the like.
|
|
23
|
+
const DEFAULT_CLASS_CONTEXTS = ['class', 'className', '*ClassName'];
|
|
24
|
+
|
|
25
|
+
const CLASS = 'class';
|
|
26
|
+
const SKIP = 'skip';
|
|
27
|
+
const OTHER = 'other';
|
|
28
|
+
|
|
29
|
+
// Expression wrappers a class string passes through on its way up to the
|
|
30
|
+
// call, attribute or property that decides its context.
|
|
31
|
+
const PASS_THROUGH = new Set([
|
|
32
|
+
'ArrayExpression',
|
|
33
|
+
'ChainExpression',
|
|
34
|
+
'JSXExpressionContainer',
|
|
35
|
+
'ObjectExpression',
|
|
36
|
+
'ParenthesizedExpression',
|
|
37
|
+
'SpreadElement',
|
|
38
|
+
'SvelteMustacheTag',
|
|
39
|
+
'TSAsExpression',
|
|
40
|
+
'TSNonNullExpression',
|
|
41
|
+
'TSSatisfiesExpression',
|
|
42
|
+
'TSTypeAssertion',
|
|
43
|
+
'TemplateLiteral',
|
|
44
|
+
'VExpressionContainer',
|
|
45
|
+
]);
|
|
46
|
+
|
|
47
|
+
// Svelte directives (`use:`, `style:`, `class:`, `bind:`, `on:` and friends)
|
|
48
|
+
// hold expressions, never class text.
|
|
49
|
+
const SVELTE_DIRECTIVES = new Set([
|
|
50
|
+
'SvelteDirective',
|
|
51
|
+
'SvelteGenericsDirective',
|
|
52
|
+
'SvelteSpecialDirective',
|
|
53
|
+
'SvelteStyleDirective',
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
const MAX_DEPTH = 64;
|
|
57
|
+
|
|
58
|
+
function nameMatcher(names) {
|
|
59
|
+
const exact = new Set();
|
|
60
|
+
const suffixes = [];
|
|
61
|
+
for (const name of names) {
|
|
62
|
+
if (typeof name !== 'string' || name.length === 0) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (name.startsWith('*') && name.length > 1) {
|
|
66
|
+
suffixes.push(name.slice(1));
|
|
67
|
+
} else {
|
|
68
|
+
exact.add(name);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return (name) => typeof name === 'string'
|
|
72
|
+
&& (exact.has(name) || suffixes.some((suffix) => name.length > suffix.length && name.endsWith(suffix)));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* `classFunctions` and `classContexts` extend the defaults; they never
|
|
77
|
+
* replace them.
|
|
78
|
+
*/
|
|
79
|
+
function createClassContextMatchers(option = {}) {
|
|
80
|
+
const functions = [...DEFAULT_CLASS_FUNCTIONS, ...(Array.isArray(option.classFunctions) ? option.classFunctions : [])];
|
|
81
|
+
const contexts = [...DEFAULT_CLASS_CONTEXTS, ...(Array.isArray(option.classContexts) ? option.classContexts : [])];
|
|
82
|
+
return {
|
|
83
|
+
isClassContextName: nameMatcher(contexts),
|
|
84
|
+
isClassFunctionName: nameMatcher(functions),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function jsxAttributeName(attribute) {
|
|
89
|
+
const { name } = attribute;
|
|
90
|
+
if (!name) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (name.type === 'JSXNamespacedName') {
|
|
94
|
+
return `${name.namespace.name}:${name.name.name}`;
|
|
95
|
+
}
|
|
96
|
+
return typeof name.name === 'string' ? name.name : null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function propertyKeyName(property) {
|
|
100
|
+
const { key } = property;
|
|
101
|
+
if (!key || property.computed) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
if (key.type === 'Identifier') {
|
|
105
|
+
return key.name;
|
|
106
|
+
}
|
|
107
|
+
if (key.type === 'Literal' && typeof key.value === 'string') {
|
|
108
|
+
return key.value;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** `cn`, `utils.cn`, `tw.div`, `tw(Button)`: the name a call or tag is matched by. */
|
|
114
|
+
function calleeNames(callee) {
|
|
115
|
+
const names = [];
|
|
116
|
+
let current = callee;
|
|
117
|
+
for (let depth = 0; current && depth < MAX_DEPTH; depth += 1) {
|
|
118
|
+
if (current.type === 'Identifier') {
|
|
119
|
+
names.push(current.name);
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
if (current.type === 'MemberExpression') {
|
|
123
|
+
if (!current.computed && current.property && current.property.type === 'Identifier') {
|
|
124
|
+
names.push(current.property.name);
|
|
125
|
+
}
|
|
126
|
+
current = current.object;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (current.type === 'CallExpression') {
|
|
130
|
+
current = current.callee;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
return names;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function vueAttributeContext(attribute, matchers) {
|
|
139
|
+
const { key } = attribute;
|
|
140
|
+
if (!attribute.directive || !key) {
|
|
141
|
+
return SKIP;
|
|
142
|
+
}
|
|
143
|
+
// vue-eslint-parser: `:class` and `v-bind:class` are a VDirectiveKey whose
|
|
144
|
+
// name is the `bind` VIdentifier and whose argument names the attribute.
|
|
145
|
+
const directive = key.name && typeof key.name === 'object' ? key.name.name : key.name;
|
|
146
|
+
const argument = key.argument && key.argument.type === 'VIdentifier' ? key.argument.name : null;
|
|
147
|
+
if (directive === 'bind' && argument && matchers.isClassContextName(argument)) {
|
|
148
|
+
return CLASS;
|
|
149
|
+
}
|
|
150
|
+
return SKIP;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Where a string node sits: CLASS (check it), SKIP (never check it: type
|
|
155
|
+
* positions, enum members, non-class markup attributes) or OTHER (check it
|
|
156
|
+
* only when it looks like a class list).
|
|
157
|
+
*/
|
|
158
|
+
function classContextOf(node, matchers) {
|
|
159
|
+
let child = node;
|
|
160
|
+
let parent = node.parent;
|
|
161
|
+
|
|
162
|
+
for (let depth = 0; parent && depth < MAX_DEPTH; depth += 1) {
|
|
163
|
+
switch (parent.type) {
|
|
164
|
+
case 'JSXAttribute':
|
|
165
|
+
return matchers.isClassContextName(jsxAttributeName(parent)) ? CLASS : SKIP;
|
|
166
|
+
case 'VAttribute':
|
|
167
|
+
return vueAttributeContext(parent, matchers);
|
|
168
|
+
case 'SvelteAttribute':
|
|
169
|
+
return parent.key && matchers.isClassContextName(parent.key.name) ? CLASS : SKIP;
|
|
170
|
+
case 'CallExpression':
|
|
171
|
+
if (!parent.arguments || !parent.arguments.includes(child)) {
|
|
172
|
+
return OTHER;
|
|
173
|
+
}
|
|
174
|
+
if (calleeNames(parent.callee).some(matchers.isClassFunctionName)) {
|
|
175
|
+
return CLASS;
|
|
176
|
+
}
|
|
177
|
+
// Any other call decides nothing by itself: its argument is a class
|
|
178
|
+
// string when the call sits in a class context
|
|
179
|
+
// (`className={mergeClasses('p-4')}`), skipped under a non-class
|
|
180
|
+
// attribute (`title={t('p-4')}`), and judged by its tokens elsewhere.
|
|
181
|
+
break;
|
|
182
|
+
case 'TaggedTemplateExpression':
|
|
183
|
+
if (parent.quasi === child) {
|
|
184
|
+
return calleeNames(parent.tag).some(matchers.isClassFunctionName) ? CLASS : OTHER;
|
|
185
|
+
}
|
|
186
|
+
return OTHER;
|
|
187
|
+
case 'Property':
|
|
188
|
+
if (parent.value === child && matchers.isClassContextName(propertyKeyName(parent))) {
|
|
189
|
+
return CLASS;
|
|
190
|
+
}
|
|
191
|
+
break;
|
|
192
|
+
case 'ConditionalExpression':
|
|
193
|
+
if (parent.test === child) {
|
|
194
|
+
return OTHER;
|
|
195
|
+
}
|
|
196
|
+
break;
|
|
197
|
+
case 'LogicalExpression':
|
|
198
|
+
break;
|
|
199
|
+
case 'BinaryExpression':
|
|
200
|
+
if (parent.operator !== '+') {
|
|
201
|
+
return OTHER;
|
|
202
|
+
}
|
|
203
|
+
break;
|
|
204
|
+
case 'ArrowFunctionExpression':
|
|
205
|
+
if (parent.body !== child) {
|
|
206
|
+
return OTHER;
|
|
207
|
+
}
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
if (SVELTE_DIRECTIVES.has(parent.type)) {
|
|
211
|
+
return SKIP;
|
|
212
|
+
}
|
|
213
|
+
if (PASS_THROUGH.has(parent.type)) {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
// TSLiteralType, TSEnumMember, TSTemplateLiteralType and every other
|
|
217
|
+
// type position: a string there is a type, not a value.
|
|
218
|
+
if (parent.type.startsWith('TS')) {
|
|
219
|
+
return SKIP;
|
|
220
|
+
}
|
|
221
|
+
return OTHER;
|
|
222
|
+
}
|
|
223
|
+
child = parent;
|
|
224
|
+
parent = parent.parent;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return OTHER;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/*
|
|
231
|
+
* A conservative "utility-shaped" test for strings outside class contexts.
|
|
232
|
+
* After the variant prefix (`md:`, `hover:`), the important marker and a
|
|
233
|
+
* leading `-` are removed, a token is utility-shaped when it is
|
|
234
|
+
* - an arbitrary value: `p-[13px]`, `bg-[#fff]`, `w-(--x)`;
|
|
235
|
+
* - a numeric step or fraction: `p-4`, `gap-2.5`, `w-1/2`, `bg-red-500`;
|
|
236
|
+
* - a size keyword step: `text-sm`, `rounded-lg`, `w-full`, `h-screen`, `m-auto`;
|
|
237
|
+
* - or one of a short list of keyword utilities (`flex`, `hidden`, `items-center`).
|
|
238
|
+
* Capitalised words, plain words and words with a colon but no utility after
|
|
239
|
+
* it are not, so prose such as "Error in p-[13px] string" holds one.
|
|
240
|
+
*/
|
|
241
|
+
const ARBITRARY_TOKEN = /^[a-z][a-z0-9-]*-(?:\[[^\]\s]+\]|\([^)\s]+\))$/;
|
|
242
|
+
const NUMERIC_TOKEN = /^[a-z][a-z-]*-\d+(?:\.\d+)?(?:\/\d+)?$/;
|
|
243
|
+
const SIZE_TOKEN = /^[a-z][a-z-]*-(?:xs|sm|base|md|lg|xl|[2-9]xl|full|screen|auto|px|none|fit|min|max)$/;
|
|
244
|
+
const KEYWORD_UTILITIES = new Set([
|
|
245
|
+
'absolute', 'antialiased', 'block', 'border', 'capitalize', 'container', 'contents', 'fixed', 'flex',
|
|
246
|
+
'flex-col', 'flex-row', 'flex-wrap', 'grid', 'grow', 'hidden', 'inline', 'inline-block', 'inline-flex',
|
|
247
|
+
'inline-grid', 'invisible', 'isolate', 'italic', 'items-baseline', 'items-center', 'items-end',
|
|
248
|
+
'items-start', 'items-stretch', 'justify-around', 'justify-between', 'justify-center', 'justify-end',
|
|
249
|
+
'justify-evenly', 'justify-start', 'lowercase', 'relative', 'rounded', 'shadow', 'shrink', 'sr-only',
|
|
250
|
+
'static', 'sticky', 'table', 'transition', 'truncate', 'underline', 'uppercase', 'visible',
|
|
251
|
+
]);
|
|
252
|
+
|
|
253
|
+
function utilityCandidate(token) {
|
|
254
|
+
let depth = 0;
|
|
255
|
+
let separator = -1;
|
|
256
|
+
for (let index = 0; index < token.length; index += 1) {
|
|
257
|
+
const character = token[index];
|
|
258
|
+
if (character === '[' || character === '(') {
|
|
259
|
+
depth += 1;
|
|
260
|
+
} else if ((character === ']' || character === ')') && depth > 0) {
|
|
261
|
+
depth -= 1;
|
|
262
|
+
} else if (character === ':' && depth === 0) {
|
|
263
|
+
separator = index;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
let candidate = token.slice(separator + 1);
|
|
267
|
+
if (candidate.startsWith('!')) {
|
|
268
|
+
candidate = candidate.slice(1);
|
|
269
|
+
} else if (candidate.endsWith('!')) {
|
|
270
|
+
candidate = candidate.slice(0, -1);
|
|
271
|
+
}
|
|
272
|
+
if (candidate.startsWith('-')) {
|
|
273
|
+
candidate = candidate.slice(1);
|
|
274
|
+
}
|
|
275
|
+
return candidate;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function isUtilityShaped(token) {
|
|
279
|
+
const candidate = utilityCandidate(token);
|
|
280
|
+
return KEYWORD_UTILITIES.has(candidate)
|
|
281
|
+
|| ARBITRARY_TOKEN.test(candidate)
|
|
282
|
+
|| NUMERIC_TOKEN.test(candidate)
|
|
283
|
+
|| SIZE_TOKEN.test(candidate);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function countUtilityShapedTokens(text) {
|
|
287
|
+
let count = 0;
|
|
288
|
+
for (const token of text.split(/\s+/)) {
|
|
289
|
+
if (token && isUtilityShaped(token)) {
|
|
290
|
+
count += 1;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return count;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The static text a string contributes to a class list: a template chunk is
|
|
298
|
+
* judged with the other chunks of its template literal.
|
|
299
|
+
*/
|
|
300
|
+
function staticTextOf(node, value) {
|
|
301
|
+
const template = node.type === 'TemplateElement' ? node.parent : null;
|
|
302
|
+
if (template && Array.isArray(template.quasis)) {
|
|
303
|
+
return template.quasis
|
|
304
|
+
.map((quasi) => (quasi.value && typeof quasi.value.cooked === 'string' ? quasi.value.cooked : (quasi.value && quasi.value.raw) || ''))
|
|
305
|
+
.join(' ');
|
|
306
|
+
}
|
|
307
|
+
return value;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** True when the rule should check this string node. */
|
|
311
|
+
function isClassString(node, value, matchers) {
|
|
312
|
+
const context = classContextOf(node, matchers);
|
|
313
|
+
if (context === CLASS) {
|
|
314
|
+
return true;
|
|
315
|
+
}
|
|
316
|
+
if (context === SKIP) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
return countUtilityShapedTokens(staticTextOf(node, value)) >= 2;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
module.exports = {
|
|
323
|
+
DEFAULT_CLASS_CONTEXTS,
|
|
324
|
+
DEFAULT_CLASS_FUNCTIONS,
|
|
325
|
+
classContextOf,
|
|
326
|
+
countUtilityShapedTokens,
|
|
327
|
+
createClassContextMatchers,
|
|
328
|
+
isClassString,
|
|
329
|
+
isUtilityShaped,
|
|
330
|
+
};
|
|
@@ -269,9 +269,10 @@ function createTokenFixer(node, sourceCode) {
|
|
|
269
269
|
* (`<div class="p-[13px]">`, a `VLiteral` in a non-directive `VAttribute`) or
|
|
270
270
|
* a Svelte component (`<div class="p-[13px] {x}">`, a `SvelteLiteral` in a
|
|
271
271
|
* `SvelteAttribute`). Bound classes (`:class="..."`, `class={...}`) are
|
|
272
|
-
* expressions, and their strings are ordinary literals.
|
|
272
|
+
* expressions, and their strings are ordinary literals. `isClassName` adds
|
|
273
|
+
* attribute names (the rule's `classContexts`); `class` always counts.
|
|
273
274
|
*/
|
|
274
|
-
function isClassAttributeText(node) {
|
|
275
|
+
function isClassAttributeText(node, isClassName = () => false) {
|
|
275
276
|
const attribute = node.parent;
|
|
276
277
|
if (!attribute || !attribute.key) {
|
|
277
278
|
return false;
|
|
@@ -280,13 +281,13 @@ function isClassAttributeText(node) {
|
|
|
280
281
|
return attribute.type === 'VAttribute'
|
|
281
282
|
&& attribute.directive === false
|
|
282
283
|
&& attribute.value === node
|
|
283
|
-
&& String(attribute.key.name).toLowerCase() === 'class';
|
|
284
|
+
&& (String(attribute.key.name).toLowerCase() === 'class' || isClassName(attribute.key.name));
|
|
284
285
|
}
|
|
285
286
|
if (node.type === 'SvelteLiteral') {
|
|
286
287
|
return attribute.type === 'SvelteAttribute'
|
|
287
288
|
&& Array.isArray(attribute.value)
|
|
288
289
|
&& attribute.value.includes(node)
|
|
289
|
-
&& attribute.key.name === 'class';
|
|
290
|
+
&& (attribute.key.name === 'class' || isClassName(attribute.key.name));
|
|
290
291
|
}
|
|
291
292
|
return false;
|
|
292
293
|
}
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
const { MAX_NOTE_LENGTH, normalizeNote, withNote } = require('../../core/options');
|
|
4
4
|
const { createTailwindClassAnalyzer, offScaleClassMessage } = require('../../core/tailwind-class-analysis');
|
|
5
5
|
const { classStringValue, createTokenFixer, isClassAttributeText } = require('../literal-spans');
|
|
6
|
+
const { createClassContextMatchers, isClassString } = require('../class-context');
|
|
6
7
|
|
|
7
8
|
const RULE_NAME = 'tailwind-class-use-scale';
|
|
8
9
|
|
|
9
|
-
function maybeCheckNodeText(node, sourceCode, context, analyzer, allowFix) {
|
|
10
|
+
function maybeCheckNodeText(node, sourceCode, context, analyzer, allowFix, matchers) {
|
|
10
11
|
const value = classStringValue(node);
|
|
11
12
|
if (!value) {
|
|
12
13
|
return;
|
|
@@ -17,15 +18,23 @@ function maybeCheckNodeText(node, sourceCode, context, analyzer, allowFix) {
|
|
|
17
18
|
return;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
// Only class strings are checked: class contexts, or elsewhere a string
|
|
22
|
+
// holding two or more utility-shaped tokens; never a type position or a
|
|
23
|
+
// non-class attribute (cs-04, fix-09). Markup class text is gated by the
|
|
24
|
+
// caller. Run after the cheap analysis, so most strings never walk up.
|
|
25
|
+
if (matchers && !isClassString(node, value, matchers)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
// Fixes replace only the raw span of the class token, so escapes and JSX
|
|
21
30
|
// or HTML entities elsewhere in the literal are kept as written (#225).
|
|
22
31
|
const replaceToken = allowFix ? createTokenFixer(node, sourceCode) : null;
|
|
23
32
|
|
|
24
33
|
for (const { analysis, segment } of findings) {
|
|
25
34
|
context.report({
|
|
26
|
-
message: withNote(offScaleClassMessage(segment.token, analysis), analyzer.note),
|
|
35
|
+
message: withNote(offScaleClassMessage(segment.token, analysis, analyzer.options.scaleName), analyzer.note),
|
|
27
36
|
node,
|
|
28
|
-
fix: replaceToken && analysis.reason
|
|
37
|
+
fix: replaceToken && analysis.reason === 'off-scale' && analysis.fixedToken
|
|
29
38
|
? replaceToken(segment.start, segment.token, analysis.fixedToken)
|
|
30
39
|
: null,
|
|
31
40
|
});
|
|
@@ -47,6 +56,14 @@ module.exports = {
|
|
|
47
56
|
allowHairlines: { type: 'boolean' },
|
|
48
57
|
allowNegative: { type: 'boolean' },
|
|
49
58
|
baseFontSize: { type: 'number' },
|
|
59
|
+
classContexts: {
|
|
60
|
+
items: { minLength: 1, type: 'string' },
|
|
61
|
+
type: 'array',
|
|
62
|
+
},
|
|
63
|
+
classFunctions: {
|
|
64
|
+
items: { minLength: 1, type: 'string' },
|
|
65
|
+
type: 'array',
|
|
66
|
+
},
|
|
50
67
|
note: { maxLength: MAX_NOTE_LENGTH, minLength: 1, type: 'string' },
|
|
51
68
|
scale: {
|
|
52
69
|
items: {
|
|
@@ -77,9 +94,13 @@ module.exports = {
|
|
|
77
94
|
const analyzer = createTailwindClassAnalyzer(option);
|
|
78
95
|
analyzer.note = normalizeNote(option.note);
|
|
79
96
|
const sourceCode = context.sourceCode || context.getSourceCode();
|
|
97
|
+
const matchers = createClassContextMatchers(option);
|
|
80
98
|
|
|
81
|
-
const Literal = (node) => maybeCheckNodeText(node, sourceCode, context, analyzer, true);
|
|
82
|
-
const TemplateElement = (node) => maybeCheckNodeText(node, sourceCode, context, analyzer, false);
|
|
99
|
+
const Literal = (node) => maybeCheckNodeText(node, sourceCode, context, analyzer, true, matchers);
|
|
100
|
+
const TemplateElement = (node) => maybeCheckNodeText(node, sourceCode, context, analyzer, false, matchers);
|
|
101
|
+
// Static class text in Vue and Svelte markup, already known to be a class
|
|
102
|
+
// attribute value.
|
|
103
|
+
const ClassText = (node) => maybeCheckNodeText(node, sourceCode, context, analyzer, true, null);
|
|
83
104
|
const scriptVisitor = { Literal, TemplateElement };
|
|
84
105
|
const parserServices = sourceCode.parserServices || context.parserServices;
|
|
85
106
|
|
|
@@ -89,8 +110,8 @@ module.exports = {
|
|
|
89
110
|
// as other linters' JS plugin runtimes) never see it.
|
|
90
111
|
if ((sourceCode.visitorKeys && sourceCode.visitorKeys.SvelteLiteral) || (parserServices && parserServices.isSvelte)) {
|
|
91
112
|
scriptVisitor.SvelteLiteral = (node) => {
|
|
92
|
-
if (isClassAttributeText(node)) {
|
|
93
|
-
|
|
113
|
+
if (isClassAttributeText(node, matchers.isClassContextName)) {
|
|
114
|
+
ClassText(node);
|
|
94
115
|
}
|
|
95
116
|
};
|
|
96
117
|
}
|
|
@@ -104,8 +125,8 @@ module.exports = {
|
|
|
104
125
|
Literal,
|
|
105
126
|
TemplateElement,
|
|
106
127
|
VLiteral(node) {
|
|
107
|
-
if (isClassAttributeText(node)) {
|
|
108
|
-
|
|
128
|
+
if (isClassAttributeText(node, matchers.isClassContextName)) {
|
|
129
|
+
ClassText(node);
|
|
109
130
|
}
|
|
110
131
|
},
|
|
111
132
|
},
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The two default scales every surface reads, defined once (dx-06, cs-01).
|
|
5
|
+
*
|
|
6
|
+
* DEFAULT_SCALE is the scale for stylesheets: what `use-scale` checks with no
|
|
7
|
+
* `scale` option, what `configs/recommended` sets, what the audit uses without
|
|
8
|
+
* `--scale`, and what `scale: "auto"` falls back to. It is the `rhythmic-4`
|
|
9
|
+
* preset.
|
|
10
|
+
*
|
|
11
|
+
* The Tailwind ladder is the scale for class strings when none is configured:
|
|
12
|
+
* the numeric spacing utilities of Tailwind's default theme, as multiples of
|
|
13
|
+
* the spacing unit (`--spacing: 0.25rem` in v4, the same quarter rem in the v3
|
|
14
|
+
* default theme). On a 16px root that is 0, 2, 4, 6, 8, ... 384px, so `p-[20px]`
|
|
15
|
+
* is the standard `p-5`, not off-scale.
|
|
16
|
+
*/
|
|
17
|
+
const DEFAULT_SCALE = Object.freeze([0, 4, 8, 12, 16, 24, 32, 40, 48, 64]);
|
|
18
|
+
|
|
19
|
+
const TAILWIND_SPACING_MULTIPLIERS = Object.freeze([
|
|
20
|
+
0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80, 96,
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
// The spacing unit as a share of the root font size: 0.25rem.
|
|
24
|
+
const TAILWIND_SPACING_UNIT_REM = 0.25;
|
|
25
|
+
|
|
26
|
+
/** Tailwind's default spacing ladder in px for a spacing unit in px (4 on a 16px root). */
|
|
27
|
+
function tailwindSpacingLadder(spacingUnitPx = 16 * TAILWIND_SPACING_UNIT_REM) {
|
|
28
|
+
return TAILWIND_SPACING_MULTIPLIERS.map((multiplier) => multiplier * spacingUnitPx);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TAILWIND_DEFAULT_SCALE = Object.freeze(tailwindSpacingLadder());
|
|
32
|
+
|
|
33
|
+
const TAILWIND_SCALE_NAME = 'Tailwind default spacing';
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
DEFAULT_SCALE,
|
|
37
|
+
TAILWIND_DEFAULT_SCALE,
|
|
38
|
+
TAILWIND_SCALE_NAME,
|
|
39
|
+
TAILWIND_SPACING_MULTIPLIERS,
|
|
40
|
+
TAILWIND_SPACING_UNIT_REM,
|
|
41
|
+
tailwindSpacingLadder,
|
|
42
|
+
};
|
package/src/presets/scales.js
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
const fs = require('node:fs');
|
|
4
4
|
const path = require('node:path');
|
|
5
5
|
|
|
6
|
+
const { DEFAULT_SCALE, TAILWIND_DEFAULT_SCALE } = require('./default-scale');
|
|
7
|
+
|
|
6
8
|
function normalizePresetName(name) {
|
|
7
9
|
if (typeof name !== 'string') {
|
|
8
10
|
return '';
|
|
@@ -24,7 +26,7 @@ function createModularScale({ base, ratio, steps }) {
|
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
const CORE_SCALE_PRESETS = Object.freeze({
|
|
27
|
-
'rhythmic-4':
|
|
29
|
+
'rhythmic-4': DEFAULT_SCALE,
|
|
28
30
|
'rhythmic-8': Object.freeze([0, 8, 16, 24, 32, 40, 48, 64, 80, 96]),
|
|
29
31
|
'product-material-8dp': Object.freeze([0, 4, 8, 12, 16, 24, 32, 40, 48, 56, 64, 72, 80]),
|
|
30
32
|
'product-atlassian-8px': Object.freeze([0, 2, 4, 6, 8, 12, 16, 20, 24, 32, 40, 48, 64, 80]),
|
|
@@ -41,6 +43,8 @@ const CORE_SCALE_PRESETS = Object.freeze({
|
|
|
41
43
|
'modular-augmented-fourth': Object.freeze(createModularScale({ base: 4, ratio: 1.41421356237, steps: 12 })),
|
|
42
44
|
'modular-perfect-fourth': Object.freeze(createModularScale({ base: 4, ratio: 4 / 3, steps: 12 })),
|
|
43
45
|
'modular-perfect-fifth': Object.freeze(createModularScale({ base: 4, ratio: 1.5, steps: 12 })),
|
|
46
|
+
// Tailwind's default spacing ladder on a 16px root: p-0.5 to p-96.
|
|
47
|
+
tailwind: TAILWIND_DEFAULT_SCALE,
|
|
44
48
|
});
|
|
45
49
|
|
|
46
50
|
const CORE_PRESET_ALIASES = Object.freeze({
|
|
@@ -112,6 +116,7 @@ function loadCommunityScales() {
|
|
|
112
116
|
try {
|
|
113
117
|
parsed = JSON.parse(fs.readFileSync(absolutePath, 'utf8'));
|
|
114
118
|
} catch {
|
|
119
|
+
// Shipped data checked by npm run scales:validate; an unreadable file is skipped.
|
|
115
120
|
continue;
|
|
116
121
|
}
|
|
117
122
|
|
|
@@ -31,13 +31,14 @@ const {
|
|
|
31
31
|
|
|
32
32
|
const {
|
|
33
33
|
collectTokenDefinitions,
|
|
34
|
+
loaderWarningsNote,
|
|
34
35
|
withResolvedScale,
|
|
35
36
|
} = require('../../core/scale-inference');
|
|
36
37
|
|
|
37
38
|
const { validatePrimary, validateNoOffscaleTransformSecondaryOptions } = require('../validate');
|
|
38
39
|
|
|
39
40
|
const { createTokenRegex, reportInvalidPreset, reportProblem, reportValueNode } = require('../report');
|
|
40
|
-
const { decisionFor, loadRcDecisions } = require('../../core/decisions');
|
|
41
|
+
const { decisionFor, loadRcDecisionWarnings, loadRcDecisions } = require('../../core/decisions');
|
|
41
42
|
const { replacementFor, tokenHoldsNote, tokenIndexFromDefinitions } = require('../../core/token-index');
|
|
42
43
|
|
|
43
44
|
const ruleName = 'rhythmguard/no-offscale-transform';
|
|
@@ -80,6 +81,17 @@ const ruleFunction = (primary, secondaryOptions) => {
|
|
|
80
81
|
}
|
|
81
82
|
|
|
82
83
|
withResolvedScale(options, root);
|
|
84
|
+
// An rc that could not be read, or a token source that failed while another
|
|
85
|
+
// gave the scale, is named on the first message in the file, once (cq-05).
|
|
86
|
+
let pendingNote = loaderWarningsNote(
|
|
87
|
+
options.readDecisions ? loadRcDecisionWarnings(process.cwd(), { baseFontSize: options.baseFontSize }) : [],
|
|
88
|
+
options.scaleInference,
|
|
89
|
+
);
|
|
90
|
+
const takeNote = () => {
|
|
91
|
+
const note = [pendingNote, options.note].filter(Boolean).join(' ');
|
|
92
|
+
pendingNote = '';
|
|
93
|
+
return note;
|
|
94
|
+
};
|
|
83
95
|
const tokenRegex = createTokenRegex(options.tokenPattern, { result, root, ruleName });
|
|
84
96
|
if (options.fixWith === 'token') {
|
|
85
97
|
options.tokenIndex = tokenIndexFromDefinitions(
|
|
@@ -115,7 +127,7 @@ const ruleFunction = (primary, secondaryOptions) => {
|
|
|
115
127
|
node.value,
|
|
116
128
|
formatLength(nearest.lower, nearestUnit),
|
|
117
129
|
formatLength(nearest.upper, nearestUnit),
|
|
118
|
-
[tokenHoldsNote(fixedValue, formatLength(nearest.nearest, nearestUnit), node.value.trim().startsWith('-')),
|
|
130
|
+
[tokenHoldsNote(fixedValue, formatLength(nearest.nearest, nearestUnit), node.value.trim().startsWith('-')), takeNote()].filter(Boolean).join(' '),
|
|
119
131
|
position,
|
|
120
132
|
position ? pxAside(node.value, options.baseFontSize) : '',
|
|
121
133
|
),
|
|
@@ -128,7 +140,7 @@ const ruleFunction = (primary, secondaryOptions) => {
|
|
|
128
140
|
|
|
129
141
|
// A value the options forbid outright says so, with no fix (issue #274).
|
|
130
142
|
const reportForbidden = (node, message, position) => {
|
|
131
|
-
reportValueNode({ decl, message: message(node.value,
|
|
143
|
+
reportValueNode({ decl, message: message(node.value, takeNote(), position), node, result, ruleName });
|
|
132
144
|
};
|
|
133
145
|
|
|
134
146
|
const checkNode = (node, avoidTokens = [], position = null) => {
|
|
@@ -190,7 +202,7 @@ const ruleFunction = (primary, secondaryOptions) => {
|
|
|
190
202
|
}
|
|
191
203
|
|
|
192
204
|
const fixedValue = options.fixToScale
|
|
193
|
-
? replacementFor(parsedLength, nearest.nearest, options, decl.prop, avoidTokens)
|
|
205
|
+
? replacementFor(parsedLength, nearest.nearest, options, decl.prop, avoidTokens, decl)
|
|
194
206
|
: null;
|
|
195
207
|
|
|
196
208
|
report(node, nearest, unit, fixedValue, position);
|
|
@@ -214,7 +226,7 @@ const ruleFunction = (primary, secondaryOptions) => {
|
|
|
214
226
|
}
|
|
215
227
|
|
|
216
228
|
const fixedValue = options.fixToScale
|
|
217
|
-
? replacementFor(parsedLength, nearest.nearest, options, decl.prop, avoidTokens)
|
|
229
|
+
? replacementFor(parsedLength, nearest.nearest, options, decl.prop, avoidTokens, decl)
|
|
218
230
|
: null;
|
|
219
231
|
|
|
220
232
|
report(node, nearest, 'px', fixedValue, position);
|