@reqvire-org/ads 0.5.18 → 0.5.20
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/dist/ads-errors.mjs +3 -3
- package/dist/analysis/owner-source.mjs +7 -1
- package/dist/analysis/syntax.mjs +10 -0
- package/dist/construction-spec.d.mts +5 -0
- package/dist/construction-spec.mjs +6 -2
- package/dist/design-context-model.d.mts +2 -0
- package/dist/design-context-model.mjs +2 -1
- package/dist/design-usage.mjs +31 -2
- package/dist/foundation-macro.d.mts +5 -0
- package/dist/guard/components.mjs +235 -0
- package/dist/guard/run.mjs +3 -3
- package/dist/lowering-properties.mjs +2 -0
- package/dist/lowering-region-layout.mjs +2 -0
- package/dist/mcp/preview-http.bundle.js +3 -3
- package/dist/mcp/preview-widget.bundle.js +13 -13
- package/dist/ownership-model.mjs +1 -0
- package/dist/preview-resources.mjs +1 -0
- package/dist/recipe-macro.d.mts +6 -0
- package/dist/recipe-model.mjs +2 -1
- package/dist/render-audit/layout-overflow.page.mjs +1 -1
- package/dist/render-audit/worker.mjs +17 -9
- package/dist/render-audit/worker.page.mjs +18 -0
- package/dist/render-audit.mjs +1 -0
- package/dist/skills/ads-designer/references/accessibility.md +3 -1
- package/dist/skills/ads-designer/references/scenarios.md +9 -2
- package/dist/skills/ads-designer/references/stress-test.md +2 -1
- package/dist/skills/ads-import/references/scenarios.md +9 -2
- package/dist/skills/ads-port/references/scenarios.md +9 -2
- package/docs/CHANGELOG.md +126 -0
- package/docs/GUIDE.md +21 -4
- package/docs/LANGUAGE.md +1 -0
- package/docs/RULES.md +1 -1
- package/package.json +1 -1
package/dist/ads-errors.mjs
CHANGED
|
@@ -2726,7 +2726,7 @@ export const runtimeErrors = {
|
|
|
2726
2726
|
guides: ["ads://guides/tokens", "ads://guides/layout"],
|
|
2727
2727
|
repair: "Make the element fluid (%, fr, min(100%, ...)), wrap it or scroll it with overflow-x: auto.",
|
|
2728
2728
|
emitters: ["src/render-audit.mts"],
|
|
2729
|
-
situations:
|
|
2729
|
+
situations: `the same overflow kinds as layout-overflow, measured in the container render (first theme, widest viewport). A review, except kind "escaped": a control spilling over its own painted box is an error in a narrow pane too. A page section (its outermost grid lays out differently at a phone's and a desktop's viewport inside the 240 px container) is not measured: a render-audit-skipped info item names it. Code passes guidanceUri ads://guides/tokens.`
|
|
2730
2730
|
},
|
|
2731
2731
|
"layout-shift": {
|
|
2732
2732
|
group: "rendered-check",
|
|
@@ -3584,10 +3584,10 @@ export const runtimeErrors = {
|
|
|
3584
3584
|
}, reviewErrors = {
|
|
3585
3585
|
"scroll-unreachable": {
|
|
3586
3586
|
group: "accessibility-semantics",
|
|
3587
|
-
rule: "A scroll container is reachable by keyboard: it or content inside it is
|
|
3587
|
+
rule: "A scroll container is reachable by keyboard: it takes focus itself (any tabIndex), or content inside it is in the Tab order (a negative tabIndex inside is not). A popover or dialog root whose frame caps its height scrolls too. Some browsers (Safari) do not focus a scroller on their own.",
|
|
3588
3588
|
guides: [RECIPES, "ads://guides/accessibility"],
|
|
3589
3589
|
message: (review) => `${review.part === "&" ? "The root" : `Part "${review.part}"`} scrolls, but neither it nor owned content in it is focusable.`,
|
|
3590
|
-
repair: "
|
|
3590
|
+
repair: 'Scroll the element that is reachable: a listbox whose options an input names (`aria-activedescendant`) scrolls itself (`overflow: "scroll-block"` and the height cap on that part, not on the popover). Otherwise give the scroll container `tabIndex={0}` and an accessible name when its content holds nothing focusable; ignore this note when public components inside it are focusable.'
|
|
3591
3591
|
},
|
|
3592
3592
|
"marker-none-unlabelled": {
|
|
3593
3593
|
group: "accessibility-semantics",
|
|
@@ -702,7 +702,8 @@ async function analyzeOwner(root, emitted, name, program, options, rowsOf, fail)
|
|
|
702
702
|
return fail(at, { kind: "pressed-state-mismatch", asset: asset.name, state, pressed });
|
|
703
703
|
}
|
|
704
704
|
function scrollReview(opening, partName, at) {
|
|
705
|
-
|
|
705
|
+
const outOfOrder = (other) => /^(?:"-\d+"|\{\s*-\s*\d+\s*\})$/.test(attr(other, "tabIndex")?.initializer?.getText() ?? "");
|
|
706
|
+
if (focusable(opening) || descendants(opening).some((other) => focusable(other) && !outOfOrder(other)))
|
|
706
707
|
return;
|
|
707
708
|
review(at, { kind: "scroll-unreachable", part: partName });
|
|
708
709
|
}
|
|
@@ -949,6 +950,11 @@ async function analyzeOwner(root, emitted, name, program, options, rowsOf, fail)
|
|
|
949
950
|
await pointerTransparent(opening, "&", context, resolve);
|
|
950
951
|
if (region.overflow.kind === "scroll")
|
|
951
952
|
scrollReview(opening, "&", node);
|
|
953
|
+
else if (region.layer !== null && region.profile !== null) {
|
|
954
|
+
const selected = selectedProfile(foundation, region.profile, region.select), frameName = selected.kind === "ok" && selected.value.kind === "composite" ? selected.value.fields.get("frame-profile") : selected.kind === "ok" && selected.value.kind === "frame" ? region.profile : void 0, frame = frameName === void 0 ? void 0 : selectedProfile(foundation, frameName);
|
|
955
|
+
if (frame?.kind === "ok" && (frame.value.fields.has("max-block-size") || frame.value.fields.has("block-size")))
|
|
956
|
+
scrollReview(opening, "&", node);
|
|
957
|
+
}
|
|
952
958
|
if (region.profile !== null && region.text === null) {
|
|
953
959
|
const selected = selectedProfile(foundation, region.profile, region.select), textProfile = selected.kind === "ok" && selected.value.kind === "composite" ? selected.value.fields.get("text-profile") : void 0, hasText = (list) => list.some((part) => part.content.kind === "text" || part.frame !== null || part.text !== null || hasText(part.parts));
|
|
954
960
|
if (textProfile !== void 0 && !hasText(region.parts))
|
package/dist/analysis/syntax.mjs
CHANGED
|
@@ -97,6 +97,16 @@ export async function parseSource(file, text, scriptKind) {
|
|
|
97
97
|
return (await lease).sourceFile;
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
|
+
export function withOpenFiles(files, run) {
|
|
101
|
+
return use(async (current) => {
|
|
102
|
+
const snapshot = await current.createSnapshot({ openFiles: files.map((file) => path.resolve(file)) });
|
|
103
|
+
try {
|
|
104
|
+
return await run(snapshot);
|
|
105
|
+
} finally {
|
|
106
|
+
await snapshot.dispose();
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
100
110
|
export function parseConfigText(file, text) {
|
|
101
111
|
return use(async (current) => {
|
|
102
112
|
const virtual = path.resolve(path.dirname(file), `.ads-config-${++sequence}.json`);
|
|
@@ -670,6 +670,11 @@ export interface TextLayout {
|
|
|
670
670
|
readonly reserveLines: 1 | 2 | 3 | null;
|
|
671
671
|
/** Truncated text clamped to at most this many lines (`wrap: "truncate"` only); `null` truncates on one line. */
|
|
672
672
|
readonly lines: 2 | 3 | null;
|
|
673
|
+
/**
|
|
674
|
+
* `hyphens: "auto"`: a word too long for its line breaks at a hyphenation point of the document's language, with a
|
|
675
|
+
* hyphen (a long heading word at 200% text on a phone); `null` breaks words only where the text allows.
|
|
676
|
+
*/
|
|
677
|
+
readonly hyphens?: "auto" | null;
|
|
673
678
|
}
|
|
674
679
|
/** What fills a visual part: the owner's bound asset or `<img>` content media, or a sanitized SVG of the inspected project the owner inserts. */
|
|
675
680
|
export type VisualMedia = "bound" | "inspected-svg";
|
|
@@ -976,8 +976,11 @@ function textLayout(part, path, read) {
|
|
|
976
976
|
wrap: wrap === void 0 || wrap === !1 ? null : wrap === !0 ? "anywhere" : read.literal(wrap, `${path}.wrap`, TEXT_WRAPS),
|
|
977
977
|
align: part.textAlign === void 0 ? null : read.literal(part.textAlign, `${path}.textAlign`, TEXT_ALIGNS),
|
|
978
978
|
reserveLines: reserved === void 0 ? null : reserved === 1 || reserved === 2 || reserved === 3 ? reserved : read.invalid(`${path}.reserveLines`, "1 | 2 | 3"),
|
|
979
|
-
lines: clamp === void 0 ? null : clamp === 2 || clamp === 3 ? clamp : read.invalid(`${path}.lines`, '2 | 3 (one line is plain wrap: "truncate")')
|
|
979
|
+
lines: clamp === void 0 ? null : clamp === 2 || clamp === 3 ? clamp : read.invalid(`${path}.lines`, '2 | 3 (one line is plain wrap: "truncate")'),
|
|
980
|
+
hyphens: part.hyphens === void 0 ? null : read.literal(part.hyphens, `${path}.hyphens`, ["auto"])
|
|
980
981
|
};
|
|
982
|
+
if (layout.hyphens === "auto" && (layout.wrap === "never" || layout.wrap === "truncate" || layout.wrap === "preserve"))
|
|
983
|
+
read.invalid(`${path}.hyphens`, `absent with wrap: "${layout.wrap}" (that text is not broken across lines)`);
|
|
981
984
|
if (layout.lines !== null && layout.wrap !== "truncate")
|
|
982
985
|
read.invalid(`${path}.wrap`, layout.wrap === "balance" || layout.wrap === "pretty" ? '"truncate" when lines clamps the text (breaks of text that is cut off are not balanced)' : '"truncate" when lines clamps the text');
|
|
983
986
|
if (layout.lines !== null && part.element !== void 0)
|
|
@@ -1045,7 +1048,7 @@ function partContent(part, path, read) {
|
|
|
1045
1048
|
case "visual":
|
|
1046
1049
|
return { kind, media: part.media === void 0 ? "bound" : read.literal(part.media, `${path}.media`, ["inspected-svg"]) };
|
|
1047
1050
|
case "vector": {
|
|
1048
|
-
for (const field of ["recipe", "parts", "text", "frame", "wrap", "textAlign", "reserveLines", "lines", "overflow", "extent", "element", "pseudo", "normalize", "enter", "sticky", "transition", "motion", "play"])
|
|
1051
|
+
for (const field of ["recipe", "parts", "text", "frame", "wrap", "textAlign", "reserveLines", "lines", "hyphens", "overflow", "extent", "element", "pseudo", "normalize", "enter", "sticky", "transition", "motion", "play"])
|
|
1049
1052
|
if (part[field] !== void 0)
|
|
1050
1053
|
read.reject({ kind: "field-unsupported", construction: "region", path: `${path}.${field}` });
|
|
1051
1054
|
return { kind, stroke: read.slot(part.stroke, `${path}.stroke`, "visual").field };
|
|
@@ -1168,6 +1171,7 @@ function regionPart(name, value, read, path, nested) {
|
|
|
1168
1171
|
"textAlign",
|
|
1169
1172
|
"reserveLines",
|
|
1170
1173
|
"lines",
|
|
1174
|
+
"hyphens",
|
|
1171
1175
|
"text",
|
|
1172
1176
|
"surface",
|
|
1173
1177
|
"frame",
|
|
@@ -13,6 +13,8 @@ export interface Brand {
|
|
|
13
13
|
/** A design system's declared contexts. The first theme, and the first brand when there are brands, is the default. */
|
|
14
14
|
export interface DesignContexts {
|
|
15
15
|
readonly themes: readonly Theme[];
|
|
16
|
+
/** `language`: the content's language (BCP 47), which previews declare on `<html lang>`. */
|
|
17
|
+
readonly language?: string;
|
|
16
18
|
/** Optional brand axis beside the themes; empty means one brand. */
|
|
17
19
|
readonly brands: readonly Brand[];
|
|
18
20
|
/**
|
|
@@ -64,7 +64,8 @@ export function foundationContextsOf(spec) {
|
|
|
64
64
|
const sections = spec.sections;
|
|
65
65
|
if (sections !== void 0 && sections !== !0)
|
|
66
66
|
return err({ axis: "both", kind: "sections-invalid", path: ["sections"], message: "sections is `true` or omitted: `true` applies the themes and brands to nested data-theme/data-brand elements." });
|
|
67
|
-
|
|
67
|
+
const language = typeof spec.language === "string" ? spec.language : void 0;
|
|
68
|
+
return ok({ themes: themes.value, brands: brands.value, ...follow.value === void 0 ? {} : { followSystem: follow.value }, ...sections === !0 ? { sections } : {}, ...language === void 0 ? {} : { language } });
|
|
68
69
|
}
|
|
69
70
|
function systemThemes(follow, themes) {
|
|
70
71
|
if (follow === void 0)
|
package/dist/design-usage.mjs
CHANGED
|
@@ -2,14 +2,23 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { isExportDeclaration, isNamedExports, isStringLiteral } from "typescript/ast";
|
|
4
4
|
import { designUsageProblems } from "./analysis/design-usage.mjs";
|
|
5
|
-
import { closeSyntax, parseSource } from "./analysis/syntax.mjs";
|
|
5
|
+
import { closeSyntax, parseSource, withOpenFiles } from "./analysis/syntax.mjs";
|
|
6
|
+
import { typedDesignUsage } from "./guard/components.mjs";
|
|
6
7
|
const wording = {
|
|
7
8
|
"design-import": (problem) => [`${problem.specifier} authors design, which belongs to the design system.`, "Declare the design in the design system and import the owner from its public entry."],
|
|
8
9
|
"component-outside-pattern": (problem) => [
|
|
9
10
|
`The component ${problem.name} is rendered outside every pattern, so nothing owns its place on the screen.`,
|
|
10
11
|
"Render it inside the pattern that arranges this screen (as its child or in a slot prop); if no pattern does, add one to the design system."
|
|
11
12
|
],
|
|
12
|
-
"local-markup": (problem) => [`<${problem.tag}> is
|
|
13
|
+
"local-markup": (problem) => [`<${problem.tag}> is markup the application creates itself, not a design-system owner.`, "Render a design-system owner; an arrangement or element the screen needs belongs in a pattern of the design system."],
|
|
14
|
+
"unlisted-component": (problem) => [
|
|
15
|
+
`${problem.name} is a component of ${problem.from}, which the guard does not list: its markup and styles are not the design system's.`,
|
|
16
|
+
`Render a design-system owner instead. If it renders no design of its own (a router's outlet, a provider, Suspense), list it in "allow" under "${problem.from}".`
|
|
17
|
+
],
|
|
18
|
+
"unchecked-component": (problem) => [
|
|
19
|
+
`${problem.name} is declared in ${problem.file}, which the guard does not check, so its markup and styles are not seen.`,
|
|
20
|
+
`Check that file too (in the guard's "include", not "exclude"), or render a design-system owner.`
|
|
21
|
+
],
|
|
13
22
|
"spread-props": () => ["A spread passes props nothing can see.", "Pass each prop explicitly."],
|
|
14
23
|
"design-prop": (problem) => [`${problem.attribute} supplies design from outside the design system.`, "Remove it; choose the treatment through the owner's own props."],
|
|
15
24
|
"imperative-design": (problem) => [`${problem.member} writes styles or creates DOM by hand.`, "Remove the write; render design-system owners with props."],
|
|
@@ -29,6 +38,26 @@ export async function checkDesignUsage(sources, entry, allowance = {}) {
|
|
|
29
38
|
}
|
|
30
39
|
return findings;
|
|
31
40
|
}
|
|
41
|
+
export async function checkApplicationDesignUsage(root, files, entry, designSystem, allowance = {}) {
|
|
42
|
+
const absolute = files.map((file) => path.resolve(root, file));
|
|
43
|
+
if (absolute.length === 0)
|
|
44
|
+
return [];
|
|
45
|
+
const scope = { root: path.resolve(root), checked: new Set(absolute), designSystem: path.resolve(designSystem), publicEntry: entry.publicEntry, allow: allowance.components ?? new Map };
|
|
46
|
+
return withOpenFiles(absolute, async (snapshot) => {
|
|
47
|
+
const findings = [];
|
|
48
|
+
for (const [index, file] of absolute.entries()) {
|
|
49
|
+
const project = await snapshot.getDefaultProjectForFile(file), source = project === void 0 ? void 0 : await project.program.getSourceFile(file);
|
|
50
|
+
if (project === void 0 || source === void 0)
|
|
51
|
+
throw Error(`TypeScript could not open ${files[index]} in a project.`);
|
|
52
|
+
const syntactic = designUsageProblems(source, entry, allowance);
|
|
53
|
+
for (const { node, problem } of await typedDesignUsage(source, project.checker, scope, syntactic)) {
|
|
54
|
+
const at = source.getLineAndCharacterOfPosition(node.getStart(source)), [message, repair] = describe(problem);
|
|
55
|
+
findings.push({ file: files[index] ?? file, line: at.line + 1, column: at.character + 1, code: `design-usage/${problem.kind}`, message, repair });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return findings;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
32
61
|
export async function designSystemEntry(publicEntry, entryFile) {
|
|
33
62
|
const source = await parseSource(path.resolve(entryFile), readFileSync(entryFile, "utf8")), patterns = new Set;
|
|
34
63
|
for (const statement of source.statements) {
|
|
@@ -47,6 +47,11 @@ export interface FoundationSpec {
|
|
|
47
47
|
* modal's scroll lock, which hides the scrollbar, shifts nothing; omitted, the browser decides.
|
|
48
48
|
*/
|
|
49
49
|
readonly scrollbarGutter?: "stable";
|
|
50
|
+
/**
|
|
51
|
+
* The language of the content, a BCP 47 tag (`"en"`, `"de-CH"`). Previews declare it on `<html lang>`, so text
|
|
52
|
+
* parts with `hyphens: "auto"` hyphenate there as in the application (which sets its own `<html lang>`).
|
|
53
|
+
*/
|
|
54
|
+
readonly language?: string;
|
|
50
55
|
}
|
|
51
56
|
/** A paint-role token, by name. */
|
|
52
57
|
export type PaintToken = `--${string}`;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
SyntaxKind,
|
|
4
|
+
isArrowFunction,
|
|
5
|
+
isCallExpression,
|
|
6
|
+
isFunctionExpression,
|
|
7
|
+
isIdentifier,
|
|
8
|
+
isImportDeclaration,
|
|
9
|
+
isJsxElement,
|
|
10
|
+
isJsxOpeningElement,
|
|
11
|
+
isJsxSelfClosingElement,
|
|
12
|
+
isNamedImports,
|
|
13
|
+
isNamespaceImport,
|
|
14
|
+
isNoSubstitutionTemplateLiteral,
|
|
15
|
+
isObjectLiteralExpression,
|
|
16
|
+
isArrayLiteralExpression,
|
|
17
|
+
isPropertyAccessExpression,
|
|
18
|
+
isStringLiteral,
|
|
19
|
+
isTaggedTemplateExpression,
|
|
20
|
+
isTemplateExpression
|
|
21
|
+
} from "typescript/ast";
|
|
22
|
+
import { SignatureKind, SymbolFlags, TypeFlags } from "typescript/async";
|
|
23
|
+
const FRAMEWORK_FACTORIES = new Set(["h", "createElement", "jsx", "jsxs", "jsxDEV"]), NOT_A_VALUE = TypeFlags.Any | TypeFlags.Unknown | TypeFlags.Never | TypeFlags.Void | TypeFlags.Undefined | TypeFlags.Null, intrinsic = (tag) => /^[a-z]/.test(tag) && !tag.includes("."), relative = (specifier) => specifier.startsWith(".") || specifier.startsWith("/"), walk = (node, visit) => {
|
|
24
|
+
visit(node);
|
|
25
|
+
node.forEachChild((child) => walk(child, visit));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class FileReading {
|
|
29
|
+
source;
|
|
30
|
+
checker;
|
|
31
|
+
scope;
|
|
32
|
+
#element;
|
|
33
|
+
constructor(source, checker, scope) {
|
|
34
|
+
this.source = source;
|
|
35
|
+
this.checker = checker;
|
|
36
|
+
this.scope = scope;
|
|
37
|
+
}
|
|
38
|
+
async element() {
|
|
39
|
+
if (this.#element !== void 0)
|
|
40
|
+
return this.#element;
|
|
41
|
+
let first;
|
|
42
|
+
walk(this.source, (node) => {
|
|
43
|
+
if (first === void 0 && (isJsxElement(node) || isJsxSelfClosingElement(node)))
|
|
44
|
+
first = node;
|
|
45
|
+
});
|
|
46
|
+
this.#element = first === void 0 ? null : await this.checker.getTypeAtLocation(first);
|
|
47
|
+
if (this.#element !== null && (this.#element.flags & NOT_A_VALUE) !== 0)
|
|
48
|
+
this.#element = null;
|
|
49
|
+
return this.#element;
|
|
50
|
+
}
|
|
51
|
+
async renders(type, depth = 0) {
|
|
52
|
+
if ((type.flags & NOT_A_VALUE) !== 0 || depth > 2)
|
|
53
|
+
return !1;
|
|
54
|
+
if ((type.flags & TypeFlags.Union) !== 0) {
|
|
55
|
+
for (const member of await type.getTypes() ?? [])
|
|
56
|
+
if (await this.renders(member, depth + 1))
|
|
57
|
+
return !0;
|
|
58
|
+
return !1;
|
|
59
|
+
}
|
|
60
|
+
const element = await this.element();
|
|
61
|
+
if (element === null)
|
|
62
|
+
return !1;
|
|
63
|
+
for (const signature of await this.checker.getSignaturesOfType(type, SignatureKind.Call)) {
|
|
64
|
+
const result = await this.checker.getReturnTypeOfSignature(signature);
|
|
65
|
+
if ((result.flags & NOT_A_VALUE) === 0 && await this.checker.isTypeAssignableTo(element, result))
|
|
66
|
+
return !0;
|
|
67
|
+
}
|
|
68
|
+
for (const signature of await this.checker.getSignaturesOfType(type, SignatureKind.Construct)) {
|
|
69
|
+
const instance = await this.checker.getReturnTypeOfSignature(signature);
|
|
70
|
+
if ((await this.checker.getPropertiesOfType(instance)).some((property) => property.name === "render"))
|
|
71
|
+
return !0;
|
|
72
|
+
}
|
|
73
|
+
return !1;
|
|
74
|
+
}
|
|
75
|
+
async origin(node) {
|
|
76
|
+
let symbol = await this.checker.getSymbolAtLocation(node);
|
|
77
|
+
if (symbol === void 0)
|
|
78
|
+
return { kind: "unknown" };
|
|
79
|
+
if ((symbol.flags & SymbolFlags.Alias) !== 0)
|
|
80
|
+
symbol = await this.checker.getAliasedSymbol(symbol);
|
|
81
|
+
const files = symbol.declarations.map((declaration) => path.resolve(String(declaration.path)));
|
|
82
|
+
if (files.length === 0)
|
|
83
|
+
return { kind: "unknown" };
|
|
84
|
+
if (files.some((file) => this.#checked(file)))
|
|
85
|
+
return { kind: "application" };
|
|
86
|
+
if (files.some((file) => this.#inDesignSystem(file)))
|
|
87
|
+
return { kind: "design-system" };
|
|
88
|
+
return { kind: "elsewhere", file: files[0] ?? "" };
|
|
89
|
+
}
|
|
90
|
+
#checked(file) {
|
|
91
|
+
return this.scope.checked.has(file) || [...this.scope.checked].some((checked) => checked.toLowerCase() === file.toLowerCase());
|
|
92
|
+
}
|
|
93
|
+
#inDesignSystem(file) {
|
|
94
|
+
const folder = this.scope.designSystem.toLowerCase() + path.sep;
|
|
95
|
+
return file.toLowerCase().startsWith(folder);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function packageOf(file) {
|
|
99
|
+
const parts = file.split(path.sep), at = parts.lastIndexOf("node_modules");
|
|
100
|
+
if (at < 0)
|
|
101
|
+
return file;
|
|
102
|
+
const first = parts[at + 1] ?? "";
|
|
103
|
+
return first.startsWith("@") ? `${first}/${parts[at + 2] ?? ""}` : first;
|
|
104
|
+
}
|
|
105
|
+
export async function typedDesignUsage(source, checker, scope, syntactic) {
|
|
106
|
+
const reading = new FileReading(source, checker, scope), settled = [], imported = new Map, namespaces = new Map, flagged = new Set, flaggedNames = new Set;
|
|
107
|
+
for (const statement of source.statements) {
|
|
108
|
+
if (!isImportDeclaration(statement) || !isStringLiteral(statement.moduleSpecifier) || statement.importClause?.phaseModifier === SyntaxKind.TypeKeyword)
|
|
109
|
+
continue;
|
|
110
|
+
const specifier = statement.moduleSpecifier.text;
|
|
111
|
+
if (relative(specifier) || specifier === scope.publicEntry || specifier.startsWith("node:"))
|
|
112
|
+
continue;
|
|
113
|
+
const clause = statement.importClause;
|
|
114
|
+
if (clause === void 0)
|
|
115
|
+
continue;
|
|
116
|
+
if (clause.name !== void 0)
|
|
117
|
+
imported.set(clause.name.text, [specifier, "default"]);
|
|
118
|
+
const bindings = clause.namedBindings;
|
|
119
|
+
if (bindings !== void 0 && isNamespaceImport(bindings))
|
|
120
|
+
namespaces.set(bindings.name.text, specifier);
|
|
121
|
+
else if (bindings !== void 0 && isNamedImports(bindings)) {
|
|
122
|
+
for (const element of bindings.elements)
|
|
123
|
+
if (!element.isTypeOnly)
|
|
124
|
+
imported.set(element.name.text, [specifier, (element.propertyName ?? element.name).text]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const allowed = (specifier, name) => scope.allow.get(specifier)?.has(name) === !0, unlisted = (node, name, from) => {
|
|
128
|
+
if (flagged.has(node))
|
|
129
|
+
return;
|
|
130
|
+
flagged.add(node);
|
|
131
|
+
settled.push({ node, problem: { kind: "unlisted-component", name, from } });
|
|
132
|
+
};
|
|
133
|
+
for (const statement of source.statements) {
|
|
134
|
+
if (!isImportDeclaration(statement) || !isStringLiteral(statement.moduleSpecifier))
|
|
135
|
+
continue;
|
|
136
|
+
const clause = statement.importClause;
|
|
137
|
+
if (clause === void 0 || clause.phaseModifier === SyntaxKind.TypeKeyword)
|
|
138
|
+
continue;
|
|
139
|
+
const names = [clause.name, ...clause.namedBindings !== void 0 && isNamedImports(clause.namedBindings) ? clause.namedBindings.elements.filter((element) => !element.isTypeOnly).map((element) => element.name) : []];
|
|
140
|
+
for (const name of names) {
|
|
141
|
+
if (name === void 0)
|
|
142
|
+
continue;
|
|
143
|
+
const binding = imported.get(name.text);
|
|
144
|
+
if (binding === void 0 || allowed(...binding) || FRAMEWORK_FACTORIES.has(binding[1]))
|
|
145
|
+
continue;
|
|
146
|
+
if (await reading.renders(await checker.getTypeAtLocation(name))) {
|
|
147
|
+
unlisted(name, binding[1] === "default" ? name.text : binding[1], binding[0]);
|
|
148
|
+
flaggedNames.add(name.text);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const designProblems = new Map;
|
|
153
|
+
for (const usage of syntactic) {
|
|
154
|
+
if (usage.problem.kind !== "local-markup") {
|
|
155
|
+
settled.push(usage);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
designProblems.set(usage.node, usage);
|
|
159
|
+
}
|
|
160
|
+
const members = [];
|
|
161
|
+
walk(source, (node) => {
|
|
162
|
+
if (isPropertyAccessExpression(node) && isIdentifier(node.expression)) {
|
|
163
|
+
const base = node.expression.text, specifier = namespaces.get(base) ?? imported.get(base)?.[0];
|
|
164
|
+
if (specifier !== void 0 && !allowed(specifier, node.name.text))
|
|
165
|
+
members.push(node);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
for (const member of members) {
|
|
169
|
+
if (!isPropertyAccessExpression(member) || !isIdentifier(member.expression))
|
|
170
|
+
continue;
|
|
171
|
+
const specifier = namespaces.get(member.expression.text) ?? imported.get(member.expression.text)?.[0] ?? "";
|
|
172
|
+
if (flaggedNames.has(member.expression.text))
|
|
173
|
+
continue;
|
|
174
|
+
if (await reading.renders(await checker.getTypeAtLocation(member)))
|
|
175
|
+
unlisted(member, `${member.expression.text}.${member.name.text}`, specifier);
|
|
176
|
+
}
|
|
177
|
+
const calls = [];
|
|
178
|
+
walk(source, (node) => {
|
|
179
|
+
if (isCallExpression(node) || isTaggedTemplateExpression(node))
|
|
180
|
+
calls.push(node);
|
|
181
|
+
});
|
|
182
|
+
for (const call of calls) {
|
|
183
|
+
const callee = isCallExpression(call) ? call.expression : isTaggedTemplateExpression(call) ? call.tag : void 0;
|
|
184
|
+
if (callee === void 0)
|
|
185
|
+
continue;
|
|
186
|
+
const root = isIdentifier(callee) ? callee : isPropertyAccessExpression(callee) && isIdentifier(callee.expression) ? callee.expression : void 0;
|
|
187
|
+
if (root === void 0)
|
|
188
|
+
continue;
|
|
189
|
+
const specifier = imported.get(root.text)?.[0] ?? namespaces.get(root.text);
|
|
190
|
+
if (specifier === void 0)
|
|
191
|
+
continue;
|
|
192
|
+
const name = isIdentifier(callee) ? imported.get(callee.text)?.[1] ?? callee.text : isPropertyAccessExpression(callee) ? callee.name.text : "";
|
|
193
|
+
if (isCallExpression(call) && FRAMEWORK_FACTORIES.has(name)) {
|
|
194
|
+
const [first] = call.arguments;
|
|
195
|
+
if (first !== void 0 && (isStringLiteral(first) || isNoSubstitutionTemplateLiteral(first) || ((await checker.getTypeAtLocation(first)).flags & TypeFlags.StringLike) !== 0))
|
|
196
|
+
settled.push({ node: call, problem: { kind: "local-markup", tag: isStringLiteral(first) || isNoSubstitutionTemplateLiteral(first) ? first.text : first.getText() } });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (allowed(specifier, name))
|
|
200
|
+
continue;
|
|
201
|
+
if (!await reading.renders(await checker.getTypeAtLocation(call)))
|
|
202
|
+
continue;
|
|
203
|
+
const literal = (argument) => isStringLiteral(argument) || isNoSubstitutionTemplateLiteral(argument) || isTemplateExpression(argument) || isObjectLiteralExpression(argument) || isArrayLiteralExpression(argument), args = isCallExpression(call) ? [...call.arguments] : [];
|
|
204
|
+
if (!(!isTaggedTemplateExpression(call) && args.length > 0 && args.every((argument) => !literal(argument)) && (await Promise.all(args.map(async (argument) => isArrowFunction(argument) || isFunctionExpression(argument) || (await reading.origin(argument)).kind !== "elsewhere"))).every(Boolean)))
|
|
205
|
+
unlisted(call, name, specifier);
|
|
206
|
+
}
|
|
207
|
+
for (const [node, usage] of designProblems) {
|
|
208
|
+
if (usage.problem.kind !== "local-markup")
|
|
209
|
+
continue;
|
|
210
|
+
const tag = isJsxOpeningElement(node) || isJsxSelfClosingElement(node) ? node.tagName : void 0;
|
|
211
|
+
if (tag === void 0 || intrinsic(usage.problem.tag)) {
|
|
212
|
+
settled.push(usage);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (((await checker.getTypeAtLocation(tag)).flags & TypeFlags.StringLike) !== 0) {
|
|
216
|
+
settled.push(usage);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const root = isIdentifier(tag) ? tag : isPropertyAccessExpression(tag) && isIdentifier(tag.expression) ? tag.expression : void 0;
|
|
220
|
+
if (root !== void 0 && (imported.has(root.text) || namespaces.has(root.text))) {
|
|
221
|
+
const [specifier, name] = imported.get(root.text) ?? [namespaces.get(root.text) ?? "", root.text], exported = isPropertyAccessExpression(tag) ? tag.name.text : name;
|
|
222
|
+
if (!allowed(specifier, exported) && !flaggedNames.has(root.text) && !flagged.has(tag))
|
|
223
|
+
unlisted(tag, isPropertyAccessExpression(tag) ? tag.getText() : exported, specifier);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
const origin = await reading.origin(tag);
|
|
227
|
+
if (origin.kind !== "elsewhere")
|
|
228
|
+
continue;
|
|
229
|
+
if (origin.file.split(path.sep).includes("node_modules"))
|
|
230
|
+
unlisted(tag, tag.getText(), packageOf(origin.file));
|
|
231
|
+
else
|
|
232
|
+
settled.push({ node: tag, problem: { kind: "unchecked-component", name: tag.getText(), file: path.relative(scope.root, origin.file).split(path.sep).join("/") } });
|
|
233
|
+
}
|
|
234
|
+
return settled.sort((a, b) => a.node.pos - b.node.pos);
|
|
235
|
+
}
|
package/dist/guard/run.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { existsSync
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import {
|
|
4
|
+
import { checkApplicationDesignUsage, designSystemEntry } from "../design-usage.mjs";
|
|
5
5
|
import { err, ok } from "../result.mjs";
|
|
6
6
|
import { readGuardConfig } from "./config.mjs";
|
|
7
7
|
import { guardScope } from "./files.mjs";
|
|
@@ -30,7 +30,7 @@ export async function runGuard(root) {
|
|
|
30
30
|
const located = entryFile(absolute, config.value);
|
|
31
31
|
if (located.kind === "err")
|
|
32
32
|
return err([located.error]);
|
|
33
|
-
const entry = await designSystemEntry(config.value.designSystem, located.value), folder = path.dirname(located.value), scope = guardScope(absolute, config.value.include, config.value.exclude, folder.startsWith(absolute + path.sep) ? folder : null),
|
|
33
|
+
const entry = await designSystemEntry(config.value.designSystem, located.value), folder = path.dirname(located.value), scope = guardScope(absolute, config.value.include, config.value.exclude, folder.startsWith(absolute + path.sep) ? folder : null), findings = await checkApplicationDesignUsage(absolute, scope.files, entry, folder, { components: config.value.allow, metadata: !0 }), inside = located.value.startsWith(absolute + path.sep), externals = await verifyExternals(absolute, located.value);
|
|
34
34
|
return ok({
|
|
35
35
|
ok: findings.length === 0 && externals.every((item) => item.problem === null),
|
|
36
36
|
designSystem: config.value.designSystem,
|
|
@@ -138,6 +138,8 @@ export function textLayoutValues(layout, part, hasText) {
|
|
|
138
138
|
}
|
|
139
139
|
if (layout.align !== null)
|
|
140
140
|
values["text-align"] = layout.align;
|
|
141
|
+
if (layout.hyphens === "auto")
|
|
142
|
+
Object.assign(values, { "-webkit-hyphens": "auto", hyphens: "auto" });
|
|
141
143
|
if (layout.reserveLines !== null) {
|
|
142
144
|
if (!hasText)
|
|
143
145
|
return err({ kind: "reserve-without-text", part });
|