@tensor_1/react 2.64.0 → 2.65.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/README.md +17 -0
- package/component-inventory.json +4 -0
- package/dist/components/AuditLog.d.ts +1 -0
- package/dist/components/LinkTile.d.ts +28 -0
- package/dist/components/LinkTile.js +38 -0
- package/dist/components/StatusPill.d.ts +11 -1
- package/dist/components/StatusPill.js +7 -2
- package/dist/index.cjs +2727 -2676
- package/dist/index.d.cts +46 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -1
- package/dist/usage-roles.d.ts +14 -1
- package/dist/usage-roles.js +11 -0
- package/package.json +9 -3
- package/page-rules/README.md +74 -0
- package/page-rules/cli.mjs +5 -0
- package/page-rules/index.mjs +158 -0
- package/page-rules/rules.mjs +669 -0
- package/page-rules/scan.mjs +281 -0
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
// UIx page rules — the rules, as pure functions over one source file (HAR-3874).
|
|
2
|
+
//
|
|
3
|
+
// The TENSOR UI council climbs (HAR-2954) and the page audit (HAR-3101) kept
|
|
4
|
+
// finding the same page-level shapes on pages nobody had reviewed. UIx's own
|
|
5
|
+
// tests hold the kit-level rules (control edges, hit areas, the calm palette),
|
|
6
|
+
// but a page can still misuse the kit, and no product except TENSOR checked
|
|
7
|
+
// that. These rules are TENSOR's two scanners (scripts/lib/ui-climb-rules.mjs
|
|
8
|
+
// and scripts/lib/kit-control-rules.mjs), ported to UIx names so every product
|
|
9
|
+
// that renders UIx runs them. The five climb rules keep TENSOR's rule ids.
|
|
10
|
+
//
|
|
11
|
+
// This module reads no files: ./scan.mjs walks a product and hands each source
|
|
12
|
+
// here, so the tests feed strings. Workspace lessons 12 and 13 carry the why.
|
|
13
|
+
|
|
14
|
+
/** @typedef {{ rule: string, file: string, line: number, text: string, msg: string }} Finding */
|
|
15
|
+
|
|
16
|
+
export const RULE_SEVERITY_TONE = 'severity-through-status-tone';
|
|
17
|
+
export const RULE_BREAK_ANYWHERE = 'label-break-anywhere';
|
|
18
|
+
export const RULE_HEADING_TWICE = 'heading-said-twice';
|
|
19
|
+
export const RULE_ROW_MENU_UNNAMED = 'row-menu-unnamed';
|
|
20
|
+
export const RULE_LIST_JOINED = 'list-joined-in-cell';
|
|
21
|
+
export const RULE_RAW_BUTTON = 'no-raw-button';
|
|
22
|
+
export const RULE_RAW_TABLE = 'no-raw-table';
|
|
23
|
+
export const RULE_RAW_FORM_CONTROL = 'no-raw-form-control';
|
|
24
|
+
export const RULE_ARBITRARY_FONT_SIZE = 'no-arbitrary-font-size';
|
|
25
|
+
|
|
26
|
+
/** Reported by the baseline and config checks, never by a source file. */
|
|
27
|
+
export const RULE_STALE_BASELINE = 'stale-baseline';
|
|
28
|
+
export const RULE_STALE_EXEMPTION = 'stale-exemption';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Every counted rule, in report order. `ignorable` rules accept an inline
|
|
32
|
+
* `// uix-page-rules-ignore <rule>: <reason>`; the kit-control rules do not,
|
|
33
|
+
* because a case the kit cannot serve is a UIx change first (workspace
|
|
34
|
+
* ADR-0038), not a page exception.
|
|
35
|
+
*/
|
|
36
|
+
export const RULES = Object.freeze({
|
|
37
|
+
[RULE_SEVERITY_TONE]: {
|
|
38
|
+
ignorable: true,
|
|
39
|
+
lesson: 'lessons/12 — severity colour comes from one ramp (critical red, high amber, medium and low neutral)',
|
|
40
|
+
fix: "Colour severity with the kit ramp: `<StatusPill tone={severityTone(severity)}>` (`severityTone` from '@tensor_1/react'). Never map \"high\" to a red tone by hand or through a generic status-tone helper: every High on the screen turns red and spends the red budget on the commonest value.",
|
|
41
|
+
},
|
|
42
|
+
[RULE_BREAK_ANYWHERE]: {
|
|
43
|
+
ignorable: true,
|
|
44
|
+
lesson: 'lessons/12 — text fits by rule: a label never breaks mid-word',
|
|
45
|
+
fix: 'Drop the mid-word break (break-all, overflow-wrap: anywhere, wrap-anywhere, wordBreak/overflowWrap in a style). Let the label wrap at spaces, shorten it, or truncate it whole with the full text in a title or tooltip. Long unbroken text in a narrow column (an e-mail address, a URL, an error message) takes `break-words` (overflow-wrap: break-word) plus `min-w-0` on its flex or grid item. Code in `font-mono`, `<code>` or `<pre>` is exempt.',
|
|
46
|
+
},
|
|
47
|
+
[RULE_HEADING_TWICE]: {
|
|
48
|
+
ignorable: true,
|
|
49
|
+
lesson: 'lessons/13 — say each heading once',
|
|
50
|
+
fix: 'Keep the title in the PageHeader only. A table names itself with an aria-label instead of a visible caption; a section gets its own heading, or none when it is the page’s only content.',
|
|
51
|
+
},
|
|
52
|
+
[RULE_ROW_MENU_UNNAMED]: {
|
|
53
|
+
ignorable: true,
|
|
54
|
+
lesson: 'lessons/13 — a row carries one quiet ⋯ menu named for its row',
|
|
55
|
+
fix: "Name the row in the menu's label or its trigger's aria-label, e.g. `aria-label={`Aktionen: ${row.title}`}` or `label={t('row_actions', { title: row.title })}`. Ten menus all called \"Aktionen\" read as one control.",
|
|
56
|
+
},
|
|
57
|
+
[RULE_LIST_JOINED]: {
|
|
58
|
+
ignorable: true,
|
|
59
|
+
lesson: 'lessons/12 — a long list in a table cell is summarised by the kit, never wrapped',
|
|
60
|
+
fix: 'Render the list with the kit’s `ListSummary` (count, the first three, "und N weitere") or `OverflowRow` instead of joining it with ", ". A joined list wraps the row to many lines.',
|
|
61
|
+
},
|
|
62
|
+
[RULE_RAW_BUTTON]: {
|
|
63
|
+
ignorable: false,
|
|
64
|
+
lesson: 'lessons/12 — staff UI is UIx only; the kit owns the button',
|
|
65
|
+
fix: 'Render the kit control from @tensor_1/react: Button (variant, size, icon, loading), ButtonLink for navigation, Chip for a toggle or filter, Tabs or Segmented for a view switch, MenuItem inside a Menu, RowAction for a row action, ExpandToggle, StarButton or CopyButton. A raw <button> redraws the kit’s focus ring, pending and disabled states and drifts from them. The one allowed case is an invisible submitter (type="submit" hidden aria-hidden). If the kit lacks the look, build it in UIx first and release it.',
|
|
66
|
+
},
|
|
67
|
+
[RULE_RAW_TABLE]: {
|
|
68
|
+
ignorable: false,
|
|
69
|
+
lesson: 'lessons/12 — staff UI is UIx only; a list of records is a kit table',
|
|
70
|
+
fix: 'Render a list of records with DataWorkspace (or the kit Table, Th, Tr and Td inside TableWrap for a matrix) from @tensor_1/react. A printed document that cannot use the screen table is listed under `exempt` in the config with its reason.',
|
|
71
|
+
},
|
|
72
|
+
[RULE_RAW_FORM_CONTROL]: {
|
|
73
|
+
ignorable: false,
|
|
74
|
+
lesson: 'lessons/12 — staff UI is UIx only; the kit owns the form controls',
|
|
75
|
+
fix: 'Render Input, NumberInput, PasswordInput, Textarea, Select, Combobox, Checkbox, Radio, Switch, DatePicker, TimePicker or FileUpload from @tensor_1/react inside a kit Field. A raw control misses the kit’s 3:1 edge, focus ring, invalid state and label wiring. `<input type="hidden">` is allowed.',
|
|
76
|
+
},
|
|
77
|
+
[RULE_ARBITRARY_FONT_SIZE]: {
|
|
78
|
+
ignorable: false,
|
|
79
|
+
lesson: 'lessons/12 — the type scale is the kit’s',
|
|
80
|
+
fix: 'Use the kit type scale: the `--uix-text-<step>` tokens (eyebrow 11, meta 12.5, body 14, h3 15, h2 24, data-hero 34, display 40 px), e.g. `text-[length:var(--uix-text-meta)]` or `fontSize: "var(--uix-text-meta)"`, never a literal size.',
|
|
81
|
+
},
|
|
82
|
+
[RULE_STALE_BASELINE]: {
|
|
83
|
+
ignorable: false,
|
|
84
|
+
lesson: 'the baseline only shrinks',
|
|
85
|
+
fix: 'The debt is paid: run `uix-page-rules baseline` and commit the smaller baseline.',
|
|
86
|
+
},
|
|
87
|
+
[RULE_STALE_EXEMPTION]: {
|
|
88
|
+
ignorable: false,
|
|
89
|
+
lesson: 'exemptions only shrink',
|
|
90
|
+
fix: 'The exempt file no longer holds the shape, or is gone: delete its entry from `exempt` in the config.',
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
/** The rules a source file can break, in report order. */
|
|
95
|
+
export const COUNTED_RULES = Object.freeze(
|
|
96
|
+
Object.keys(RULES).filter((rule) => rule !== RULE_STALE_BASELINE && rule !== RULE_STALE_EXEMPTION),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
/** The rule options a product may set (see ./scan.mjs for the config file). */
|
|
100
|
+
export const DEFAULT_OPTIONS = Object.freeze({
|
|
101
|
+
/** Generic tone helpers that must never colour a severity: `statusTone('severity', …)`. */
|
|
102
|
+
genericToneHelpers: Object.freeze(['statusTone']),
|
|
103
|
+
/** Components whose `title` is the page title. */
|
|
104
|
+
pageTitleComponents: Object.freeze(['PageHeader']),
|
|
105
|
+
/** Components that render a row's ⋯ menu. */
|
|
106
|
+
rowMenuComponents: Object.freeze(['Menu', 'ActionMenu']),
|
|
107
|
+
/** rule → file → reason: a structural exception to one rule. */
|
|
108
|
+
exempt: Object.freeze({}),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// ── lexing ────────────────────────────────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Lex a TSX source just far enough for these rules. Returns
|
|
115
|
+
* code — the source with comment and string text blanked to spaces
|
|
116
|
+
* (newlines kept, so offsets and line numbers stay), which a tag
|
|
117
|
+
* regex can read as code and JSX only;
|
|
118
|
+
* strings — every string and template literal as { start, text }, a
|
|
119
|
+
* template's text parts joined by a space where `${…}` stood.
|
|
120
|
+
* A quote that reaches the end of its line unclosed is JSX text (an apostrophe
|
|
121
|
+
* in prose), not a string: it stays code and lexing goes on after it. A regex
|
|
122
|
+
* literal (`/\/*$/`, `/`/g`) is skipped whole, so its `/*` or backtick opens
|
|
123
|
+
* nothing, and `//` right after a `:` is a URL (`http://…`), not a comment.
|
|
124
|
+
* With `{ blankStrings: false }` only comments are blanked: the line rules read
|
|
125
|
+
* class strings but never a commented-out line.
|
|
126
|
+
* Known limit: two apostrophes on one line of raw JSX text read as a string.
|
|
127
|
+
*/
|
|
128
|
+
export function lexSource(src, { blankStrings = true } = {}) {
|
|
129
|
+
const chars = src.split('');
|
|
130
|
+
const strings = [];
|
|
131
|
+
const frames = [];
|
|
132
|
+
const n = src.length;
|
|
133
|
+
const blankAll = (from, to) => {
|
|
134
|
+
for (let k = from; k < to; k += 1) if (chars[k] !== '\n') chars[k] = ' ';
|
|
135
|
+
};
|
|
136
|
+
const blank = blankStrings ? blankAll : () => {};
|
|
137
|
+
const templateText = (frame, j) => {
|
|
138
|
+
const from = j;
|
|
139
|
+
let text = '';
|
|
140
|
+
while (j < n) {
|
|
141
|
+
const ch = src[j];
|
|
142
|
+
if (ch === '\\') {
|
|
143
|
+
text += src.slice(j, j + 2);
|
|
144
|
+
j += 2;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (ch === '`') {
|
|
148
|
+
blank(from, j);
|
|
149
|
+
frame.parts.push(text);
|
|
150
|
+
strings.push({ start: frame.start, text: frame.parts.join(' ') });
|
|
151
|
+
return j + 1;
|
|
152
|
+
}
|
|
153
|
+
if (ch === '$' && src[j + 1] === '{') {
|
|
154
|
+
blank(from, j);
|
|
155
|
+
frame.parts.push(text);
|
|
156
|
+
frame.depth = 0;
|
|
157
|
+
frames.push(frame);
|
|
158
|
+
return j + 2;
|
|
159
|
+
}
|
|
160
|
+
text += ch;
|
|
161
|
+
j += 1;
|
|
162
|
+
}
|
|
163
|
+
blank(from, n);
|
|
164
|
+
frame.parts.push(text);
|
|
165
|
+
strings.push({ start: frame.start, text: frame.parts.join(' ') });
|
|
166
|
+
return n;
|
|
167
|
+
};
|
|
168
|
+
let i = 0;
|
|
169
|
+
while (i < n) {
|
|
170
|
+
const ch = src[i];
|
|
171
|
+
const next = src[i + 1];
|
|
172
|
+
if (ch === '/' && next === '/' && src[i - 1] !== ':') {
|
|
173
|
+
const eol = src.indexOf('\n', i);
|
|
174
|
+
const end = eol === -1 ? n : eol;
|
|
175
|
+
blankAll(i, end);
|
|
176
|
+
i = end;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (ch === '/' && next === '*') {
|
|
180
|
+
const close = src.indexOf('*/', i + 2);
|
|
181
|
+
const end = close === -1 ? n : close + 2;
|
|
182
|
+
blankAll(i, end);
|
|
183
|
+
i = end;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (ch === '/' && regexCanStart(src, i)) {
|
|
187
|
+
const end = regexLiteralEnd(src, i);
|
|
188
|
+
if (end > 0) {
|
|
189
|
+
blank(i + 1, end - 1);
|
|
190
|
+
i = end;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (ch === "'" || ch === '"') {
|
|
195
|
+
let j = i + 1;
|
|
196
|
+
let text = '';
|
|
197
|
+
while (j < n && src[j] !== ch && src[j] !== '\n') {
|
|
198
|
+
if (src[j] === '\\') {
|
|
199
|
+
text += src.slice(j, j + 2);
|
|
200
|
+
j += 2;
|
|
201
|
+
} else {
|
|
202
|
+
text += src[j];
|
|
203
|
+
j += 1;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (j < n && src[j] === ch) {
|
|
207
|
+
strings.push({ start: i, text });
|
|
208
|
+
blank(i + 1, j);
|
|
209
|
+
i = j + 1;
|
|
210
|
+
} else {
|
|
211
|
+
i += 1;
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (ch === '`') {
|
|
216
|
+
i = templateText({ start: i, parts: [], depth: 0 }, i + 1);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const top = frames[frames.length - 1];
|
|
220
|
+
if (top && ch === '{') {
|
|
221
|
+
top.depth += 1;
|
|
222
|
+
} else if (top && ch === '}') {
|
|
223
|
+
if (top.depth === 0) {
|
|
224
|
+
frames.pop();
|
|
225
|
+
i = templateText(top, i + 1);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
top.depth -= 1;
|
|
229
|
+
}
|
|
230
|
+
i += 1;
|
|
231
|
+
}
|
|
232
|
+
return { code: chars.join(''), strings };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const REGEX_AFTER = new Set(['(', ',', '=', ':', '[', '!', '&', '|', '?', '{', ';', '+', '-', '*', '%', '~', '^']);
|
|
236
|
+
const REGEX_AFTER_WORD = new Set(['return', 'typeof', 'case', 'void', 'delete', 'in', 'of', 'yield', 'await', 'new', 'else', 'do']);
|
|
237
|
+
|
|
238
|
+
/** True when a `/` at `i` starts a regex literal rather than a division or a JSX `</` / `/>`. */
|
|
239
|
+
export function regexCanStart(src, i) {
|
|
240
|
+
if (src[i + 1] === '>' || src[i + 1] === '/' || src[i + 1] === '*') return false;
|
|
241
|
+
let k = i - 1;
|
|
242
|
+
while (k >= 0 && /\s/.test(src[k])) k -= 1;
|
|
243
|
+
if (k < 0) return true;
|
|
244
|
+
const prev = src[k];
|
|
245
|
+
if (REGEX_AFTER.has(prev)) return true;
|
|
246
|
+
if (/[\w$]/.test(prev)) {
|
|
247
|
+
let w = k;
|
|
248
|
+
while (w >= 0 && /[\w$]/.test(src[w])) w -= 1;
|
|
249
|
+
return REGEX_AFTER_WORD.has(src.slice(w + 1, k + 1));
|
|
250
|
+
}
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The end offset (exclusive, flags included) of a regex literal at `i`, or -1 if it does not close on its line. */
|
|
255
|
+
export function regexLiteralEnd(src, i) {
|
|
256
|
+
let inClass = false;
|
|
257
|
+
for (let j = i + 1; j < src.length; j += 1) {
|
|
258
|
+
const c = src[j];
|
|
259
|
+
if (c === '\n') return -1;
|
|
260
|
+
if (c === '\\') {
|
|
261
|
+
j += 1;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (c === '[') inClass = true;
|
|
265
|
+
else if (c === ']') inClass = false;
|
|
266
|
+
else if (c === '/' && !inClass) {
|
|
267
|
+
let end = j + 1;
|
|
268
|
+
while (end < src.length && /[a-z]/.test(src[end])) end += 1;
|
|
269
|
+
return end;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return -1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** The source with comments blanked (strings kept), for rules that read lines. */
|
|
276
|
+
export function withoutComments(rel, src) {
|
|
277
|
+
if (!/\.css$/.test(rel)) return lexSource(src, { blankStrings: false }).code;
|
|
278
|
+
return src.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** A function from a source offset to its 1-based line number. */
|
|
282
|
+
export function lineIndex(src) {
|
|
283
|
+
const starts = [0];
|
|
284
|
+
for (let k = 0; k < src.length; k += 1) if (src[k] === '\n') starts.push(k + 1);
|
|
285
|
+
return (offset) => {
|
|
286
|
+
let lo = 0;
|
|
287
|
+
let hi = starts.length - 1;
|
|
288
|
+
while (lo < hi) {
|
|
289
|
+
const mid = (lo + hi + 1) >> 1;
|
|
290
|
+
if (starts[mid] <= offset) lo = mid;
|
|
291
|
+
else hi = mid - 1;
|
|
292
|
+
}
|
|
293
|
+
return lo + 1;
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* The end offset (exclusive) of the JSX opening tag that starts at `start`
|
|
299
|
+
* (`<Name …>` or `<Name … />`): the first `>` outside braces and quotes.
|
|
300
|
+
*/
|
|
301
|
+
export function openingTagEnd(src, start) {
|
|
302
|
+
let depth = 0;
|
|
303
|
+
let quote = null;
|
|
304
|
+
for (let i = start + 1; i < src.length; i += 1) {
|
|
305
|
+
const c = src[i];
|
|
306
|
+
if (quote) {
|
|
307
|
+
if (c === quote && src[i - 1] !== '\\') quote = null;
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (c === '"' || c === "'" || c === '`') quote = c;
|
|
311
|
+
else if (c === '{') depth += 1;
|
|
312
|
+
else if (c === '}') depth -= 1;
|
|
313
|
+
else if (c === '>' && depth === 0 && src[i - 1] !== '=') return i + 1;
|
|
314
|
+
}
|
|
315
|
+
return src.length;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The attribute text of the JSX tag whose name ends at `start`, up to its `>`.
|
|
320
|
+
* Every `{…}` expression is blanked to `{}` unless it is a bare string literal,
|
|
321
|
+
* so an arrow's `>` in `onClick={() => …}` neither ends the tag early nor lets
|
|
322
|
+
* a string inside a handler count as an attribute.
|
|
323
|
+
*/
|
|
324
|
+
export function attributeText(src, start) {
|
|
325
|
+
let depth = 0;
|
|
326
|
+
let quote = null;
|
|
327
|
+
let out = '';
|
|
328
|
+
let expr = '';
|
|
329
|
+
for (let i = start; i < src.length; i += 1) {
|
|
330
|
+
const ch = src[i];
|
|
331
|
+
if (quote) {
|
|
332
|
+
if (ch === '\\') i += 1;
|
|
333
|
+
else if (ch === quote) quote = null;
|
|
334
|
+
if (depth === 0) out += ch;
|
|
335
|
+
else expr += ch;
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
339
|
+
quote = ch;
|
|
340
|
+
if (depth === 0) out += ch;
|
|
341
|
+
else expr += ch;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
if (ch === '{') {
|
|
345
|
+
if (depth === 0) expr = '';
|
|
346
|
+
else expr += ch;
|
|
347
|
+
depth += 1;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (ch === '}') {
|
|
351
|
+
depth -= 1;
|
|
352
|
+
if (depth === 0) {
|
|
353
|
+
const literal = /^\s*(?:"[^"]*"|'[^']*'|`[^`$]*`)\s*$/.test(expr);
|
|
354
|
+
out += literal ? `{${expr}}` : '{}';
|
|
355
|
+
} else {
|
|
356
|
+
expr += ch;
|
|
357
|
+
}
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (depth > 0) {
|
|
361
|
+
expr += ch;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (ch === '>') return out;
|
|
365
|
+
out += ch;
|
|
366
|
+
}
|
|
367
|
+
return out;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* The JSX expression of the attribute value that opens at `open` (a `{`), with
|
|
372
|
+
* its braces balanced and quoted text skipped. Null when the braces never close.
|
|
373
|
+
*/
|
|
374
|
+
export function attributeExpression(tag, open) {
|
|
375
|
+
let depth = 0;
|
|
376
|
+
let quote = null;
|
|
377
|
+
for (let i = open; i < tag.length; i += 1) {
|
|
378
|
+
const c = tag[i];
|
|
379
|
+
if (quote) {
|
|
380
|
+
if (c === quote && tag[i - 1] !== '\\') quote = null;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (c === '"' || c === "'" || c === '`') quote = c;
|
|
384
|
+
else if (c === '{') depth += 1;
|
|
385
|
+
else if (c === '}' && --depth === 0) return tag.slice(open + 1, i).trim();
|
|
386
|
+
}
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// ── rule helpers ──────────────────────────────────────────────────────────────
|
|
391
|
+
|
|
392
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
393
|
+
const isCommentLine = (line) => /^\s*(\/\/|\/\*|\*)/.test(line);
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* An inline exception: `uix-page-rules-ignore <rule>: <reason>` (TENSOR's
|
|
397
|
+
* `compliance-lint-ignore <rule>: <reason>` is read too) on the flagged line or
|
|
398
|
+
* the line above, or on line 1 for a file-level rule. The reason is required.
|
|
399
|
+
*/
|
|
400
|
+
export function hasIgnore(lines, idx, rule, fileLevel = false) {
|
|
401
|
+
const re = new RegExp(`(?:uix-page-rules-ignore|compliance-lint-ignore)\\s+${escapeRe(rule)}\\s*:\\s*\\S`);
|
|
402
|
+
return re.test(lines[idx] ?? '') || re.test(lines[idx - 1] ?? '') || (fileLevel && re.test(lines[0] ?? ''));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** An invisible submitter: `type="submit"`, `hidden`, `aria-hidden`. */
|
|
406
|
+
export function isHiddenSubmitter(attrs) {
|
|
407
|
+
return (
|
|
408
|
+
/(?:^|\s)type=(?:"submit"|'submit'|\{\s*["'`]submit["'`]\s*\})/.test(attrs) &&
|
|
409
|
+
/(?:^|\s)hidden(?=\s|\/|$|=\{\s*true\s*\})/.test(attrs) &&
|
|
410
|
+
/(?:^|\s)aria-hidden(?=[\s=/]|$)/.test(attrs) &&
|
|
411
|
+
!/(?:^|\s)aria-hidden=(?:"false"|'false'|\{\s*false\s*\}|\{\s*["'`]false["'`]\s*\})/.test(attrs)
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** `<input type="hidden">` carries a form value and draws nothing. */
|
|
416
|
+
export function isHiddenInput(attrs) {
|
|
417
|
+
return /(?:^|\s)type=(?:"hidden"|'hidden'|\{\s*["'`]hidden["'`]\s*\})/.test(attrs);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* A class token without its variants: `sm:hover:px-2` → `px-2`, `!px-2` →
|
|
422
|
+
* `px-2`. A `:` inside brackets (`text-[length:12px]`) is part of the value.
|
|
423
|
+
*/
|
|
424
|
+
export function baseUtility(token) {
|
|
425
|
+
let depth = 0;
|
|
426
|
+
let cut = 0;
|
|
427
|
+
for (let k = 0; k < token.length; k += 1) {
|
|
428
|
+
const ch = token[k];
|
|
429
|
+
if (ch === '[') depth += 1;
|
|
430
|
+
else if (ch === ']') depth = Math.max(0, depth - 1);
|
|
431
|
+
else if (ch === ':' && depth === 0) cut = k + 1;
|
|
432
|
+
}
|
|
433
|
+
return token.slice(cut).replace(/^!/, '').replace(/!$/, '');
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const utilities = (text) => text.split(/\s+/).filter(Boolean).map(baseUtility);
|
|
437
|
+
|
|
438
|
+
/** `title={t('k')}` → {kind:'key', value:'k'}; `title="X"` → {kind:'literal', value:'X'}. */
|
|
439
|
+
export function propToken(tag, prop) {
|
|
440
|
+
const key = new RegExp(`\\b${prop}=\\{\\s*t\\(\\s*(['"\`])([^'"\`]+)\\1\\s*\\)\\s*\\}`).exec(tag);
|
|
441
|
+
if (key) return { kind: 'key', value: key[2] };
|
|
442
|
+
const lit = new RegExp(`\\b${prop}=(['"])([^'"]+)\\1`).exec(tag);
|
|
443
|
+
if (lit) return { kind: 'literal', value: lit[2] };
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* The translation namespace `t` is bound to at `offset`: the last
|
|
449
|
+
* `const t = useTranslations('ns')` / `await getTranslations('ns')` before it
|
|
450
|
+
* (next-intl), or null when the file binds none. Two components in one file
|
|
451
|
+
* can each bind their own `t`, so `t('title')` twice is not always one text.
|
|
452
|
+
*/
|
|
453
|
+
export function namespaceAt(src, offset) {
|
|
454
|
+
let ns = null;
|
|
455
|
+
for (const m of src.matchAll(/\bconst\s+t\s*=\s*(?:await\s+)?(?:useTranslations|getTranslations)\(\s*(?:\{[^}]*?namespace\s*:\s*)?(['"`])([^'"`]+)\1/g)) {
|
|
456
|
+
if (m.index > offset) break;
|
|
457
|
+
ns = m[2];
|
|
458
|
+
}
|
|
459
|
+
return ns;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** The places a repeated title reads as a heading: heading props and h1–h4. */
|
|
463
|
+
function headingRepeatRes(token) {
|
|
464
|
+
const v = escapeRe(token.value);
|
|
465
|
+
if (token.kind === 'key') {
|
|
466
|
+
const call = `\\{\\s*t\\(\\s*['"\`]${v}['"\`]\\s*\\)\\s*\\}`;
|
|
467
|
+
return [new RegExp(`\\b(?:title|heading|caption)=${call}`, 'g'), new RegExp(`<h[1-4][^>]*>\\s*${call}`, 'g')];
|
|
468
|
+
}
|
|
469
|
+
return [
|
|
470
|
+
new RegExp(`\\b(?:title|heading|caption)=(['"])${v}\\1`, 'g'),
|
|
471
|
+
new RegExp(`<h[1-4][^>]*>\\s*${v}\\s*<`, 'g'),
|
|
472
|
+
];
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* A menu names its row when any of its labels (`label`, or the trigger's
|
|
477
|
+
* `aria-label`) interpolates something. A literal, `{t('k')}` alone or a bare
|
|
478
|
+
* string expression names nothing.
|
|
479
|
+
*/
|
|
480
|
+
export function labelNamesRow(tag) {
|
|
481
|
+
for (const m of tag.matchAll(/\b(?:aria-label|label)=/g)) {
|
|
482
|
+
const at = m.index + m[0].length;
|
|
483
|
+
if (tag[at] !== '{') continue;
|
|
484
|
+
const expr = attributeExpression(tag, at);
|
|
485
|
+
if (expr === null) continue;
|
|
486
|
+
if (/^t\(\s*(['"`])[^'"`]+\1\s*\)$/.test(expr)) continue;
|
|
487
|
+
if (/^(['"])[^'"]*\1$/.test(expr)) continue;
|
|
488
|
+
if (/^`[^`$]*`$/.test(expr)) continue;
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const BREAK_ANYWHERE_RE =
|
|
495
|
+
/(?<![\w-])break-all(?![\w-])|overflow-wrap\s*:\s*anywhere|\[overflow-wrap:anywhere\]|(?<![\w-])wrap-anywhere(?![\w-])|\boverflowWrap\s*:\s*(['"`])anywhere\1|\bwordBreak\s*:\s*(['"`])break-all\2/;
|
|
496
|
+
/** A hash, key or URL in code type may break anywhere: it has no words to keep. */
|
|
497
|
+
const CODE_TOKEN_RE = /\bfont-mono\b|<code\b|<pre\b/;
|
|
498
|
+
/**
|
|
499
|
+
* "high" sent to a red tone by hand: an object map, a ternary or a one-line
|
|
500
|
+
* case. Tone names are lower case, so a priority label ('high:high': 'P1') is
|
|
501
|
+
* not a tone.
|
|
502
|
+
*/
|
|
503
|
+
const HIGH = '(?:high|High|HIGH)';
|
|
504
|
+
const RED_TONE = `['"\`](?:danger|critical|p1)['"\`]`;
|
|
505
|
+
// Matched over the whole comment-blanked source, so a Prettier-wrapped
|
|
506
|
+
// `case 'high':\n return 'danger';` is one match.
|
|
507
|
+
const HIGH_TO_RED_RES = [
|
|
508
|
+
new RegExp(`(?<![\\w:-])['"\`]?${HIGH}['"\`]?[ \\t]*:[ \\t]*${RED_TONE}`, 'g'),
|
|
509
|
+
new RegExp(`['"\`]${HIGH}['"\`]\\s*\\)?\\s*\\?\\s*${RED_TONE}`, 'g'),
|
|
510
|
+
new RegExp(`\\bcase\\s+['"\`]${HIGH}['"\`]\\s*:\\s*(?:return\\s+)?${RED_TONE}`, 'g'),
|
|
511
|
+
];
|
|
512
|
+
const JOIN_COMMA_RE = /\.join\(\s*(['"`]),\s?\1\s*\)/;
|
|
513
|
+
/**
|
|
514
|
+
* A table row's data: TanStack's `row.original`, or `row.` / `getValue()` on
|
|
515
|
+
* the line of a column's `cell:` / `render:` renderer. A `row` from a plain
|
|
516
|
+
* `.map` in a description list or a form value is not a cell.
|
|
517
|
+
*/
|
|
518
|
+
const isRowCellData = (line) =>
|
|
519
|
+
/\brow\.original\b/.test(line) ||
|
|
520
|
+
(/\b(?:cell|render)\s*:/.test(line) && /\brow\.[A-Za-z_$]|\bgetValue\b/.test(line));
|
|
521
|
+
/** A tooltip `title={….join(', ')}` holds the full list on purpose; the cell shows less. */
|
|
522
|
+
const TOOLTIP_JOIN_RE = /\btitle=\{[^}]*\.join\(/;
|
|
523
|
+
/** A row's cell or action renderer: TanStack `cell:`, `row.original`, DataWorkspace `rowActions` / `render: (row) =>`. */
|
|
524
|
+
const ROW_CONTEXT_RE = /\bcell\s*:|\brow\.original\b|\browActions\s*[=:]|\brender\s*:\s*\(?\s*(?:row|item|record|r)\b/;
|
|
525
|
+
const HORIZONTAL_PADDING = /^p[xlrse]-(?!0$)/;
|
|
526
|
+
const LITERAL_FONT_SIZE = /^text-\[(?:length:)?(?:-?\d|-?\.\d|calc\(|clamp\(|min\(|max\()/;
|
|
527
|
+
/** After `fontSize`: a style key (`: 12`, `: '0.8rem'`) or a JSX prop (`={12}`, `="12px"`). */
|
|
528
|
+
const INLINE_FONT_SIZE = /^\s*(?::\s*|=\{\s*|=)(?:-?\.?\d|(["'`])\s*-?\.?\d)/;
|
|
529
|
+
|
|
530
|
+
/** Exported for the pill rule that waits on HAR-3817; not counted yet. */
|
|
531
|
+
export const isHandBuiltPill = (text) => {
|
|
532
|
+
const tokens = utilities(text);
|
|
533
|
+
return tokens.includes('rounded-full') && tokens.some((t) => HORIZONTAL_PADDING.test(t));
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const kindOf = (rel) => {
|
|
537
|
+
if (/\.css$/.test(rel)) return 'css';
|
|
538
|
+
if (/\.(tsx|jsx)$/.test(rel)) return 'jsx';
|
|
539
|
+
return 'script';
|
|
540
|
+
};
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Every finding in one source file (`rel` is the repo-relative posix path).
|
|
544
|
+
* `.css` files get the mid-word-break rule only; `.ts`/`.js` files the
|
|
545
|
+
* severity and mid-word-break rules; `.tsx`/`.jsx` files all of them.
|
|
546
|
+
*
|
|
547
|
+
* @param {string} rel
|
|
548
|
+
* @param {string} src
|
|
549
|
+
* @param {Partial<typeof DEFAULT_OPTIONS>} [options]
|
|
550
|
+
* @returns {Finding[]}
|
|
551
|
+
*/
|
|
552
|
+
export function scanSource(rel, src, options = {}) {
|
|
553
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
554
|
+
const kind = kindOf(rel);
|
|
555
|
+
const lines = src.split('\n');
|
|
556
|
+
const out = [];
|
|
557
|
+
const exempt = (rule) => Object.hasOwn(opts.exempt?.[rule] ?? {}, rel);
|
|
558
|
+
const push = (rule, line, msg) => {
|
|
559
|
+
if (exempt(rule)) return;
|
|
560
|
+
out.push({ rule, file: rel, line, text: (lines[line - 1] ?? '').trim().slice(0, 160), msg });
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
// The line rules read the source with comments blanked (strings kept), so a
|
|
564
|
+
// trailing `// never use break-all`, a `{/* … */}` in JSX or the middle of a
|
|
565
|
+
// CSS block comment is not a finding. Ignore tags are read from `lines`.
|
|
566
|
+
const clean = withoutComments(rel, src);
|
|
567
|
+
const cleanLines = clean.split('\n');
|
|
568
|
+
const lineOf = lineIndex(src);
|
|
569
|
+
|
|
570
|
+
if (kind !== 'css') {
|
|
571
|
+
const toneHelperRe = opts.genericToneHelpers.length
|
|
572
|
+
? new RegExp(`\\b(?:${opts.genericToneHelpers.map(escapeRe).join('|')})\\(\\s*['"\`]severity['"\`]`, 'g')
|
|
573
|
+
: null;
|
|
574
|
+
const seen = new Set();
|
|
575
|
+
const flag = (index, msg) => {
|
|
576
|
+
const line = lineOf(index);
|
|
577
|
+
if (seen.has(line) || hasIgnore(lines, line - 1, RULE_SEVERITY_TONE)) return;
|
|
578
|
+
seen.add(line);
|
|
579
|
+
push(RULE_SEVERITY_TONE, line, msg);
|
|
580
|
+
};
|
|
581
|
+
for (const m of toneHelperRe ? clean.matchAll(toneHelperRe) : []) {
|
|
582
|
+
flag(m.index, 'A generic status-tone helper colours this severity, so "high" turns red; use severityTone(severity).');
|
|
583
|
+
}
|
|
584
|
+
for (const re of HIGH_TO_RED_RES) {
|
|
585
|
+
for (const m of clean.matchAll(re)) {
|
|
586
|
+
flag(m.index, '"high" is mapped to a red tone by hand; the severity ramp keeps high amber: use severityTone(severity).');
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
cleanLines.forEach((text, i) => {
|
|
592
|
+
if (BREAK_ANYWHERE_RE.test(text) && !CODE_TOKEN_RE.test(text) && !hasIgnore(lines, i, RULE_BREAK_ANYWHERE)) {
|
|
593
|
+
push(RULE_BREAK_ANYWHERE, i + 1, 'A mid-word break (break-all / overflow-wrap: anywhere / wrap-anywhere) splits labels inside words.');
|
|
594
|
+
}
|
|
595
|
+
if (
|
|
596
|
+
kind === 'jsx' &&
|
|
597
|
+
JOIN_COMMA_RE.test(text) &&
|
|
598
|
+
isRowCellData(text) &&
|
|
599
|
+
!TOOLTIP_JOIN_RE.test(text) &&
|
|
600
|
+
!hasIgnore(lines, i, RULE_LIST_JOINED)
|
|
601
|
+
) {
|
|
602
|
+
push(RULE_LIST_JOINED, i + 1, 'A row\'s list joined with ", " in a table cell wraps the row; use the kit\'s ListSummary.');
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
if (kind !== 'jsx') return out.sort((a, b) => a.line - b.line || COUNTED_RULES.indexOf(a.rule) - COUNTED_RULES.indexOf(b.rule));
|
|
607
|
+
|
|
608
|
+
const { code, strings } = lexSource(src);
|
|
609
|
+
|
|
610
|
+
// heading said twice — judged against the first page-title component's title.
|
|
611
|
+
const titleRe = new RegExp(`<(?:${opts.pageTitleComponents.map(escapeRe).join('|')})(?=[\\s/>])`, 'g');
|
|
612
|
+
const headerSpans = [...code.matchAll(titleRe)].map((m) => [m.index, openingTagEnd(src, m.index)]);
|
|
613
|
+
if (opts.pageTitleComponents.length && headerSpans.length > 0) {
|
|
614
|
+
const [s, e] = headerSpans[0];
|
|
615
|
+
const token = propToken(src.slice(s, e), 'title');
|
|
616
|
+
if (token) {
|
|
617
|
+
const inHeader = (off) => headerSpans.some(([a, b]) => off >= a && off < b);
|
|
618
|
+
const titleNs = token.kind === 'key' ? namespaceAt(src, s) : null;
|
|
619
|
+
for (const re of headingRepeatRes(token)) {
|
|
620
|
+
for (const m of clean.matchAll(re)) {
|
|
621
|
+
if (inHeader(m.index)) continue;
|
|
622
|
+
if (token.kind === 'key' && namespaceAt(src, m.index) !== titleNs) continue;
|
|
623
|
+
const line = lineOf(m.index);
|
|
624
|
+
if (hasIgnore(lines, line - 1, RULE_HEADING_TWICE, true)) continue;
|
|
625
|
+
const said = token.kind === 'key' ? `t('${token.value}')` : `"${token.value}"`;
|
|
626
|
+
push(RULE_HEADING_TWICE, line, `The page title (${said}) is said again as a heading or caption.`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// a row menu whose labels do not name the row.
|
|
633
|
+
if (opts.rowMenuComponents.length) {
|
|
634
|
+
const menuRe = new RegExp(`<(${opts.rowMenuComponents.map(escapeRe).join('|')})(?=[\\s/>])`, 'g');
|
|
635
|
+
for (const m of code.matchAll(menuRe)) {
|
|
636
|
+
const line = lineOf(m.index);
|
|
637
|
+
const before = lines.slice(Math.max(0, line - 41), line);
|
|
638
|
+
if (!before.some((l) => ROW_CONTEXT_RE.test(l))) continue;
|
|
639
|
+
if (labelNamesRow(src.slice(m.index, openingTagEnd(src, m.index)))) continue;
|
|
640
|
+
if (hasIgnore(lines, line - 1, RULE_ROW_MENU_UNNAMED)) continue;
|
|
641
|
+
push(RULE_ROW_MENU_UNNAMED, line, `<${m[1]}> in a table row has no label that names its row.`);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// kit controls drawn by hand.
|
|
646
|
+
for (const m of code.matchAll(/<button(?=[\s/>])/g)) {
|
|
647
|
+
if (isHiddenSubmitter(attributeText(src, m.index + m[0].length))) continue;
|
|
648
|
+
push(RULE_RAW_BUTTON, lineOf(m.index), 'A raw <button> where the kit has Button, ButtonLink, Chip, MenuItem or RowAction.');
|
|
649
|
+
}
|
|
650
|
+
for (const m of code.matchAll(/<table(?=[\s/>])/g)) {
|
|
651
|
+
push(RULE_RAW_TABLE, lineOf(m.index), 'A raw <table> where the kit has DataWorkspace and Table.');
|
|
652
|
+
}
|
|
653
|
+
for (const m of code.matchAll(/<(input|select|textarea)(?=[\s/>])/g)) {
|
|
654
|
+
if (m[1] === 'input' && isHiddenInput(attributeText(src, m.index + m[0].length))) continue;
|
|
655
|
+
push(RULE_RAW_FORM_CONTROL, lineOf(m.index), `A raw <${m[1]}> where the kit has the form control.`);
|
|
656
|
+
}
|
|
657
|
+
for (const { start, text } of strings) {
|
|
658
|
+
for (const t of utilities(text)) {
|
|
659
|
+
if (LITERAL_FONT_SIZE.test(t)) push(RULE_ARBITRARY_FONT_SIZE, lineOf(start), `A literal font size (${t}) off the kit type scale.`);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
for (const m of code.matchAll(/\bfontSize(?=\s*[:=])/g)) {
|
|
663
|
+
if (INLINE_FONT_SIZE.test(src.slice(m.index + m[0].length, m.index + m[0].length + 24))) {
|
|
664
|
+
push(RULE_ARBITRARY_FONT_SIZE, lineOf(m.index), 'A literal inline fontSize off the kit type scale.');
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
return out.sort((a, b) => a.line - b.line || COUNTED_RULES.indexOf(a.rule) - COUNTED_RULES.indexOf(b.rule));
|
|
669
|
+
}
|