@reqvire-org/ads-guard 0.5.2 → 0.5.4
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
CHANGED
|
@@ -75,7 +75,6 @@ files are never checked. The report counts them.
|
|
|
75
75
|
| Code | Meaning | What to do |
|
|
76
76
|
| --- | --- | --- |
|
|
77
77
|
| `design-usage/local-markup` | An element or a local component that is not imported from the design system. | Render a design-system owner. An arrangement the screen needs is a pattern of the design system. |
|
|
78
|
-
| `design-usage/prose-element-undeclared` | A plain element inside a prose owner (long-form content) that the owner's recipe does not declare. | Declare the element in the design system's prose recipe, or write the content with the elements it declares. |
|
|
79
78
|
| `design-usage/component-outside-pattern` | A component of the design system rendered outside every pattern. | A pattern arranges the screen; a component fills a pattern, as its child or in a slot prop. |
|
|
80
79
|
| `design-usage/design-prop` | `style`, `className`, `css` or `dangerouslySetInnerHTML`. | Choose the treatment through the owner's own props. |
|
|
81
80
|
| `design-usage/spread-props` | A JSX spread. | Pass each prop explicitly. |
|
|
@@ -135,13 +134,9 @@ await closeDesignUsage(); // releases the parser
|
|
|
135
134
|
|
|
136
135
|
## Long-form content
|
|
137
136
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
Content that arrives as an HTML string is passed to a component of the design
|
|
146
|
-
system that takes it as a prop (such as the starter's `ProseHtml`); injecting it
|
|
147
|
-
in application source is `design-usage/design-prop`. Sanitise the string first.
|
|
137
|
+
Long-form content (articles, documentation, Markdown or a content system's rich
|
|
138
|
+
text) is rendered by a design-system pattern from a content tree: the
|
|
139
|
+
application passes data, never plain markup or an HTML string. Plain markup in
|
|
140
|
+
application source is `design-usage/local-markup`, and injecting an HTML string
|
|
141
|
+
is `design-usage/design-prop`. Adapt Markdown, HTML (parsed and sanitised) or
|
|
142
|
+
rich text to the tree the design system's content pattern takes.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/design-usage.mts
|
|
2
|
-
import {
|
|
2
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
3
3
|
import path2 from "node:path";
|
|
4
4
|
import { isExportDeclaration, isNamedExports, isStringLiteral as isStringLiteral2 } from "typescript/ast";
|
|
5
5
|
|
|
@@ -26,68 +26,6 @@ import {
|
|
|
26
26
|
isStringLiteral,
|
|
27
27
|
isVariableDeclaration
|
|
28
28
|
} from "typescript/ast";
|
|
29
|
-
|
|
30
|
-
// src/prose-model.mts
|
|
31
|
-
var PROSE_ELEMENTS = {
|
|
32
|
-
paragraph: ["p"],
|
|
33
|
-
heading1: ["h1"],
|
|
34
|
-
heading2: ["h2"],
|
|
35
|
-
heading3: ["h3"],
|
|
36
|
-
heading4: ["h4"],
|
|
37
|
-
heading5: ["h5"],
|
|
38
|
-
heading6: ["h6"],
|
|
39
|
-
list: ["ul", "ol"],
|
|
40
|
-
item: ["li"],
|
|
41
|
-
quote: ["blockquote"],
|
|
42
|
-
rule: ["hr"],
|
|
43
|
-
pre: ["pre"],
|
|
44
|
-
table: ["table"],
|
|
45
|
-
headerCell: ["th"],
|
|
46
|
-
cell: ["td"],
|
|
47
|
-
figure: ["figure"],
|
|
48
|
-
caption: ["figcaption"],
|
|
49
|
-
media: ["img", "video", "iframe"],
|
|
50
|
-
descriptions: ["dl"],
|
|
51
|
-
term: ["dt"],
|
|
52
|
-
definition: ["dd"],
|
|
53
|
-
link: ["a"],
|
|
54
|
-
strong: ["strong", "b"],
|
|
55
|
-
emphasis: ["em", "i"],
|
|
56
|
-
deleted: ["del", "s"],
|
|
57
|
-
code: ["code"],
|
|
58
|
-
sub: ["sub"],
|
|
59
|
-
sup: ["sup"],
|
|
60
|
-
kbd: ["kbd"],
|
|
61
|
-
mark: ["mark"]
|
|
62
|
-
};
|
|
63
|
-
var PROSE_ELEMENT_NAMES = Object.keys(PROSE_ELEMENTS);
|
|
64
|
-
var PROSE_TAGS = new Map(PROSE_ELEMENT_NAMES.flatMap((name) => PROSE_ELEMENTS[name].map((tag) => [tag, name])));
|
|
65
|
-
var PROSE_STRUCTURE = new Map([
|
|
66
|
-
["thead", "table"],
|
|
67
|
-
["tbody", "table"],
|
|
68
|
-
["tfoot", "table"],
|
|
69
|
-
["tr", "table"],
|
|
70
|
-
["br", null]
|
|
71
|
-
]);
|
|
72
|
-
var isProseTag = (tag) => PROSE_TAGS.has(tag) || PROSE_STRUCTURE.has(tag);
|
|
73
|
-
var PROSE_MANIFEST = "ads-prose.json";
|
|
74
|
-
function parseProseManifest(text) {
|
|
75
|
-
const owners = new Map;
|
|
76
|
-
let value;
|
|
77
|
-
try {
|
|
78
|
-
value = JSON.parse(text);
|
|
79
|
-
} catch {
|
|
80
|
-
return owners;
|
|
81
|
-
}
|
|
82
|
-
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
83
|
-
return owners;
|
|
84
|
-
for (const [name, tags] of Object.entries(value))
|
|
85
|
-
if (Array.isArray(tags))
|
|
86
|
-
owners.set(name, new Set(tags.filter((tag) => typeof tag === "string" && isProseTag(tag))));
|
|
87
|
-
return owners;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// src/analysis/design-usage.mts
|
|
91
29
|
function walk(node, visit) {
|
|
92
30
|
visit(node);
|
|
93
31
|
node.forEachChild((child) => walk(child, visit));
|
|
@@ -161,7 +99,7 @@ function isMetadata(node) {
|
|
|
161
99
|
return true;
|
|
162
100
|
return !node.attributes.properties.some((attribute) => isJsxAttribute(attribute) && attribute.name.getText() === "rel" && attribute.initializer !== undefined && /stylesheet/.test(attribute.initializer.getText()));
|
|
163
101
|
}
|
|
164
|
-
function designUsageProblems(source, { publicEntry, patterns
|
|
102
|
+
function designUsageProblems(source, { publicEntry, patterns }, allowance = {}) {
|
|
165
103
|
const problems = [];
|
|
166
104
|
const { names, namespaces } = designSystemImports(source, publicEntry);
|
|
167
105
|
const allowed = allowedImports(source, allowance.components ?? new Map);
|
|
@@ -178,32 +116,14 @@ function designUsageProblems(source, { publicEntry, patterns, prose = new Map },
|
|
|
178
116
|
}
|
|
179
117
|
return false;
|
|
180
118
|
};
|
|
181
|
-
const proseOwner = (node) => {
|
|
182
|
-
for (let current = node.parent;current !== undefined; current = current.parent) {
|
|
183
|
-
if (!isJsxElement(current) || current.openingElement === node)
|
|
184
|
-
continue;
|
|
185
|
-
const tag = current.openingElement.tagName;
|
|
186
|
-
if (isIdentifier(tag) && isProseTag(tag.text))
|
|
187
|
-
continue;
|
|
188
|
-
const name = exportedName(tag);
|
|
189
|
-
const tags = name === undefined ? undefined : prose.get(name);
|
|
190
|
-
return name === undefined || tags === undefined ? undefined : { name, tags };
|
|
191
|
-
}
|
|
192
|
-
return;
|
|
193
|
-
};
|
|
194
119
|
for (const statement of source.statements)
|
|
195
120
|
if (isImportDeclaration(statement) && isStringLiteral(statement.moduleSpecifier) && statement.importClause?.phaseModifier !== SyntaxKind.TypeKeyword && authorsDesign(statement.moduleSpecifier.text))
|
|
196
121
|
problems.push({ node: statement.moduleSpecifier, problem: { kind: "design-import", specifier: statement.moduleSpecifier.text } });
|
|
197
122
|
walk(source, (node) => {
|
|
198
123
|
if (isJsxOpeningElement(node) || isJsxSelfClosingElement(node)) {
|
|
199
|
-
if (isAllowed(node.tagName)) {} else if (!fromDesignSystem(node.tagName))
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (owner === undefined)
|
|
203
|
-
problems.push({ node, problem: { kind: "local-markup", tag } });
|
|
204
|
-
else if (!owner.tags.has(tag))
|
|
205
|
-
problems.push({ node, problem: { kind: "prose-element-undeclared", tag, owner: owner.name } });
|
|
206
|
-
} else if (!isPattern(node.tagName) && !insidePattern(node) && inScreenFunction(node))
|
|
124
|
+
if (isAllowed(node.tagName)) {} else if (!fromDesignSystem(node.tagName))
|
|
125
|
+
problems.push({ node, problem: { kind: "local-markup", tag: node.tagName.getText() } });
|
|
126
|
+
else if (!isPattern(node.tagName) && !insidePattern(node) && inScreenFunction(node))
|
|
207
127
|
problems.push({ node, problem: { kind: "component-outside-pattern", name: exportedName(node.tagName) ?? node.tagName.getText() } });
|
|
208
128
|
for (const attribute of node.attributes.properties) {
|
|
209
129
|
if (isJsxSpreadAttribute(attribute))
|
|
@@ -408,10 +328,6 @@ var wording = {
|
|
|
408
328
|
"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."
|
|
409
329
|
],
|
|
410
330
|
"local-markup": (problem) => [`<${problem.tag}> is not imported from the design system.`, "Render a design-system owner; an arrangement or element the screen needs belongs in a pattern of the design system."],
|
|
411
|
-
"prose-element-undeclared": (problem) => [
|
|
412
|
-
`<${problem.tag}> is content of ${problem.owner}, whose recipe declares no such element: it is drawn without a design.`,
|
|
413
|
-
`Declare the element in the prose recipe of ${problem.owner} (\`elements\`), or write the content with the elements it declares.`
|
|
414
|
-
],
|
|
415
331
|
"spread-props": () => ["A spread passes props nothing can see.", "Pass each prop explicitly."],
|
|
416
332
|
"design-prop": (problem) => [`${problem.attribute} supplies design from outside the design system.`, "Remove it; choose the treatment through the owner's own props."],
|
|
417
333
|
"imperative-design": (problem) => [`${problem.member} writes styles or creates DOM by hand.`, "Remove the write; render design-system owners with props."],
|
|
@@ -448,14 +364,12 @@ async function designSystemEntry2(publicEntry, entryFile) {
|
|
|
448
364
|
patterns.add(element.name.text);
|
|
449
365
|
}
|
|
450
366
|
}
|
|
451
|
-
|
|
452
|
-
const prose = existsSync2(manifest) ? parseProseManifest(readFileSync2(manifest, "utf8")) : new Map;
|
|
453
|
-
return { publicEntry, patterns, prose };
|
|
367
|
+
return { publicEntry, patterns };
|
|
454
368
|
}
|
|
455
369
|
var closeDesignUsage2 = () => closeSyntax();
|
|
456
370
|
|
|
457
371
|
// src/guard/config.mts
|
|
458
|
-
import { existsSync as
|
|
372
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3, statSync } from "node:fs";
|
|
459
373
|
import path3 from "node:path";
|
|
460
374
|
|
|
461
375
|
// src/result.mts
|
|
@@ -475,7 +389,7 @@ var insideRoot = (root, relative) => {
|
|
|
475
389
|
};
|
|
476
390
|
function readGuardConfig2(root) {
|
|
477
391
|
const file = path3.join(root, GUARD_CONFIG_FILE2);
|
|
478
|
-
if (!
|
|
392
|
+
if (!existsSync2(file))
|
|
479
393
|
return err([`${GUARD_CONFIG_FILE2} is missing in ${root}. Create it with "designSystem" and "include".`]);
|
|
480
394
|
let value;
|
|
481
395
|
try {
|
|
@@ -492,13 +406,13 @@ function readGuardConfig2(root) {
|
|
|
492
406
|
const { designSystem, entry, include, exclude = [], allow = {} } = value;
|
|
493
407
|
if (typeof designSystem !== "string" || designSystem.trim() === "")
|
|
494
408
|
problems.push('"designSystem" is the specifier the application imports the design system by, such as "@acme/design".');
|
|
495
|
-
if (entry !== undefined && (typeof entry !== "string" || !insideRoot(root, entry) || !
|
|
409
|
+
if (entry !== undefined && (typeof entry !== "string" || !insideRoot(root, entry) || !existsSync2(path3.resolve(root, entry))))
|
|
496
410
|
problems.push(`"entry" is the path of the design system's public entry file, relative to this file. It does not exist.`);
|
|
497
411
|
if (!isStrings(include) || include.length === 0)
|
|
498
412
|
problems.push(`"include" lists the folders with the application's sources, such as ["src"].`);
|
|
499
413
|
else
|
|
500
414
|
for (const folder of include)
|
|
501
|
-
if (!insideRoot(root, folder) || !
|
|
415
|
+
if (!insideRoot(root, folder) || !existsSync2(path3.resolve(root, folder)) || !statSync(path3.resolve(root, folder)).isDirectory())
|
|
502
416
|
problems.push(`"include" names ${folder}, which is not a folder under this file. Wildcards are not supported.`);
|
|
503
417
|
if (!isStrings(exclude))
|
|
504
418
|
problems.push('"exclude" lists folders and files, such as ["src/generated"].');
|
|
@@ -506,7 +420,7 @@ function readGuardConfig2(root) {
|
|
|
506
420
|
for (const item of exclude) {
|
|
507
421
|
const resolved = path3.resolve(root, item);
|
|
508
422
|
const within = include.some((folder) => resolved.startsWith(path3.resolve(root, folder) + path3.sep));
|
|
509
|
-
if (!insideRoot(root, item) || !
|
|
423
|
+
if (!insideRoot(root, item) || !existsSync2(resolved))
|
|
510
424
|
problems.push(`"exclude" names ${item}, which does not exist. Remove it. Wildcards are not supported.`);
|
|
511
425
|
else if (!within)
|
|
512
426
|
problems.push(`"exclude" names ${item}, which is not under any "include" folder. Remove it.`);
|
|
@@ -535,7 +449,7 @@ function readGuardConfig2(root) {
|
|
|
535
449
|
}
|
|
536
450
|
|
|
537
451
|
// src/guard/run.mts
|
|
538
|
-
import { existsSync as
|
|
452
|
+
import { existsSync as existsSync3, readFileSync as readFileSync4 } from "node:fs";
|
|
539
453
|
import { createRequire } from "node:module";
|
|
540
454
|
import path5 from "node:path";
|
|
541
455
|
|
|
@@ -588,7 +502,7 @@ function entryFile(root, config) {
|
|
|
588
502
|
const specifier = config.designSystem;
|
|
589
503
|
if (specifier.startsWith(".")) {
|
|
590
504
|
const target = path5.resolve(root, specifier);
|
|
591
|
-
const found = [target, ...ENTRY_NAMES.map((name) => path5.join(target, name))].find((candidate) =>
|
|
505
|
+
const found = [target, ...ENTRY_NAMES.map((name) => path5.join(target, name))].find((candidate) => existsSync3(candidate) && /\.[cm]?[jt]sx?$/.test(candidate));
|
|
592
506
|
return found === undefined ? err(`"designSystem" is ${specifier}, and no entry file is at ${target}.`) : ok(found);
|
|
593
507
|
}
|
|
594
508
|
try {
|
package/dist/cli.mjs
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -15,12 +15,6 @@ export type DesignUsageProblem =
|
|
|
15
15
|
readonly kind: "local-markup";
|
|
16
16
|
readonly tag: string;
|
|
17
17
|
}
|
|
18
|
-
/** A plain element inside a prose owner that the owner's recipe does not declare: it would be drawn undesigned. */
|
|
19
|
-
| {
|
|
20
|
-
readonly kind: "prose-element-undeclared";
|
|
21
|
-
readonly tag: string;
|
|
22
|
-
readonly owner: string;
|
|
23
|
-
}
|
|
24
18
|
/** A JSX spread: props nothing can see. */
|
|
25
19
|
| {
|
|
26
20
|
readonly kind: "spread-props";
|
|
@@ -50,11 +44,6 @@ export interface DesignSystemEntry {
|
|
|
50
44
|
readonly publicEntry: string;
|
|
51
45
|
/** The exported names that are patterns: what arranges a screen. Every other export is a component, which fills a pattern. */
|
|
52
46
|
readonly patterns: ReadonlySet<string>;
|
|
53
|
-
/**
|
|
54
|
-
* The exported names that are prose owners (long-form content), each with the plain tags its recipe
|
|
55
|
-
* declares. Plain flow markup inside one is its content, not markup of the file's own.
|
|
56
|
-
*/
|
|
57
|
-
readonly prose?: ReadonlyMap<string, ReadonlySet<string>>;
|
|
58
47
|
}
|
|
59
48
|
/** What a source package may use beside the design system. A design system's mocks use none of it. */
|
|
60
49
|
export interface DesignUsageAllowance {
|
|
@@ -69,4 +58,4 @@ export interface DesignUsageAllowance {
|
|
|
69
58
|
readonly metadata?: boolean;
|
|
70
59
|
}
|
|
71
60
|
/** Every design-use problem of `source`, in source order. */
|
|
72
|
-
export declare function designUsageProblems(source: SourceFile, { publicEntry, patterns
|
|
61
|
+
export declare function designUsageProblems(source: SourceFile, { publicEntry, patterns }: DesignSystemEntry, allowance?: DesignUsageAllowance): readonly DesignUsage[];
|
package/package.json
CHANGED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
/** A prose element's name in a recipe, with the tags it selects. */
|
|
2
|
-
export declare const PROSE_ELEMENTS: {
|
|
3
|
-
readonly paragraph: readonly ["p"];
|
|
4
|
-
readonly heading1: readonly ["h1"];
|
|
5
|
-
readonly heading2: readonly ["h2"];
|
|
6
|
-
readonly heading3: readonly ["h3"];
|
|
7
|
-
readonly heading4: readonly ["h4"];
|
|
8
|
-
readonly heading5: readonly ["h5"];
|
|
9
|
-
readonly heading6: readonly ["h6"];
|
|
10
|
-
readonly list: readonly ["ul", "ol"];
|
|
11
|
-
readonly item: readonly ["li"];
|
|
12
|
-
readonly quote: readonly ["blockquote"];
|
|
13
|
-
readonly rule: readonly ["hr"];
|
|
14
|
-
readonly pre: readonly ["pre"];
|
|
15
|
-
readonly table: readonly ["table"];
|
|
16
|
-
readonly headerCell: readonly ["th"];
|
|
17
|
-
readonly cell: readonly ["td"];
|
|
18
|
-
readonly figure: readonly ["figure"];
|
|
19
|
-
readonly caption: readonly ["figcaption"];
|
|
20
|
-
readonly media: readonly ["img", "video", "iframe"];
|
|
21
|
-
readonly descriptions: readonly ["dl"];
|
|
22
|
-
readonly term: readonly ["dt"];
|
|
23
|
-
readonly definition: readonly ["dd"];
|
|
24
|
-
readonly link: readonly ["a"];
|
|
25
|
-
readonly strong: readonly ["strong", "b"];
|
|
26
|
-
readonly emphasis: readonly ["em", "i"];
|
|
27
|
-
readonly deleted: readonly ["del", "s"];
|
|
28
|
-
readonly code: readonly ["code"];
|
|
29
|
-
readonly sub: readonly ["sub"];
|
|
30
|
-
readonly sup: readonly ["sup"];
|
|
31
|
-
readonly kbd: readonly ["kbd"];
|
|
32
|
-
readonly mark: readonly ["mark"];
|
|
33
|
-
};
|
|
34
|
-
export type ProseElementName = keyof typeof PROSE_ELEMENTS;
|
|
35
|
-
export declare const PROSE_ELEMENT_NAMES: readonly ProseElementName[];
|
|
36
|
-
/** Every tag prose styles, with the element it belongs to. */
|
|
37
|
-
export declare const PROSE_TAGS: ReadonlyMap<string, ProseElementName>;
|
|
38
|
-
/**
|
|
39
|
-
* Tags that carry no design of their own but are part of content: a table's row groups and rows belong
|
|
40
|
-
* to its `table` element, and a line break belongs to any prose. The markup checks accept them as
|
|
41
|
-
* content; no rule is written for them.
|
|
42
|
-
*/
|
|
43
|
-
export declare const PROSE_STRUCTURE: ReadonlyMap<string, ProseElementName | null>;
|
|
44
|
-
/** Whether a tag can be content of a prose owner: a styled tag or a structural one. */
|
|
45
|
-
export declare const isProseTag: (tag: string) => boolean;
|
|
46
|
-
/** The content tags a recipe that declares `names` accepts: their own tags, and the structure that comes with them. */
|
|
47
|
-
export declare function proseContentTags(names: Iterable<ProseElementName>): ReadonlySet<string>;
|
|
48
|
-
/** Elements that stand as blocks in the flow: they take the rhythm before them. */
|
|
49
|
-
export declare const PROSE_BLOCKS: readonly ProseElementName[];
|
|
50
|
-
/** The elements whose text can sit directly inside `container`: what its background must carry. */
|
|
51
|
-
export declare function proseContents(container: ProseElementName): readonly ProseElementName[];
|
|
52
|
-
/** The selector of one tag inside the prose root: plain markup only. */
|
|
53
|
-
export declare const proseSelector: (tag: string) => string;
|
|
54
|
-
/** `&:hover` on an element's selector: the state is the element's own. */
|
|
55
|
-
export declare const proseState: (selector: string, condition: string) => string;
|
|
56
|
-
/** The first plain element of the root and of a plain container: its start margin is cancelled. */
|
|
57
|
-
export declare const PROSE_FIRST: readonly string[];
|
|
58
|
-
/**
|
|
59
|
-
* A component placed directly in the prose root (it has a class) is a block of the flow like any
|
|
60
|
-
* other: it takes the rhythm before it, unless it comes first. Space between siblings is the
|
|
61
|
-
* container's to give; the component's own design is untouched. A component inside a paragraph or
|
|
62
|
-
* another plain element is inline content and is left alone.
|
|
63
|
-
*/
|
|
64
|
-
export declare const PROSE_COMPONENT = "& > :not(:first-child)[class]";
|
|
65
|
-
type Values = Readonly<Record<string, string>>;
|
|
66
|
-
/**
|
|
67
|
-
* The browser defaults ADS replaces on an element, whatever the recipe declares for it: margins
|
|
68
|
-
* (the rhythm owns the space between blocks), a quote's and a figure's indent, a rule's inset border,
|
|
69
|
-
* media wider than its column, a table's doubled borders.
|
|
70
|
-
*/
|
|
71
|
-
export declare function proseDefaults(name: ProseElementName): Values;
|
|
72
|
-
/**
|
|
73
|
-
* The emitted record of a design system's prose owners, beside its public entry: exported name → the
|
|
74
|
-
* plain tags its recipe declares. The design-use check of application source reads it, because an
|
|
75
|
-
* emitted owner no longer carries its recipe.
|
|
76
|
-
*/
|
|
77
|
-
export declare const PROSE_MANIFEST = "ads-prose.json";
|
|
78
|
-
export declare const proseManifestText: (owners: ReadonlyMap<string, ReadonlySet<string>>) => string;
|
|
79
|
-
/** The owners a manifest text records; entries that are not a list of prose tags are dropped. */
|
|
80
|
-
export declare function parseProseManifest(text: string): ReadonlyMap<string, ReadonlySet<string>>;
|
|
81
|
-
export {};
|