@starci/eslint-grammar-core 0.20.2 → 0.21.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/CHANGELOG.md +15 -1
- package/README.md +5 -4
- package/dist/app-shell.js +1 -1
- package/dist/grammar-boundary.js +5 -5
- package/dist/icon.d.ts +0 -4
- package/dist/icon.js +1 -64
- package/dist/index.js +4 -2
- package/dist/lib/app-shell-cluster.js +1 -1
- package/dist/lib/app-shell-owner.js +1 -1
- package/dist/lib/app-shell-scope.js +1 -1
- package/dist/lib/app-shell-visible.js +0 -1
- package/dist/lib/app-shell.js +2 -2
- package/dist/lib/grammar-imports.d.ts +16 -0
- package/dist/lib/grammar-imports.js +37 -0
- package/dist/lib/why.js +31 -6
- package/dist/page-header.d.ts +11 -4
- package/dist/page-header.js +12 -42
- package/dist/page.d.ts +3 -3
- package/dist/page.js +21 -21
- package/dist/pane.js +3 -3
- package/dist/status-colors.js +2 -2
- package/dist/surface-card.d.ts +9 -0
- package/dist/surface-card.js +191 -0
- package/dist/surface-depth.d.ts +1 -1
- package/dist/surface-depth.js +53 -49
- package/dist/text-rules.d.ts +18 -0
- package/dist/text-rules.js +160 -0
- package/dist/tokens.d.ts +0 -2
- package/dist/tokens.js +0 -38
- package/docs/app-shell.md +3 -3
- package/docs/grammar-boundary.md +1 -1
- package/docs/page-header.md +15 -12
- package/docs/page.md +5 -5
- package/docs/status-colors.md +1 -1
- package/docs/surface-card.md +57 -0
- package/docs/surface-depth.md +16 -14
- package/package.json +3 -3
- package/runtime/bundle.json +2 -1
- package/runtime/knowledge/grammars/core/playbook.yaml +41 -51
- package/runtime/knowledge/hfs/canon-pins.yaml +7 -7
- package/runtime/knowledge/hfs/rules.yaml +286 -40
- package/runtime/knowledge/hfs/slots.yaml +5 -5
- package/runtime/knowledge/patterns/fe/ui.yaml +0 -1
- package/runtime/knowledge/ui/proof/anatomy-source.yaml +1 -1
- package/runtime/scripts/hfs/architecture/component-tiers.mjs +161 -0
- package/runtime/scripts/hfs/architecture/config.mjs +1 -1
- package/runtime/scripts/hfs/slot-manifest-shape.mjs +16 -1
- package/dist/typography.d.ts +0 -13
- package/dist/typography.js +0 -72
|
@@ -196,7 +196,7 @@ tiers:
|
|
|
196
196
|
feature: {mayImport: [feature, components, hooks, foundation, modules, package], mayImportKinds: {feature: {from: [pages, layouts], to: [overlays]}}} # pages and layouts compose overlays and blocks; data goes through hooks/swr
|
|
197
197
|
# A component never reaches data: config, routes, i18n and types are foundation; every other module and the api
|
|
198
198
|
# transport are reached through a hook (owner decision 2026-09-29).
|
|
199
|
-
components: {mayImport: [components, hooks, foundation, package], lowerLayerOnly: true} # blocks, composites, branches, leaves
|
|
199
|
+
components: {mayImport: [components, hooks, foundation, package], lowerLayerOnly: true} # blocks, frames, composites, branches, leaves
|
|
200
200
|
# Across domains a hook is imported only through the other domain's public index (hooks/<domain>/index.ts), never deep.
|
|
201
201
|
hooks: {mayImport: [hooks, foundation, modules, transport, package], mayImportSlots: {transport: {from: [fe.hooks.swr]}}} # only hooks/swr imports an operation
|
|
202
202
|
foundation: {mayImport: [foundation, package], acyclic: true} # modules/{config,i18n,routes,types}
|
|
@@ -2227,12 +2227,12 @@ slots:
|
|
|
2227
2227
|
- id: fe.components
|
|
2228
2228
|
since: 3.1.0
|
|
2229
2229
|
profiles: [fe]
|
|
2230
|
-
path: "apps/<app>/src/components/{blocks,composites,branches,leaves}/<name>/"
|
|
2230
|
+
path: "apps/<app>/src/components/{blocks,frames,composites,branches,leaves}/<name>/"
|
|
2231
2231
|
presence: optional
|
|
2232
2232
|
tracked: tracked
|
|
2233
2233
|
tier: components
|
|
2234
2234
|
owner: true
|
|
2235
|
-
layers: [blocks, composites, branches, leaves] # a layer imports only the layers after it
|
|
2235
|
+
layers: [blocks, frames, composites, branches, leaves] # a layer imports only the layers after it
|
|
2236
2236
|
requires: [index.tsx]
|
|
2237
2237
|
allows: [index.tsx, component.tsx, classNames.ts]
|
|
2238
2238
|
roles: {entry: index.tsx, drawing: component.tsx, styles: classNames.ts} # the connected entry, the pure drawing half and the styling declarations of a component owner
|
|
@@ -2575,12 +2575,12 @@ slots:
|
|
|
2575
2575
|
tracked: tracked
|
|
2576
2576
|
tier: package
|
|
2577
2577
|
owner: true
|
|
2578
|
-
kinds: [composites, branches, leaves] # the layer folders under src/ (a package owns no blocks)
|
|
2578
|
+
kinds: [frames, composites, branches, leaves] # the layer folders under src/ (a package owns no blocks)
|
|
2579
2579
|
roles: {entry: index.tsx, drawing: component.tsx, styles: classNames.ts}
|
|
2580
2580
|
requires: [package.json, src/index.ts, tsconfig.json]
|
|
2581
2581
|
tests: none
|
|
2582
2582
|
budget: {component.tsx: 300, index.tsx: 200, dataHooks: 6, useState: 6}
|
|
2583
|
-
why: built to dist with explicit named exports; only composites, branches and leaves; no unused export; no colour literal
|
|
2583
|
+
why: built to dist with explicit named exports; only frames, composites, branches and leaves; no unused export; no colour literal
|
|
2584
2584
|
rules: [FE_PACKAGE_SHAPE, HFS_UNUSED_EXPORT, FE_STYLE_TOKEN_ONLY]
|
|
2585
2585
|
- id: fe.package.api
|
|
2586
2586
|
since: 3.1.0
|
|
@@ -69,7 +69,7 @@ rules:
|
|
|
69
69
|
Applications compose the published surface and control and omit the field variant, allowing
|
|
70
70
|
the Grammar to resolve their layer; they do not reproduce surface paint or force a static
|
|
71
71
|
variant. Controls whose anatomy does not vary by layer keep their published treatment. The
|
|
72
|
-
|
|
72
|
+
field `variant` is not in the prop types, so a static variant does not compile; rendered
|
|
73
73
|
treatment is checked through DRAW_NESTED_VARIANT, recognising painted Grammar surface stamps.
|
|
74
74
|
The family's own recipe defines its props, values and supported anatomy
|
|
75
75
|
elements:
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { byCodeUnit } from '../../lib/list.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* The component tier ladder, checked on the source of a component library and of a component tree.
|
|
6
|
+
*
|
|
7
|
+
* FE_GRAMMAR_TIER_DIRECTION a grammar component imports a tier above it (the ladder leaf, branch, composite, frame: a tier imports
|
|
8
|
+
* nothing above itself), type-only imports included, through a shared untiered module as well;
|
|
9
|
+
* `tiers.grammar` of knowledge/hfs/slots.yaml is the matrix
|
|
10
|
+
* FE_GRAMMAR_TIER_STAMP a `data-tier` stamp names a tier other than the folder the file sits in (`atom`, `primitive` and
|
|
11
|
+
* `composition` are not tiers)
|
|
12
|
+
* FE_TIER_DIRECTION (layers) a component imports a layer at or above its own: blocks, frames, composites, branches, leaves
|
|
13
|
+
*
|
|
14
|
+
* A library is a folder holding `src/core/<tier>/<Name>/`; a tree is a folder holding `components/<layer>/<Unit>/` that no hfs
|
|
15
|
+
* front-end slot already judges (an app without hfs.json, such as the harness desktop app).
|
|
16
|
+
*/
|
|
17
|
+
export const COMPONENT_TIER_RULE_IDS = ['FE_GRAMMAR_TIER_DIRECTION', 'FE_GRAMMAR_TIER_STAMP'];
|
|
18
|
+
const SKIPPED = new Set(['node_modules', '.git', 'dist', '.next', 'storybook-static', 'proof']);
|
|
19
|
+
const SOURCE = /\.(?:ts|tsx)$/u;
|
|
20
|
+
const TEST_OR_STORY = /\.(?:stories|spec|test)\.[jt]sx?$/u;
|
|
21
|
+
const IMPORT = /(?:\bimport|\bexport)\b[^'"`;]*?\bfrom\s*['"]([^'"]+)['"]|\bimport\s*['"]([^'"]+)['"]|\bimport\(\s*['"]([^'"]+)['"]\s*\)/gu;
|
|
22
|
+
const STAMP = /["']?data-tier["']?\s*[=:]\s*\{?\s*["']([a-z-]+)["']/gu;
|
|
23
|
+
const LIBRARY_FILE = /^(.*?)src\/core\/(leaf|branch|composite|frame)\/([^/]+)\//u;
|
|
24
|
+
function walk(root, dir = '') {
|
|
25
|
+
const out = [];
|
|
26
|
+
for (const entry of fs.readdirSync(path.join(root, dir), { withFileTypes: true })) {
|
|
27
|
+
if (SKIPPED.has(entry.name))
|
|
28
|
+
continue;
|
|
29
|
+
const rel = dir === '' ? entry.name : `${dir}/${entry.name}`;
|
|
30
|
+
if (entry.isDirectory())
|
|
31
|
+
out.push(...walk(root, rel));
|
|
32
|
+
else if (SOURCE.test(entry.name) && !entry.name.endsWith('.d.ts') && !TEST_OR_STORY.test(entry.name))
|
|
33
|
+
out.push(rel);
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
const lineOf = (text, index) => text.slice(0, index).split('\n').length;
|
|
38
|
+
/** The file an import specifier names, as a repo-relative path in `files`, or null (a package, or a file outside the walk). */
|
|
39
|
+
function resolveImport(files, from, specifier) {
|
|
40
|
+
if (!specifier.startsWith('.'))
|
|
41
|
+
return null;
|
|
42
|
+
const base = path.posix.normalize(path.posix.join(path.posix.dirname(from), specifier)).replace(/\.(?:js|mjs|cjs)$/u, '');
|
|
43
|
+
for (const candidate of [`${base}.ts`, `${base}.tsx`, `${base}/index.ts`, `${base}/index.tsx`, base])
|
|
44
|
+
if (files.has(candidate))
|
|
45
|
+
return candidate;
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
function edgesOf(root, files, cache, file) {
|
|
49
|
+
const cached = cache.get(file);
|
|
50
|
+
if (cached)
|
|
51
|
+
return cached;
|
|
52
|
+
const text = fs.readFileSync(path.join(root, ...file.split('/')), 'utf8');
|
|
53
|
+
const edges = [];
|
|
54
|
+
for (const match of text.matchAll(IMPORT)) {
|
|
55
|
+
const specifier = match[1] ?? match[2] ?? match[3] ?? '';
|
|
56
|
+
const to = resolveImport(files, file, specifier);
|
|
57
|
+
if (to !== null)
|
|
58
|
+
edges.push({ to, line: lineOf(text, match.index ?? 0), specifier });
|
|
59
|
+
}
|
|
60
|
+
const entry = { text, edges };
|
|
61
|
+
cache.set(file, entry);
|
|
62
|
+
return entry;
|
|
63
|
+
}
|
|
64
|
+
/** The tier (the folder under `src/core`) and the component of a grammar source file, or null for a file outside the ladder. */
|
|
65
|
+
const gradeOf = (file) => {
|
|
66
|
+
const match = LIBRARY_FILE.exec(file);
|
|
67
|
+
return match === null ? null : { tier: match[2], unit: `${match[1]}${match[2]}/${match[3]}` };
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Check the component libraries under `root` against `matrix` (`tiers.grammar`). Not applicable when no folder holds
|
|
71
|
+
* `src/core/<tier>/<Name>/`. A file outside the tier folders (a shared helper under `src/core`) is not a tier, but an import that
|
|
72
|
+
* reaches a higher tier through it is the importing tier's edge.
|
|
73
|
+
*/
|
|
74
|
+
export function checkGrammarLibraries({ root, matrix }) {
|
|
75
|
+
const all = walk(root).filter(file => /(?:^|\/)src\//u.test(file));
|
|
76
|
+
const library = all.filter(file => gradeOf(file) !== null);
|
|
77
|
+
if (!library.length)
|
|
78
|
+
return { violations: [], coverage: { status: 'not-applicable' } };
|
|
79
|
+
const files = new Set(all);
|
|
80
|
+
const cache = new Map();
|
|
81
|
+
const violations = [];
|
|
82
|
+
const tiers = new Set(Object.keys(matrix));
|
|
83
|
+
let edgesChecked = 0;
|
|
84
|
+
let stamps = 0;
|
|
85
|
+
for (const file of library.sort(byCodeUnit)) {
|
|
86
|
+
const from = gradeOf(file);
|
|
87
|
+
const { text, edges } = edgesOf(root, files, cache, file);
|
|
88
|
+
for (const stamp of text.matchAll(STAMP)) {
|
|
89
|
+
stamps += 1;
|
|
90
|
+
if (stamp[1] !== from.tier) {
|
|
91
|
+
violations.push({ ruleId: 'FE_GRAMMAR_TIER_STAMP', path: file, line: lineOf(text, stamp.index ?? 0), column: 1,
|
|
92
|
+
message: `${file} stamps data-tier="${stamp[1]}" and sits in the ${from.tier} folder: the stamp is the folder (${[...tiers].join(', ')}).` });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Follow shared untiered modules, so a leaf cannot reach a frame by one hop of indirection.
|
|
96
|
+
const queue = edges.map(edge => ({ edge, via: [] }));
|
|
97
|
+
const seen = new Set();
|
|
98
|
+
for (let next = queue.shift(); next !== undefined; next = queue.shift()) {
|
|
99
|
+
const { edge, via } = next;
|
|
100
|
+
const target = gradeOf(edge.to);
|
|
101
|
+
if (target === null) {
|
|
102
|
+
if (seen.has(edge.to) || !edge.to.includes('/src/') && !edge.to.startsWith('src/'))
|
|
103
|
+
continue;
|
|
104
|
+
seen.add(edge.to);
|
|
105
|
+
for (const onward of edgesOf(root, files, cache, edge.to).edges)
|
|
106
|
+
queue.push({ edge: { ...onward, line: edge.line }, via: [...via, edge.to] });
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (target.unit === from.unit)
|
|
110
|
+
continue;
|
|
111
|
+
edgesChecked += 1;
|
|
112
|
+
if ((matrix[from.tier]?.mayImport ?? []).includes(target.tier))
|
|
113
|
+
continue;
|
|
114
|
+
violations.push({ ruleId: 'FE_GRAMMAR_TIER_DIRECTION', path: file, line: edge.line, column: 1, specifier: edge.specifier, resolvedPath: edge.to,
|
|
115
|
+
message: `${file} (${from.tier}) imports ${edge.to} (${target.tier})${via.length ? ` through ${via.join(' -> ')}` : ''}: a ${from.tier} may import only ${(matrix[from.tier]?.mayImport ?? []).join(', ') || 'nothing'}. Type-only imports count.` });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const unique = new Map(violations.map(violation => [`${violation.ruleId}|${violation.path}|${violation.line}|${violation.resolvedPath ?? ''}`, violation]));
|
|
119
|
+
return { violations: [...unique.values()], coverage: { status: 'checked', files: library.length, edgesChecked, stamps } };
|
|
120
|
+
}
|
|
121
|
+
const LAYER_FILE = /^(.*?)components\/([^/]+)\/([^/]+)\//u;
|
|
122
|
+
/**
|
|
123
|
+
* Check a component tree under `root` that no hfs slot judges: a component imports only the layers after its own (`layers` in order,
|
|
124
|
+
* the first layer imports all below it). The layers are the folder names of slot fe.components.
|
|
125
|
+
*/
|
|
126
|
+
export function checkComponentLayers({ root, layers }) {
|
|
127
|
+
const all = walk(root).filter(file => /(?:^|\/)src\//u.test(file));
|
|
128
|
+
const placed = (file) => {
|
|
129
|
+
const match = LAYER_FILE.exec(file);
|
|
130
|
+
const layer = match === null ? -1 : layers.indexOf(match[2]);
|
|
131
|
+
return match === null || layer < 0 ? null : { layer, unit: `${match[1]}components/${match[2]}/${match[3]}` };
|
|
132
|
+
};
|
|
133
|
+
const tree = all.filter(file => placed(file) !== null);
|
|
134
|
+
if (!tree.length)
|
|
135
|
+
return { violations: [], coverage: { status: 'not-applicable' } };
|
|
136
|
+
const files = new Set(all);
|
|
137
|
+
const cache = new Map();
|
|
138
|
+
const violations = [];
|
|
139
|
+
let edgesChecked = 0;
|
|
140
|
+
for (const file of tree.sort(byCodeUnit)) {
|
|
141
|
+
const from = placed(file);
|
|
142
|
+
for (const edge of edgesOf(root, files, cache, file).edges) {
|
|
143
|
+
const target = placed(edge.to);
|
|
144
|
+
if (target === null || target.unit === from.unit)
|
|
145
|
+
continue;
|
|
146
|
+
edgesChecked += 1;
|
|
147
|
+
if (target.layer > from.layer)
|
|
148
|
+
continue;
|
|
149
|
+
violations.push({ ruleId: 'FE_TIER_DIRECTION', path: file, line: edge.line, column: 1, specifier: edge.specifier, resolvedPath: edge.to,
|
|
150
|
+
message: `${file}: a ${layers[from.layer]} component may import only the layers after it (${layers.slice(from.layer + 1).join(', ') || 'none'}); it imports a ${layers[target.layer]}. Type-only imports count.` });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { violations, coverage: { status: 'checked', files: tree.length, edgesChecked } };
|
|
154
|
+
}
|
|
155
|
+
/** The machine check: the grammar libraries of the repository, judged by `tiers.grammar` of the slot manifest. */
|
|
156
|
+
export function checkGrammarTiers({ config }) {
|
|
157
|
+
const matrix = config.hfs.manifest.tiers.grammar;
|
|
158
|
+
if (matrix === undefined)
|
|
159
|
+
return { violations: [], coverage: { status: 'not-applicable' } };
|
|
160
|
+
return checkGrammarLibraries({ root: config.root, matrix });
|
|
161
|
+
}
|
|
@@ -208,7 +208,7 @@ function inferredLayout(root, workspaces) {
|
|
|
208
208
|
// A design-system workspace whose src carries tier dirs (leaves/branches/…) IS a component root;
|
|
209
209
|
// collecting its src/components too would nest two roots in one role and fail the disjoint check.
|
|
210
210
|
const tierSources = new Set(workspaces.map(prefix => `${prefix}/src`)
|
|
211
|
-
.filter(source => ['leaves', 'branches', 'blocks', 'overlays', 'composites', 'layouts', 'product-shells', 'pages']
|
|
211
|
+
.filter(source => ['leaves', 'branches', 'blocks', 'frames', 'overlays', 'composites', 'layouts', 'product-shells', 'pages']
|
|
212
212
|
.some(tier => existingDirectory(root, `${source}/${tier}`))));
|
|
213
213
|
const components = collect('src/components')
|
|
214
214
|
.filter(relative => !tierSources.has(path.posix.dirname(relative)));
|
|
@@ -49,13 +49,27 @@ function checkProfileMaps(m, bad) {
|
|
|
49
49
|
continue;
|
|
50
50
|
}
|
|
51
51
|
for (const extra of Object.keys(m[key]))
|
|
52
|
-
if (!isSide(extra))
|
|
52
|
+
if (!isSide(extra) && !(key === 'tiers' && extra === COMPONENT_LIBRARY))
|
|
53
53
|
bad.push(`${key}.${extra} is not a profile`);
|
|
54
54
|
for (const profile of PROFILES)
|
|
55
55
|
if (!(profile in m[key]))
|
|
56
56
|
bad.push(`${key}.${profile} is missing`);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
+
/** The tier matrix of a component library (packages/grammar), beside the be and fe profiles: tiers.grammar. */
|
|
60
|
+
const COMPONENT_LIBRARY = 'grammar';
|
|
61
|
+
function checkComponentLibraryTiers(m, bad) {
|
|
62
|
+
const tiers = m.tiers?.[COMPONENT_LIBRARY];
|
|
63
|
+
if (tiers === undefined)
|
|
64
|
+
return;
|
|
65
|
+
bad.push(...tierMapProblems(COMPONENT_LIBRARY, tiers));
|
|
66
|
+
if (!isPlainObject(tiers))
|
|
67
|
+
return;
|
|
68
|
+
for (const [tier, def] of Object.entries(tiers))
|
|
69
|
+
for (const target of def?.mayImport ?? [])
|
|
70
|
+
if (!(target in tiers))
|
|
71
|
+
bad.push(`tiers.${COMPONENT_LIBRARY}.${tier}.mayImport names ${target}, which is not a ${COMPONENT_LIBRARY} tier`);
|
|
72
|
+
}
|
|
59
73
|
function checkAppKindAndTierShapes(m, bad) {
|
|
60
74
|
for (const profile of PROFILES) {
|
|
61
75
|
const kinds = m.appKinds?.[profile];
|
|
@@ -66,6 +80,7 @@ function checkAppKindAndTierShapes(m, bad) {
|
|
|
66
80
|
continue;
|
|
67
81
|
bad.push(...tierMapProblems(profile, tiers));
|
|
68
82
|
}
|
|
83
|
+
checkComponentLibraryTiers(m, bad);
|
|
69
84
|
}
|
|
70
85
|
function checkSlotShapes(m, bad) {
|
|
71
86
|
if (!Array.isArray(m.slots) || !m.slots.length) {
|
package/dist/typography.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Rule } from "eslint";
|
|
2
|
-
/** A heading comes from the component that owns its tag and its size as one decision. */
|
|
3
|
-
export declare const noHeadingTagOutsideHeadingComponent: Rule.RuleModule;
|
|
4
|
-
/** The rules this law contributes to the plugin. */
|
|
5
|
-
export declare const rules: Record<string, Rule.RuleModule>;
|
|
6
|
-
/**
|
|
7
|
-
* The level this law asks for, as the plugin's own opinion.
|
|
8
|
-
*
|
|
9
|
-
* Exact: a tag name in a file that is not the component that owns it. A repository adopting this
|
|
10
|
-
* with history should expect the deeper levels to report a structure problem rather than a styling
|
|
11
|
-
* one, and those take longer to fix than the message suggests.
|
|
12
|
-
*/
|
|
13
|
-
export declare const recommended: Record<string, "error">;
|
package/dist/typography.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
2
|
-
/**
|
|
3
|
-
* The rule that holds `typography.md`.
|
|
4
|
-
*
|
|
5
|
-
* MOST OF THIS LAW IS A UNION, and what is left is one shape a type cannot see: a heading TAG
|
|
6
|
-
* written by hand. The level union stops a fifth step being asked for; nothing in the type system
|
|
7
|
-
* notices an `<h2>` in a file that never mentions the heading component at all.
|
|
8
|
-
*
|
|
9
|
-
* The rule's own twin lives in `tokens.ts`: that one catches a heading assembled out of a large
|
|
10
|
-
* size and a heavy weight on some other element. Between them they cover both ways a heading gets
|
|
11
|
-
* hand-rolled - with the right tag and the wrong look, or the right look and no tag.
|
|
12
|
-
*/
|
|
13
|
-
import { classOf, inSlot, isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
14
|
-
/** The one owner that writes the heading tag: the component named `Heading` in the leaves layer of an app (slot `fe.components`; the owner name is the one name this rule keeps). */
|
|
15
|
-
const isHeadingComponent = (context) => {
|
|
16
|
-
const owner = classOf(context);
|
|
17
|
-
return inSlot(context, "fe.components") && owner.kind === "leaves" && owner.bindings?.name === "Heading";
|
|
18
|
-
};
|
|
19
|
-
/** The tags a document outline is built from. */
|
|
20
|
-
const HEADING_TAGS = new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
|
|
21
|
-
/** The deepest level the component draws; below this a page has nested too far. */
|
|
22
|
-
const DEEPEST_LEVEL = 4;
|
|
23
|
-
/** Intrinsic (lowercase) tag name, or null for a component. */
|
|
24
|
-
const tagName = (opening) => {
|
|
25
|
-
const name = opening && opening.name;
|
|
26
|
-
if (!name || name.type !== "JSXIdentifier")
|
|
27
|
-
return null;
|
|
28
|
-
return name.name === name.name.toLowerCase() ? name.name : null;
|
|
29
|
-
};
|
|
30
|
-
// -- TYPESET-1 · TYPESET-2 -------------------------------------------------------------------------------
|
|
31
|
-
/** A heading comes from the component that owns its tag and its size as one decision. */
|
|
32
|
-
export const noHeadingTagOutsideHeadingComponent = defineRule({
|
|
33
|
-
meta: {
|
|
34
|
-
type: "problem",
|
|
35
|
-
docs: { description: "Heading tags are written by the heading component, from a level." },
|
|
36
|
-
schema: [],
|
|
37
|
-
messages: {
|
|
38
|
-
tag: "`<{{tag}}>` written here throws away the two facts the heading component exists to keep together: the tag a screen reader builds the document outline from, and the size a reader sees. Set separately they drift, and a screen's third-largest text becomes its first heading. Render the heading component with a level of {{level}}; one prop decides both, so they cannot disagree.",
|
|
39
|
-
tooDeep: "`<{{tag}}>` is deeper than the scale goes: levels stop at {{deepest}}, because a fifth means the page has nested further than a reader can hold. That is a structure problem wearing a styling problem's clothes - flatten the section, then render the title with a level the scale has.",
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
create(context) {
|
|
43
|
-
if (!isProductSource(context) || isHeadingComponent(context))
|
|
44
|
-
return {};
|
|
45
|
-
return {
|
|
46
|
-
JSXOpeningElement(node) {
|
|
47
|
-
const tag = tagName(node);
|
|
48
|
-
if (!tag || !HEADING_TAGS.has(tag))
|
|
49
|
-
return;
|
|
50
|
-
const level = Number(tag.slice(1));
|
|
51
|
-
if (level > DEEPEST_LEVEL) {
|
|
52
|
-
context.report({ node, messageId: "tooDeep", data: { tag, deepest: DEEPEST_LEVEL } });
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
context.report({ node, messageId: "tag", data: { tag, level } });
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
/** The rules this law contributes to the plugin. */
|
|
61
|
-
export const rules = {
|
|
62
|
-
"no-heading-tag-outside-heading-component": noHeadingTagOutsideHeadingComponent,
|
|
63
|
-
};
|
|
64
|
-
defineFamilyRules(rules, "knowledge/ui/presentation/font.yaml");
|
|
65
|
-
/**
|
|
66
|
-
* The level this law asks for, as the plugin's own opinion.
|
|
67
|
-
*
|
|
68
|
-
* Exact: a tag name in a file that is not the component that owns it. A repository adopting this
|
|
69
|
-
* with history should expect the deeper levels to report a structure problem rather than a styling
|
|
70
|
-
* one, and those take longer to fix than the message suggests.
|
|
71
|
-
*/
|
|
72
|
-
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|