@starci/eslint-grammar-core 0.27.1 → 0.28.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 +19 -0
- package/README.md +2 -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 +18 -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 +50 -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/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/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 +102 -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
|
+
/** 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 `<SurfaceCard kind=\"list\">` when the list is the 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">;
|
|
@@ -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 { attributeOf, descendants, elementOf, grammarBindings, isListRow, keyName, literalAttribute, propertyOf } from "./lib/row-context.js";
|
|
4
|
+
/**
|
|
5
|
+
* The table and row type law (`FE_TABLE_CELL_TYPE`): the cells of a DataTable and the copy of a ListRow take the row type sizes,
|
|
6
|
+
* the primary at md 14 / 500 (`Text isStrong`) and the secondary at sm 13 muted (`Text size="sm" tone="muted"`). A heading-size
|
|
7
|
+
* Text, a `Heading`, a `Label` or an `h1`-`h6` in a cell or a row is a heading drawn in a list.
|
|
8
|
+
*
|
|
9
|
+
* WHAT IT READS.
|
|
10
|
+
* - the function a `DataTable` takes as `renderCell` (inline, or a function or arrow declared by name in the file), and the
|
|
11
|
+
* `editor` function of a column object in its `columns` literal;
|
|
12
|
+
* - the `title` and `description` of a ListRow data object: a row of the `items` of a list collection (`{ kind: "list", items }`,
|
|
13
|
+
* or `Section` / `SurfaceCard` written `kind="list"`), through `.map` callbacks, or the `row` of a `<ListRow>`.
|
|
14
|
+
* In those, `Text size="figure"`, any `Heading`, any `Label` and the tags `h1`-`h6` are findings. A row list declared elsewhere
|
|
15
|
+
* and passed by name is not followed.
|
|
16
|
+
*/
|
|
17
|
+
const HEADING_TAGS = new Set(["h1", "h2", "h3", "h4", "h5", "h6"]);
|
|
18
|
+
const isFunction = (node) => node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration";
|
|
19
|
+
export const tableCellType = defineRule({
|
|
20
|
+
meta: {
|
|
21
|
+
type: "problem",
|
|
22
|
+
docs: { description: "FE_TABLE_CELL_TYPE: no heading-size Text, Heading, Label or h1-h6 in a DataTable cell or a ListRow." },
|
|
23
|
+
schema: [],
|
|
24
|
+
messages: {
|
|
25
|
+
heading: "{{what}} in {{where}} is a heading drawn in a list. The primary of a row or a cell is md 14 / 500 (`<Text isStrong>`), the secondary sm 13 muted (`<Text size=\"sm\" tone=\"muted\">`).",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
create(context) {
|
|
29
|
+
if (!isProductSource(context))
|
|
30
|
+
return {};
|
|
31
|
+
let bound = new Map();
|
|
32
|
+
const source = context.sourceCode;
|
|
33
|
+
const reported = new Set();
|
|
34
|
+
/** Report every heading-size element under a node. */
|
|
35
|
+
const scan = (root, where) => {
|
|
36
|
+
for (const node of descendants(root, source)) {
|
|
37
|
+
if (node.type !== "JSXElement" || reported.has(node))
|
|
38
|
+
continue;
|
|
39
|
+
const { grammar, tag } = elementOf(node.openingElement, bound);
|
|
40
|
+
let what = null;
|
|
41
|
+
if (grammar === "Text" && literalAttribute(node.openingElement, "size") === "figure")
|
|
42
|
+
what = "`Text size=\"figure\"`";
|
|
43
|
+
else if (grammar === "Heading" || grammar === "Label")
|
|
44
|
+
what = `\`${grammar}\``;
|
|
45
|
+
else if (tag !== null && HEADING_TAGS.has(tag))
|
|
46
|
+
what = `\`<${tag}>\``;
|
|
47
|
+
if (what === null)
|
|
48
|
+
continue;
|
|
49
|
+
reported.add(node);
|
|
50
|
+
context.report({ node, messageId: "heading", data: { what, where } });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
/** The function a renderCell expression names: itself, or a function declared by that name in this file. */
|
|
54
|
+
const functionOf = (expression) => {
|
|
55
|
+
if (isFunction(expression))
|
|
56
|
+
return expression;
|
|
57
|
+
if (expression.type !== "Identifier")
|
|
58
|
+
return null;
|
|
59
|
+
let scope = source.getScope(expression);
|
|
60
|
+
while (scope) {
|
|
61
|
+
const variable = scope.set.get(expression.name);
|
|
62
|
+
if (variable) {
|
|
63
|
+
for (const def of variable.defs) {
|
|
64
|
+
if (def.type === "FunctionName")
|
|
65
|
+
return def.node;
|
|
66
|
+
{
|
|
67
|
+
const init = def.node.init;
|
|
68
|
+
if (init && isFunction(init))
|
|
69
|
+
return init;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
scope = scope.upper;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
Program(program) {
|
|
80
|
+
bound = grammarBindings(program);
|
|
81
|
+
},
|
|
82
|
+
JSXElement(element) {
|
|
83
|
+
if (elementOf(element.openingElement, bound).grammar !== "DataTable")
|
|
84
|
+
return;
|
|
85
|
+
const render = attributeOf(element.openingElement, "renderCell")?.value;
|
|
86
|
+
if (render?.type === "JSXExpressionContainer") {
|
|
87
|
+
const fn = functionOf(render.expression);
|
|
88
|
+
if (fn)
|
|
89
|
+
scan(fn, "a DataTable cell");
|
|
90
|
+
}
|
|
91
|
+
const columns = attributeOf(element.openingElement, "columns")?.value;
|
|
92
|
+
if (columns?.type !== "JSXExpressionContainer" || columns.expression.type !== "ArrayExpression")
|
|
93
|
+
return;
|
|
94
|
+
for (const column of columns.expression.elements) {
|
|
95
|
+
if (column?.type !== "ObjectExpression")
|
|
96
|
+
continue;
|
|
97
|
+
const editor = propertyOf(column, "editor")?.value;
|
|
98
|
+
if (editor && isFunction(editor))
|
|
99
|
+
scan(editor, "a DataTable cell");
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
ObjectExpression(object) {
|
|
103
|
+
if (!isListRow(object, bound))
|
|
104
|
+
return;
|
|
105
|
+
for (const property of object.properties) {
|
|
106
|
+
if (property.type !== "Property")
|
|
107
|
+
continue;
|
|
108
|
+
const name = keyName(property);
|
|
109
|
+
if (name === "title" || name === "description")
|
|
110
|
+
scan(property.value, "a ListRow");
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
/** The rules this law contributes to the plugin. */
|
|
117
|
+
export const rules = {
|
|
118
|
+
"table-cell-type": tableCellType,
|
|
119
|
+
};
|
|
120
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#TABLE-CELL-TYPE");
|
|
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/tabs.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** No pill in JSX: the pill is the `switch` header zone of a card. */
|
|
3
|
+
export declare const tabsSwitchZone: Rule.RuleModule;
|
|
4
|
+
/** At most two underline layers. */
|
|
5
|
+
export declare const tabsDepth: 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">;
|
package/dist/tabs.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { isProductSource } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
3
|
+
/**
|
|
4
|
+
* The Tabs laws (`contexts/ui/components/tabs.md`, owner-approved 2026-11-09): where the pill stands, and how many
|
|
5
|
+
* underline layers a page stacks.
|
|
6
|
+
*
|
|
7
|
+
* `tabs-switch-zone`. The pill (`Tabs appearance="primary"`) is a mode or period switch of one block. The Grammar draws it
|
|
8
|
+
* only as the `switch` header zone of a `SurfaceCard` (`headers={[{ kind: "switch", label, items, selectedKey, onSelect }]}`);
|
|
9
|
+
* a `<Tabs appearance="primary">` written in JSX (a page body, under the page tabs, a pane, a drawer) is a finding. The
|
|
10
|
+
* item checks (two to four, no count, no href) stay in `no-primary-tabs-misuse`.
|
|
11
|
+
*
|
|
12
|
+
* `tabs-depth`. A page stacks at most two underline layers: the page tabs (`Page header.tabs`) and one region set. A `Tabs`
|
|
13
|
+
* is nested in another when it stands in the panel the other selects: inside a conditional or `&&` whose test reads the
|
|
14
|
+
* other's `selectedKey`, or, for page tabs, in the children of a `Page` whose `header` carries `tabs`. A drawer, dialog,
|
|
15
|
+
* modal, popover or sheet starts its own surface and ends the climb. Every tab is one size, so a layer's size is not judged; only the count is.
|
|
16
|
+
*/
|
|
17
|
+
/** The element name of a JSX opening element (`Tabs`, or `Grammar.Tabs` as `Tabs`). */
|
|
18
|
+
const elementName = (opening) => {
|
|
19
|
+
const name = opening.name;
|
|
20
|
+
if (name.type === "JSXIdentifier")
|
|
21
|
+
return name.name;
|
|
22
|
+
if (name.type === "JSXMemberExpression")
|
|
23
|
+
return name.property.name;
|
|
24
|
+
return null;
|
|
25
|
+
};
|
|
26
|
+
/** The attribute of an opening element by name, or undefined. */
|
|
27
|
+
const attributeOf = (opening, name) => opening.attributes.find((entry) => entry.type === "JSXAttribute" && entry.name.type === "JSXIdentifier" && entry.name.name === name);
|
|
28
|
+
/** The string literal an attribute passes (`kind="filter"` or `kind={"filter"}`), or null when it is absent or dynamic. */
|
|
29
|
+
const literalOf = (attribute) => {
|
|
30
|
+
if (attribute === undefined || attribute.value === null)
|
|
31
|
+
return null;
|
|
32
|
+
const value = attribute.value.type === "JSXExpressionContainer" ? attribute.value.expression : attribute.value;
|
|
33
|
+
return value.type === "Literal" && typeof value.value === "string" ? value.value : null;
|
|
34
|
+
};
|
|
35
|
+
/** Whether a `Tabs` element draws the pill. */
|
|
36
|
+
const isPill = (opening) => literalOf(attributeOf(opening, "appearance")) === "primary";
|
|
37
|
+
/** No pill in JSX: the pill is the `switch` header zone of a card. */
|
|
38
|
+
export const tabsSwitchZone = defineRule({
|
|
39
|
+
meta: {
|
|
40
|
+
type: "problem",
|
|
41
|
+
docs: {
|
|
42
|
+
description: "A pill (Tabs appearance=\"primary\") stands only as the switch header zone of a SurfaceCard; written in JSX anywhere else it is a finding.",
|
|
43
|
+
url: "knowledge/ui/presentation/grammar-laws.yaml#TABS-SWITCH-ZONE",
|
|
44
|
+
},
|
|
45
|
+
schema: [],
|
|
46
|
+
messages: {
|
|
47
|
+
pill: "A pill (`Tabs appearance=\"primary\"`) switches a mode or period of one block and is drawn only as the `switch` header zone of its card: `headers={[{ kind: \"switch\", label, items, selectedKey, onSelect }]}`, two to four short options with no count and no href. Written here it stands loose in the page. Views of a page are page tabs (`Page header.tabs`); views or filters inside a region are underline `Tabs`.",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
create(context) {
|
|
51
|
+
if (!isProductSource(context))
|
|
52
|
+
return {};
|
|
53
|
+
return {
|
|
54
|
+
JSXOpeningElement(opening) {
|
|
55
|
+
if (elementName(opening) !== "Tabs" || !isPill(opening))
|
|
56
|
+
return;
|
|
57
|
+
context.report({ node: opening, messageId: "pill" });
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
/** A boundary that starts its own surface: the page tabs above it are not its parent layer. */
|
|
63
|
+
const SURFACE_BOUNDARIES = new Set(["Drawer", "Dialog", "Modal", "Popover", "Sheet"]);
|
|
64
|
+
/** Whether a source text reads a name as a whole word or member chain (`view` in `view === "a"`, not in `preview`). */
|
|
65
|
+
const reads = (text, name) => {
|
|
66
|
+
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
67
|
+
return new RegExp(`(?<![\\w$.])${escaped}(?![\\w$])`).test(text);
|
|
68
|
+
};
|
|
69
|
+
/** Whether a `Page` element's `header` passes an object literal that carries `tabs`. */
|
|
70
|
+
const pageHasHeaderTabs = (page) => {
|
|
71
|
+
const header = attributeOf(page.openingElement, "header");
|
|
72
|
+
const value = header?.value?.type === "JSXExpressionContainer" ? header.value.expression : null;
|
|
73
|
+
return value?.type === "ObjectExpression" && value.properties.some((property) => property.type === "Property" && property.key.type === "Identifier" && property.key.name === "tabs");
|
|
74
|
+
};
|
|
75
|
+
/** At most two underline layers. */
|
|
76
|
+
export const tabsDepth = defineRule({
|
|
77
|
+
meta: {
|
|
78
|
+
type: "problem",
|
|
79
|
+
docs: {
|
|
80
|
+
description: "A page stacks at most two underline Tabs layers (page tabs and one region set).",
|
|
81
|
+
url: "knowledge/ui/presentation/grammar-laws.yaml#TABS-DEPTH",
|
|
82
|
+
},
|
|
83
|
+
schema: [],
|
|
84
|
+
messages: {
|
|
85
|
+
depth: "This underline `Tabs` is layer {{layers}} ({{chain}}). A page stacks at most two underline layers: the page tabs (`Page header.tabs`) and one region set inside a tab's panel. Fold the third level into a `switch` zone, a filter zone of a card, a list, or a drawer.",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
create(context) {
|
|
89
|
+
if (!isProductSource(context))
|
|
90
|
+
return {};
|
|
91
|
+
const underlines = [];
|
|
92
|
+
return {
|
|
93
|
+
JSXOpeningElement(opening) {
|
|
94
|
+
if (elementName(opening) !== "Tabs" || isPill(opening))
|
|
95
|
+
return;
|
|
96
|
+
const key = attributeOf(opening, "selectedKey");
|
|
97
|
+
const expression = key?.value?.type === "JSXExpressionContainer" ? key.value.expression : null;
|
|
98
|
+
const readable = expression !== null && expression.type !== "JSXEmptyExpression" && expression.type !== "Literal";
|
|
99
|
+
underlines.push({ opening, key: readable ? context.sourceCode.getText(expression) : null });
|
|
100
|
+
},
|
|
101
|
+
"Program:exit"() {
|
|
102
|
+
for (const current of underlines) {
|
|
103
|
+
const owners = new Set();
|
|
104
|
+
let pageTabs = false;
|
|
105
|
+
let child = current.opening;
|
|
106
|
+
for (let node = child.parent; node !== undefined && node !== null; child = node, node = node.parent) {
|
|
107
|
+
if (node.type === "JSXElement") {
|
|
108
|
+
const name = elementName(node.openingElement);
|
|
109
|
+
if (name !== null && SURFACE_BOUNDARIES.has(name))
|
|
110
|
+
break;
|
|
111
|
+
if (name === "Page" && node.children.includes(child) && pageHasHeaderTabs(node))
|
|
112
|
+
pageTabs = true;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
let test = null;
|
|
116
|
+
if (node.type === "ConditionalExpression" && child !== node.test)
|
|
117
|
+
test = node.test;
|
|
118
|
+
else if (node.type === "LogicalExpression" && node.operator === "&&" && child === node.right)
|
|
119
|
+
test = node.left;
|
|
120
|
+
else if (node.type === "SwitchCase" && node.parent.type === "SwitchStatement" && node.consequent.includes(child))
|
|
121
|
+
test = node.parent.discriminant;
|
|
122
|
+
if (test === null)
|
|
123
|
+
continue;
|
|
124
|
+
const text = context.sourceCode.getText(test);
|
|
125
|
+
for (const other of underlines)
|
|
126
|
+
if (other !== current && other.key !== null && reads(text, other.key))
|
|
127
|
+
owners.add(other);
|
|
128
|
+
}
|
|
129
|
+
const layers = owners.size + 1 + (pageTabs ? 1 : 0);
|
|
130
|
+
if (layers < 2)
|
|
131
|
+
continue;
|
|
132
|
+
const chain = `${pageTabs ? "page tabs, " : ""}${owners.size > 0 ? `${String(owners.size)} enclosing set${owners.size > 1 ? "s" : ""}, ` : ""}this set`;
|
|
133
|
+
if (layers > 2)
|
|
134
|
+
context.report({ node: current.opening, messageId: "depth", data: { layers: String(layers), chain } });
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
/** The rules this law contributes to the plugin. */
|
|
141
|
+
export const rules = {
|
|
142
|
+
"tabs-switch-zone": tabsSwitchZone,
|
|
143
|
+
"tabs-depth": tabsDepth,
|
|
144
|
+
};
|
|
145
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#TABS-SWITCH-ZONE");
|
|
146
|
+
/** Every rule of this law is an error. */
|
|
147
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-grammar/${name}`, "error"]));
|