@starci/eslint-grammar-core 0.27.1 → 0.30.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 +29 -0
- package/README.md +3 -1
- package/dist/button-arrow.d.ts +7 -0
- package/dist/button-arrow.js +146 -0
- package/dist/field-label.d.ts +7 -0
- package/dist/field-label.js +83 -0
- package/dist/gap-proximity.d.ts +7 -0
- package/dist/gap-proximity.js +194 -0
- package/dist/index.js +22 -0
- package/dist/lib/cross-file-inputs.js +3 -0
- package/dist/lib/grammar-jsx.d.ts +27 -0
- package/dist/lib/grammar-jsx.js +136 -0
- package/dist/lib/row-context.d.ts +25 -0
- package/dist/lib/row-context.js +134 -0
- package/dist/lib/why.js +60 -0
- package/dist/page-breadcrumbs.d.ts +7 -0
- package/dist/page-breadcrumbs.js +103 -0
- package/dist/row-title-link.d.ts +6 -0
- package/dist/row-title-link.js +172 -0
- package/dist/same-line-size.d.ts +6 -0
- package/dist/same-line-size.js +93 -0
- package/dist/single-child-card.d.ts +7 -0
- package/dist/single-child-card.js +103 -0
- package/dist/suggestion-chips.d.ts +6 -0
- package/dist/suggestion-chips.js +122 -0
- package/dist/surface-card.js +1 -1
- package/dist/surface-in-surface.d.ts +7 -0
- package/dist/surface-in-surface.js +84 -0
- package/dist/table-cell-type.d.ts +6 -0
- package/dist/table-cell-type.js +122 -0
- package/dist/tabs.d.ts +9 -0
- package/dist/tabs.js +147 -0
- package/docs/button-arrow.md +34 -0
- package/docs/field-label.md +26 -0
- package/docs/gap-proximity.md +31 -0
- package/docs/page-breadcrumbs.md +28 -0
- package/docs/row-title-link.md +30 -0
- package/docs/same-line-size.md +26 -0
- package/docs/single-child-card.md +37 -0
- package/docs/suggestion-chips.md +31 -0
- package/docs/surface-in-surface.md +28 -0
- package/docs/table-cell-type.md +32 -0
- package/docs/tabs.md +48 -0
- package/package.json +4 -4
- package/runtime/bundle.json +1 -0
- package/runtime/knowledge/ui/presentation/grammar-laws.yaml +121 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
3
|
+
import { attributeOf, descendants, elementOf, grammarBindings, isListRow, keyName, propertyOf } from "./lib/row-context.js";
|
|
4
|
+
/**
|
|
5
|
+
* The row title link law (`FE_ROW_TITLE_LINK`): when a row opens, its title is plain text and the row is the one pressable. A
|
|
6
|
+
* `Link` inside a pressable row is a second target on the same row (nested interactive elements, two tab stops, one destination).
|
|
7
|
+
*
|
|
8
|
+
* WHAT IT READS.
|
|
9
|
+
* - a ListRow data object (a row of the `items` of a list collection, or the `row` of a `<ListRow>`) whose `title` holds a
|
|
10
|
+
* `Link` while the same object carries `onPress`, `onAction`, `href`, `onFollow` or `chevron`;
|
|
11
|
+
* - a `DataTable` that opens its rows (`onRowAction`, or an inline `rows` literal or `.map` whose objects carry an `href`)
|
|
12
|
+
* whose `renderCell` returns a `Link` for the row-header column (the column marked `isRowHeader`, else the first one of an
|
|
13
|
+
* inline `columns` literal). A `Link` in a branch of another column is not judged; a `Link` outside any column branch is.
|
|
14
|
+
*/
|
|
15
|
+
/** The keys of a ListRow data object that make the whole row pressable. */
|
|
16
|
+
const PRESSABLE_KEYS = ["onPress", "onAction", "href", "onFollow", "chevron"];
|
|
17
|
+
const isFunction = (node) => node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration";
|
|
18
|
+
/** The string literals a test expression compares against. */
|
|
19
|
+
const literalsOf = (test, source) => {
|
|
20
|
+
const found = [];
|
|
21
|
+
for (const node of descendants(test, source)) {
|
|
22
|
+
if (node.type === "Literal" && typeof node.value === "string")
|
|
23
|
+
found.push(node.value);
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
};
|
|
27
|
+
export const rowTitleLink = defineRule({
|
|
28
|
+
meta: {
|
|
29
|
+
type: "problem",
|
|
30
|
+
docs: { description: "FE_ROW_TITLE_LINK: a pressable row's title is plain text, never a Link." },
|
|
31
|
+
schema: [],
|
|
32
|
+
messages: {
|
|
33
|
+
row: "This Link is the title of a row that already opens (`{{key}}`). The row is the one pressable: write the title as plain text (`<Text isStrong>`) and keep the destination on the row.",
|
|
34
|
+
cell: "This Link is the row-header cell of a DataTable whose rows open (`{{why}}`). The row is the one pressable: write the cell as plain text (`<Text isStrong>`) and keep the destination on the row.",
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
create(context) {
|
|
38
|
+
if (!isProductSource(context))
|
|
39
|
+
return {};
|
|
40
|
+
let bound = new Map();
|
|
41
|
+
const source = context.sourceCode;
|
|
42
|
+
const linksIn = (root) => {
|
|
43
|
+
const found = [];
|
|
44
|
+
for (const node of descendants(root, source)) {
|
|
45
|
+
if (node.type === "JSXElement" && elementOf(node.openingElement, bound).grammar === "Link")
|
|
46
|
+
found.push(node);
|
|
47
|
+
}
|
|
48
|
+
return found;
|
|
49
|
+
};
|
|
50
|
+
/** The function a renderCell expression names: itself, or a function declared by that name in this file. */
|
|
51
|
+
const functionOf = (expression) => {
|
|
52
|
+
if (isFunction(expression))
|
|
53
|
+
return expression;
|
|
54
|
+
if (expression.type !== "Identifier")
|
|
55
|
+
return null;
|
|
56
|
+
let scope = source.getScope(expression);
|
|
57
|
+
while (scope) {
|
|
58
|
+
const variable = scope.set.get(expression.name);
|
|
59
|
+
if (variable) {
|
|
60
|
+
for (const def of variable.defs) {
|
|
61
|
+
if (def.type === "FunctionName")
|
|
62
|
+
return def.node;
|
|
63
|
+
{
|
|
64
|
+
const init = def.node.init;
|
|
65
|
+
if (init && isFunction(init))
|
|
66
|
+
return init;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
scope = scope.upper;
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
};
|
|
75
|
+
/** The id of the row-header column of an inline `columns` literal, or null when it cannot be read. */
|
|
76
|
+
const headerOf = (opening) => {
|
|
77
|
+
const columns = attributeOf(opening, "columns")?.value;
|
|
78
|
+
if (columns?.type !== "JSXExpressionContainer" || columns.expression.type !== "ArrayExpression")
|
|
79
|
+
return null;
|
|
80
|
+
const objects = columns.expression.elements.filter((entry) => entry?.type === "ObjectExpression");
|
|
81
|
+
const marked = objects.find((column) => {
|
|
82
|
+
const value = propertyOf(column, "isRowHeader")?.value;
|
|
83
|
+
return value?.type === "Literal" && value.value === true;
|
|
84
|
+
}) ?? objects[0];
|
|
85
|
+
const id = marked ? propertyOf(marked, "id")?.value : undefined;
|
|
86
|
+
return id?.type === "Literal" && typeof id.value === "string" ? id.value : null;
|
|
87
|
+
};
|
|
88
|
+
/** What makes the rows of a DataTable open, or null when nothing does. */
|
|
89
|
+
const opensRows = (opening) => {
|
|
90
|
+
if (attributeOf(opening, "onRowAction") !== null)
|
|
91
|
+
return "onRowAction";
|
|
92
|
+
const rows = attributeOf(opening, "rows")?.value;
|
|
93
|
+
if (rows?.type !== "JSXExpressionContainer")
|
|
94
|
+
return null;
|
|
95
|
+
for (const node of descendants(rows.expression, source)) {
|
|
96
|
+
if (node.type === "ObjectExpression" && propertyOf(node, "href") !== null)
|
|
97
|
+
return "row href";
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
};
|
|
101
|
+
/** Whether a Link in a renderCell function stands in the branch of the header column (or in no column branch at all). */
|
|
102
|
+
const inHeaderBranch = (link, fn, header) => {
|
|
103
|
+
let constrained = false;
|
|
104
|
+
for (let node = link, parent = node.parent; parent != null && node !== fn; node = parent, parent = parent.parent) {
|
|
105
|
+
let tests = null;
|
|
106
|
+
let inConsequent = true;
|
|
107
|
+
if (parent.type === "SwitchCase") {
|
|
108
|
+
tests = parent.test === null ? [] : literalsOf(parent.test, source);
|
|
109
|
+
}
|
|
110
|
+
else if ((parent.type === "ConditionalExpression" || parent.type === "IfStatement") && parent.test !== node) {
|
|
111
|
+
tests = literalsOf(parent.test, source);
|
|
112
|
+
inConsequent = parent.consequent === node;
|
|
113
|
+
}
|
|
114
|
+
else if (parent.type === "LogicalExpression" && parent.right === node) {
|
|
115
|
+
tests = literalsOf(parent.left, source);
|
|
116
|
+
}
|
|
117
|
+
if (tests === null || tests.length === 0) {
|
|
118
|
+
if (parent.type === "SwitchCase" && parent.test === null)
|
|
119
|
+
return false;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
constrained = true;
|
|
123
|
+
if (!inConsequent || header === null || !tests.includes(header))
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
return !constrained || header !== null;
|
|
127
|
+
};
|
|
128
|
+
return {
|
|
129
|
+
Program(program) {
|
|
130
|
+
bound = grammarBindings(program);
|
|
131
|
+
},
|
|
132
|
+
ObjectExpression(object) {
|
|
133
|
+
if (!isListRow(object, bound))
|
|
134
|
+
return;
|
|
135
|
+
const title = propertyOf(object, "title");
|
|
136
|
+
if (title === null)
|
|
137
|
+
return;
|
|
138
|
+
const key = PRESSABLE_KEYS.find((name) => object.properties.some((property) => property.type === "Property" && keyName(property) === name));
|
|
139
|
+
if (key === undefined)
|
|
140
|
+
return;
|
|
141
|
+
for (const link of linksIn(title.value))
|
|
142
|
+
context.report({ node: link, messageId: "row", data: { key } });
|
|
143
|
+
},
|
|
144
|
+
JSXElement(element) {
|
|
145
|
+
const opening = element.openingElement;
|
|
146
|
+
if (elementOf(opening, bound).grammar !== "DataTable")
|
|
147
|
+
return;
|
|
148
|
+
const why = opensRows(opening);
|
|
149
|
+
if (why === null)
|
|
150
|
+
return;
|
|
151
|
+
const render = attributeOf(opening, "renderCell")?.value;
|
|
152
|
+
if (render?.type !== "JSXExpressionContainer")
|
|
153
|
+
return;
|
|
154
|
+
const fn = functionOf(render.expression);
|
|
155
|
+
if (fn === null)
|
|
156
|
+
return;
|
|
157
|
+
const header = headerOf(opening);
|
|
158
|
+
for (const link of linksIn(fn)) {
|
|
159
|
+
if (inHeaderBranch(link, fn, header))
|
|
160
|
+
context.report({ node: link, messageId: "cell", data: { why } });
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
/** The rules this law contributes to the plugin. */
|
|
167
|
+
export const rules = {
|
|
168
|
+
"row-title-link": rowTitleLink,
|
|
169
|
+
};
|
|
170
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#ROW-TITLE-LINK");
|
|
171
|
+
/** Every rule of this law is an error. */
|
|
172
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
export declare const sameLineSize: Rule.RuleModule;
|
|
3
|
+
/** The rules this law contributes to the plugin. */
|
|
4
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
5
|
+
/** Every rule of this law is an error. */
|
|
6
|
+
export declare const recommended: Record<string, "error">;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
3
|
+
import { attributeOf, elementOf, grammarBindings, literalAttribute } from "./lib/row-context.js";
|
|
4
|
+
/**
|
|
5
|
+
* The same line size law (`FE_SAME_LINE_SIZE`): one line, one size. The `Text` children that stand side by side on one line,
|
|
6
|
+
* the direct children of one `Cluster` (and of a `Text` that holds `Text` runs, whose own size starts the line), share one `size`; an absent `size` is `md`.
|
|
7
|
+
*
|
|
8
|
+
* WHAT IT READS. The direct `Text` children of a `Cluster` or a `Text`, through fragments, `cond && <Text />` and ternaries. A
|
|
9
|
+
* `Text` whose `size` is not a literal (a variable, a spread) is skipped. A `Badge`, `StatusDot`, `Chip` or `TagGroup` is not a
|
|
10
|
+
* `Text`, so it is exempt. The first `Text` sets the line's size; each later `Text` of another size is a finding.
|
|
11
|
+
*/
|
|
12
|
+
/** The line containers: a Cluster lays its children on one line, a Text holds runs. */
|
|
13
|
+
const LINES = new Set(["Cluster", "Text"]);
|
|
14
|
+
/** The size a Text states: its literal `size`, `md` when it has none, null when it cannot be read. */
|
|
15
|
+
const sizeOf = (opening) => {
|
|
16
|
+
if (opening.attributes.some((entry) => entry.type === "JSXSpreadAttribute"))
|
|
17
|
+
return null;
|
|
18
|
+
if (attributeOf(opening, "size") === null)
|
|
19
|
+
return "md";
|
|
20
|
+
return literalAttribute(opening, "size");
|
|
21
|
+
};
|
|
22
|
+
export const sameLineSize = defineRule({
|
|
23
|
+
meta: {
|
|
24
|
+
type: "problem",
|
|
25
|
+
docs: { description: "FE_SAME_LINE_SIZE: the Text children of one Cluster line share one size." },
|
|
26
|
+
schema: [],
|
|
27
|
+
messages: {
|
|
28
|
+
mixed: "This Text is `{{size}}` and its line's first Text is `{{first}}`. One line, one size: pick the size of the line's role (md for a primary, sm for a secondary or a meta line) and give every Text of the line that size. A Badge or a chip may stay as it is.",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
create(context) {
|
|
32
|
+
if (!isProductSource(context))
|
|
33
|
+
return {};
|
|
34
|
+
let bound = new Map();
|
|
35
|
+
/** The Text elements that are direct children of a line, unwrapped through fragments, logical and conditional expressions. */
|
|
36
|
+
const textsOf = (children) => {
|
|
37
|
+
const found = [];
|
|
38
|
+
const visit = (node) => {
|
|
39
|
+
if (node === null)
|
|
40
|
+
return;
|
|
41
|
+
switch (node.type) {
|
|
42
|
+
case "JSXElement":
|
|
43
|
+
if (elementOf(node.openingElement, bound).grammar === "Text")
|
|
44
|
+
found.push(node);
|
|
45
|
+
return;
|
|
46
|
+
case "JSXFragment":
|
|
47
|
+
node.children.forEach(visit);
|
|
48
|
+
return;
|
|
49
|
+
case "JSXExpressionContainer":
|
|
50
|
+
visit(node.expression);
|
|
51
|
+
return;
|
|
52
|
+
case "LogicalExpression":
|
|
53
|
+
visit(node.right);
|
|
54
|
+
return;
|
|
55
|
+
case "ConditionalExpression":
|
|
56
|
+
visit(node.consequent);
|
|
57
|
+
visit(node.alternate);
|
|
58
|
+
return;
|
|
59
|
+
default:
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
children.forEach(visit);
|
|
63
|
+
return found;
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
Program(program) {
|
|
67
|
+
bound = grammarBindings(program);
|
|
68
|
+
},
|
|
69
|
+
JSXElement(element) {
|
|
70
|
+
const { grammar } = elementOf(element.openingElement, bound);
|
|
71
|
+
if (grammar === null || !LINES.has(grammar))
|
|
72
|
+
return;
|
|
73
|
+
let first = grammar === "Text" ? sizeOf(element.openingElement) : null;
|
|
74
|
+
for (const text of textsOf(element.children)) {
|
|
75
|
+
const size = sizeOf(text.openingElement);
|
|
76
|
+
if (size === null)
|
|
77
|
+
continue;
|
|
78
|
+
if (first === null)
|
|
79
|
+
first = size;
|
|
80
|
+
else if (size !== first)
|
|
81
|
+
context.report({ node: text.openingElement, messageId: "mixed", data: { size, first } });
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
/** The rules this law contributes to the plugin. */
|
|
88
|
+
export const rules = {
|
|
89
|
+
"same-line-size": sameLineSize,
|
|
90
|
+
};
|
|
91
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#SAME-LINE-SIZE");
|
|
92
|
+
/** Every rule of this law is an error. */
|
|
93
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** A table, list or chart standing alone is the surface, never wrapped in a card. */
|
|
3
|
+
export declare const singleChildCard: Rule.RuleModule;
|
|
4
|
+
/** The rules this law contributes to the plugin. */
|
|
5
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
6
|
+
/** Every rule of this law is an error. */
|
|
7
|
+
export declare const recommended: Record<string, "error">;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { attributeOf, elementNameOf, grammarName, hasSpread, renderedChildren, stringProp } from "./lib/grammar-jsx.js";
|
|
3
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
4
|
+
/**
|
|
5
|
+
* The lone surface law (owner, 2026-11-09, SINGLE-CHILD-CARD): a card groups two or more different parts (figures + list +
|
|
6
|
+
* actions) or carries zones (tabs, search, head actions). A table, a list or a chart standing alone IS the surface: the
|
|
7
|
+
* title sits above it as a `Section` head and one line sits under it as `Section caption` (muted, sm). The Grammar draws a
|
|
8
|
+
* standalone `DataTable` (`surface="card"`) and every `Section kind="list"` as ONE frame (border, card radius), so a
|
|
9
|
+
* `SurfaceCard` around either is a second frame around the first.
|
|
10
|
+
*
|
|
11
|
+
* WHAT IT READS. A Grammar `SurfaceCard` (imported from `@starci/grammar`) with no zones: no `headers` or `footers` prop (an
|
|
12
|
+
* empty array literal counts as none) and no `labelEnd` (head actions). Its body is read through fragments, `cond && <x/>`,
|
|
13
|
+
* `cond ? <x/> : <y/>` and `.map`, ignoring blank text. It is refused when that body is ONE element and that element is:
|
|
14
|
+
* - a table: `DataTable`;
|
|
15
|
+
* - a list: `ListBox`, `ItemList`, `DisclosureList`, `Timeline`, or `Section kind="list"`;
|
|
16
|
+
* - a chart: `BarChart`, `ProportionBar`, `BubbleMap`, `FlowGraph`, `MermaidDiagram`.
|
|
17
|
+
* It is also refused when the card is the data-driven form of the same thing: `SurfaceCard kind="table"` or `kind="list"`.
|
|
18
|
+
*
|
|
19
|
+
* DECIDED. `Meter`, `Progress` and `ProgressCircle` are single values, not charts. `DescriptionList` (term/value facts of one
|
|
20
|
+
* record) and `Accordion` are not read as lists here: a record's facts are the `kind="details"` card. `SurfaceCard
|
|
21
|
+
* kind="figures"`, `details`, `accordion` and `tile` are not judged. `SurfaceCard` rules for kinds, depth and nesting stay with
|
|
22
|
+
* `surface-card-kind`, `no-raised-surface-in-surface` and `surface-depth`; none is read again here.
|
|
23
|
+
*
|
|
24
|
+
* WHAT IT LEAVES ALONE. A card with two or more body children, a card with zones or `labelEnd`, a card with a JSX spread (a
|
|
25
|
+
* prop may arrive that the source does not show), a non-Grammar `SurfaceCard`, and non-product source.
|
|
26
|
+
*
|
|
27
|
+
* WHERE IT APPLIES. Product source (`isProductSource`).
|
|
28
|
+
*/
|
|
29
|
+
/** The table, list and chart components that are a surface on their own, with the word the message names them by. */
|
|
30
|
+
const LONE = new Map([
|
|
31
|
+
["DataTable", "table"],
|
|
32
|
+
["ListBox", "list"],
|
|
33
|
+
["ItemList", "list"],
|
|
34
|
+
["DisclosureList", "list"],
|
|
35
|
+
["Timeline", "list"],
|
|
36
|
+
["BarChart", "chart"],
|
|
37
|
+
["ProportionBar", "chart"],
|
|
38
|
+
["BubbleMap", "chart"],
|
|
39
|
+
["FlowGraph", "chart"],
|
|
40
|
+
["MermaidDiagram", "chart"],
|
|
41
|
+
]);
|
|
42
|
+
/** True when an attribute is an empty array literal (`headers={[]}`), which zones nothing. */
|
|
43
|
+
const isEmptyArray = (attribute) => {
|
|
44
|
+
const value = attribute.value;
|
|
45
|
+
return value?.type === "JSXExpressionContainer" && value.expression.type === "ArrayExpression" && value.expression.elements.length === 0;
|
|
46
|
+
};
|
|
47
|
+
/** True when the card carries a zone or head actions: a `headers` or `footers` prop that is not an empty array, or a `labelEnd`. */
|
|
48
|
+
const hasZones = (opening) => {
|
|
49
|
+
const zoned = ["headers", "footers"].some((name) => {
|
|
50
|
+
const attribute = attributeOf(opening, name);
|
|
51
|
+
return attribute !== undefined && !isEmptyArray(attribute);
|
|
52
|
+
});
|
|
53
|
+
return zoned || attributeOf(opening, "labelEnd") !== undefined;
|
|
54
|
+
};
|
|
55
|
+
/** What the lone child is, as the message names it (`DataTable`, `Section kind="list"`), or null when it is not a surface on its own. */
|
|
56
|
+
const loneChild = (context, card) => {
|
|
57
|
+
const body = renderedChildren(card.children);
|
|
58
|
+
if (body.length !== 1)
|
|
59
|
+
return null;
|
|
60
|
+
const child = body[0];
|
|
61
|
+
if (child?.type !== "JSXElement")
|
|
62
|
+
return null;
|
|
63
|
+
const name = elementNameOf(child);
|
|
64
|
+
if (name === null || grammarName(context, child.openingElement) === null)
|
|
65
|
+
return null;
|
|
66
|
+
if (name === "Section")
|
|
67
|
+
return stringProp(child.openingElement, "kind") === "list" ? "Section kind=\"list\"" : null;
|
|
68
|
+
return LONE.has(name) ? name : null;
|
|
69
|
+
};
|
|
70
|
+
/** A table, list or chart standing alone is the surface, never wrapped in a card. */
|
|
71
|
+
export const singleChildCard = defineRule({
|
|
72
|
+
meta: {
|
|
73
|
+
type: "problem",
|
|
74
|
+
docs: { description: "SINGLE-CHILD-CARD: a SurfaceCard with no zones whose only content is a table, list or chart is a second frame; the title is a Section head." },
|
|
75
|
+
schema: [],
|
|
76
|
+
messages: {
|
|
77
|
+
lone: "A {{child}} standing alone is the surface: drop the SurfaceCard, put its title in a `Section` head (`title`) and one line under it in `Section caption`. A card groups two or more different parts, or carries zones (tabs, search, head actions).",
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
create(context) {
|
|
81
|
+
if (!isProductSource(context))
|
|
82
|
+
return {};
|
|
83
|
+
return {
|
|
84
|
+
JSXElement(element) {
|
|
85
|
+
const opening = element.openingElement;
|
|
86
|
+
if (grammarName(context, opening) !== "SurfaceCard" || hasSpread(opening) || hasZones(opening))
|
|
87
|
+
return;
|
|
88
|
+
const kind = stringProp(opening, "kind");
|
|
89
|
+
const child = kind === "table" || kind === "list" ? kind : loneChild(context, element);
|
|
90
|
+
if (child === null)
|
|
91
|
+
return;
|
|
92
|
+
context.report({ node: opening, messageId: "lone", data: { child: kind === child ? child : `\`${child}\`` } });
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
/** The rules this law contributes to the plugin. */
|
|
98
|
+
export const rules = {
|
|
99
|
+
"single-child-card": singleChildCard,
|
|
100
|
+
};
|
|
101
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#SINGLE-CHILD-CARD");
|
|
102
|
+
/** Every rule of this law is an error. */
|
|
103
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
export declare const suggestionChips: Rule.RuleModule;
|
|
3
|
+
/** The rules this law contributes to the plugin. */
|
|
4
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
5
|
+
/** Every rule of this law is an error. */
|
|
6
|
+
export declare const recommended: Record<string, "error">;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
3
|
+
import { renderedChildren } from "./lib/grammar-jsx.js";
|
|
4
|
+
import { attributeOf, elementOf, grammarBindings, literalAttribute } from "./lib/row-context.js";
|
|
5
|
+
/**
|
|
6
|
+
* The suggestion chips law (`FE_SUGGESTION_CHIPS`): a row of small neutral Buttons that each send their own label is a set of
|
|
7
|
+
* quick replies or suggested questions, and that is `SuggestionChips` (pill chips in a labelled toolbar), not a Button row.
|
|
8
|
+
*
|
|
9
|
+
* WHAT IT READS. The rendered children of a Grammar `Cluster` or `Stack`, or a `div` (looked through fragments, `cond && x`,
|
|
10
|
+
* ternaries and `.map`). A child is a suggestion Button when it is a Grammar `Button` that:
|
|
11
|
+
* - states `size="sm"` literally;
|
|
12
|
+
* - has no `intent`, or a literal `intent` of `neutral` or `alternative`;
|
|
13
|
+
* - has an `onPress` arrow function whose one call passes the same expression as the Button's single child
|
|
14
|
+
* (`{q}` and `send(q)`, `{item.label}` and `ask(item.label)`), or the same string literal as the literal child text.
|
|
15
|
+
* The parent is a finding when two or more such Buttons are its siblings, or one of them is drawn by a `.map`.
|
|
16
|
+
* Anything not readable this cheaply (a spread, a computed size or intent, a handler that is not an inline arrow, a label with
|
|
17
|
+
* several children) is not flagged.
|
|
18
|
+
*/
|
|
19
|
+
const PARENTS = new Set(["Cluster", "Stack"]);
|
|
20
|
+
const NEUTRAL = new Set(["neutral", "alternative"]);
|
|
21
|
+
/** The single expression or text a Button states as its label: `{q}` as the node `q`, text as its string. */
|
|
22
|
+
const labelOf = (element) => {
|
|
23
|
+
const children = element.children.filter((child) => !(child.type === "JSXText" && child.value.trim() === ""));
|
|
24
|
+
if (children.length !== 1)
|
|
25
|
+
return null;
|
|
26
|
+
const child = children[0];
|
|
27
|
+
if (child.type === "JSXText")
|
|
28
|
+
return { text: child.value.trim() };
|
|
29
|
+
if (child.type !== "JSXExpressionContainer" || child.expression.type === "JSXEmptyExpression")
|
|
30
|
+
return null;
|
|
31
|
+
if (child.expression.type === "Literal" && typeof child.expression.value === "string")
|
|
32
|
+
return { text: child.expression.value };
|
|
33
|
+
return { node: child.expression };
|
|
34
|
+
};
|
|
35
|
+
/** The argument expressions of the one call an inline `onPress` arrow makes, or null. */
|
|
36
|
+
const callArguments = (handler) => {
|
|
37
|
+
let body = handler.body;
|
|
38
|
+
if (body.type === "BlockStatement") {
|
|
39
|
+
const only = body.body.length === 1 ? body.body[0] : undefined;
|
|
40
|
+
body = only?.type === "ExpressionStatement" ? only.expression : null;
|
|
41
|
+
}
|
|
42
|
+
return body !== null && body.type === "CallExpression" ? body.arguments : null;
|
|
43
|
+
};
|
|
44
|
+
/** True when the handler of a Button passes its own label. */
|
|
45
|
+
const sendsOwnLabel = (element, source) => {
|
|
46
|
+
const label = labelOf(element);
|
|
47
|
+
const press = attributeOf(element.openingElement, "onPress")?.value;
|
|
48
|
+
if (label === null || press == null || press.type !== "JSXExpressionContainer" || press.expression.type !== "ArrowFunctionExpression")
|
|
49
|
+
return false;
|
|
50
|
+
const args = callArguments(press.expression);
|
|
51
|
+
if (args === null)
|
|
52
|
+
return false;
|
|
53
|
+
return args.some((arg) => {
|
|
54
|
+
if ("text" in label)
|
|
55
|
+
return arg.type === "Literal" && arg.value === label.text;
|
|
56
|
+
return arg.type !== "Literal" && source.getText(arg) === source.getText(label.node);
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
/** True when an element sits inside a `.map` callback below its parent. */
|
|
60
|
+
const inMap = (element, parent) => {
|
|
61
|
+
for (let node = element.parent; node !== undefined && node !== parent; node = node.parent) {
|
|
62
|
+
if (node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression")
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
};
|
|
67
|
+
export const suggestionChips = defineRule({
|
|
68
|
+
meta: {
|
|
69
|
+
type: "problem",
|
|
70
|
+
docs: { description: "FE_SUGGESTION_CHIPS: a row of neutral sm Buttons that send their own label is SuggestionChips." },
|
|
71
|
+
schema: [],
|
|
72
|
+
messages: {
|
|
73
|
+
chips: "This is a suggestion, drawn as a Button in a row. Use `SuggestionChips` (pill chips, a labelled toolbar) for quick replies and suggested questions: `<SuggestionChips label=\"...\" items={[{ id, label }]} onSelect={(id) => ...} />`.",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
create(context) {
|
|
77
|
+
if (!isProductSource(context))
|
|
78
|
+
return {};
|
|
79
|
+
let bound = new Map();
|
|
80
|
+
const source = context.sourceCode;
|
|
81
|
+
/** A Grammar Button that is small, neutral and sends its own label. */
|
|
82
|
+
const isSuggestion = (node) => {
|
|
83
|
+
if (node.type !== "JSXElement")
|
|
84
|
+
return false;
|
|
85
|
+
const opening = node.openingElement;
|
|
86
|
+
if (elementOf(opening, bound).grammar !== "Button")
|
|
87
|
+
return false;
|
|
88
|
+
if (opening.attributes.some((entry) => entry.type === "JSXSpreadAttribute"))
|
|
89
|
+
return false;
|
|
90
|
+
if (literalAttribute(opening, "size") !== "sm")
|
|
91
|
+
return false;
|
|
92
|
+
if (attributeOf(opening, "intent") !== null) {
|
|
93
|
+
const intent = literalAttribute(opening, "intent");
|
|
94
|
+
if (intent === null || !NEUTRAL.has(intent))
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
return sendsOwnLabel(node, source);
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
Program(program) {
|
|
101
|
+
bound = grammarBindings(program);
|
|
102
|
+
},
|
|
103
|
+
JSXElement(element) {
|
|
104
|
+
const { grammar, tag } = elementOf(element.openingElement, bound);
|
|
105
|
+
if (!(grammar !== null && PARENTS.has(grammar)) && tag !== "div")
|
|
106
|
+
return;
|
|
107
|
+
const found = renderedChildren(element.children).filter(isSuggestion);
|
|
108
|
+
if (found.length < 2 && !found.some((entry) => inMap(entry, element)))
|
|
109
|
+
return;
|
|
110
|
+
for (const entry of found)
|
|
111
|
+
context.report({ node: entry.openingElement, messageId: "chips" });
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
/** The rules this law contributes to the plugin. */
|
|
117
|
+
export const rules = {
|
|
118
|
+
"suggestion-chips": suggestionChips,
|
|
119
|
+
};
|
|
120
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#SUGGESTION-CHIPS");
|
|
121
|
+
/** Every rule of this law is an error. */
|
|
122
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
package/dist/surface-card.js
CHANGED
|
@@ -161,7 +161,7 @@ export const surfaceCardKind = defineRule({
|
|
|
161
161
|
docs: { description: "R355: a SurfaceCard of the body kind holds no hand list, no Divider and no SectionHeader or Heading." },
|
|
162
162
|
schema: [],
|
|
163
163
|
messages: {
|
|
164
|
-
list: "`<{{element}}>` is a hand-built list in a `body` card. Rows are `<
|
|
164
|
+
list: "`<{{element}}>` is a hand-built list in a `body` card. Rows are `<Section kind=\"list\" items={...} empty={...} />` (with zones: `<SurfaceCard kind=\"list\" headers={...}>`), which draws every row the same way.",
|
|
165
165
|
divider: "A `Divider` in a card is refused: the card draws its own seams. Bands divided by a line are the typed `headers` and `footers` zones: `<SurfaceCard headers={[{ kind: \"figures\", items }]} />`.",
|
|
166
166
|
zone: "`<{{element}}>` stands in a `custom` zone of a card. Filter and search are the typed zones (`{ kind: \"filter\" | \"search\" | \"toolbar\" }`) and the card draws its own seams, so a `custom` zone holds no `Tabs`, `SearchField` or `Divider`.",
|
|
167
167
|
title: "`<{{element}}>` stands as a second title inside a card. The card has a `label` (and `fact`, `labelEnd`, `note`); a sub-section of one subject is a typed header or footer zone.",
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** Rows divided by lines inside a surface are the Grammar's list, never a hand-made one. */
|
|
3
|
+
export declare const surfaceInSurface: Rule.RuleModule;
|
|
4
|
+
/** The rules this law contributes to the plugin. */
|
|
5
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
6
|
+
/** Every rule of this law is an error. */
|
|
7
|
+
export declare const recommended: Record<string, "error">;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { elementNameOf, grammarName, nameOf, renderedChildren, stringProp } from "./lib/grammar-jsx.js";
|
|
3
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
4
|
+
/**
|
|
5
|
+
* The row list law (owner, 2026-11-09, SURFACE-IN-SURFACE): rows divided by lines that stand inside a surface are a
|
|
6
|
+
* nested surface (a 1px separator border, the card radius, clipped). The Grammar draws that itself for a
|
|
7
|
+
* `Section kind="list"` placed in a card body, a drawer, a dialog, a pane band or a rail. The bare form an app can still
|
|
8
|
+
* reach is a hand-made row list, which has no edge, no radius and no clip.
|
|
9
|
+
*
|
|
10
|
+
* WHAT IT READS. A `Stack` (Grammar), `ul`, `ol` or `div` whose rendered children (looked through fragments,
|
|
11
|
+
* `cond && <x/>` and `.map`) include a Grammar `Divider` between other children, inside a `SurfaceCard`, `Drawer`,
|
|
12
|
+
* `Dialog`, `PaneSection`, or the `rail` of a `Grid kind="main-rail"`. Only the list element is reported.
|
|
13
|
+
*
|
|
14
|
+
* WHAT IT LEAVES ALONE. A card inside a card is `no-raised-surface-in-surface` (R340) and a `Divider` directly in a
|
|
15
|
+
* `body` SurfaceCard is `surface-card-kind` (R355); neither is read again here. A list outside a surface, a
|
|
16
|
+
* Divider that separates nothing (the only child), and a list in an overlay `trigger` are not judged.
|
|
17
|
+
*
|
|
18
|
+
* WHERE IT APPLIES. Product source (`isProductSource`).
|
|
19
|
+
*/
|
|
20
|
+
/** The surfaces a hand-made row list does not stand in bare. */
|
|
21
|
+
const HOSTS = new Set(["SurfaceCard", "Drawer", "Dialog", "PaneSection"]);
|
|
22
|
+
/** The plain elements a hand-made list is written with. */
|
|
23
|
+
const PLAIN_LISTS = new Set(["ul", "ol", "div"]);
|
|
24
|
+
/** True when the element is a row list candidate: a Grammar `Stack`, or a plain `ul`, `ol` or `div`. */
|
|
25
|
+
const isListElement = (context, element) => {
|
|
26
|
+
const opening = element.openingElement;
|
|
27
|
+
if (opening.name.type === "JSXIdentifier" && PLAIN_LISTS.has(opening.name.name))
|
|
28
|
+
return true;
|
|
29
|
+
return grammarName(context, opening) === "Stack";
|
|
30
|
+
};
|
|
31
|
+
/** True when the list's children are divided by a Grammar `Divider`. */
|
|
32
|
+
const dividedByDividers = (context, element) => {
|
|
33
|
+
const children = renderedChildren(element.children);
|
|
34
|
+
const dividers = children.filter((child) => child.type === "JSXElement" && elementNameOf(child) === "Divider" && grammarName(context, child.openingElement) === "Divider");
|
|
35
|
+
return dividers.length > 0 && children.length > dividers.length;
|
|
36
|
+
};
|
|
37
|
+
/** The surface that encloses a node in the same file, or null. Attributes are not content except a main-rail grid's `rail`. */
|
|
38
|
+
const enclosingSurface = (context, node) => {
|
|
39
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
40
|
+
if (current.type === "JSXAttribute") {
|
|
41
|
+
const opening = current.parent;
|
|
42
|
+
const isRail = current.name.type === "JSXIdentifier" && current.name.name === "rail" && grammarName(context, opening) === "Grid" && stringProp(opening, "kind") === "main-rail";
|
|
43
|
+
return isRail ? "the rail of a `Grid kind=\"main-rail\"`" : null;
|
|
44
|
+
}
|
|
45
|
+
if (current.type !== "JSXElement")
|
|
46
|
+
continue;
|
|
47
|
+
const name = grammarName(context, current.openingElement);
|
|
48
|
+
if (name !== null && HOSTS.has(name))
|
|
49
|
+
return `a \`${name}\``;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
};
|
|
53
|
+
/** Rows divided by lines inside a surface are the Grammar's list, never a hand-made one. */
|
|
54
|
+
export const surfaceInSurface = defineRule({
|
|
55
|
+
meta: {
|
|
56
|
+
type: "problem",
|
|
57
|
+
docs: { description: "SURFACE-IN-SURFACE: a row list divided by Dividers inside a card, drawer, dialog, pane band or rail is a Section kind=list or a SurfaceCard kind=list." },
|
|
58
|
+
schema: [],
|
|
59
|
+
messages: {
|
|
60
|
+
handMade: "This `{{element}}` is a hand-made row list inside {{host}}: its rows are divided by `Divider` elements, so it has no edge, no card radius and no clip. Use `<Section kind=\"list\">` for rows inside a surface (the Grammar draws them as a nested surface), or a `Section kind=\"list\"` with its own frame when the list stands alone (a card around a lone list is refused by `single-child-card`).",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
create(context) {
|
|
64
|
+
if (!isProductSource(context))
|
|
65
|
+
return {};
|
|
66
|
+
return {
|
|
67
|
+
JSXElement(element) {
|
|
68
|
+
if (!isListElement(context, element) || !dividedByDividers(context, element))
|
|
69
|
+
return;
|
|
70
|
+
const host = enclosingSurface(context, element);
|
|
71
|
+
if (host === null)
|
|
72
|
+
return;
|
|
73
|
+
context.report({ node: element.openingElement, messageId: "handMade", data: { element: nameOf(element.openingElement.name) ?? "list", host } });
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
/** The rules this law contributes to the plugin. */
|
|
79
|
+
export const rules = {
|
|
80
|
+
"surface-in-surface": surfaceInSurface,
|
|
81
|
+
};
|
|
82
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#SURFACE-IN-SURFACE");
|
|
83
|
+
/** Every rule of this law is an error. */
|
|
84
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
export declare const tableCellType: Rule.RuleModule;
|
|
3
|
+
/** The rules this law contributes to the plugin. */
|
|
4
|
+
export declare const rules: Record<string, Rule.RuleModule>;
|
|
5
|
+
/** Every rule of this law is an error. */
|
|
6
|
+
export declare const recommended: Record<string, "error">;
|