@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
package/dist/page.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { inSlot, isProductSource, kindOfFile } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { refuseGrammarExports } from "./lib/grammar-imports.js";
|
|
2
3
|
import { PURE_HALF, blockDrawsPage, mountedOnlyUnderAuthGroup, segmentFrame } from "./lib/page-frame.js";
|
|
3
4
|
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
4
5
|
/**
|
|
@@ -6,15 +7,16 @@ import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
|
6
7
|
* and its rhythm.
|
|
7
8
|
*
|
|
8
9
|
* - R324 (`FE_PAGE_TABS_OUTSIDE_HEADER`): page tabs are the header's tabs. A `Tabs` that is the first element child of a
|
|
9
|
-
* `Page` (the top of the body, right under the band)
|
|
10
|
-
*
|
|
10
|
+
* `Page` (the top of the body, right under the band) is a page tab bar drawn outside the band: it misses the separator
|
|
11
|
+
* it stands on and takes the body's inset and gap.
|
|
11
12
|
* - R325 (`FE_ROUTE_PAGE_OUTSIDE_PAGE`): a route draws exactly one `Page` and nothing beside it. The page owner
|
|
12
13
|
* (`fe.feature`, kind `pages`) draws it itself, through its pure half (`./component`), or through the one connected
|
|
13
14
|
* block it renders as its root, where client data builds the header a server owner cannot build. A route segment
|
|
14
15
|
* `layout` whose owner (`fe.feature`, kind `layouts`) draws, in the same ways, a `Page` whose `header` carries `tabs` is
|
|
15
16
|
* the one `Page` of every route under it: those page owners draw body content only, so a `Page` they draw is a second
|
|
16
17
|
* one, and they need none. A `Page` (or a block that draws one) with an element sibling, a `Page` inside another, or a
|
|
17
|
-
* bare `PageHeader` or `PageContainer`
|
|
18
|
+
* bare `PageHeader` or `PageContainer` imported from `@starci/grammar` is the finding of its own: both are Page's parts and
|
|
19
|
+
* are not exported, so the law reads the import and nothing else of them. A page
|
|
18
20
|
* owner file that draws JSX but no `Page` in one of those ways has no page frame at all, unless a segment layout frames
|
|
19
21
|
* every route that mounts it, or every such route lies under the auth route group `(auth)` with its own layout: that
|
|
20
22
|
* layout draws the auth frame, the page frame of its sign-in, sign-up, password and callback pages.
|
|
@@ -48,27 +50,22 @@ const renderedChildren = (children) => children.filter((child) => {
|
|
|
48
50
|
return child.expression.type !== "JSXEmptyExpression";
|
|
49
51
|
return true;
|
|
50
52
|
});
|
|
51
|
-
/** The `Tabs` element drawn as page tabs
|
|
53
|
+
/** The `Tabs` element drawn as page tabs at the top of a `Page` body, outside the header band, or null. */
|
|
52
54
|
export const tabsOutsideHeader = (parent) => {
|
|
53
55
|
const children = renderedChildren(parent.children);
|
|
54
56
|
const first = children[0];
|
|
55
57
|
if (elementName(parent) === "Page" && elementName(first) === "Tabs")
|
|
56
58
|
return first;
|
|
57
|
-
for (let index = 0; index + 1 < children.length; index += 1) {
|
|
58
|
-
const next = children[index + 1];
|
|
59
|
-
if (elementName(children[index]) === "PageHeader" && elementName(next) === "Tabs")
|
|
60
|
-
return next;
|
|
61
|
-
}
|
|
62
59
|
return null;
|
|
63
60
|
};
|
|
64
|
-
/** Page tabs are the header's `tabs`, never a `Tabs` at the top of the body
|
|
61
|
+
/** Page tabs are the header's `tabs`, never a `Tabs` at the top of the body. */
|
|
65
62
|
export const pageTabsInHeader = defineRule({
|
|
66
63
|
meta: {
|
|
67
64
|
type: "problem",
|
|
68
|
-
docs: { description: "R324: page tabs are Page header.tabs
|
|
65
|
+
docs: { description: "R324: page tabs are Page header.tabs, never a Tabs at the top of the body." },
|
|
69
66
|
schema: [],
|
|
70
67
|
messages: {
|
|
71
|
-
outside: "This `Tabs` is the page's tab bar drawn under the header band instead of on it: it misses the separator the page tabs stand on and takes the body's inset. Pass it as `header.tabs` of `Page
|
|
68
|
+
outside: "This `Tabs` is the page's tab bar drawn under the header band instead of on it: it misses the separator the page tabs stand on and takes the body's inset. Pass it as `header.tabs` of `Page`: `{ label, items, selectedKey, onSelect }`, or link tabs whose items carry an `href` each.",
|
|
72
69
|
},
|
|
73
70
|
},
|
|
74
71
|
create(context) {
|
|
@@ -85,6 +82,7 @@ export const pageTabsInHeader = defineRule({
|
|
|
85
82
|
};
|
|
86
83
|
},
|
|
87
84
|
});
|
|
85
|
+
/** The Grammar exports that are Page's parts and no longer public. */
|
|
88
86
|
const PAGE_PARTS = new Set(["PageHeader", "PageContainer"]);
|
|
89
87
|
/** The owner kind a file belongs to: a page owner or a layout owner (`fe.feature`, kind `pages` or `layouts`), or null. */
|
|
90
88
|
const ownerKind = (context) => {
|
|
@@ -108,17 +106,17 @@ const enclosingElement = (node) => {
|
|
|
108
106
|
};
|
|
109
107
|
/**
|
|
110
108
|
* A route draws exactly one `Page` and nothing beside it: the page owner draws it, its pure half does, or the one connected
|
|
111
|
-
* block it renders as its root does. `PageHeader` and `PageContainer` are Page's own parts.
|
|
109
|
+
* block it renders as its root does. `PageHeader` and `PageContainer` are Page's own parts, refused where they are imported.
|
|
112
110
|
*/
|
|
113
111
|
export const routePageIsOnePage = defineRule({
|
|
114
112
|
meta: {
|
|
115
113
|
type: "problem",
|
|
116
|
-
docs: { description: "R325: a route draws one Page (in its page owner, its pure half or its one root block, or in the layout of its tabbed segment) with nothing beside it, never a
|
|
114
|
+
docs: { description: "R325: a route draws one Page (in its page owner, its pure half or its one root block, or in the layout of its tabbed segment) with nothing beside it, never a PageHeader or PageContainer imported by hand, and never no Page at all." },
|
|
117
115
|
schema: [],
|
|
118
116
|
messages: {
|
|
119
117
|
sibling: "This `{{name}}` draws the route's `Page` and has a sibling: a route page is one `Page`, and whatever sits beside it escapes the header band, the measure and the rhythm `Page` owns. Move the sibling into the `Page` body (as the block's children when a block draws the `Page`), or into `header` when it heads the page.",
|
|
120
118
|
twice: "This `{{name}}` draws a second `Page` inside the route's `Page`{{framer}}: a route page is exactly one `Page`, so the band, the measure and the rhythm would be decided twice. Draw its content as the body of the one `Page`.",
|
|
121
|
-
part: "`{{name}}` is
|
|
119
|
+
part: "`{{name}}` is a part of `Page`, not an export: a page or layout owner composes `Page` (`header` for the band, `measure` for the body), so the inset and the rhythm have one owner. Replace it with `Page`.",
|
|
122
120
|
missing: "This page owner draws no `Page`: a route page is one Grammar `Page`, which owns the header band, the measure and the rhythm, so without it the page has no heading band and its body sits on no inset. Wrap what it draws in a `Page` whose `header.title` is the page heading (never a `Heading` of its own), draw it through the owner's pure half in `./component`, or render as the root the one connected block that draws the `Page` from client data.",
|
|
123
121
|
},
|
|
124
122
|
},
|
|
@@ -146,8 +144,16 @@ export const routePageIsOnePage = defineRule({
|
|
|
146
144
|
const opening = element.openingElement.name;
|
|
147
145
|
return opening.type === "JSXIdentifier" && appImports.has(opening.name) && blockDrawsPage(context, element.openingElement);
|
|
148
146
|
};
|
|
147
|
+
const parts = refuseGrammarExports(PAGE_PARTS, ({ node, name }) => {
|
|
148
|
+
// The part's own finding already says to draw a `Page` instead; the missing Page is not reported twice.
|
|
149
|
+
drawsPage = true;
|
|
150
|
+
context.report({ node, messageId: "part", data: { name } });
|
|
151
|
+
});
|
|
149
152
|
return {
|
|
153
|
+
MemberExpression: parts.MemberExpression,
|
|
154
|
+
JSXMemberExpression: parts.JSXMemberExpression,
|
|
150
155
|
ImportDeclaration(declaration) {
|
|
156
|
+
parts.ImportDeclaration(declaration);
|
|
151
157
|
const source = String(declaration.source.value);
|
|
152
158
|
const into = PURE_HALF.test(source) ? pureHalf : APP_SOURCE.test(source) ? appImports : null;
|
|
153
159
|
if (into === null || declaration.importKind === "type")
|
|
@@ -161,12 +167,6 @@ export const routePageIsOnePage = defineRule({
|
|
|
161
167
|
const opening = element.openingElement.name;
|
|
162
168
|
if (opening.type === "JSXIdentifier" && pureHalf.has(opening.name))
|
|
163
169
|
drawsPage = true;
|
|
164
|
-
if (name !== null && PAGE_PARTS.has(name)) {
|
|
165
|
-
// The part's own finding already says to draw a `Page` instead; the missing Page is not reported twice.
|
|
166
|
-
drawsPage = true;
|
|
167
|
-
context.report({ node: element, messageId: "part", data: { name } });
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
170
|
if (!drawsFrame(element, name))
|
|
171
171
|
return;
|
|
172
172
|
drawsPage = true;
|
package/dist/pane.js
CHANGED
|
@@ -29,7 +29,7 @@ import { defineFamilyRules, defineRule, exemptFreestyle } from "./lib/rule-contr
|
|
|
29
29
|
/** The Grammar package entries a component can be imported from. */
|
|
30
30
|
const GRAMMAR_SOURCE = /^@starci\/grammar(?:\/(?:common|core|offset-pop))?$/;
|
|
31
31
|
/** The bounded surfaces a pane grid is not drawn in. */
|
|
32
|
-
const SURFACES = new Set(["SurfaceCard"
|
|
32
|
+
const SURFACES = new Set(["SurfaceCard"]);
|
|
33
33
|
/** The helpers that join class strings. */
|
|
34
34
|
const CLASS_BUILDERS = new Set(["cn", "clsx", "cx", "classNames", "classnames", "twMerge", "twJoin", "cva", "tv"]);
|
|
35
35
|
/** How many same-file components a section's root follows. */
|
|
@@ -363,7 +363,7 @@ const wrongFrame = (grid) => {
|
|
|
363
363
|
const name = nameOf(current.openingElement.name);
|
|
364
364
|
if (name === "Freestyle")
|
|
365
365
|
return null;
|
|
366
|
-
if (name === "Pane" || name === "Page" ||
|
|
366
|
+
if (name === "Pane" || name === "Page" || (name !== null && SURFACES.has(name)))
|
|
367
367
|
frames.push(current);
|
|
368
368
|
else if (frames.length === 0)
|
|
369
369
|
wrapped = true;
|
|
@@ -388,7 +388,7 @@ const wrongFrame = (grid) => {
|
|
|
388
388
|
export const paneGridPage = defineRule({
|
|
389
389
|
meta: {
|
|
390
390
|
type: "problem",
|
|
391
|
-
docs: { description: "R348: a PaneGrid is the one child of Page chrome=\"panes\" or stands inside a Pane, never in a
|
|
391
|
+
docs: { description: "R348: a PaneGrid is the one child of Page chrome=\"panes\" or stands inside a Pane, never in a card or another page chrome.", url: "knowledge/hfs/rules.yaml#R348" },
|
|
392
392
|
schema: [],
|
|
393
393
|
messages: {
|
|
394
394
|
frame: "This `PaneGrid` is drawn in {{where}}. A grid is the whole body of a page that is panes, so `<Page chrome=\"panes\">` attaches it flush under the header, full width and to the viewport bottom, with no card, radius or gap; inside a pane it nests as the pane's one child. Make it the child of `Page chrome=\"panes\"` (or of a `Pane`) and drop the wrapper.",
|
package/dist/status-colors.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
2
2
|
/**
|
|
3
|
-
* The rule that holds `status-colors.md` (HFS R61 `FE_STYLE_TOKEN_ONLY`, the
|
|
4
|
-
*
|
|
3
|
+
* The rule that holds `status-colors.md` (HFS R61 `FE_STYLE_TOKEN_ONLY`, the status-pair rule for class names; no gate
|
|
4
|
+
* judges the colours an app's theme gives the pair).
|
|
5
5
|
*
|
|
6
6
|
* A STATUS TONE x PAINTS IN TWO WAYS (owner ruling `status-soft-pair-on-background`). Content that sits ON A BACKGROUND OF THE
|
|
7
7
|
* TONE uses the pair `x-soft` and `x-soft-foreground`, the way a HeroUI flat Chip does. A mark that STANDS ALONE, with no
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** A body card does not hold what a kind draws: hand rows, a divider, a second title. */
|
|
3
|
+
export declare const surfaceCardKind: Rule.RuleModule;
|
|
4
|
+
/** A labelled card repeated in a card grid is a tile. */
|
|
5
|
+
export declare const noLabelInGrid: Rule.RuleModule;
|
|
6
|
+
/** The rules this law contributes to the plugin. */
|
|
7
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
8
|
+
/** Every rule of this law is an error. */
|
|
9
|
+
export declare const recommended: Record<string, "error">;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { importOf } from "@starci/eslint-canon-fe/lib/bindings.mjs";
|
|
2
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
3
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
4
|
+
/**
|
|
5
|
+
* The card laws (HFS R355 `FE_SURFACE_CARD_KIND`, R366 `FE_LABEL_IN_GRID`): a `SurfaceCard` is one component with a `kind`,
|
|
6
|
+
* and what a kind draws is not drawn by hand around it.
|
|
7
|
+
*
|
|
8
|
+
* R355. A card of the default `body` kind holds copy and controls. It does not hold the things a kind draws for it: a
|
|
9
|
+
* hand-built list (`ul`, `ol`, `li`), a divider, a `SectionHeader` or a `Heading` standing as a second title (the card has a
|
|
10
|
+
* `label`),. Rows are `kind="list"`, key figures
|
|
11
|
+
* `kind="figures"`, faces divided by seams `kind="bands"`.
|
|
12
|
+
*
|
|
13
|
+
* R366. A card with a `label` heads a section; a peer you recognise and open is a `kind="tile"` and names itself inside. A
|
|
14
|
+
* labelled card repeated in a `Grid kind="cards"` (two or more, or one written in a `.map`) is a tile drawn with the wrong
|
|
15
|
+
* anatomy.
|
|
16
|
+
*
|
|
17
|
+
* WHAT THEY READ. The direct children of one JSX element named `SurfaceCard` or `Grid` imported from
|
|
18
|
+
* `@starci/grammar` (or `G.SurfaceCard`); a child is read through `cond && <x/>`, `cond ? <x/> : <y/>` and `items.map(() =>
|
|
19
|
+
* <x/>)`. A project component placed in a card is judged in its own file.
|
|
20
|
+
*
|
|
21
|
+
* WHERE THEY APPLY. Product source (`isProductSource`).
|
|
22
|
+
*/
|
|
23
|
+
const GRAMMAR_SOURCE = /^@starci\/grammar(?:\/(?:common|core|offset-pop))?$/;
|
|
24
|
+
/** The element name of a JSX name: `SurfaceCard`, or `G.SurfaceCard` as `SurfaceCard`. */
|
|
25
|
+
const nameOf = (name) => {
|
|
26
|
+
if (name.type === "JSXIdentifier")
|
|
27
|
+
return name.name;
|
|
28
|
+
if (name.type === "JSXMemberExpression")
|
|
29
|
+
return name.property.name;
|
|
30
|
+
return null;
|
|
31
|
+
};
|
|
32
|
+
/** The leftmost identifier of a JSX name (`G` of `G.Pane`), or null. */
|
|
33
|
+
const rootOf = (name) => {
|
|
34
|
+
if (name.type === "JSXIdentifier")
|
|
35
|
+
return name;
|
|
36
|
+
if (name.type === "JSXMemberExpression")
|
|
37
|
+
return rootOf(name.object);
|
|
38
|
+
return null;
|
|
39
|
+
};
|
|
40
|
+
/** The Grammar component an opening element names, or null for another source or a plain element. */
|
|
41
|
+
const grammarComponent = (context, opening) => {
|
|
42
|
+
const root = rootOf(opening.name);
|
|
43
|
+
if (root === null || !/^[A-Za-z]/.test(root.name))
|
|
44
|
+
return null;
|
|
45
|
+
const imported = importOf(context, root);
|
|
46
|
+
if (imported === null || imported === undefined || !GRAMMAR_SOURCE.test(imported.source))
|
|
47
|
+
return null;
|
|
48
|
+
return nameOf(opening.name);
|
|
49
|
+
};
|
|
50
|
+
/** The string a static attribute value spells, or null. */
|
|
51
|
+
const staticString = (value) => {
|
|
52
|
+
if (value === null)
|
|
53
|
+
return null;
|
|
54
|
+
if (value.type === "Literal")
|
|
55
|
+
return typeof value.value === "string" ? value.value : null;
|
|
56
|
+
if (value.type !== "JSXExpressionContainer")
|
|
57
|
+
return null;
|
|
58
|
+
const expression = value.expression;
|
|
59
|
+
if (expression.type === "Literal")
|
|
60
|
+
return typeof expression.value === "string" ? expression.value : null;
|
|
61
|
+
return expression.type === "TemplateLiteral" && expression.expressions.length === 0 ? expression.quasis[0]?.value.cooked ?? null : null;
|
|
62
|
+
};
|
|
63
|
+
/** The attribute of an opening element by name, or undefined. */
|
|
64
|
+
const attributeOf = (opening, name) => {
|
|
65
|
+
const found = opening.attributes.find((entry) => entry.type === "JSXAttribute" && entry.name.type === "JSXIdentifier" && entry.name.name === name);
|
|
66
|
+
return found !== undefined && found.type === "JSXAttribute" ? found : undefined;
|
|
67
|
+
};
|
|
68
|
+
/** True when the card opens as the default `body` kind: no `kind`, or `kind="body"`. */
|
|
69
|
+
const isBodyCard = (opening) => {
|
|
70
|
+
const kind = attributeOf(opening, "kind");
|
|
71
|
+
return kind === undefined || staticString(kind.value) === "body";
|
|
72
|
+
};
|
|
73
|
+
/** The elements a child position renders, read through conditionals, logical `&&` and `.map`. */
|
|
74
|
+
const elementsOf = (node, inMap = false) => {
|
|
75
|
+
if (node === null || node === undefined)
|
|
76
|
+
return [];
|
|
77
|
+
switch (node.type) {
|
|
78
|
+
case "JSXElement":
|
|
79
|
+
return [{ node, inMap }];
|
|
80
|
+
case "JSXFragment":
|
|
81
|
+
return node.children.flatMap((child) => elementsOf(child, inMap));
|
|
82
|
+
case "JSXExpressionContainer":
|
|
83
|
+
return elementsOf(node.expression, inMap);
|
|
84
|
+
case "ConditionalExpression":
|
|
85
|
+
return [...elementsOf(node.consequent, inMap), ...elementsOf(node.alternate, inMap)];
|
|
86
|
+
case "LogicalExpression":
|
|
87
|
+
return node.operator === "&&" ? elementsOf(node.right, inMap) : [...elementsOf(node.left, inMap), ...elementsOf(node.right, inMap)];
|
|
88
|
+
case "CallExpression": {
|
|
89
|
+
const callee = node.callee;
|
|
90
|
+
const method = callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" ? callee.property.name : null;
|
|
91
|
+
const callback = node.arguments[0];
|
|
92
|
+
if (method !== "map" || callback === undefined || (callback.type !== "ArrowFunctionExpression" && callback.type !== "FunctionExpression"))
|
|
93
|
+
return [];
|
|
94
|
+
if (callback.body.type !== "BlockStatement")
|
|
95
|
+
return elementsOf(callback.body, true);
|
|
96
|
+
return callback.body.body.flatMap((statement) => (statement.type === "ReturnStatement" ? elementsOf(statement.argument, true) : []));
|
|
97
|
+
}
|
|
98
|
+
default:
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/** The elements a JSX element renders as direct children. */
|
|
103
|
+
const childElements = (element) => element.children.flatMap((child) => elementsOf(child));
|
|
104
|
+
/** The raw elements a body card never holds by hand. */
|
|
105
|
+
const HAND_ELEMENTS = new Set(["ul", "ol", "li", "dl"]);
|
|
106
|
+
/** The Grammar components a body card never holds as a direct child. */
|
|
107
|
+
const HAND_COMPONENTS = new Map([
|
|
108
|
+
["Divider", "divider"],
|
|
109
|
+
["SectionHeader", "title"],
|
|
110
|
+
["Heading", "title"],
|
|
111
|
+
]);
|
|
112
|
+
/** A body card does not hold what a kind draws: hand rows, a divider, a second title. */
|
|
113
|
+
export const surfaceCardKind = defineRule({
|
|
114
|
+
meta: {
|
|
115
|
+
type: "problem",
|
|
116
|
+
docs: { description: "R355: a SurfaceCard of the body kind holds no hand list, no Divider and no SectionHeader or Heading." },
|
|
117
|
+
schema: [],
|
|
118
|
+
messages: {
|
|
119
|
+
list: "`<{{element}}>` is a hand-built list in a `body` card. Rows are `<SurfaceCard kind=\"list\" items={...} empty={...} />`, and the card draws every row the same way.",
|
|
120
|
+
divider: "A `Divider` in a card is refused: the card draws its own seams. Faces divided by a line are `<SurfaceCard kind=\"bands\" bands={[...]} />`.",
|
|
121
|
+
title: "`<{{element}}>` stands as a second title inside a card. The card has a `label` (and `fact`, `labelEnd`, `footer`); a sub-section of one subject is a band with its own `label` in `kind=\"bands\"`.",
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
create(context) {
|
|
125
|
+
if (!isProductSource(context))
|
|
126
|
+
return {};
|
|
127
|
+
return {
|
|
128
|
+
JSXElement(element) {
|
|
129
|
+
const opening = element.openingElement;
|
|
130
|
+
const component = grammarComponent(context, opening);
|
|
131
|
+
if (component === "SurfaceCard" && isBodyCard(opening)) {
|
|
132
|
+
for (const { node } of childElements(element)) {
|
|
133
|
+
const child = node.openingElement;
|
|
134
|
+
if (child.name.type === "JSXIdentifier" && HAND_ELEMENTS.has(child.name.name)) {
|
|
135
|
+
context.report({ node: child, messageId: "list", data: { element: child.name.name } });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const childComponent = grammarComponent(context, child);
|
|
139
|
+
const rule = childComponent === null ? undefined : HAND_COMPONENTS.get(childComponent);
|
|
140
|
+
if (rule === "divider")
|
|
141
|
+
context.report({ node: child, messageId: "divider" });
|
|
142
|
+
else if (rule === "title")
|
|
143
|
+
context.report({ node: child, messageId: "title", data: { element: childComponent ?? "" } });
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
/** A labelled card repeated in a card grid is a tile. */
|
|
151
|
+
export const noLabelInGrid = defineRule({
|
|
152
|
+
meta: {
|
|
153
|
+
type: "problem",
|
|
154
|
+
docs: { description: "R366: a labelled SurfaceCard repeated in a Grid kind=\"cards\" is a tile: it names itself inside." },
|
|
155
|
+
schema: [],
|
|
156
|
+
messages: {
|
|
157
|
+
tile: "A card with a `label` repeated in a `Grid kind=\"cards\"`: the label sits outside the material, so the grid reads as headings over boxes. A peer you recognise and open is `<SurfaceCard kind=\"tile\" name mark description badge action />`, named inside beside its mark. Comparing a fact across peers is a `kind=\"list\"` card.",
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
create(context) {
|
|
161
|
+
if (!isProductSource(context))
|
|
162
|
+
return {};
|
|
163
|
+
return {
|
|
164
|
+
JSXElement(element) {
|
|
165
|
+
const opening = element.openingElement;
|
|
166
|
+
if (grammarComponent(context, opening) !== "Grid")
|
|
167
|
+
return;
|
|
168
|
+
const kind = attributeOf(opening, "kind");
|
|
169
|
+
if (kind === undefined || staticString(kind.value) !== "cards")
|
|
170
|
+
return;
|
|
171
|
+
const labelled = childElements(element).filter(({ node }) => {
|
|
172
|
+
const card = node.openingElement;
|
|
173
|
+
return grammarComponent(context, card) === "SurfaceCard" && attributeOf(card, "label") !== undefined && attributeOf(card, "kind") === undefined;
|
|
174
|
+
});
|
|
175
|
+
const repeated = labelled.length >= 2 || labelled.some(({ inMap }) => inMap);
|
|
176
|
+
if (!repeated)
|
|
177
|
+
return;
|
|
178
|
+
for (const { node } of labelled)
|
|
179
|
+
context.report({ node: node.openingElement, messageId: "tile" });
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
/** The rules this law contributes to the plugin. */
|
|
185
|
+
export const rules = {
|
|
186
|
+
"surface-card-kind": surfaceCardKind,
|
|
187
|
+
"no-label-in-grid": noLabelInGrid,
|
|
188
|
+
};
|
|
189
|
+
defineFamilyRules(rules, "knowledge/hfs/rules.yaml#R355");
|
|
190
|
+
/** Every rule of this law is an error. */
|
|
191
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
package/dist/surface-depth.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Rule } from "eslint";
|
|
2
|
-
/** A surface inside another surface
|
|
2
|
+
/** A surface inside another surface is refused. */
|
|
3
3
|
export declare const noRaisedSurfaceInSurface: Rule.RuleModule;
|
|
4
4
|
/** The rules this law contributes to the plugin. */
|
|
5
5
|
export declare const rules: Record<string, Rule.RuleModule>;
|
package/dist/surface-depth.js
CHANGED
|
@@ -1,26 +1,23 @@
|
|
|
1
|
+
import { importOf } from "@starci/eslint-canon-fe/lib/bindings.mjs";
|
|
1
2
|
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
3
|
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
3
4
|
/**
|
|
4
|
-
* The surface
|
|
5
|
-
* `
|
|
6
|
-
* raised (
|
|
5
|
+
* The surface nesting law (HFS R340 `FE_RAISED_SURFACE_IN_SURFACE`): a surface never stands inside another surface. A
|
|
6
|
+
* `SurfaceCard` inside another `SurfaceCard`, inside a `Pane surface`, or inside a `Dialog`, `Drawer`, `AlertDialog` or
|
|
7
|
+
* `Popover` is refused; the Grammar has one depth, raised, and draws no nested edge (owner decision: surface in surface is
|
|
8
|
+
* banned totally, dialogs and drawers included). The Grammar throws the same refusal in development; this is its
|
|
9
|
+
* same-file half, and R356 closes it across files.
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
* `
|
|
10
|
-
* `
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* WHAT IT READS. A `depth` attribute whose value is the string `"top"` (a literal, or an expression container holding one)
|
|
14
|
-
* on a depth-accepting surface element (`SurfaceListCard` or `<ns>.SurfaceListCard`), when a `SurfaceCard`,
|
|
15
|
-
* `SurfaceListCard` or `SurfaceAccordionCard` element of the same file encloses it. A depth computed at run time is not
|
|
16
|
-
* judged, and neither is a surface nobody encloses in this file (its parent may nest it; Grammar resolves that).
|
|
11
|
+
* WHAT IT READS. A `SurfaceCard` element (or `<ns>.SurfaceCard`) imported from `@starci/grammar` that a `SurfaceCard`,
|
|
12
|
+
* `Dialog`, `Drawer`, `AlertDialog`, `Popover` or `Pane` with a `surface` prop of the same file encloses. An overlay's
|
|
13
|
+
* `trigger` is not its content, so a card written in a trigger is not enclosed by it. A surface nobody encloses in this file
|
|
14
|
+
* (its parent may nest it) is judged where the parent is written.
|
|
17
15
|
*
|
|
18
16
|
* WHERE IT APPLIES. Product source (`isProductSource`).
|
|
19
17
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const DEPTH_PROP = new Set(["SurfaceListCard"]);
|
|
18
|
+
const GRAMMAR_SOURCE = /^@starci\/grammar(?:\/(?:common|core|offset-pop))?$/;
|
|
19
|
+
/** Surfaces and overlays whose content is a surface. */
|
|
20
|
+
const HOSTS = new Set(["SurfaceCard", "Dialog", "Drawer", "AlertDialog", "Popover", "Pane"]);
|
|
24
21
|
/** The element name of a JSX name: `SurfaceCard`, or `G.SurfaceCard` as `SurfaceCard`. */
|
|
25
22
|
const nameOf = (name) => {
|
|
26
23
|
if (name.type === "JSXIdentifier")
|
|
@@ -29,56 +26,63 @@ const nameOf = (name) => {
|
|
|
29
26
|
return name.property.name;
|
|
30
27
|
return null;
|
|
31
28
|
};
|
|
32
|
-
/**
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
/** The leftmost identifier of a JSX name. */
|
|
30
|
+
const rootOf = (name) => {
|
|
31
|
+
if (name.type === "JSXIdentifier")
|
|
32
|
+
return name;
|
|
33
|
+
if (name.type === "JSXMemberExpression")
|
|
34
|
+
return rootOf(name.object);
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
/** True when an opening element names the Grammar component `wanted`, or one of a set. */
|
|
38
|
+
const isGrammar = (context, opening, wanted) => {
|
|
39
|
+
const root = rootOf(opening.name);
|
|
40
|
+
const name = nameOf(opening.name);
|
|
41
|
+
if (root === null || name === null || !wanted.has(name))
|
|
42
|
+
return null;
|
|
43
|
+
const imported = importOf(context, root);
|
|
44
|
+
return imported !== null && imported !== undefined && GRAMMAR_SOURCE.test(imported.source) ? name : null;
|
|
45
|
+
};
|
|
46
|
+
/** The host that encloses this card's content in the same file, or null. A host's attributes (a `trigger`) do not enclose. */
|
|
47
|
+
const enclosingHost = (context, opening) => {
|
|
48
|
+
let previous = opening;
|
|
49
|
+
for (let current = opening.parent?.parent; current !== undefined && current !== null; previous = current, current = current.parent) {
|
|
37
50
|
if (current.type !== "JSXElement")
|
|
38
51
|
continue;
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
|
|
52
|
+
// `previous` is a child of the element, or its opening tag when the walk came up through an attribute.
|
|
53
|
+
if (previous === current.openingElement)
|
|
54
|
+
continue;
|
|
55
|
+
const name = isGrammar(context, current.openingElement, HOSTS);
|
|
56
|
+
if (name === null)
|
|
57
|
+
continue;
|
|
58
|
+
if (name === "Pane" && !current.openingElement.attributes.some((entry) => entry.type === "JSXAttribute" && entry.name.type === "JSXIdentifier" && entry.name.name === "surface"))
|
|
59
|
+
continue;
|
|
60
|
+
return name;
|
|
42
61
|
}
|
|
43
|
-
return
|
|
44
|
-
};
|
|
45
|
-
/** True when an attribute value is statically the string `"top"`. */
|
|
46
|
-
const isTop = (value) => {
|
|
47
|
-
if (value === null)
|
|
48
|
-
return false;
|
|
49
|
-
if (value.type === "Literal")
|
|
50
|
-
return value.value === "top";
|
|
51
|
-
if (value.type !== "JSXExpressionContainer")
|
|
52
|
-
return false;
|
|
53
|
-
const expression = value.expression;
|
|
54
|
-
if (expression.type === "Literal")
|
|
55
|
-
return expression.value === "top";
|
|
56
|
-
return expression.type === "TemplateLiteral" && expression.expressions.length === 0 && expression.quasis.length === 1 && expression.quasis[0]?.value.cooked === "top";
|
|
62
|
+
return null;
|
|
57
63
|
};
|
|
58
|
-
/** A surface inside another surface
|
|
64
|
+
/** A surface inside another surface is refused. */
|
|
59
65
|
export const noRaisedSurfaceInSurface = defineRule({
|
|
60
66
|
meta: {
|
|
61
67
|
type: "problem",
|
|
62
|
-
docs: { description: "R340: a
|
|
68
|
+
docs: { description: "R340: a SurfaceCard never stands inside another SurfaceCard, a Pane surface, or a Dialog, Drawer, AlertDialog or Popover." },
|
|
63
69
|
schema: [],
|
|
64
70
|
messages: {
|
|
65
|
-
|
|
71
|
+
nested: "This `SurfaceCard` stands inside {{host}}: a surface never stands inside a surface (the Grammar has one depth and draws no nested edge). Give the content to the host's own body, or use a kind of the outer card (`bands`, `list`, `figures`) for what needs its own faces.",
|
|
66
72
|
},
|
|
67
73
|
},
|
|
68
74
|
create(context) {
|
|
69
75
|
if (!isProductSource(context))
|
|
70
76
|
return {};
|
|
71
77
|
return {
|
|
72
|
-
|
|
73
|
-
if (
|
|
74
|
-
return;
|
|
75
|
-
const opening = attribute.parent;
|
|
76
|
-
if (opening.type !== "JSXOpeningElement")
|
|
78
|
+
JSXOpeningElement(opening) {
|
|
79
|
+
if (isGrammar(context, opening, new Set(["SurfaceCard"])) === null)
|
|
77
80
|
return;
|
|
78
|
-
const
|
|
79
|
-
if (
|
|
81
|
+
const host = enclosingHost(context, opening);
|
|
82
|
+
if (host === null)
|
|
80
83
|
return;
|
|
81
|
-
|
|
84
|
+
const label = host === "SurfaceCard" ? "another `SurfaceCard`" : host === "Pane" ? "a `Pane surface`" : `a \`${host}\``;
|
|
85
|
+
context.report({ node: opening, messageId: "nested", data: { host: label } });
|
|
82
86
|
},
|
|
83
87
|
};
|
|
84
88
|
},
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/**
|
|
3
|
+
* The laws of text and emphasis (HFS R357, R358, R359).
|
|
4
|
+
*
|
|
5
|
+
* text-inline-only (R357) `Text` is a run of words: a block component inside it is a layout drawn in a paragraph.
|
|
6
|
+
* no-text-heading (R358) a strong `Text` that only heads a group of blocks is a hand heading: use `Section`, a card
|
|
7
|
+
* `label` or `Heading`, which own the level, the spacing and the outline.
|
|
8
|
+
* one-highlight-per-render (R359) a render states one highlighted thing (G-9): a second `isHighlight` dilutes the first.
|
|
9
|
+
*
|
|
10
|
+
* WHAT THEY READ. Grammar names are resolved through the file's own imports from `@starci/grammar`, so an alias or a local
|
|
11
|
+
* component of the same name is not judged. A spread attribute or a computed value is not judged.
|
|
12
|
+
*/
|
|
13
|
+
/** Components that lay content out as blocks: none of them belongs inside a run of text. */
|
|
14
|
+
export declare const BLOCK_COMPONENTS: ReadonlySet<string>;
|
|
15
|
+
/** The rules this law contributes to the plugin. */
|
|
16
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
17
|
+
/** Every rule of this law is an error. */
|
|
18
|
+
export declare const recommended: Record<string, "error">;
|