@starci/eslint-grammar-core 0.20.1 → 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 +23 -2
- 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 +33 -8
- 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.d.ts +10 -4
- package/dist/status-colors.js +103 -29
- 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 +17 -8
- 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 +374 -36
- 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.d.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import type { Rule } from "eslint";
|
|
2
|
-
|
|
3
|
-
export declare const solidTextClasses: (text: string) => {
|
|
2
|
+
type PaintedClass = {
|
|
4
3
|
token: string;
|
|
5
4
|
family: string;
|
|
6
5
|
tone: string;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
6
|
+
};
|
|
7
|
+
/** Every solid-tone text class in one class string. */
|
|
8
|
+
export declare const solidTextClasses: (text: string) => PaintedClass[];
|
|
9
|
+
/** Every soft-foreground text class in one class string. */
|
|
10
|
+
export declare const softTextClasses: (text: string) => PaintedClass[];
|
|
11
|
+
/** The tones whose soft tint a class string sets as a background. */
|
|
12
|
+
export declare const softBackgroundTones: (text: string) => string[];
|
|
13
|
+
/** Content on a tone's tint wears the soft foreground; a mark with no tint behind it wears the tone. */
|
|
9
14
|
export declare const statusTextUsesSoftForeground: Rule.RuleModule;
|
|
10
15
|
/** The rules this law contributes to the plugin. */
|
|
11
16
|
export declare const rules: Record<string, Rule.RuleModule>;
|
|
12
17
|
/** Every rule of this law is an error. */
|
|
13
18
|
export declare const recommended: Record<string, "error">;
|
|
19
|
+
export {};
|
package/dist/status-colors.js
CHANGED
|
@@ -1,30 +1,44 @@
|
|
|
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
|
-
* A STATUS TONE
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* make that ink readable (3:1 on the tint and on the page); the solid tone is only the mid-luminance hue of the brand
|
|
10
|
-
* and reads 3.8:1 or worse on white, so as a text colour it fails the contrast the soft pair was built to give.
|
|
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
|
+
* TONE uses the pair `x-soft` and `x-soft-foreground`, the way a HeroUI flat Chip does. A mark that STANDS ALONE, with no
|
|
8
|
+
* background of the tone behind it, uses `x` itself. A solid fill takes `x-foreground`.
|
|
11
9
|
*
|
|
12
10
|
* WHAT IT READS. Every static class string a component writes: the `className` / `class` attribute of an element (through
|
|
13
|
-
* `cn()`, `clsx()`, conditionals, arrays, template text), and a string in a constant or in a `classes` entry. The tones
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* tone (`text-success`, `fill-danger`, `stroke-warning`, `decoration-info`, with any variant and opacity).
|
|
11
|
+
* `cn()`, `clsx()`, conditionals, arrays, template text), and a string in a constant or in a `classes` entry. The tones are the
|
|
12
|
+
* grammar's `STATUS_TONES` (`lib/status-tones.generated.ts`, generated from the grammar with the stylelint vocabulary), never a
|
|
13
|
+
* list typed here. Two findings:
|
|
17
14
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
15
|
+
* - ON A TINT: a class string that sets `bg-x-soft` and also paints text, an icon or a text decoration with the solid tone
|
|
16
|
+
* (`text-x`, `fill-x`, `stroke-x`, `decoration-x`, with any variant and opacity). The content on the tint takes
|
|
17
|
+
* `text-x-soft-foreground`.
|
|
18
|
+
* - ALONE: the soft foreground (`text-x-soft-foreground`) in the `className` of an element when neither that element nor an
|
|
19
|
+
* enclosing element of the same file sets `bg-x-soft`. A mark with no tint behind it takes `text-x`. A constant, an array in
|
|
20
|
+
* a constant or a `classes` entry is not judged for this finding, since the element that wears it is not in view.
|
|
21
|
+
*
|
|
22
|
+
* OFFSET POP. The family's light-theme tones are pastel fills (1.3 to 1.5:1 as bare text), and the owner chose to keep them
|
|
23
|
+
* ("1. Pastel"): a bare mark wears the soft foreground there. When the selected Grammar is `offset-pop` the ALONE finding is
|
|
24
|
+
* not reported; the ON A TINT finding still is.
|
|
25
|
+
*
|
|
26
|
+
* WHAT IT LEAVES ALONE: a bare solid tone (`text-success` with no `bg-success-soft` on the element), the soft pair on its tint, a
|
|
27
|
+
* solid fill with its own ink (`bg-success text-success-foreground`), a solid border, ring or outline (`border-danger`,
|
|
28
|
+
* `ring-warning`: a boundary is not text), and a class that only shares a prefix (`text-successful`).
|
|
21
29
|
*/
|
|
22
30
|
import { attributeValue } from "@starci/eslint-canon-fe/lib/ast.mjs";
|
|
23
31
|
import { STATUS_TONES } from "./lib/status-tones.generated.js";
|
|
24
32
|
/** The utility families that paint text, an icon or a text decoration. */
|
|
25
33
|
const TEXT_PAINT = ["text", "fill", "stroke", "decoration"];
|
|
34
|
+
/** An optional opacity suffix: `/60` or `/[.4]`. */
|
|
35
|
+
const OPACITY = "(?:/(?:\\d+|\\[[^\\]]*\\]))?";
|
|
26
36
|
/** `text-success`, `text-success/60`, `text-success-hover`, with an optional `!` and any variants before it. */
|
|
27
|
-
const SOLID_TEXT = new RegExp(`^!?(${TEXT_PAINT.join("|")})-(${STATUS_TONES.join("|")})(?:-hover)
|
|
37
|
+
const SOLID_TEXT = new RegExp(`^!?(${TEXT_PAINT.join("|")})-(${STATUS_TONES.join("|")})(?:-hover)?${OPACITY}$`);
|
|
38
|
+
/** `text-success-soft-foreground`, with an optional `!`, opacity and any variants before it. */
|
|
39
|
+
const SOFT_TEXT = new RegExp(`^!?(${TEXT_PAINT.join("|")})-(${STATUS_TONES.join("|")})-soft-foreground${OPACITY}$`);
|
|
40
|
+
/** `bg-success-soft`, `bg-success-soft-hover`, with an optional `!`, opacity and any variants before it. */
|
|
41
|
+
const SOFT_BACKGROUND = new RegExp(`^!?bg-(${STATUS_TONES.join("|")})-soft(?:-hover)?${OPACITY}$`);
|
|
28
42
|
/** The class after its variants: `md:hover:text-success` and `[&>svg]:text-success` both end at `text-success`. */
|
|
29
43
|
const utilityOf = (token) => {
|
|
30
44
|
let depth = 0;
|
|
@@ -39,16 +53,28 @@ const utilityOf = (token) => {
|
|
|
39
53
|
}
|
|
40
54
|
return token.slice(cut + 1);
|
|
41
55
|
};
|
|
42
|
-
/** Every
|
|
43
|
-
|
|
56
|
+
/** Every class of one class string that the pattern reads, as `{ token, family, tone }`. */
|
|
57
|
+
const paintedClasses = (text, pattern) => text
|
|
44
58
|
.split(/\s+/)
|
|
45
59
|
.filter(Boolean)
|
|
46
60
|
.flatMap((token) => {
|
|
47
|
-
const match =
|
|
61
|
+
const match = pattern.exec(utilityOf(token));
|
|
48
62
|
const family = match?.[1];
|
|
49
63
|
const tone = match?.[2];
|
|
50
64
|
return family !== undefined && tone !== undefined ? [{ token, family, tone }] : [];
|
|
51
65
|
});
|
|
66
|
+
/** Every solid-tone text class in one class string. */
|
|
67
|
+
export const solidTextClasses = (text) => paintedClasses(text, SOLID_TEXT);
|
|
68
|
+
/** Every soft-foreground text class in one class string. */
|
|
69
|
+
export const softTextClasses = (text) => paintedClasses(text, SOFT_TEXT);
|
|
70
|
+
/** The tones whose soft tint a class string sets as a background. */
|
|
71
|
+
export const softBackgroundTones = (text) => text
|
|
72
|
+
.split(/\s+/)
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.flatMap((token) => {
|
|
75
|
+
const tone = SOFT_BACKGROUND.exec(utilityOf(token))?.[1];
|
|
76
|
+
return tone === undefined ? [] : [tone];
|
|
77
|
+
});
|
|
52
78
|
/** The string nodes inside an expression that builds a class string: literals, template text, keys, and everything a call, array or condition carries. */
|
|
53
79
|
function* stringNodes(node) {
|
|
54
80
|
if (!node || typeof node.type !== "string")
|
|
@@ -97,32 +123,80 @@ function* stringNodes(node) {
|
|
|
97
123
|
default:
|
|
98
124
|
}
|
|
99
125
|
}
|
|
100
|
-
/**
|
|
126
|
+
/** The tones whose soft tint some string of the list sets as a background. */
|
|
127
|
+
const tintsOf = (strings) => new Set(strings.flatMap(({ text }) => softBackgroundTones(text)));
|
|
128
|
+
/** True for the `className` and `class` attributes of an element. */
|
|
129
|
+
const isClassAttribute = (node) => node.type === "JSXAttribute" && node.name.type === "JSXIdentifier" && (node.name.name === "className" || node.name.name === "class");
|
|
130
|
+
/** The tones whose tint an element enclosing the attribute's own element sets in its `className`, in the same file. */
|
|
131
|
+
const enclosingTints = (attribute) => {
|
|
132
|
+
const tones = new Set();
|
|
133
|
+
for (let current = attribute.parent?.parent?.parent; current !== undefined && current !== null; current = current.parent) {
|
|
134
|
+
if (current.type !== "JSXElement")
|
|
135
|
+
continue;
|
|
136
|
+
for (const candidate of current.openingElement.attributes) {
|
|
137
|
+
if (candidate.type !== "JSXAttribute" || !isClassAttribute(candidate))
|
|
138
|
+
continue;
|
|
139
|
+
for (const tone of tintsOf([...stringNodes(attributeValue(candidate))]))
|
|
140
|
+
tones.add(tone);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return tones;
|
|
144
|
+
};
|
|
145
|
+
/** The Grammar the app selected (`settings.starci.hfs.grammar`), or null when the settings carry none. */
|
|
146
|
+
const grammarOf = (settings) => {
|
|
147
|
+
const starci = settings?.["starci"];
|
|
148
|
+
if (typeof starci !== "object" || starci === null)
|
|
149
|
+
return null;
|
|
150
|
+
const hfs = Reflect.get(starci, "hfs");
|
|
151
|
+
if (typeof hfs !== "object" || hfs === null)
|
|
152
|
+
return null;
|
|
153
|
+
const grammar = Reflect.get(hfs, "grammar");
|
|
154
|
+
return typeof grammar === "string" ? grammar : null;
|
|
155
|
+
};
|
|
156
|
+
/** Content on a tone's tint wears the soft foreground; a mark with no tint behind it wears the tone. */
|
|
101
157
|
export const statusTextUsesSoftForeground = defineRule({
|
|
102
158
|
meta: {
|
|
103
159
|
type: "problem",
|
|
104
|
-
docs: { description: "Status
|
|
160
|
+
docs: { description: "Status content on a tone background takes the soft pair; a status mark with no background of the tone takes the tone itself." },
|
|
105
161
|
schema: [],
|
|
106
162
|
messages: {
|
|
107
|
-
|
|
163
|
+
onTint: "`{{token}}` paints text or an icon with the solid `{{tone}}` tone on a `bg-{{tone}}-soft` tint. Content that sits on a background of the tone takes the pair: `{{family}}-{{tone}}-soft-foreground` on `bg-{{tone}}-soft`.",
|
|
164
|
+
alone: "`{{token}}` paints a status mark with the soft foreground, but no `bg-{{tone}}-soft` is behind it on this element or on an element that encloses it in this file. A mark that stands alone, with no background of the tone, takes the tone itself: `{{family}}-{{tone}}`. Put it on a `bg-{{tone}}-soft` tint to keep the soft pair.",
|
|
108
165
|
},
|
|
109
166
|
},
|
|
110
167
|
create(context) {
|
|
111
168
|
// A `classes` entry inside a constant is reached twice (as part of the constant, and as the entry): one finding per string.
|
|
169
|
+
// Offset Pop's light theme draws a bare mark with the soft foreground (owner choice "1. Pastel"), so the lone soft foreground is not judged there.
|
|
170
|
+
const aloneJudged = grammarOf(context.settings) !== "offset-pop";
|
|
112
171
|
const seen = new Set();
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
continue;
|
|
117
|
-
seen.add(node);
|
|
172
|
+
const fresh = (strings) => [...strings].filter(({ node }) => !seen.has(node) && seen.add(node));
|
|
173
|
+
const reportOnTint = (strings, tints) => {
|
|
174
|
+
for (const { node, text } of strings) {
|
|
118
175
|
for (const { token, family, tone } of solidTextClasses(text))
|
|
119
|
-
|
|
176
|
+
if (tints.has(tone))
|
|
177
|
+
context.report({ node, messageId: "onTint", data: { token, family, tone } });
|
|
120
178
|
}
|
|
121
179
|
};
|
|
180
|
+
const check = (root) => {
|
|
181
|
+
const strings = fresh(stringNodes(root));
|
|
182
|
+
reportOnTint(strings, tintsOf(strings));
|
|
183
|
+
};
|
|
122
184
|
return {
|
|
123
185
|
JSXAttribute(node) {
|
|
124
|
-
if (
|
|
125
|
-
|
|
186
|
+
if (!isClassAttribute(node))
|
|
187
|
+
return;
|
|
188
|
+
const strings = fresh(stringNodes(attributeValue(node)));
|
|
189
|
+
const own = tintsOf(strings);
|
|
190
|
+
reportOnTint(strings, own);
|
|
191
|
+
if (!aloneJudged)
|
|
192
|
+
return;
|
|
193
|
+
const enclosing = enclosingTints(node);
|
|
194
|
+
for (const { node: stringNode, text } of strings) {
|
|
195
|
+
for (const { token, family, tone } of softTextClasses(text)) {
|
|
196
|
+
if (!own.has(tone) && !enclosing.has(tone))
|
|
197
|
+
context.report({ node: stringNode, messageId: "alone", data: { token, family, tone } });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
126
200
|
},
|
|
127
201
|
VariableDeclarator(node) {
|
|
128
202
|
check(node.init);
|
|
@@ -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>;
|