@greenharness/greenrack-eslint-plugin 0.0.0-reserved → 7.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.
- package/LICENSE +21 -0
- package/README.md +194 -1
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/index.d.ts +36 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +23 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/no-deprecated-table-parts.d.ts +17 -0
- package/dist/rules/no-deprecated-table-parts.d.ts.map +1 -0
- package/dist/rules/no-deprecated-table-parts.js +92 -0
- package/dist/rules/no-deprecated-table-parts.js.map +1 -0
- package/dist/rules/no-flex-grow-web.d.ts +22 -0
- package/dist/rules/no-flex-grow-web.d.ts.map +1 -0
- package/dist/rules/no-flex-grow-web.js +38 -0
- package/dist/rules/no-flex-grow-web.js.map +1 -0
- package/dist/rules/no-raw-breakpoint-query.d.ts +16 -0
- package/dist/rules/no-raw-breakpoint-query.d.ts.map +1 -0
- package/dist/rules/no-raw-breakpoint-query.js +78 -0
- package/dist/rules/no-raw-breakpoint-query.js.map +1 -0
- package/dist/rules/no-raw-design-values.d.ts +60 -0
- package/dist/rules/no-raw-design-values.d.ts.map +1 -0
- package/dist/rules/no-raw-design-values.js +657 -0
- package/dist/rules/no-raw-design-values.js.map +1 -0
- package/dist/rules/no-raw-html-element.d.ts +24 -0
- package/dist/rules/no-raw-html-element.d.ts.map +1 -0
- package/dist/rules/no-raw-html-element.js +112 -0
- package/dist/rules/no-raw-html-element.js.map +1 -0
- package/dist/rules/no-raw-layer-values.d.ts +28 -0
- package/dist/rules/no-raw-layer-values.d.ts.map +1 -0
- package/dist/rules/no-raw-layer-values.js +185 -0
- package/dist/rules/no-raw-layer-values.js.map +1 -0
- package/dist/rules/no-tokens-barrel-import.d.ts +17 -0
- package/dist/rules/no-tokens-barrel-import.d.ts.map +1 -0
- package/dist/rules/no-tokens-barrel-import.js +0 -0
- package/dist/rules/no-tokens-barrel-import.js.map +1 -0
- package/dist/rules/prefer-ds-form-components.d.ts +11 -0
- package/dist/rules/prefer-ds-form-components.d.ts.map +1 -0
- package/dist/rules/prefer-ds-form-components.js +92 -0
- package/dist/rules/prefer-ds-form-components.js.map +1 -0
- package/dist/util/glob.d.ts +5 -0
- package/dist/util/glob.d.ts.map +1 -0
- package/dist/util/glob.js +55 -0
- package/dist/util/glob.js.map +1 -0
- package/dist/util/imports.d.ts +20 -0
- package/dist/util/imports.d.ts.map +1 -0
- package/dist/util/imports.js +74 -0
- package/dist/util/imports.js.map +1 -0
- package/dist/util/jsx.d.ts +14 -0
- package/dist/util/jsx.d.ts.map +1 -0
- package/dist/util/jsx.js +121 -0
- package/dist/util/jsx.js.map +1 -0
- package/dist/util/tokens.d.ts +74 -0
- package/dist/util/tokens.d.ts.map +1 -0
- package/dist/util/tokens.js +510 -0
- package/dist/util/tokens.js.map +1 -0
- package/package.json +57 -4
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import { BORDER_SHORTHAND_PROPERTIES, BORDER_TOKENS_BY_PX, BORDER_WIDTH_PROPERTIES, BORDER_WIDTH_TOKENS, BREAKPOINT_TOKENS_BY_PX, COLOR_TOKENS, COLOR_TOKENS_NORMALIZED, DURATION_TOKENS_BY_MS, EASING_TOKENS, FONT_SIZE_TOKENS_BY_REM, FONT_WEIGHT_TOKENS, MICRO_SPACING_TOKENS_BY_PX, OUTLINE_OFFSET_TOKENS, OUTLINE_WIDTH_TOKENS, RADII_PROPERTIES, RADII_TOKENS_BY_PX, SHADOW_TOKENS, SIZE_PROPERTIES, SIZE_TOKENS_BY_PX, SPACING_PROPERTIES, SPACING_TOKENS_BY_PX, normalizeHex, normalizeValue, } from '../util/tokens.js';
|
|
2
|
+
import { ensureNamedImport } from '../util/imports.js';
|
|
3
|
+
import { matchesAnyGlob } from '../util/glob.js';
|
|
4
|
+
const HEX_RE = /#(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{6}|[0-9a-fA-F]{3,4})\b/;
|
|
5
|
+
const FUNC_COLOR_RE = /\b(?:rgba?|hsla?)\([^)]*\)/;
|
|
6
|
+
const BREAKPOINT_KEY_RE = /^@(?:media|container)\b.*\b(?:min|max)-(?:width|height)\s*:\s*(\d+)px/;
|
|
7
|
+
const TOKENS_PKG = '@greenharness/greenrack-tokens';
|
|
8
|
+
const FONT_SIZE_PROPERTIES = new Set(['fontSize']);
|
|
9
|
+
const FONT_WEIGHT_PROPERTIES = new Set(['fontWeight']);
|
|
10
|
+
const SHADOW_PROPERTIES = new Set(['boxShadow']);
|
|
11
|
+
const DURATION_PROPERTIES = new Set(['transitionDuration', 'animationDuration']);
|
|
12
|
+
const EASING_PROPERTIES = new Set(['transitionTimingFunction', 'animationTimingFunction']);
|
|
13
|
+
const PX_RE = /^(-?)(\d+(?:\.\d+)?)px$/;
|
|
14
|
+
const STATIC_PX_RE = /\d+(?:\.\d+)?px/;
|
|
15
|
+
const TRACK_PROPERTIES = new Set([
|
|
16
|
+
'gridTemplateColumns',
|
|
17
|
+
'gridTemplateRows',
|
|
18
|
+
'gridAutoColumns',
|
|
19
|
+
'gridAutoRows',
|
|
20
|
+
]);
|
|
21
|
+
const WIDTH_PROPERTIES = new Set([
|
|
22
|
+
'width',
|
|
23
|
+
'minWidth',
|
|
24
|
+
'maxWidth',
|
|
25
|
+
'inlineSize',
|
|
26
|
+
'minInlineSize',
|
|
27
|
+
'maxInlineSize',
|
|
28
|
+
]);
|
|
29
|
+
const HEIGHT_PROPERTIES = new Set([
|
|
30
|
+
'height',
|
|
31
|
+
'minHeight',
|
|
32
|
+
'maxHeight',
|
|
33
|
+
'blockSize',
|
|
34
|
+
'minBlockSize',
|
|
35
|
+
'maxBlockSize',
|
|
36
|
+
]);
|
|
37
|
+
/**
|
|
38
|
+
* The axis a size token names: `…W`/`…MinW`/`…MaxW` is a width and
|
|
39
|
+
* `…H`/`…MinH`/`…MaxH` a height, with an optional step after it
|
|
40
|
+
* (`popoverWSm`, `listMaxHSm`). Anything else (`touchTarget`, `gridColSm`)
|
|
41
|
+
* fits both.
|
|
42
|
+
*/
|
|
43
|
+
function tokenAxis(token) {
|
|
44
|
+
const match = /[a-z](?:Min|Max)?([WH])(?:Xs|Sm|Md|Lg|Xl)?$/.exec(token);
|
|
45
|
+
if (!match)
|
|
46
|
+
return null;
|
|
47
|
+
return match[1] === 'W' ? 'width' : 'height';
|
|
48
|
+
}
|
|
49
|
+
function propertyAxis(property) {
|
|
50
|
+
if (WIDTH_PROPERTIES.has(property))
|
|
51
|
+
return 'width';
|
|
52
|
+
if (HEIGHT_PROPERTIES.has(property))
|
|
53
|
+
return 'height';
|
|
54
|
+
return null; // flexBasis runs along either axis
|
|
55
|
+
}
|
|
56
|
+
const entriesOf = (table) => Object.entries(table).flatMap(([px, tokens]) => tokens.map((token) => [token, Number(px)]));
|
|
57
|
+
const borderTokens = (allowed) => entriesOf(BORDER_TOKENS_BY_PX).filter(([token]) => allowed.includes(token));
|
|
58
|
+
/** Which token set a measure property draws from; null for any other property. */
|
|
59
|
+
function measureFor(property) {
|
|
60
|
+
if (SIZE_PROPERTIES.has(property)) {
|
|
61
|
+
const axis = propertyAxis(property);
|
|
62
|
+
const tokens = entriesOf(SIZE_TOKENS_BY_PX).filter(([token]) => {
|
|
63
|
+
const own = tokenAxis(token);
|
|
64
|
+
return own === null || axis === null || own === axis;
|
|
65
|
+
});
|
|
66
|
+
return { group: 'sizes', tokens };
|
|
67
|
+
}
|
|
68
|
+
if (property === 'outlineOffset') {
|
|
69
|
+
return { group: 'borders', tokens: borderTokens(OUTLINE_OFFSET_TOKENS) };
|
|
70
|
+
}
|
|
71
|
+
if (property === 'outlineWidth' || property === 'outline') {
|
|
72
|
+
return { group: 'borders', tokens: borderTokens(OUTLINE_WIDTH_TOKENS) };
|
|
73
|
+
}
|
|
74
|
+
if (BORDER_WIDTH_PROPERTIES.has(property) || BORDER_SHORTHAND_PROPERTIES.has(property)) {
|
|
75
|
+
return { group: 'borders', tokens: borderTokens(BORDER_WIDTH_TOKENS) };
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
/** Tokens of `measure` at exactly `px`, in key order. */
|
|
80
|
+
function candidatesAt(measure, px) {
|
|
81
|
+
return measure.tokens.filter(([, value]) => value === px).map(([token]) => token);
|
|
82
|
+
}
|
|
83
|
+
/** How far a value may sit from a token for that token to be its nearest role. */
|
|
84
|
+
const NEAREST_ROLE_TOLERANCE = 0.1;
|
|
85
|
+
/**
|
|
86
|
+
* The tokens at the nearest value within ±10% of `px`, as `[value, tokens]`
|
|
87
|
+
* pairs: one pair, or two (smaller value first) when two values are equally
|
|
88
|
+
* near. Empty when nothing is that close.
|
|
89
|
+
*/
|
|
90
|
+
function nearestRoles(measure, px) {
|
|
91
|
+
const limit = px * NEAREST_ROLE_TOLERANCE;
|
|
92
|
+
const values = [...new Set(measure.tokens.map(([, value]) => value))];
|
|
93
|
+
const near = values.filter((value) => Math.abs(value - px) <= limit);
|
|
94
|
+
if (near.length === 0)
|
|
95
|
+
return [];
|
|
96
|
+
const best = Math.min(...near.map((value) => Math.abs(value - px)));
|
|
97
|
+
return near
|
|
98
|
+
.filter((value) => Math.abs(value - px) === best)
|
|
99
|
+
.sort((a, b) => a - b)
|
|
100
|
+
.map((value) => [value, candidatesAt(measure, value)]);
|
|
101
|
+
}
|
|
102
|
+
const scale = (table) => Object.keys(table)
|
|
103
|
+
.map(Number)
|
|
104
|
+
.sort((a, b) => a - b)
|
|
105
|
+
.join(' / ');
|
|
106
|
+
/**
|
|
107
|
+
* Inside css.create() style objects, flag raw design values — anything with a
|
|
108
|
+
* token equivalent, and anything on a tokenised property that is off the
|
|
109
|
+
* scale:
|
|
110
|
+
*
|
|
111
|
+
* - colors: a hex / rgb() / hsl() literal anywhere. A palette value suggests
|
|
112
|
+
* its token; an off-palette one reports without a fix.
|
|
113
|
+
* - spacing and radius: a px value on the properties where that mapping is
|
|
114
|
+
* unambiguous. On the scale → suggest the token; off the scale (`marginTop:
|
|
115
|
+
* 23`) → report, because "not a token" is the finding.
|
|
116
|
+
* - breakpoints: a px inside a `@media` / `@container` condition key. On the
|
|
117
|
+
* scale → suggest the `breakpoints.<key>` template-literal key.
|
|
118
|
+
* - font size (px or rem), font weight, box-shadow, transition/animation
|
|
119
|
+
* duration and timing function: a literal suggests its token when one
|
|
120
|
+
* matches, else reports.
|
|
121
|
+
*
|
|
122
|
+
* - sizes and border widths: a px length on width/height (and min/max,
|
|
123
|
+
* flexBasis, block/inline size), on border*Width / outlineWidth /
|
|
124
|
+
* outlineOffset and on the width inside a border* / outline shorthand,
|
|
125
|
+
* negatives included. Each property has its own candidate tokens (sizes.*,
|
|
126
|
+
* borders hairline/strong/accent, focusRing, focusOffset/focusOffsetSm),
|
|
127
|
+
* filtered by axis: a `…W` token is only offered on a width property and a
|
|
128
|
+
* `…H` token on a height one. One match suggests it; several give one
|
|
129
|
+
* suggestion each; none names the nearest role (every token at the nearest
|
|
130
|
+
* value within ±10%, both values smaller first on a tie), or says to add a
|
|
131
|
+
* token when nothing is that close. Percent, viewport, em/rem/ch, keywords
|
|
132
|
+
* and calc() without a px are fine.
|
|
133
|
+
* - box-shadow in a template literal: a px length in its static text is a raw
|
|
134
|
+
* shadow (`inset 0 0 0 1px ${colors.border}`); use a `shadows.*` token.
|
|
135
|
+
*
|
|
136
|
+
* - shorthands: on a spacing or radius property a multi-word string
|
|
137
|
+
* ('4px 8px') is split on whitespace and each nonzero px word is checked
|
|
138
|
+
* as a single value, without a suggestion (a swap would rewrite the whole
|
|
139
|
+
* string). 0, auto, %, rem, em, var() and calc() words pass.
|
|
140
|
+
* - inline styles: a `style={{...}}` object literal on any JSX element is
|
|
141
|
+
* walked like a css.create style, without suggestions (plain React has no
|
|
142
|
+
* token imports). References, arrays, spreads, custom-property keys and
|
|
143
|
+
* computed keys are ignored. Option `inlineStyle: false` turns it off.
|
|
144
|
+
*
|
|
145
|
+
* Option `exemptFiles` (globs) silences the rule for whole files.
|
|
146
|
+
*
|
|
147
|
+
* Skipped on purpose: 0, negatives on the spacing properties, transitions
|
|
148
|
+
* ('opacity 150ms'), identifiers, member expressions, template literals and
|
|
149
|
+
* calls — the rule reads literals, not expressions (the size, border-width
|
|
150
|
+
* and box-shadow properties also read static px text in template literals).
|
|
151
|
+
*/
|
|
152
|
+
export const noRawDesignValues = {
|
|
153
|
+
defaultOptions: [{}],
|
|
154
|
+
meta: {
|
|
155
|
+
type: 'suggestion',
|
|
156
|
+
docs: {
|
|
157
|
+
description: 'Prefer design tokens over raw colors, lengths, sizes, border widths, breakpoints, type, shadows and motion in css.create styles',
|
|
158
|
+
},
|
|
159
|
+
hasSuggestions: true,
|
|
160
|
+
schema: [
|
|
161
|
+
{
|
|
162
|
+
type: 'object',
|
|
163
|
+
properties: {
|
|
164
|
+
factories: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
165
|
+
spacingProperties: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
166
|
+
radiiProperties: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
167
|
+
exemptFiles: { type: 'array', items: { type: 'string' }, uniqueItems: true },
|
|
168
|
+
inlineStyle: { type: 'boolean' },
|
|
169
|
+
},
|
|
170
|
+
additionalProperties: false,
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
messages: {
|
|
174
|
+
rawColorToken: "'{{value}}' is the {{token}} token. Use `colors.{{token}}` from {{pkg}}/tokens/colors.css so it follows the theme.",
|
|
175
|
+
rawColor: "'{{value}}' is not in the palette. Use a semantic token from {{pkg}}/tokens/colors.css instead of a raw color.",
|
|
176
|
+
rawSpacing: '{{value}} on `{{property}}` is the {{token}} spacing token. Use `spacing.{{token}}` from {{pkg}}/tokens/spacing.css.',
|
|
177
|
+
rawRadius: '{{value}} on `{{property}}` is the {{token}} radius token. Use `radii.{{token}}` from {{pkg}}/tokens/spacing.css.',
|
|
178
|
+
rawMicroSpacing: '{{value}} on `{{property}}` is off the spacing scale; it is the {{token}} micro-spacing token. Use `microSpacing.{{token}}` from {{pkg}}/tokens/spacing.css, or the scale if the nudge is not needed.',
|
|
179
|
+
offScaleSpacing: '{{value}} on `{{property}}` is off the spacing scale ({{scale}}px). Use the nearest `spacing.*` token from {{pkg}}/tokens/spacing.css, or disable this line with the reason.',
|
|
180
|
+
offScaleRadius: '{{value}} on `{{property}}` is off the radius scale ({{scale}}px). Use the nearest `radii.*` token from {{pkg}}/tokens/spacing.css, or disable this line with the reason.',
|
|
181
|
+
rawBreakpoint: '{{value}}px in `{{key}}` is a raw breakpoint. Use `breakpoints.{{token}}` from {{pkg}}/tokens/breakpoints.css in a template-literal key (the scale is {{scale}}px).',
|
|
182
|
+
rawFontSize: '{{value}} on `fontSize` is the {{group}}.{{token}} token. Use it from {{pkg}}/tokens/typography.css.',
|
|
183
|
+
rawFontWeight: '{{value}} on `fontWeight` is the typography.{{token}} token. Use it from {{pkg}}/tokens/typography.css.',
|
|
184
|
+
rawShadow: "'{{value}}' on `boxShadow` is a raw shadow. Use `shadows.{{token}}` from {{pkg}}/tokens/shadows.css.",
|
|
185
|
+
rawDuration: "'{{value}}' on `{{property}}` is a raw duration. Use `duration.{{token}}` from {{pkg}}/tokens/motion.css.",
|
|
186
|
+
rawEasing: "'{{value}}' on `{{property}}` is a raw easing. Use `easing.{{token}}` from {{pkg}}/tokens/motion.css.",
|
|
187
|
+
rawMeasure: '{{value}} on `{{property}}` is the {{tokens}} token. Use it from {{pkg}}/tokens/{{group}}.css.',
|
|
188
|
+
ambiguousMeasure: '{{value}} on `{{property}}` matches several {{group}} tokens ({{tokens}}). Pick the one that says what this measure is, from {{pkg}}/tokens/{{group}}.css.',
|
|
189
|
+
missingMeasureToken: '{{value}} on `{{property}}` has no token. Add a token (design-system.a-missing-token-is-added-not-approximated) instead of using a raw value.',
|
|
190
|
+
nearestMeasureRole: '{{value}} on `{{property}}` has no token; nearest role: {{nearest}}. If this measure plays that role, use its token and take its value; if not, add a token (design-system.a-missing-token-is-added-not-approximated).',
|
|
191
|
+
rawShadowLength: '{{value}} on `boxShadow` builds a shadow from a raw px length. Use a `shadows.*` token from {{pkg}}/tokens/shadows.css.',
|
|
192
|
+
rawTrack: '{{value}} on `{{property}}` sizes a track in px. Put a size token inside it: `minmax(${sizes.gridColSm}, 1fr)` from {{pkg}}/tokens/sizes.css.',
|
|
193
|
+
replaceWithToken: 'Replace with {{replacement}}',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
create(context) {
|
|
197
|
+
const options = context.options[0] ?? {};
|
|
198
|
+
const factories = options.factories ?? ['css.create'];
|
|
199
|
+
const spacingProperties = options.spacingProperties
|
|
200
|
+
? new Set(options.spacingProperties)
|
|
201
|
+
: SPACING_PROPERTIES;
|
|
202
|
+
const radiiProperties = options.radiiProperties
|
|
203
|
+
? new Set(options.radiiProperties)
|
|
204
|
+
: RADII_PROPERTIES;
|
|
205
|
+
const sourceCode = context.sourceCode;
|
|
206
|
+
const exemptFiles = options.exemptFiles ?? [];
|
|
207
|
+
const inlineStyle = options.inlineStyle ?? true;
|
|
208
|
+
if (matchesAnyGlob(context.filename, context.cwd, exemptFiles))
|
|
209
|
+
return {};
|
|
210
|
+
function calleeText(node) {
|
|
211
|
+
return sourceCode.getText(node.callee);
|
|
212
|
+
}
|
|
213
|
+
/** A suggestion that swaps `node` for `<importName>.<token>`, importing it. */
|
|
214
|
+
function suggestReplacement(node, replacement, importName, importPath, render = (local) => replacement.replace(`${importName}.`, `${local}.`)) {
|
|
215
|
+
return [
|
|
216
|
+
{
|
|
217
|
+
messageId: 'replaceWithToken',
|
|
218
|
+
data: { replacement },
|
|
219
|
+
fix(fixer) {
|
|
220
|
+
const { local, fixes } = ensureNamedImport(sourceCode, fixer, `${TOKENS_PKG}/${importPath}`, importName);
|
|
221
|
+
return [...fixes, fixer.replaceText(node, render(local))];
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
];
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* A px length on a size / border-width property. `fixable` is off for the
|
|
228
|
+
* width inside a shorthand, where a swap would have to rewrite the string.
|
|
229
|
+
*/
|
|
230
|
+
function checkMeasure(node, property, measure, px, negative, display, fixable, suggest) {
|
|
231
|
+
if (px === 0)
|
|
232
|
+
return;
|
|
233
|
+
const candidates = candidatesAt(measure, px);
|
|
234
|
+
const data = {
|
|
235
|
+
value: display,
|
|
236
|
+
property,
|
|
237
|
+
group: measure.group,
|
|
238
|
+
tokens: candidates.map((token) => `${measure.group}.${token}`).join(', '),
|
|
239
|
+
pkg: TOKENS_PKG,
|
|
240
|
+
};
|
|
241
|
+
if (candidates.length === 0) {
|
|
242
|
+
const nearest = nearestRoles(measure, px);
|
|
243
|
+
if (nearest.length === 0) {
|
|
244
|
+
context.report({ node, messageId: 'missingMeasureToken', data });
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const text = nearest
|
|
248
|
+
.map(([value, tokens]) => `${tokens.map((token) => `\`${measure.group}.${token}\``).join(', ')} (${value})`)
|
|
249
|
+
.join(', ');
|
|
250
|
+
context.report({ node, messageId: 'nearestMeasureRole', data: { ...data, nearest: text } });
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
const suggestions = fixable && suggest
|
|
254
|
+
? candidates.flatMap((token) => suggestReplacement(node, `${measure.group}.${token}`, measure.group, `tokens/${measure.group}.css`, negative ? (local) => `\`calc(-1 * \${${local}.${token}})\`` : undefined))
|
|
255
|
+
: [];
|
|
256
|
+
context.report({
|
|
257
|
+
node,
|
|
258
|
+
messageId: candidates.length > 1 ? 'ambiguousMeasure' : 'rawMeasure',
|
|
259
|
+
data,
|
|
260
|
+
suggest: suggestions,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/** Static text holding a `<number>px` inside calc() or a template literal. */
|
|
264
|
+
function checkStaticPx(node, property, text, display) {
|
|
265
|
+
if (STATIC_PX_RE.test(text)) {
|
|
266
|
+
context.report({
|
|
267
|
+
node,
|
|
268
|
+
messageId: 'missingMeasureToken',
|
|
269
|
+
data: { value: display, property },
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function checkMeasureString(node, property, measure, value, suggest) {
|
|
274
|
+
const text = value.trim();
|
|
275
|
+
const display = `'${value}'`;
|
|
276
|
+
const px = PX_RE.exec(text);
|
|
277
|
+
if (px) {
|
|
278
|
+
checkMeasure(node, property, measure, Number(px[2]), px[1] === '-', display, true, suggest);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (BORDER_SHORTHAND_PROPERTIES.has(property)) {
|
|
282
|
+
// The width is a bare px word outside any function call.
|
|
283
|
+
const words = text.replace(/\([^)]*\)/g, ' ').split(/\s+/);
|
|
284
|
+
const width = words.map((word) => PX_RE.exec(word)).find(Boolean);
|
|
285
|
+
if (width) {
|
|
286
|
+
checkMeasure(node, property, measure, Number(width[2]), false, display, false, suggest);
|
|
287
|
+
}
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (/\bcalc\(/.test(text))
|
|
291
|
+
checkStaticPx(node, property, text, display);
|
|
292
|
+
}
|
|
293
|
+
function checkColorValue(node, raw, suggest) {
|
|
294
|
+
const match = HEX_RE.exec(raw) ?? FUNC_COLOR_RE.exec(raw);
|
|
295
|
+
if (!match)
|
|
296
|
+
return;
|
|
297
|
+
const literal = match[0];
|
|
298
|
+
const token = literal.startsWith('#')
|
|
299
|
+
? COLOR_TOKENS[normalizeHex(literal)]
|
|
300
|
+
: COLOR_TOKENS_NORMALIZED[normalizeValue(literal)];
|
|
301
|
+
if (token) {
|
|
302
|
+
// Only suggest a direct replacement when the literal IS the color —
|
|
303
|
+
// inside a compound value (a boxShadow) the rewrite would be wrong.
|
|
304
|
+
const isWholeValue = raw.trim() === literal;
|
|
305
|
+
context.report({
|
|
306
|
+
node,
|
|
307
|
+
messageId: 'rawColorToken',
|
|
308
|
+
data: { value: literal, token, pkg: TOKENS_PKG },
|
|
309
|
+
suggest: isWholeValue && suggest
|
|
310
|
+
? suggestReplacement(node, `colors.${token}`, 'colors', 'tokens/colors.css')
|
|
311
|
+
: [],
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
context.report({ node, messageId: 'rawColor', data: { value: literal, pkg: TOKENS_PKG } });
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function checkPxValue(node, property, px, display, suggest) {
|
|
319
|
+
if (spacingProperties.has(property)) {
|
|
320
|
+
const token = SPACING_TOKENS_BY_PX[px];
|
|
321
|
+
const micro = MICRO_SPACING_TOKENS_BY_PX[px];
|
|
322
|
+
if (!token && micro) {
|
|
323
|
+
context.report({
|
|
324
|
+
node,
|
|
325
|
+
messageId: 'rawMicroSpacing',
|
|
326
|
+
data: { value: display, property, token: micro, pkg: TOKENS_PKG },
|
|
327
|
+
suggest: suggest
|
|
328
|
+
? suggestReplacement(node, `microSpacing.${micro}`, 'microSpacing', 'tokens/spacing.css')
|
|
329
|
+
: [],
|
|
330
|
+
});
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
if (!token) {
|
|
334
|
+
context.report({
|
|
335
|
+
node,
|
|
336
|
+
messageId: 'offScaleSpacing',
|
|
337
|
+
data: { value: display, property, scale: scale(SPACING_TOKENS_BY_PX), pkg: TOKENS_PKG },
|
|
338
|
+
});
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
context.report({
|
|
342
|
+
node,
|
|
343
|
+
messageId: 'rawSpacing',
|
|
344
|
+
data: { value: display, property, token, pkg: TOKENS_PKG },
|
|
345
|
+
suggest: suggest
|
|
346
|
+
? suggestReplacement(node, `spacing.${token}`, 'spacing', 'tokens/spacing.css')
|
|
347
|
+
: [],
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
else if (radiiProperties.has(property)) {
|
|
351
|
+
const token = RADII_TOKENS_BY_PX[px];
|
|
352
|
+
if (!token) {
|
|
353
|
+
context.report({
|
|
354
|
+
node,
|
|
355
|
+
messageId: 'offScaleRadius',
|
|
356
|
+
data: { value: display, property, scale: scale(RADII_TOKENS_BY_PX), pkg: TOKENS_PKG },
|
|
357
|
+
});
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
context.report({
|
|
361
|
+
node,
|
|
362
|
+
messageId: 'rawRadius',
|
|
363
|
+
data: { value: display, property, token, pkg: TOKENS_PKG },
|
|
364
|
+
suggest: suggest
|
|
365
|
+
? suggestReplacement(node, `radii.${token}`, 'radii', 'tokens/spacing.css')
|
|
366
|
+
: [],
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
else if (FONT_SIZE_PROPERTIES.has(property)) {
|
|
370
|
+
checkFontSize(node, px / 16, display, suggest);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function checkFontSize(node, rem, display, suggest) {
|
|
374
|
+
const hit = FONT_SIZE_TOKENS_BY_REM[rem];
|
|
375
|
+
if (!hit)
|
|
376
|
+
return;
|
|
377
|
+
context.report({
|
|
378
|
+
node,
|
|
379
|
+
messageId: 'rawFontSize',
|
|
380
|
+
data: { value: display, group: hit.group, token: hit.token, pkg: TOKENS_PKG },
|
|
381
|
+
suggest: suggest
|
|
382
|
+
? suggestReplacement(node, `${hit.group}.${hit.token}`, hit.group, 'tokens/typography.css')
|
|
383
|
+
: [],
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
function checkFontWeight(node, weight, display, suggest) {
|
|
387
|
+
const token = FONT_WEIGHT_TOKENS[weight];
|
|
388
|
+
if (!token)
|
|
389
|
+
return;
|
|
390
|
+
context.report({
|
|
391
|
+
node,
|
|
392
|
+
messageId: 'rawFontWeight',
|
|
393
|
+
data: { value: display, token, pkg: TOKENS_PKG },
|
|
394
|
+
suggest: suggest
|
|
395
|
+
? suggestReplacement(node, `typography.${token}`, 'typography', 'tokens/typography.css')
|
|
396
|
+
: [],
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
function checkStringValue(node, property, value, suggest) {
|
|
400
|
+
const px = /^(\d+(?:\.\d+)?)px$/.exec(value);
|
|
401
|
+
const rem = /^(\d+(?:\.\d+)?)rem$/.exec(value);
|
|
402
|
+
const ms = /^(\d+(?:\.\d+)?)ms$/.exec(value);
|
|
403
|
+
const display = `'${value}'`;
|
|
404
|
+
if (px) {
|
|
405
|
+
checkPxValue(node, property, Number(px[1]), display, suggest);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
// A shorthand ('4px 8px', '6px 14px'): every nonzero px word is a value of
|
|
409
|
+
// its own. 0, auto, %, rem, var() and calc() words are not px words and
|
|
410
|
+
// pass. No suggestion: swapping one word would rewrite the whole string.
|
|
411
|
+
if ((spacingProperties.has(property) || radiiProperties.has(property)) &&
|
|
412
|
+
/\s/.test(value.trim())) {
|
|
413
|
+
const words = value
|
|
414
|
+
.replace(/\([^)]*\)/g, ' ')
|
|
415
|
+
.trim()
|
|
416
|
+
.split(/\s+/);
|
|
417
|
+
for (const word of words) {
|
|
418
|
+
const match = /^(\d+(?:\.\d+)?)px$/.exec(word);
|
|
419
|
+
if (match && Number(match[1]) > 0) {
|
|
420
|
+
checkPxValue(node, property, Number(match[1]), `'${word}'`, false);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (rem && FONT_SIZE_PROPERTIES.has(property)) {
|
|
426
|
+
checkFontSize(node, Number(rem[1]), display, suggest);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (FONT_WEIGHT_PROPERTIES.has(property) && /^\d{3}$/.test(value)) {
|
|
430
|
+
checkFontWeight(node, Number(value), display, suggest);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
if (SHADOW_PROPERTIES.has(property)) {
|
|
434
|
+
if (value === 'none')
|
|
435
|
+
return;
|
|
436
|
+
const token = SHADOW_TOKENS[normalizeValue(value)];
|
|
437
|
+
context.report({
|
|
438
|
+
node,
|
|
439
|
+
messageId: 'rawShadow',
|
|
440
|
+
data: { value, token: token ?? '*', pkg: TOKENS_PKG },
|
|
441
|
+
suggest: token && suggest
|
|
442
|
+
? suggestReplacement(node, `shadows.${token}`, 'shadows', 'tokens/shadows.css')
|
|
443
|
+
: [],
|
|
444
|
+
});
|
|
445
|
+
// A palette shadow's own rgba() is the token, not a second finding.
|
|
446
|
+
if (!token)
|
|
447
|
+
checkColorValue(node, value, suggest);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (DURATION_PROPERTIES.has(property) && ms) {
|
|
451
|
+
const token = DURATION_TOKENS_BY_MS[Number(ms[1])];
|
|
452
|
+
context.report({
|
|
453
|
+
node,
|
|
454
|
+
messageId: 'rawDuration',
|
|
455
|
+
data: { value, property, token: token ?? '*', pkg: TOKENS_PKG },
|
|
456
|
+
suggest: token && suggest
|
|
457
|
+
? suggestReplacement(node, `duration.${token}`, 'duration', 'tokens/motion.css')
|
|
458
|
+
: [],
|
|
459
|
+
});
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (EASING_PROPERTIES.has(property)) {
|
|
463
|
+
const token = EASING_TOKENS[normalizeValue(value)];
|
|
464
|
+
context.report({
|
|
465
|
+
node,
|
|
466
|
+
messageId: 'rawEasing',
|
|
467
|
+
data: { value, property, token: token ?? '*', pkg: TOKENS_PKG },
|
|
468
|
+
suggest: token && suggest
|
|
469
|
+
? suggestReplacement(node, `easing.${token}`, 'easing', 'tokens/motion.css')
|
|
470
|
+
: [],
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function checkValue(node, property, suggest, inline = false) {
|
|
475
|
+
// Grid tracks: a px anywhere in the track list is a raw size, the same
|
|
476
|
+
// as on width (`'minmax(240px, 1fr)'`). Fractions, % and tokens pass.
|
|
477
|
+
if (property && TRACK_PROPERTIES.has(property)) {
|
|
478
|
+
const text = node.type === 'Literal' && typeof node.value === 'string'
|
|
479
|
+
? node.value
|
|
480
|
+
: node.type === 'TemplateLiteral'
|
|
481
|
+
? node.quasis.map((q) => q.value.cooked ?? q.value.raw).join(' ')
|
|
482
|
+
: null;
|
|
483
|
+
if (text !== null && /(?<![\w.-])(?!0+(?:\.0+)?px)\d+(?:\.\d+)?px\b/.test(text)) {
|
|
484
|
+
context.report({
|
|
485
|
+
node,
|
|
486
|
+
messageId: 'rawTrack',
|
|
487
|
+
data: { value: sourceCode.getText(node), property, pkg: TOKENS_PKG },
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
if (node.type !== 'ObjectExpression')
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
if (property && SHADOW_PROPERTIES.has(property) && node.type === 'TemplateLiteral') {
|
|
494
|
+
const text = node.quasis.map((q) => q.value.cooked ?? q.value.raw).join(' ');
|
|
495
|
+
if (/(?<![\w.-])(?!0+(?:\.0+)?px)\d+(?:\.\d+)?px\b/.test(text)) {
|
|
496
|
+
context.report({
|
|
497
|
+
node,
|
|
498
|
+
messageId: 'rawShadowLength',
|
|
499
|
+
data: { value: sourceCode.getText(node), pkg: TOKENS_PKG },
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
const measure = property ? measureFor(property) : null;
|
|
505
|
+
if (property && measure) {
|
|
506
|
+
if (node.type === 'Literal' && typeof node.value === 'number') {
|
|
507
|
+
checkMeasure(node, property, measure, node.value, false, String(node.value), true, suggest);
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
if (node.type === 'UnaryExpression' &&
|
|
511
|
+
node.operator === '-' &&
|
|
512
|
+
node.argument.type === 'Literal' &&
|
|
513
|
+
typeof node.argument.value === 'number' &&
|
|
514
|
+
!BORDER_SHORTHAND_PROPERTIES.has(property)) {
|
|
515
|
+
const px = node.argument.value;
|
|
516
|
+
checkMeasure(node, property, measure, px, true, `-${px}`, true, suggest);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (node.type === 'TemplateLiteral') {
|
|
520
|
+
const text = node.quasis.map((q) => q.value.cooked ?? q.value.raw).join('');
|
|
521
|
+
checkStaticPx(node, property, text, sourceCode.getText(node));
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (node.type === 'Literal' && typeof node.value === 'string') {
|
|
525
|
+
checkColorValue(node, node.value, suggest);
|
|
526
|
+
checkMeasureString(node, property, measure, node.value, suggest);
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (node.type === 'Literal') {
|
|
531
|
+
if (typeof node.value === 'string') {
|
|
532
|
+
if (property && SHADOW_PROPERTIES.has(property)) {
|
|
533
|
+
checkStringValue(node, property, node.value, suggest);
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
checkColorValue(node, node.value, suggest);
|
|
537
|
+
if (property)
|
|
538
|
+
checkStringValue(node, property, node.value, suggest);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
else if (typeof node.value === 'number' && property && node.value > 0) {
|
|
542
|
+
if (FONT_WEIGHT_PROPERTIES.has(property)) {
|
|
543
|
+
checkFontWeight(node, node.value, String(node.value), suggest);
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
checkPxValue(node, property, node.value, String(node.value), suggest);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (node.type === 'ObjectExpression') {
|
|
552
|
+
walkStyleObject(node, property, suggest, inline);
|
|
553
|
+
}
|
|
554
|
+
// Everything else (identifiers, member expressions, template literals,
|
|
555
|
+
// calls, negatives via UnaryExpression) is deliberately skipped.
|
|
556
|
+
}
|
|
557
|
+
/** A `@media (min-width: 768px)` key: report, and offer the const key. */
|
|
558
|
+
function checkConditionKey(prop, key, suggest) {
|
|
559
|
+
const match = BREAKPOINT_KEY_RE.exec(key);
|
|
560
|
+
if (!match)
|
|
561
|
+
return;
|
|
562
|
+
const px = Number(match[1]);
|
|
563
|
+
const token = BREAKPOINT_TOKENS_BY_PX[px];
|
|
564
|
+
const data = {
|
|
565
|
+
value: String(px),
|
|
566
|
+
key,
|
|
567
|
+
token: token ?? '*',
|
|
568
|
+
scale: scale(BREAKPOINT_TOKENS_BY_PX),
|
|
569
|
+
pkg: TOKENS_PKG,
|
|
570
|
+
};
|
|
571
|
+
if (!token) {
|
|
572
|
+
context.report({ node: prop.key, messageId: 'rawBreakpoint', data });
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
// css.create needs a computed key for a const; brackets included so a
|
|
576
|
+
// plain string key becomes `[`@media (... ${breakpoints.md})`]`.
|
|
577
|
+
const template = key.replace(`${px}px`, `\${%LOCAL%.${token}}`);
|
|
578
|
+
context.report({
|
|
579
|
+
node: prop.key,
|
|
580
|
+
messageId: 'rawBreakpoint',
|
|
581
|
+
data,
|
|
582
|
+
suggest: !suggest
|
|
583
|
+
? []
|
|
584
|
+
: suggestReplacement(prop.key, `breakpoints.${token}`, 'breakpoints', 'tokens/breakpoints.css', (local) => {
|
|
585
|
+
const inner = '`' + template.replace('%LOCAL%', local) + '`';
|
|
586
|
+
return prop.computed ? inner : `[${inner}]`;
|
|
587
|
+
}),
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
function walkStyleObject(obj, property, suggest, inline = false) {
|
|
591
|
+
for (const prop of obj.properties) {
|
|
592
|
+
if (prop.type !== 'Property')
|
|
593
|
+
continue;
|
|
594
|
+
// An inline style's computed keys and custom properties (`--wb-vw`) hold
|
|
595
|
+
// no design value we can attribute.
|
|
596
|
+
if (inline && prop.computed)
|
|
597
|
+
continue;
|
|
598
|
+
const key = prop.key.type === 'Identifier'
|
|
599
|
+
? prop.key.name
|
|
600
|
+
: prop.key.type === 'Literal'
|
|
601
|
+
? String(prop.key.value)
|
|
602
|
+
: null;
|
|
603
|
+
if (inline && key !== null && key.startsWith('--'))
|
|
604
|
+
continue;
|
|
605
|
+
if (key === null) {
|
|
606
|
+
// Computed key — can't attribute a property; still scan for colors.
|
|
607
|
+
// (Deliberately css.create only: inline computed keys are skipped above.)
|
|
608
|
+
checkValue(prop.value, null, suggest, inline);
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
// Condition keys (default, :hover, @media …) keep the enclosing
|
|
612
|
+
// property; anything else IS the property.
|
|
613
|
+
const isCondition = key === 'default' || /^[:@]/.test(key);
|
|
614
|
+
if (isCondition)
|
|
615
|
+
checkConditionKey(prop, key, suggest);
|
|
616
|
+
checkValue(prop.value, isCondition ? property : key, suggest, inline);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return {
|
|
620
|
+
JSXAttribute(node) {
|
|
621
|
+
if (!inlineStyle)
|
|
622
|
+
return;
|
|
623
|
+
if (node.name.type !== 'JSXIdentifier' || node.name.name !== 'style')
|
|
624
|
+
return;
|
|
625
|
+
const container = node.value;
|
|
626
|
+
if (container?.type !== 'JSXExpressionContainer')
|
|
627
|
+
return;
|
|
628
|
+
// style={styles.x}, arrays and identifiers are not literals to read.
|
|
629
|
+
if (container.expression.type !== 'ObjectExpression')
|
|
630
|
+
return;
|
|
631
|
+
// No suggestion: plain-React consumers have no .css.ts token imports.
|
|
632
|
+
walkStyleObject(container.expression, null, false, true);
|
|
633
|
+
},
|
|
634
|
+
CallExpression(node) {
|
|
635
|
+
if (!factories.includes(calleeText(node)))
|
|
636
|
+
return;
|
|
637
|
+
const arg = node.arguments[0];
|
|
638
|
+
if (!arg || arg.type !== 'ObjectExpression')
|
|
639
|
+
return;
|
|
640
|
+
// Top level is the style-name level: css.create({styleName: {...}}).
|
|
641
|
+
for (const prop of arg.properties) {
|
|
642
|
+
if (prop.type !== 'Property')
|
|
643
|
+
continue;
|
|
644
|
+
const value = prop.value;
|
|
645
|
+
if (value.type === 'ObjectExpression')
|
|
646
|
+
walkStyleObject(value, null, true);
|
|
647
|
+
// Dynamic styles: css.create({x: (arg) => ({...})})
|
|
648
|
+
if ((value.type === 'ArrowFunctionExpression' || value.type === 'FunctionExpression') &&
|
|
649
|
+
value.body.type === 'ObjectExpression') {
|
|
650
|
+
walkStyleObject(value.body, null, true);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
},
|
|
654
|
+
};
|
|
655
|
+
},
|
|
656
|
+
};
|
|
657
|
+
//# sourceMappingURL=no-raw-design-values.js.map
|