@multiplatform.one/config 7.66.0 → 7.68.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/README.md +55 -8
- package/design-lint.mjs +752 -28
- package/lib/index.js +1 -1
- package/lib/{vite-B4uRAr9Z.js → vite-ByHQLuSq.js} +29 -2
- package/lib/vite.js +2 -2
- package/package.json +19 -3
- package/src/phosphorNativeInk.spec.ts +117 -0
- package/src/shims/phosphorIconBase.native.js +5 -0
- package/src/shims/phosphorNativeProbe.d.ts +6 -0
- package/src/shims/phosphorNativeProbe.js +54 -0
- package/src/shims/phosphorThemeInk.d.ts +11 -0
- package/src/shims/phosphorThemeInk.js +50 -0
- package/src/vite.ts +35 -1
- package/types/vite.d.ts +10 -0
- package/types/vite.d.ts.map +1 -1
package/design-lint.mjs
CHANGED
|
@@ -63,6 +63,11 @@ const fieldTag =
|
|
|
63
63
|
const actionTag =
|
|
64
64
|
/^(?:Button|IconButton|TamaguiButton|Pressable|TouchableOpacity|TouchableHighlight|button)$|\.Button$/;
|
|
65
65
|
const rowTag = /^(?:XStack|XGroup)$|\.XGroup$/;
|
|
66
|
+
// LC-176 ICON-BUTTON: an icon-only button is a bare tinted glyph. A prop that
|
|
67
|
+
// paints a plate or a ring around the glyph needs the over-media plate.
|
|
68
|
+
const buttonTag = /^(?:Button|IconButton|TamaguiButton|PageBarButton)$|\.Button$/;
|
|
69
|
+
const platePaint =
|
|
70
|
+
/^(?:outlined|circular|bordered|elevate|elevation|bg|backgroundColor|background|borderWidth|borderColor|variant)$/;
|
|
66
71
|
const tokenFactories = new Set([
|
|
67
72
|
"createTokens",
|
|
68
73
|
"createTamagui",
|
|
@@ -136,27 +141,239 @@ export function mpoRoleOf(componentName) {
|
|
|
136
141
|
return found && { role: found, stem };
|
|
137
142
|
}
|
|
138
143
|
// A spread of a knob bundle that carries surface paint (knobProps.surface).
|
|
144
|
+
// FZ-7680: on native every glyph must reach MPO's theme-ink IconBase, which
|
|
145
|
+
// the toolchain swaps in behind the bare `@phosphor-icons/react` import. A
|
|
146
|
+
// deep subpath or phosphor-react-native itself skips that swap and paints a
|
|
147
|
+
// missing or token colour black. Only the MPO icon shims may name them.
|
|
148
|
+
const phosphorBypass = /^(?:@phosphor-icons\/react\/.+|phosphor-react-native(?:\/.*)?)$/;
|
|
149
|
+
const phosphorShim = /(?:^|[\\/])shims[\\/][^\\/]*phosphor[^\\/]*$/i;
|
|
139
150
|
const surfaceSpread = /\b(?:surface|elevation|containerRadius|radius|border|card|panel)\w*$/i;
|
|
140
151
|
const rawPrimitive =
|
|
141
152
|
/^(?:View|XStack|YStack|Stack|ZStack|HStack|VStack|SafeAreaView|ScrollView|Pressable|TouchableOpacity|TouchableHighlight|Text|SizableText|Paragraph|Animated\.View|div|section|nav|header|footer|aside|span|li|ul)$/;
|
|
142
153
|
const surfacePaint =
|
|
143
154
|
/^(?:bg|backgroundColor|background|borderRadius|br|border(?:Top|Bottom|Left|Right|Start|End)?(?:Width|Color)?|border(?:Top|Bottom)(?:Left|Right|Start|End)Radius|elevation|elevate|bordered|shadow(?:Color|Opacity|Radius|Offset)?|boxShadow)$/;
|
|
144
155
|
|
|
156
|
+
// FZ-8033: the rules harvested from SHC (FZ-8025) that every MPO app and
|
|
157
|
+
// package carries, public/* included.
|
|
158
|
+
export const harvestRules = [
|
|
159
|
+
"api-vocabulary",
|
|
160
|
+
"art-tint",
|
|
161
|
+
"copy-ampersand",
|
|
162
|
+
"copy-em-dash",
|
|
163
|
+
"copy-plural",
|
|
164
|
+
"copy-title-case",
|
|
165
|
+
"action-word",
|
|
166
|
+
"error-copy",
|
|
167
|
+
"link-chrome",
|
|
168
|
+
"refresh-label",
|
|
169
|
+
"user-words",
|
|
170
|
+
"web-paint",
|
|
171
|
+
"zoom-button",
|
|
172
|
+
"zoom-lock",
|
|
173
|
+
];
|
|
145
174
|
export const designRules = [
|
|
146
175
|
"field-action",
|
|
147
176
|
"field-focus",
|
|
148
177
|
"hex",
|
|
149
178
|
"geometry",
|
|
179
|
+
"icon-button",
|
|
150
180
|
"inline-spacing",
|
|
151
181
|
"mpo-role",
|
|
182
|
+
"phosphor-import",
|
|
152
183
|
"raw-control",
|
|
153
184
|
"raw-icon",
|
|
154
185
|
"styled-mpo",
|
|
155
186
|
"token-file",
|
|
187
|
+
...harvestRules,
|
|
156
188
|
];
|
|
157
189
|
// Rules whose baseline records each offender, so a new offender fails even
|
|
158
190
|
// when another one was fixed in the same change.
|
|
159
|
-
export const identityRules = new Set(["mpo-role"]);
|
|
191
|
+
export const identityRules = new Set(["icon-button", "mpo-role", ...harvestRules]);
|
|
192
|
+
// LC-203 USER-WORDS: implementation words a user never reads. A baseline's
|
|
193
|
+
// bannedWords replaces this list for its app; --init seeds it.
|
|
194
|
+
export const houseBannedWords = [
|
|
195
|
+
"API",
|
|
196
|
+
"GraphQL",
|
|
197
|
+
"HTTP",
|
|
198
|
+
"JSON",
|
|
199
|
+
"RPC",
|
|
200
|
+
"SQL",
|
|
201
|
+
"Tamagui",
|
|
202
|
+
"UUID",
|
|
203
|
+
"WebSocket",
|
|
204
|
+
"endpoint",
|
|
205
|
+
"localStorage",
|
|
206
|
+
"null",
|
|
207
|
+
"payload",
|
|
208
|
+
"stderr",
|
|
209
|
+
"stdout",
|
|
210
|
+
"undefined",
|
|
211
|
+
"websocket",
|
|
212
|
+
];
|
|
213
|
+
// LC-202: proper names keep their capitals, so they are not Title Case.
|
|
214
|
+
export const houseProperNames = [
|
|
215
|
+
"Kitchen Sink",
|
|
216
|
+
"Multiplatform One",
|
|
217
|
+
"Pro Manager",
|
|
218
|
+
"Self Hosted Cloud",
|
|
219
|
+
];
|
|
220
|
+
const copyAttribute =
|
|
221
|
+
/^(?:label|title|subtitle|description|placeholder|aria-label|aria-description|accessibilityLabel|accessibilityHint|helperText|hint|message|heading|caption|tooltip|emptyText|emptyTitle|emptyDescription|confirmLabel|confirmText|cancelLabel|cancelText|actionLabel|successMessage)$/;
|
|
222
|
+
const errorKey = /(?:^error|Error|^fail|Fail|Failure)\w*$/;
|
|
223
|
+
const pluralHedge = /[a-z]{2,}\(s\)(?=[\s"'`?.,;:!)]|$)/;
|
|
224
|
+
const errorLead =
|
|
225
|
+
/^(?:\d|\{x\}\s*:|[A-Z][A-Z0-9]*_[A-Z0-9_]*\b|[A-Z]{2,}[A-Z0-9]*(?::|\s+\d)|[a-z][a-z0-9]*(?:[A-Z_.][A-Za-z0-9_]*)+\b)/;
|
|
226
|
+
const webPaintKey =
|
|
227
|
+
/^(?:mixBlendMode|clipPath|textShadow(?:Color|Offset|Radius)?|backdropFilter|WebkitBackdropFilter|mask|maskImage|maskSize|WebkitMask|WebkitMaskImage|WebkitMaskSize)$/;
|
|
228
|
+
// LC-260 ICONS-OVER-WORDS: a standard action is drawn as its symbol, its word
|
|
229
|
+
// lives in the accessibility label. Only a form's submit and a destructive
|
|
230
|
+
// confirm stay words (HIG).
|
|
231
|
+
const actionWord =
|
|
232
|
+
/^(?:add|remove|replace|delete|edit|save|cancel|close|done|share|search|filter|sort|upload|download|copy|refresh|settings|more|back|next|send|export|import)$/i;
|
|
233
|
+
const wordControl =
|
|
234
|
+
/^(?:Button|TamaguiButton|Pressable|TouchableOpacity|TouchableHighlight|button|(?!Icon|Action)[A-Z]\w*Button)$|\.Button$/;
|
|
235
|
+
const destructiveWord = /^(?:delete|remove)$/i;
|
|
236
|
+
const destructiveHome = /Confirm|AlertDialog|Danger|Destructive/;
|
|
237
|
+
const destructiveTone = /^(?:intent|tone|severity|theme|variant)$/;
|
|
238
|
+
const destructiveValue = /destructive|danger|red|error/i;
|
|
239
|
+
const backdropKey = /^(?:backdropFilter|WebkitBackdropFilter)$/;
|
|
240
|
+
const materialHome = /(?:^|[\\/])components[\\/]src[\\/]Material[\\/]index\.tsx$/;
|
|
241
|
+
const cssFilter =
|
|
242
|
+
/^\s*(?:blur|grayscale|brightness|contrast|saturate|sepia|invert|hue-rotate|drop-shadow)\(/;
|
|
243
|
+
const cssWebPaint =
|
|
244
|
+
/(?:^|[;{\s"'`])(?:mix-blend-mode|backdrop-filter|-webkit-backdrop-filter|clip-path|text-shadow|-webkit-mask(?:-image)?|mask-image)\s*:/;
|
|
245
|
+
const zoomLock = /user-scalable\s*=\s*(?:no|0)\b|maximum-scale\s*=\s*1(?:\.0)?\b/;
|
|
246
|
+
const linkTag = /^(?:a|Anchor|Link|TextLink|InlineLink|ExternalLink)$|\.(?:Anchor|Link)$/;
|
|
247
|
+
const linkPaint =
|
|
248
|
+
/^(?:color|fontWeight|textDecoration|textDecorationLine|textDecorationColor|textDecorationStyle)$/;
|
|
249
|
+
const imageTag = /^(?:Image|img|ImageBackground|AvatarImage|FastImage|ExpoImage)$|\.Image$/;
|
|
250
|
+
const glyphSource = /glyph|icon|symbol|mono/i;
|
|
251
|
+
const hue =
|
|
252
|
+
/^(?:red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose|gray|grey|slate|black|white|brown)$/;
|
|
253
|
+
const bannedCache = new Map();
|
|
254
|
+
function bannedPattern(words) {
|
|
255
|
+
const key = words.join("\0");
|
|
256
|
+
if (!bannedCache.has(key))
|
|
257
|
+
bannedCache.set(
|
|
258
|
+
key,
|
|
259
|
+
words.length
|
|
260
|
+
? new RegExp(
|
|
261
|
+
`(?<![\\w-])(?:${words.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})(?![\\w-])`,
|
|
262
|
+
)
|
|
263
|
+
: undefined,
|
|
264
|
+
);
|
|
265
|
+
return bannedCache.get(key);
|
|
266
|
+
}
|
|
267
|
+
const clip = (text) => {
|
|
268
|
+
const flat = text.replace(/\s+/g, " ").replaceAll(";", ",").trim();
|
|
269
|
+
return flat.length > 60 ? `${flat.slice(0, 57)}...` : flat;
|
|
270
|
+
};
|
|
271
|
+
function titleCase(text, names) {
|
|
272
|
+
let value = text.replace(/\{x\}/g, " ");
|
|
273
|
+
for (const proper of names) value = value.replaceAll(proper, " ");
|
|
274
|
+
if (/[/=<>$_@#\\|{}[\]]/.test(value)) return false;
|
|
275
|
+
const words = value
|
|
276
|
+
.split(/\s+/)
|
|
277
|
+
.map((word) => word.replace(/^[^A-Za-z]+|[^A-Za-z']+$/g, ""))
|
|
278
|
+
.filter(Boolean);
|
|
279
|
+
if (words.length < 2 || words.length > 8 || !/^[A-Z]/.test(words[0])) return false;
|
|
280
|
+
// Every later word capitalised, small words included. "Terms of Service"
|
|
281
|
+
// reads the same as "Sign in with Google", so a lowercase word clears it.
|
|
282
|
+
const rest = words.slice(1);
|
|
283
|
+
return (
|
|
284
|
+
rest.some((word) => /^[A-Z][a-z']+$/.test(word)) && !rest.some((word) => /^[a-z]/.test(word))
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
// The copy rules (LC-202, 203, 204, 216, 217) over one UI string.
|
|
288
|
+
export function copyFindings(text, { error = false, bannedWords, properNames } = {}) {
|
|
289
|
+
const value = text
|
|
290
|
+
.replace(/\{\{[^}]*\}\}|\{[^{}]*\}/g, "{x}")
|
|
291
|
+
.replace(/\s+/g, " ")
|
|
292
|
+
.trim();
|
|
293
|
+
const found = [];
|
|
294
|
+
if (!value || value === "—") return found;
|
|
295
|
+
const quoted = `"${clip(value)}"`;
|
|
296
|
+
if (value.includes("—"))
|
|
297
|
+
found.push([
|
|
298
|
+
"copy-em-dash",
|
|
299
|
+
`${quoted} carries an em dash; use a period, comma or colon (LC-202).`,
|
|
300
|
+
]);
|
|
301
|
+
if (!/[A-Za-z]/.test(value)) return found;
|
|
302
|
+
if (pluralHedge.test(value) && !/https?\(s\)/.test(value))
|
|
303
|
+
found.push([
|
|
304
|
+
"copy-plural",
|
|
305
|
+
`${quoted} hedges a plural; count through t()'s plural form (LC-202).`,
|
|
306
|
+
]);
|
|
307
|
+
if (/(?:^|\s)&(?:\s|$)/.test(value))
|
|
308
|
+
found.push(["copy-ampersand", `${quoted} uses "&"; write "and" (LC-202).`]);
|
|
309
|
+
if (titleCase(value, properNames ?? houseProperNames))
|
|
310
|
+
found.push(["copy-title-case", `${quoted} is Title Case; UI copy is sentence case (LC-202).`]);
|
|
311
|
+
const word = value.match(bannedPattern(bannedWords ?? houseBannedWords) ?? /$^/)?.[0];
|
|
312
|
+
if (word)
|
|
313
|
+
found.push([
|
|
314
|
+
"user-words",
|
|
315
|
+
`${quoted} says "${word}", an implementation word; use the app's user-facing name (LC-203).`,
|
|
316
|
+
]);
|
|
317
|
+
if (error && errorLead.test(value))
|
|
318
|
+
found.push([
|
|
319
|
+
"error-copy",
|
|
320
|
+
`${quoted} leads with a code; say what failed, then what to do (LC-204).`,
|
|
321
|
+
]);
|
|
322
|
+
if (/^(?:Refresh|Reload)$/i.test(value))
|
|
323
|
+
found.push([
|
|
324
|
+
"refresh-label",
|
|
325
|
+
`${quoted} is a standing refresh control; live data refreshes itself (LC-216).`,
|
|
326
|
+
]);
|
|
327
|
+
if (/^Zoom (?:in|out)$/i.test(value))
|
|
328
|
+
found.push(["zoom-button", `${quoted} is a zoom button; zoom is a gesture (LC-217).`]);
|
|
329
|
+
return found;
|
|
330
|
+
}
|
|
331
|
+
// A string catalog: a file in an i18n, locales or localizations directory,
|
|
332
|
+
// directly or under a locale code.
|
|
333
|
+
const catalogFile =
|
|
334
|
+
/(?:^|[\\/])(?:i18n|locales|localizations)[\\/](?:[a-z]{2}(?:[-_][A-Za-z]{2,4})?[\\/])?(?!(?:index|env|resources|package|tsconfig)\.)[\w.-]+\.(?:json|[cm]?[jt]s)$/;
|
|
335
|
+
function lineOf(source, index) {
|
|
336
|
+
let line = 1;
|
|
337
|
+
for (let at = source.indexOf("\n"); at !== -1 && at < index; at = source.indexOf("\n", at + 1))
|
|
338
|
+
line++;
|
|
339
|
+
return line;
|
|
340
|
+
}
|
|
341
|
+
// A JSON string catalog: every leaf is UI copy, and a key path naming an
|
|
342
|
+
// error or failure makes it error copy.
|
|
343
|
+
function inspectCatalog(source, filename, options) {
|
|
344
|
+
let data;
|
|
345
|
+
try {
|
|
346
|
+
data = JSON.parse(source);
|
|
347
|
+
} catch {
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
const errors = [];
|
|
351
|
+
let cursor = 0;
|
|
352
|
+
(function walk(value, keys) {
|
|
353
|
+
if (typeof value === "string") {
|
|
354
|
+
const at = source.indexOf(JSON.stringify(value), cursor);
|
|
355
|
+
if (at !== -1) cursor = at;
|
|
356
|
+
for (const [rule, message] of copyFindings(value, {
|
|
357
|
+
...options,
|
|
358
|
+
error: keys.some((key) => errorKey.test(key)),
|
|
359
|
+
}))
|
|
360
|
+
errors.push(`${filename}:${lineOf(source, cursor)}:1: [${rule}] ${message}`);
|
|
361
|
+
} else if (value && typeof value === "object")
|
|
362
|
+
for (const [key, inner] of Object.entries(value)) walk(inner, [...keys, key]);
|
|
363
|
+
})(data, []);
|
|
364
|
+
return errors;
|
|
365
|
+
}
|
|
366
|
+
function inspectHtml(source, filename) {
|
|
367
|
+
return source
|
|
368
|
+
.split("\n")
|
|
369
|
+
.flatMap((line, index) =>
|
|
370
|
+
zoomLock.test(line)
|
|
371
|
+
? [
|
|
372
|
+
`${filename}:${index + 1}:1: [zoom-lock] ${clip(line.match(zoomLock)[0])} disables pinch zoom (LC-196).`,
|
|
373
|
+
]
|
|
374
|
+
: [],
|
|
375
|
+
);
|
|
376
|
+
}
|
|
160
377
|
export function isFreeformBoard(filename, projectRoot = process.cwd()) {
|
|
161
378
|
const root = path.resolve(projectRoot);
|
|
162
379
|
const absolute = path.resolve(root, filename);
|
|
@@ -172,8 +389,12 @@ export function isFreeformBoard(filename, projectRoot = process.cwd()) {
|
|
|
172
389
|
);
|
|
173
390
|
}
|
|
174
391
|
|
|
175
|
-
export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
392
|
+
export function inspectDesign(source, filename, projectRoot = process.cwd(), options = {}) {
|
|
176
393
|
if (isFreeformBoard(filename, projectRoot)) return [];
|
|
394
|
+
if (/\.json$/.test(filename)) return inspectCatalog(source, filename, options);
|
|
395
|
+
if (/\.html?$/.test(filename)) return inspectHtml(source, filename);
|
|
396
|
+
const catalog = catalogFile.test(filename);
|
|
397
|
+
const material = materialHome.test(filename);
|
|
177
398
|
const file = ts.createSourceFile(
|
|
178
399
|
filename,
|
|
179
400
|
source,
|
|
@@ -181,23 +402,28 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
181
402
|
true,
|
|
182
403
|
ts.ScriptKind.TSX,
|
|
183
404
|
);
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
405
|
+
let lazyChecker;
|
|
406
|
+
const typeChecker = () => {
|
|
407
|
+
if (lazyChecker) return lazyChecker;
|
|
408
|
+
const compilerOptions = {
|
|
409
|
+
noLib: true,
|
|
410
|
+
noResolve: true,
|
|
411
|
+
types: [],
|
|
412
|
+
jsx: ts.JsxEmit.Preserve,
|
|
413
|
+
};
|
|
414
|
+
const host = ts.createCompilerHost(compilerOptions);
|
|
415
|
+
host.getSourceFile = (requested) => (requested === filename ? file : undefined);
|
|
416
|
+
host.fileExists = (requested) => requested === filename;
|
|
417
|
+
host.readFile = (requested) => (requested === filename ? source : undefined);
|
|
418
|
+
host.directoryExists = () => false;
|
|
419
|
+
lazyChecker = ts.createProgram([filename], compilerOptions, host).getTypeChecker();
|
|
420
|
+
return lazyChecker;
|
|
189
421
|
};
|
|
190
|
-
const host = ts.createCompilerHost(options);
|
|
191
|
-
host.getSourceFile = (requested) => (requested === filename ? file : undefined);
|
|
192
|
-
host.fileExists = (requested) => requested === filename;
|
|
193
|
-
host.readFile = (requested) => (requested === filename ? source : undefined);
|
|
194
|
-
host.directoryExists = () => false;
|
|
195
|
-
const checker = ts.createProgram([filename], options, host).getTypeChecker();
|
|
196
422
|
const errors = [];
|
|
197
423
|
function importedFrom(node, test) {
|
|
198
424
|
const value = resolve(node);
|
|
199
425
|
const root = ts.isPropertyAccessExpression(value) ? value.expression : value;
|
|
200
|
-
const declarations =
|
|
426
|
+
const declarations = typeChecker().getSymbolAtLocation(root)?.declarations ?? [];
|
|
201
427
|
return declarations.some((declaration) => {
|
|
202
428
|
let current = declaration;
|
|
203
429
|
while (current && !ts.isImportDeclaration(current)) current = current.parent;
|
|
@@ -255,7 +481,7 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
255
481
|
)
|
|
256
482
|
return resolve(node.expression, seen);
|
|
257
483
|
const declaration = ts.isIdentifier(node)
|
|
258
|
-
?
|
|
484
|
+
? typeChecker().getSymbolAtLocation(node)?.valueDeclaration
|
|
259
485
|
: undefined;
|
|
260
486
|
if (
|
|
261
487
|
declaration &&
|
|
@@ -291,7 +517,7 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
291
517
|
function referenceName(node) {
|
|
292
518
|
const value = resolve(node);
|
|
293
519
|
const declaration = ts.isIdentifier(value)
|
|
294
|
-
?
|
|
520
|
+
? typeChecker().getSymbolAtLocation(value)?.declarations?.[0]
|
|
295
521
|
: undefined;
|
|
296
522
|
if (declaration && ts.isImportSpecifier(declaration))
|
|
297
523
|
return (declaration.propertyName ?? declaration.name).text;
|
|
@@ -416,6 +642,331 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
416
642
|
const { line, character } = file.getLineAndCharacterOfPosition(node.getStart(file));
|
|
417
643
|
errors.push(`${filename}:${line + 1}:${character + 1}: [${rule}] ${message}`);
|
|
418
644
|
}
|
|
645
|
+
const tCall = (node) =>
|
|
646
|
+
ts.isCallExpression(node) && /^(?:t|\w+\.t)$/.test(node.expression.getText(file));
|
|
647
|
+
function keyPath(node) {
|
|
648
|
+
const keys = [];
|
|
649
|
+
for (let current = node; current; current = current.parent)
|
|
650
|
+
if (ts.isPropertyAssignment(current)) keys.push(name(current.name));
|
|
651
|
+
return keys;
|
|
652
|
+
}
|
|
653
|
+
// Where a string literal lands decides whether it is UI copy: a JSX child,
|
|
654
|
+
// a copy attribute or key, t()'s key and defaultValue, or a catalog value.
|
|
655
|
+
function copyContext(node) {
|
|
656
|
+
let child = node;
|
|
657
|
+
let parent = node.parent;
|
|
658
|
+
while (
|
|
659
|
+
parent &&
|
|
660
|
+
(ts.isParenthesizedExpression(parent) ||
|
|
661
|
+
ts.isAsExpression(parent) ||
|
|
662
|
+
ts.isSatisfiesExpression(parent) ||
|
|
663
|
+
ts.isTemplateSpan(parent) ||
|
|
664
|
+
(ts.isConditionalExpression(parent) && parent.condition !== child) ||
|
|
665
|
+
(ts.isBinaryExpression(parent) &&
|
|
666
|
+
(parent.operatorToken.kind === ts.SyntaxKind.PlusToken ||
|
|
667
|
+
parent.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken ||
|
|
668
|
+
parent.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
669
|
+
(parent.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken &&
|
|
670
|
+
parent.right === child))))
|
|
671
|
+
) {
|
|
672
|
+
child = parent;
|
|
673
|
+
parent = parent.parent;
|
|
674
|
+
}
|
|
675
|
+
if (!parent) return undefined;
|
|
676
|
+
if (ts.isTemplateExpression(parent)) return undefined;
|
|
677
|
+
if (ts.isJsxExpression(parent)) {
|
|
678
|
+
if (ts.isJsxElement(parent.parent) || ts.isJsxFragment(parent.parent)) return {};
|
|
679
|
+
parent = parent.parent;
|
|
680
|
+
}
|
|
681
|
+
if (ts.isJsxAttribute(parent)) {
|
|
682
|
+
const key = name(parent.name);
|
|
683
|
+
if (copyAttribute.test(key)) return { error: errorKey.test(key) };
|
|
684
|
+
return errorKey.test(key) ? { error: true } : undefined;
|
|
685
|
+
}
|
|
686
|
+
if (ts.isPropertyAssignment(parent) && parent.initializer === child) {
|
|
687
|
+
const key = name(parent.name);
|
|
688
|
+
if (catalog) return { error: keyPath(parent).some((item) => errorKey.test(item)) };
|
|
689
|
+
if (key === "defaultValue" && tCall(parent.parent.parent)) {
|
|
690
|
+
const id = parent.parent.parent.arguments[0];
|
|
691
|
+
return { error: !!id && ts.isStringLiteralLike(id) && /error|fail/i.test(id.text) };
|
|
692
|
+
}
|
|
693
|
+
if (copyAttribute.test(key)) return { error: errorKey.test(key) };
|
|
694
|
+
return errorKey.test(key) ? { error: true } : undefined;
|
|
695
|
+
}
|
|
696
|
+
if (tCall(parent) && parent.arguments[0] === child && /\s/.test(node.getText(file))) return {};
|
|
697
|
+
return undefined;
|
|
698
|
+
}
|
|
699
|
+
function stringText(node) {
|
|
700
|
+
if (ts.isJsxText(node))
|
|
701
|
+
return node.text
|
|
702
|
+
.replaceAll("&", "&")
|
|
703
|
+
.replaceAll("—", "—")
|
|
704
|
+
.replaceAll(" ", " ");
|
|
705
|
+
if (ts.isTemplateExpression(node))
|
|
706
|
+
return [node.head.text, ...node.templateSpans.map((span) => span.literal.text)].join("{x}");
|
|
707
|
+
return node.text;
|
|
708
|
+
}
|
|
709
|
+
function checkCopy(node) {
|
|
710
|
+
const context = ts.isJsxText(node) ? {} : copyContext(node);
|
|
711
|
+
const text = stringText(node);
|
|
712
|
+
if (!ts.isJsxText(node)) {
|
|
713
|
+
if (zoomLock.test(text))
|
|
714
|
+
report(node, "zoom-lock", `${clip(text.match(zoomLock)[0])} disables pinch zoom (LC-196).`);
|
|
715
|
+
if (cssWebPaint.test(text))
|
|
716
|
+
report(
|
|
717
|
+
node,
|
|
718
|
+
"web-paint",
|
|
719
|
+
`${clip(text.match(cssWebPaint)[0])} is CSS React Native cannot draw (LC-206).`,
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
if (!context) return;
|
|
723
|
+
for (const [rule, message] of copyFindings(text, { ...options, ...context }))
|
|
724
|
+
report(node, rule, message);
|
|
725
|
+
}
|
|
726
|
+
function literalTypes(type) {
|
|
727
|
+
if (!type) return [];
|
|
728
|
+
if (ts.isUnionTypeNode(type)) return type.types.flatMap(literalTypes);
|
|
729
|
+
if (ts.isParenthesizedTypeNode(type)) return literalTypes(type.type);
|
|
730
|
+
if (ts.isLiteralTypeNode(type) && ts.isStringLiteral(type.literal)) return [type.literal.text];
|
|
731
|
+
return [];
|
|
732
|
+
}
|
|
733
|
+
const booleanType = (type) =>
|
|
734
|
+
!!type &&
|
|
735
|
+
(type.kind === ts.SyntaxKind.BooleanKeyword ||
|
|
736
|
+
(ts.isLiteralTypeNode(type) &&
|
|
737
|
+
[ts.SyntaxKind.TrueKeyword, ts.SyntaxKind.FalseKeyword].includes(type.literal.kind)));
|
|
738
|
+
// LC-180 API-VOCABULARY over the props a module exports.
|
|
739
|
+
function checkProps(declaration) {
|
|
740
|
+
const members = ts.isInterfaceDeclaration(declaration)
|
|
741
|
+
? declaration.members
|
|
742
|
+
: (function collect(type) {
|
|
743
|
+
if (ts.isTypeLiteralNode(type)) return [...type.members];
|
|
744
|
+
if (ts.isIntersectionTypeNode(type) || ts.isUnionTypeNode(type))
|
|
745
|
+
return type.types.flatMap(collect);
|
|
746
|
+
if (ts.isParenthesizedTypeNode(type)) return collect(type.type);
|
|
747
|
+
return [];
|
|
748
|
+
})(declaration.type);
|
|
749
|
+
const owner = declaration.name.text;
|
|
750
|
+
for (const member of members) {
|
|
751
|
+
if (!ts.isPropertySignature(member) || !member.name) continue;
|
|
752
|
+
const prop = name(member.name);
|
|
753
|
+
const literals = literalTypes(member.type);
|
|
754
|
+
let problem;
|
|
755
|
+
if (/^(?:density|scale)$/.test(prop) || (prop === "level" && literals.length))
|
|
756
|
+
problem = `${prop} is a synonym for size; take a size token`;
|
|
757
|
+
else if (prop === "enabled") problem = "enabled is a negative of disabled; use disabled";
|
|
758
|
+
else if (/^(?:isOpen|opened|isVisible|onVisibleChange|onIsOpenChange)$/.test(prop))
|
|
759
|
+
problem = `${prop} names disclosure; use open, defaultOpen and onOpenChange`;
|
|
760
|
+
else if (
|
|
761
|
+
/^(?:color|tone|intent|severity|variant|kind|status)$/.test(prop) &&
|
|
762
|
+
literals.some((value) => hue.test(value))
|
|
763
|
+
)
|
|
764
|
+
problem = `${prop} is a hue-valued enum; semantic colour is intent, severity or tone`;
|
|
765
|
+
else if (
|
|
766
|
+
/^(?:variant|appearance)$/.test(prop) &&
|
|
767
|
+
literals.some((value) => /^(?:danger|destructive|success|warning|error|info)$/.test(value))
|
|
768
|
+
)
|
|
769
|
+
problem = `${prop} carries a semantic colour; that is intent, severity or tone`;
|
|
770
|
+
else if (prop === "kind" && literals.some((value) => /^(?:ghost|outline|solid)$/.test(value)))
|
|
771
|
+
problem = "kind names a look; use the stock variant names";
|
|
772
|
+
else if (
|
|
773
|
+
/^(?:no|not|non|disable|dont|without|hide|skip|omit|exclude)[A-Z]/.test(prop) &&
|
|
774
|
+
booleanType(member.type)
|
|
775
|
+
)
|
|
776
|
+
problem = `${prop} is a negative boolean; name it positively, default false`;
|
|
777
|
+
if (problem) report(member, "api-vocabulary", `${owner}.${problem} (LC-180).`);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
const exported = (node) =>
|
|
781
|
+
!!node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
782
|
+
function stylePaints(node, test, seen = new Set()) {
|
|
783
|
+
const value = resolve(node);
|
|
784
|
+
if (seen.has(value)) return undefined;
|
|
785
|
+
seen.add(value);
|
|
786
|
+
if (ts.isConditionalExpression(value))
|
|
787
|
+
return stylePaints(value.whenTrue, test, seen) ?? stylePaints(value.whenFalse, test, seen);
|
|
788
|
+
if (ts.isArrayLiteralExpression(value))
|
|
789
|
+
for (const item of value.elements) {
|
|
790
|
+
const inner = stylePaints(item, test, seen);
|
|
791
|
+
if (inner) return inner;
|
|
792
|
+
}
|
|
793
|
+
if (!ts.isObjectLiteralExpression(value)) return undefined;
|
|
794
|
+
for (const prop of value.properties) {
|
|
795
|
+
if (ts.isSpreadAssignment(prop)) {
|
|
796
|
+
const inner = stylePaints(prop.expression, test, seen);
|
|
797
|
+
if (inner) return inner;
|
|
798
|
+
} else if (prop.name && test.test(name(prop.name))) return name(prop.name);
|
|
799
|
+
else if (ts.isPropertyAssignment(prop) && /style$/i.test(name(prop.name))) {
|
|
800
|
+
const inner = stylePaints(prop.initializer, test, seen);
|
|
801
|
+
if (inner) return inner;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
return undefined;
|
|
805
|
+
}
|
|
806
|
+
function paintedBy(element, test) {
|
|
807
|
+
for (const attr of element.attributes.properties) {
|
|
808
|
+
if (ts.isJsxSpreadAttribute(attr)) continue;
|
|
809
|
+
const key = name(attr.name);
|
|
810
|
+
if (test.test(key)) return key;
|
|
811
|
+
const value =
|
|
812
|
+
attr.initializer && ts.isJsxExpression(attr.initializer)
|
|
813
|
+
? attr.initializer.expression
|
|
814
|
+
: undefined;
|
|
815
|
+
if (value && /style$/i.test(key)) {
|
|
816
|
+
const inner = stylePaints(value, test);
|
|
817
|
+
if (inner) return inner;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return undefined;
|
|
821
|
+
}
|
|
822
|
+
function visibleText(node) {
|
|
823
|
+
if (!node) return [];
|
|
824
|
+
if (ts.isJsxText(node)) return node.text.trim() ? [node.text.trim()] : [];
|
|
825
|
+
if (ts.isStringLiteralLike(node)) return [node.text];
|
|
826
|
+
if (ts.isJsxExpression(node) || ts.isParenthesizedExpression(node))
|
|
827
|
+
return visibleText(node.expression);
|
|
828
|
+
if (tCall(node)) {
|
|
829
|
+
const [id, fallback] = node.arguments;
|
|
830
|
+
if (fallback && ts.isStringLiteralLike(fallback)) return [fallback.text];
|
|
831
|
+
const value =
|
|
832
|
+
fallback &&
|
|
833
|
+
ts.isObjectLiteralExpression(fallback) &&
|
|
834
|
+
fallback.properties.find(
|
|
835
|
+
(prop) => ts.isPropertyAssignment(prop) && name(prop.name) === "defaultValue",
|
|
836
|
+
)?.initializer;
|
|
837
|
+
if (value && ts.isStringLiteralLike(value)) return [value.text];
|
|
838
|
+
if (id && ts.isStringLiteralLike(id)) return [id.text.split(/[.:]/).pop()];
|
|
839
|
+
return [];
|
|
840
|
+
}
|
|
841
|
+
if (ts.isJsxElement(node) && /Text$/.test(tagText(node.openingElement.tagName)))
|
|
842
|
+
return node.children.flatMap(visibleText);
|
|
843
|
+
return [];
|
|
844
|
+
}
|
|
845
|
+
function actionLabel(element) {
|
|
846
|
+
const parts = ["label", "title", "children"].flatMap((key) => {
|
|
847
|
+
const attr = attribute(element, key);
|
|
848
|
+
return attr?.initializer ? visibleText(attr.initializer) : [];
|
|
849
|
+
});
|
|
850
|
+
if (ts.isJsxElement(element)) parts.push(...element.children.flatMap(visibleText));
|
|
851
|
+
return parts
|
|
852
|
+
.join(" ")
|
|
853
|
+
.replace(/(?:…|\.\.\.|:)\s*$/, "")
|
|
854
|
+
.trim();
|
|
855
|
+
}
|
|
856
|
+
function keptAsWord(element, word) {
|
|
857
|
+
if (attribute(element, "type")?.initializer?.getText(file).includes("submit")) return true;
|
|
858
|
+
for (let current = element.parent; current; current = current.parent) {
|
|
859
|
+
const owner =
|
|
860
|
+
ts.isJsxElement(current) || ts.isJsxSelfClosingElement(current)
|
|
861
|
+
? tagText(opening(current).tagName)
|
|
862
|
+
: (ts.isFunctionDeclaration(current) || ts.isVariableDeclaration(current)) &&
|
|
863
|
+
current.name &&
|
|
864
|
+
ts.isIdentifier(current.name)
|
|
865
|
+
? current.name.text
|
|
866
|
+
: "";
|
|
867
|
+
if (/^Form\.Trigger$/.test(owner)) return true;
|
|
868
|
+
if (destructiveWord.test(word) && destructiveHome.test(owner)) return true;
|
|
869
|
+
}
|
|
870
|
+
if (!destructiveWord.test(word)) return false;
|
|
871
|
+
if (destructiveHome.test(tagText(opening(element).tagName))) return true;
|
|
872
|
+
return opening(element).attributes.properties.some(
|
|
873
|
+
(attr) =>
|
|
874
|
+
ts.isJsxAttribute(attr) &&
|
|
875
|
+
destructiveTone.test(name(attr.name)) &&
|
|
876
|
+
destructiveValue.test(attr.initializer?.getText(file) ?? ""),
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
function platedIconButton(element) {
|
|
880
|
+
const tag = opening(element);
|
|
881
|
+
if (!buttonTag.test(tagText(tag.tagName)) || !iconOnly(element)) return undefined;
|
|
882
|
+
if (attribute(element, "verb") || attribute(element, "children")) return undefined;
|
|
883
|
+
const plate = attribute(element, "plate");
|
|
884
|
+
if (plate?.initializer && /["'](?:media|primary)["']/.test(plate.initializer.getText(file)))
|
|
885
|
+
return undefined;
|
|
886
|
+
return tag.attributes.properties.find((attr) => {
|
|
887
|
+
if (!ts.isJsxAttribute(attr)) return false;
|
|
888
|
+
const key = name(attr.name);
|
|
889
|
+
const value =
|
|
890
|
+
attr.initializer && ts.isJsxExpression(attr.initializer)
|
|
891
|
+
? attr.initializer.expression
|
|
892
|
+
: attr.initializer;
|
|
893
|
+
const off = !!value && (inert(value) || value.kind === ts.SyntaxKind.FalseKeyword);
|
|
894
|
+
if (key === "chromeless") return !!value && value.kind === ts.SyntaxKind.FalseKeyword;
|
|
895
|
+
return platePaint.test(key) && !off;
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
function checkIconButton(element) {
|
|
899
|
+
const plated = platedIconButton(element);
|
|
900
|
+
if (plated)
|
|
901
|
+
report(
|
|
902
|
+
plated,
|
|
903
|
+
"icon-button",
|
|
904
|
+
`An icon-only button is a bare tinted glyph; drop ${name(plated.name)}, or pass plate="media" over imagery (LC-176 ICON-BUTTON).`,
|
|
905
|
+
);
|
|
906
|
+
}
|
|
907
|
+
// LC-260 ICONS-OVER-WORDS at a pressable's visible label.
|
|
908
|
+
function checkActionWord(element) {
|
|
909
|
+
if (!wordControl.test(tagText(opening(element).tagName))) return;
|
|
910
|
+
// A domain verb that shares a standard word (Frappe's docstatus Cancel)
|
|
911
|
+
// names itself: `// action-word-escape: <reason>` inside the tag.
|
|
912
|
+
if (opening(element).getText(file).includes("action-word-escape:")) return;
|
|
913
|
+
const label = actionLabel(element);
|
|
914
|
+
if (!actionWord.test(label) || keptAsWord(element, label)) return;
|
|
915
|
+
report(
|
|
916
|
+
element,
|
|
917
|
+
"action-word",
|
|
918
|
+
`"${label}" is a standard action spelled as a word; draw its icon and put the word in the accessibility label (LC-260).`,
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
// LC-181 LINK-CHROME and LC-207 ART-NEVER-TINTED at a JSX use site.
|
|
922
|
+
function checkElement(element) {
|
|
923
|
+
const tag = tagText(element.tagName);
|
|
924
|
+
if (linkTag.test(tag)) {
|
|
925
|
+
const key = paintedBy(element, linkPaint);
|
|
926
|
+
if (key)
|
|
927
|
+
report(
|
|
928
|
+
element,
|
|
929
|
+
"link-chrome",
|
|
930
|
+
`${tag} sets ${key}; a link's ink, weight and underline come from the theme (LC-181).`,
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
if (imageTag.test(tag) && !svgArtwork(element) && paintedBy(element, /^tintColor$/)) {
|
|
934
|
+
const source = attribute(element, "source") ?? attribute(element, "src");
|
|
935
|
+
if (!source?.initializer || !glyphSource.test(source.initializer.getText(file)))
|
|
936
|
+
report(
|
|
937
|
+
element,
|
|
938
|
+
"art-tint",
|
|
939
|
+
`${tag} tints an image that is not a glyph asset; multi-colour art keeps its colours (LC-207).`,
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
for (const attr of element.attributes.properties)
|
|
943
|
+
if (
|
|
944
|
+
ts.isJsxAttribute(attr) &&
|
|
945
|
+
webPaintKey.test(name(attr.name)) &&
|
|
946
|
+
!(material && backdropKey.test(name(attr.name))) &&
|
|
947
|
+
!svgArtwork(element) &&
|
|
948
|
+
!/^[a-z]/.test(tag)
|
|
949
|
+
)
|
|
950
|
+
report(attr, "web-paint", `${name(attr.name)} is paint React Native cannot draw (LC-206).`);
|
|
951
|
+
}
|
|
952
|
+
function checkPaintKey(prop) {
|
|
953
|
+
if (!prop.name || ts.isComputedPropertyName(prop.name)) return;
|
|
954
|
+
const key = name(prop.name);
|
|
955
|
+
if (webPaintKey.test(key)) {
|
|
956
|
+
if (!(material && backdropKey.test(key)))
|
|
957
|
+
report(prop, "web-paint", `${key} is paint React Native cannot draw (LC-206).`);
|
|
958
|
+
} else if (
|
|
959
|
+
key === "filter" &&
|
|
960
|
+
ts.isPropertyAssignment(prop) &&
|
|
961
|
+
ts.isStringLiteralLike(prop.initializer) &&
|
|
962
|
+
cssFilter.test(prop.initializer.text)
|
|
963
|
+
)
|
|
964
|
+
report(
|
|
965
|
+
prop,
|
|
966
|
+
"web-paint",
|
|
967
|
+
`filter ${clip(prop.initializer.text)} is CSS React Native cannot draw (LC-206).`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
419
970
|
let tokenFile = false;
|
|
420
971
|
if (/^(?:design-)?tokens\.[cm]?[jt]sx?$/.test(path.basename(filename))) {
|
|
421
972
|
tokenFile = true;
|
|
@@ -444,7 +995,7 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
444
995
|
if (ts.isSpreadAssignment(prop)) inspectProps(prop.expression, seen, deep);
|
|
445
996
|
if (ts.isPropertyAssignment(prop) || ts.isShorthandPropertyAssignment(prop)) {
|
|
446
997
|
const initializer = ts.isShorthandPropertyAssignment(prop)
|
|
447
|
-
?
|
|
998
|
+
? typeChecker().getShorthandAssignmentValueSymbol(prop)?.valueDeclaration?.initializer
|
|
448
999
|
: prop.initializer;
|
|
449
1000
|
if (!initializer) return;
|
|
450
1001
|
const key = name(prop.name);
|
|
@@ -458,7 +1009,55 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
458
1009
|
}
|
|
459
1010
|
});
|
|
460
1011
|
}
|
|
1012
|
+
function moduleImported(node) {
|
|
1013
|
+
if (
|
|
1014
|
+
(ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
|
|
1015
|
+
node.moduleSpecifier &&
|
|
1016
|
+
ts.isStringLiteralLike(node.moduleSpecifier)
|
|
1017
|
+
)
|
|
1018
|
+
return node.moduleSpecifier.text;
|
|
1019
|
+
if (
|
|
1020
|
+
ts.isCallExpression(node) &&
|
|
1021
|
+
(node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
1022
|
+
(ts.isIdentifier(node.expression) && node.expression.text === "require")) &&
|
|
1023
|
+
node.arguments[0] &&
|
|
1024
|
+
ts.isStringLiteralLike(node.arguments[0])
|
|
1025
|
+
)
|
|
1026
|
+
return node.arguments[0].text;
|
|
1027
|
+
return undefined;
|
|
1028
|
+
}
|
|
1029
|
+
const phosphorShimFile = phosphorShim.test(filename);
|
|
461
1030
|
function visit(node) {
|
|
1031
|
+
if (
|
|
1032
|
+
ts.isStringLiteral(node) ||
|
|
1033
|
+
ts.isNoSubstitutionTemplateLiteral(node) ||
|
|
1034
|
+
ts.isTemplateExpression(node) ||
|
|
1035
|
+
ts.isJsxText(node)
|
|
1036
|
+
)
|
|
1037
|
+
checkCopy(node);
|
|
1038
|
+
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) checkElement(node);
|
|
1039
|
+
if (ts.isPropertyAssignment(node) || ts.isShorthandPropertyAssignment(node))
|
|
1040
|
+
checkPaintKey(node);
|
|
1041
|
+
if (
|
|
1042
|
+
(ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) &&
|
|
1043
|
+
/Props$/.test(node.name.text) &&
|
|
1044
|
+
exported(node)
|
|
1045
|
+
)
|
|
1046
|
+
checkProps(node);
|
|
1047
|
+
if (
|
|
1048
|
+
ts.isIdentifier(node) &&
|
|
1049
|
+
/^MagnifyingGlass(?:Plus|Minus)(?:Icon)?$/.test(node.text) &&
|
|
1050
|
+
!ts.isImportSpecifier(node.parent) &&
|
|
1051
|
+
!ts.isImportClause(node.parent)
|
|
1052
|
+
)
|
|
1053
|
+
report(node, "zoom-button", `${node.text} draws a zoom button; zoom is a gesture (LC-217).`);
|
|
1054
|
+
const imported = moduleImported(node);
|
|
1055
|
+
if (imported && !phosphorShimFile && phosphorBypass.test(imported))
|
|
1056
|
+
report(
|
|
1057
|
+
node,
|
|
1058
|
+
"phosphor-import",
|
|
1059
|
+
`Import icons from "@phosphor-icons/react"; "${imported}" skips MPO's native theme-ink IconBase and paints black in dark mode (FZ-7680).`,
|
|
1060
|
+
);
|
|
462
1061
|
const icon =
|
|
463
1062
|
ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)
|
|
464
1063
|
? node.tagName
|
|
@@ -472,6 +1071,10 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
472
1071
|
"Wrap Phosphor icons in themed() before rendering, or pass the component to an MPO icon slot.",
|
|
473
1072
|
);
|
|
474
1073
|
if (ts.isJsxElement(node)) checkFieldAction(node);
|
|
1074
|
+
if (ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node)) {
|
|
1075
|
+
checkActionWord(node);
|
|
1076
|
+
checkIconButton(node);
|
|
1077
|
+
}
|
|
475
1078
|
if (
|
|
476
1079
|
!tokenFile &&
|
|
477
1080
|
ts.isCallExpression(node) &&
|
|
@@ -579,7 +1182,7 @@ export function inspectDesign(source, filename, projectRoot = process.cwd()) {
|
|
|
579
1182
|
}
|
|
580
1183
|
function localFrame(tag) {
|
|
581
1184
|
if (!ts.isIdentifier(tag)) return undefined;
|
|
582
|
-
const declaration =
|
|
1185
|
+
const declaration = typeChecker().getSymbolAtLocation(tag)?.valueDeclaration;
|
|
583
1186
|
if (!declaration || !ts.isVariableDeclaration(declaration) || !declaration.initializer)
|
|
584
1187
|
return undefined;
|
|
585
1188
|
return styledPrimitive(unwrapComponent(declaration.initializer));
|
|
@@ -783,7 +1386,9 @@ export function designFiles(input, projectRoot = process.cwd()) {
|
|
|
783
1386
|
const info = statSync(root);
|
|
784
1387
|
const eligible = (filename) =>
|
|
785
1388
|
!isFreeformBoard(path.resolve(root, filename), projectRoot) &&
|
|
786
|
-
/\.[cm]?[jt]sx?$/.test(filename)
|
|
1389
|
+
(/\.[cm]?[jt]sx?$/.test(filename) ||
|
|
1390
|
+
/\.html?$/.test(filename) ||
|
|
1391
|
+
(/\.json$/.test(filename) && catalogFile.test(filename))) &&
|
|
787
1392
|
!/\.(?:test|spec|stories|config|gen|generated|d)\.[cm]?[jt]sx?$/.test(filename);
|
|
788
1393
|
if (info.isFile()) return eligible(root) ? [root] : [];
|
|
789
1394
|
let candidates;
|
|
@@ -840,27 +1445,52 @@ export function countDesign(errors) {
|
|
|
840
1445
|
return { total: errors.length, rules };
|
|
841
1446
|
}
|
|
842
1447
|
|
|
843
|
-
|
|
1448
|
+
// Baselines overlap (public/backoffice sits under public), so one run
|
|
1449
|
+
// inspects each file once per word list.
|
|
1450
|
+
const inspected = new Map();
|
|
1451
|
+
function inspectCached(filename, options) {
|
|
1452
|
+
const source = readFileSync(filename, "utf8");
|
|
1453
|
+
const key = `${filename}\0${JSON.stringify(options)}`;
|
|
1454
|
+
const hit = inspected.get(key);
|
|
1455
|
+
if (hit?.source === source) return hit.errors;
|
|
1456
|
+
const errors = inspectDesign(source, filename, process.cwd(), options);
|
|
1457
|
+
inspected.set(key, { source, errors });
|
|
1458
|
+
return errors;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
export function scanDesign(paths, options = {}) {
|
|
844
1462
|
const files = [...new Set(paths.flatMap((input) => designFiles(input)))];
|
|
845
1463
|
if (!files.length) throw new Error("No application source files found in the supplied paths.");
|
|
846
|
-
const errors = files.flatMap((filename) =>
|
|
847
|
-
inspectDesign(readFileSync(filename, "utf8"), filename),
|
|
848
|
-
);
|
|
1464
|
+
const errors = files.flatMap((filename) => inspectCached(filename, options));
|
|
849
1465
|
return { files, errors, ...countDesign(errors) };
|
|
850
1466
|
}
|
|
851
1467
|
|
|
852
1468
|
function parseArgs(args) {
|
|
853
|
-
const options = {
|
|
1469
|
+
const options = {
|
|
1470
|
+
paths: [],
|
|
1471
|
+
baselines: [],
|
|
1472
|
+
frozen: false,
|
|
1473
|
+
init: false,
|
|
1474
|
+
list: false,
|
|
1475
|
+
only: [],
|
|
1476
|
+
verifyCi: false,
|
|
1477
|
+
};
|
|
854
1478
|
for (let index = 0; index < args.length; index++) {
|
|
855
1479
|
const arg = args[index];
|
|
856
1480
|
if (arg === "--baseline") {
|
|
857
1481
|
if (!args[index + 1]) throw new Error("--baseline needs a file.");
|
|
858
1482
|
options.baselines.push(args[++index]);
|
|
859
1483
|
} else if (arg === "--only") {
|
|
1484
|
+
if (args[index + 1] === "harvest") {
|
|
1485
|
+
options.only.push(...harvestRules);
|
|
1486
|
+
index++;
|
|
1487
|
+
continue;
|
|
1488
|
+
}
|
|
860
1489
|
if (!designRules.includes(args[index + 1]))
|
|
861
|
-
throw new Error(`--only needs one of ${designRules.join(", ")}.`);
|
|
1490
|
+
throw new Error(`--only needs harvest or one of ${designRules.join(", ")}.`);
|
|
862
1491
|
options.only.push(args[++index]);
|
|
863
1492
|
} else if (arg === "--frozen") options.frozen = true;
|
|
1493
|
+
else if (arg === "--verify-ci") options.verifyCi = true;
|
|
864
1494
|
else if (arg === "--init") options.init = true;
|
|
865
1495
|
else if (arg === "--list") options.list = true;
|
|
866
1496
|
else if (arg.startsWith("--")) throw new Error(`Unknown option ${arg}.`);
|
|
@@ -894,7 +1524,7 @@ export function offenderKey(error, base) {
|
|
|
894
1524
|
|
|
895
1525
|
function offendersOf(scan, base) {
|
|
896
1526
|
const offenders = {};
|
|
897
|
-
for (const rule of [...identityRules].sort()) {
|
|
1527
|
+
for (const rule of [...identityRules].filter((item) => item in scan.rules).sort()) {
|
|
898
1528
|
const keys = {};
|
|
899
1529
|
for (const error of scan.errors.filter((item) => ruleOf(item) === rule)) {
|
|
900
1530
|
const key = offenderKey(error, base);
|
|
@@ -919,7 +1549,16 @@ function ratchet(file, options) {
|
|
|
919
1549
|
// A baseline cut with --only ratchets just those rules (the desk packages
|
|
920
1550
|
// carry LC-175 without taking on the app-only rules).
|
|
921
1551
|
const only = previous ? previous.only : options.only.length ? options.only : undefined;
|
|
922
|
-
|
|
1552
|
+
// LC-203: each app registers its banned words (and LC-202 its proper
|
|
1553
|
+
// names) in its own baseline.
|
|
1554
|
+
const words = {
|
|
1555
|
+
bannedWords: previous?.bannedWords ?? houseBannedWords,
|
|
1556
|
+
properNames: previous?.properNames ?? houseProperNames,
|
|
1557
|
+
};
|
|
1558
|
+
const full = scanDesign(
|
|
1559
|
+
paths.map((input) => path.resolve(base, input)),
|
|
1560
|
+
words,
|
|
1561
|
+
);
|
|
923
1562
|
const scan = only
|
|
924
1563
|
? (() => {
|
|
925
1564
|
const errors = full.errors.filter((error) => only.includes(ruleOf(error)));
|
|
@@ -935,6 +1574,7 @@ function ratchet(file, options) {
|
|
|
935
1574
|
const next = {
|
|
936
1575
|
paths,
|
|
937
1576
|
...(only ? { only } : {}),
|
|
1577
|
+
...(!only || only.includes("user-words") ? words : {}),
|
|
938
1578
|
total: scan.total,
|
|
939
1579
|
rules: Object.fromEntries(Object.entries(scan.rules).sort()),
|
|
940
1580
|
offenders: offendersOf(scan, base),
|
|
@@ -970,7 +1610,7 @@ function ratchet(file, options) {
|
|
|
970
1610
|
: ""
|
|
971
1611
|
}${
|
|
972
1612
|
added.length
|
|
973
|
-
? `; new offenders:\n ${added.join("\n ")}\
|
|
1613
|
+
? `; new offenders:\n ${added.join("\n ")}\nFix each the way its LC rule says (LC-175: render the MPO component). A rename of a listed offender renames its key in ${file}.\n`
|
|
974
1614
|
: ". "
|
|
975
1615
|
}${summary(scan)}.\n`,
|
|
976
1616
|
);
|
|
@@ -1004,9 +1644,93 @@ function ratchet(file, options) {
|
|
|
1004
1644
|
return 0;
|
|
1005
1645
|
}
|
|
1006
1646
|
|
|
1647
|
+
// LC-192 CHECKS-TRAVEL: an MPO app's CI runs this lint frozen against every
|
|
1648
|
+
// design baseline it commits. CI lines are followed one level into package
|
|
1649
|
+
// scripts and Makefile recipes.
|
|
1650
|
+
export function verifyCi(root = process.cwd()) {
|
|
1651
|
+
const read = (file) => {
|
|
1652
|
+
try {
|
|
1653
|
+
return readFileSync(path.join(root, file), "utf8");
|
|
1654
|
+
} catch {
|
|
1655
|
+
return "";
|
|
1656
|
+
}
|
|
1657
|
+
};
|
|
1658
|
+
const workflows = existsSync(path.join(root, ".github/workflows"))
|
|
1659
|
+
? readdirSync(path.join(root, ".github/workflows"))
|
|
1660
|
+
.filter((file) => /\.ya?ml$/.test(file))
|
|
1661
|
+
.map((file) => read(`.github/workflows/${file}`))
|
|
1662
|
+
: [];
|
|
1663
|
+
const ci = [read(".gitlab-ci.yml"), ...workflows].join("\n");
|
|
1664
|
+
if (!ci.trim())
|
|
1665
|
+
return ["no .gitlab-ci.yml or .github/workflows; CI must run mpo-design-lint --frozen."];
|
|
1666
|
+
let scripts = {};
|
|
1667
|
+
try {
|
|
1668
|
+
scripts = JSON.parse(read("package.json") || "{}").scripts ?? {};
|
|
1669
|
+
} catch {}
|
|
1670
|
+
const makefile = read("Makefile");
|
|
1671
|
+
const recipe = (target) => {
|
|
1672
|
+
const match = makefile.match(
|
|
1673
|
+
new RegExp(`^${target.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}:.*\n((?:\t.*\n?)*)`, "m"),
|
|
1674
|
+
);
|
|
1675
|
+
return match ? match[1] : "";
|
|
1676
|
+
};
|
|
1677
|
+
const expand = (line, depth = 0) => {
|
|
1678
|
+
if (depth > 3) return line;
|
|
1679
|
+
const parts = [line];
|
|
1680
|
+
for (const match of line.matchAll(/\b(?:pnpm|npm run|yarn)\s+(?:-s\s+|run\s+)?([\w:.-]+)/g))
|
|
1681
|
+
if (scripts[match[1]]) parts.push(expand(scripts[match[1]], depth + 1));
|
|
1682
|
+
for (const match of line.matchAll(/\bmake\s+(?:-s\s+)?([\w/.-]+)/g))
|
|
1683
|
+
parts.push(expand(recipe(match[1]), depth + 1));
|
|
1684
|
+
return parts.join("\n");
|
|
1685
|
+
};
|
|
1686
|
+
const lint = /mpo-design-lint|lint-mpo-design|design-lint\.mjs/;
|
|
1687
|
+
const runs = ci
|
|
1688
|
+
.split("\n")
|
|
1689
|
+
.filter((line) => !/^\s*#/.test(line))
|
|
1690
|
+
.map((line) => expand(line))
|
|
1691
|
+
.filter((text) => lint.test(text));
|
|
1692
|
+
if (!runs.length) return ["CI never runs mpo-design-lint (LC-192)."];
|
|
1693
|
+
const frozen = runs.filter((text) => /--frozen\b/.test(text));
|
|
1694
|
+
if (!frozen.length) return ["CI runs mpo-design-lint without --frozen (LC-192)."];
|
|
1695
|
+
const text = frozen.join("\n");
|
|
1696
|
+
return committedBaselines(root)
|
|
1697
|
+
.filter((file) => !text.includes(file))
|
|
1698
|
+
.map(
|
|
1699
|
+
(file) => `${file} is committed but CI never passes it to mpo-design-lint --frozen (LC-192).`,
|
|
1700
|
+
);
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
/**
|
|
1704
|
+
* Every committed design baseline: `design-baseline.json`, a scoped
|
|
1705
|
+
* `design-baseline.<scope>.json` and a per-rule `design-<rule>-baseline.json`
|
|
1706
|
+
* (the phosphor ratchet), so none can drop out of the frozen CI run.
|
|
1707
|
+
*/
|
|
1708
|
+
export function committedBaselines(root) {
|
|
1709
|
+
try {
|
|
1710
|
+
return execFileSync("git", ["ls-files", "-z", "--", "*design*baseline*.json"], {
|
|
1711
|
+
cwd: root,
|
|
1712
|
+
encoding: "utf8",
|
|
1713
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
1714
|
+
})
|
|
1715
|
+
.split("\0")
|
|
1716
|
+
.filter(Boolean);
|
|
1717
|
+
} catch {
|
|
1718
|
+
return [];
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1007
1722
|
export function runDesignLint(args = process.argv.slice(2)) {
|
|
1008
1723
|
try {
|
|
1009
1724
|
const options = parseArgs(args);
|
|
1725
|
+
if (options.verifyCi) {
|
|
1726
|
+
const problems = verifyCi(options.paths[0] ?? process.cwd());
|
|
1727
|
+
if (problems.length) {
|
|
1728
|
+
process.stderr.write(`${problems.map((item) => `MPO design guard: ${item}`).join("\n")}\n`);
|
|
1729
|
+
return 1;
|
|
1730
|
+
}
|
|
1731
|
+
process.stdout.write("MPO design guard: CI runs the design lint frozen (LC-192).\n");
|
|
1732
|
+
return 0;
|
|
1733
|
+
}
|
|
1010
1734
|
if (options.baselines.length)
|
|
1011
1735
|
return options.baselines.reduce((status, file) => ratchet(file, options) || status, 0);
|
|
1012
1736
|
const scan = scanDesign(options.paths.length ? options.paths : scopeFrom(process.cwd()));
|