@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,136 @@
|
|
|
1
|
+
import { importOf } from "@starci/eslint-canon-fe/lib/bindings.mjs";
|
|
2
|
+
const GRAMMAR_SOURCE = /^@starci\/grammar(?:\/(?:common|core|offset-pop))?$/;
|
|
3
|
+
/** The element name of a JSX name: `Stack`, or `G.Stack` as `Stack`. */
|
|
4
|
+
export const nameOf = (name) => {
|
|
5
|
+
if (name.type === "JSXIdentifier")
|
|
6
|
+
return name.name;
|
|
7
|
+
if (name.type === "JSXMemberExpression")
|
|
8
|
+
return name.property.name;
|
|
9
|
+
return null;
|
|
10
|
+
};
|
|
11
|
+
/** The leftmost identifier of a JSX name (`G` of `G.Stack`), or null. */
|
|
12
|
+
const rootOf = (name) => {
|
|
13
|
+
if (name.type === "JSXIdentifier")
|
|
14
|
+
return name;
|
|
15
|
+
if (name.type === "JSXMemberExpression")
|
|
16
|
+
return rootOf(name.object);
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
19
|
+
/** The Grammar component an opening element names (imported from `@starci/grammar`), or null for another source or a plain element. */
|
|
20
|
+
export const grammarName = (context, opening) => {
|
|
21
|
+
const root = rootOf(opening.name);
|
|
22
|
+
if (root === null || !/^[A-Z]/.test(root.name))
|
|
23
|
+
return null;
|
|
24
|
+
const imported = importOf(context, root);
|
|
25
|
+
if (imported === null || imported === undefined || !GRAMMAR_SOURCE.test(imported.source))
|
|
26
|
+
return null;
|
|
27
|
+
return nameOf(opening.name);
|
|
28
|
+
};
|
|
29
|
+
/** The attribute `name` of an opening element, or undefined. */
|
|
30
|
+
export const attributeOf = (opening, name) => opening.attributes.find((entry) => entry.type === "JSXAttribute" && entry.name.type === "JSXIdentifier" && entry.name.name === name);
|
|
31
|
+
/** True when the element carries a JSX spread, so a prop may arrive that the source does not show. */
|
|
32
|
+
export const hasSpread = (opening) => opening.attributes.some((entry) => entry.type === "JSXSpreadAttribute");
|
|
33
|
+
/** The string a literal attribute value spells (`"x"`, `{"x"}`, `` {`x`} ``), or null. */
|
|
34
|
+
export const literalString = (attribute) => {
|
|
35
|
+
const value = attribute?.value;
|
|
36
|
+
if (value === undefined || value === null)
|
|
37
|
+
return null;
|
|
38
|
+
if (value.type === "Literal")
|
|
39
|
+
return typeof value.value === "string" ? value.value : null;
|
|
40
|
+
if (value.type !== "JSXExpressionContainer")
|
|
41
|
+
return null;
|
|
42
|
+
const expression = value.expression;
|
|
43
|
+
if (expression.type === "Literal")
|
|
44
|
+
return typeof expression.value === "string" ? expression.value : null;
|
|
45
|
+
if (expression.type === "TemplateLiteral" && expression.expressions.length === 0)
|
|
46
|
+
return expression.quasis[0]?.value.cooked ?? null;
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
/** The literal string prop `name` of an opening element, or null when absent or not a literal. */
|
|
50
|
+
export const stringProp = (opening, name) => literalString(attributeOf(opening, name));
|
|
51
|
+
/** True for the expressions that render nothing: `null`, `undefined`, booleans. */
|
|
52
|
+
const rendersNothing = (node) => (node.type === "Literal" && (node.value === null || typeof node.value === "boolean")) || (node.type === "Identifier" && node.name === "undefined");
|
|
53
|
+
/** True for `<Fragment>` and `<React.Fragment>`: a plain fragment with a name. */
|
|
54
|
+
export const isFragmentElement = (element) => {
|
|
55
|
+
const name = element.openingElement.name;
|
|
56
|
+
return (name.type === "JSXIdentifier" && name.name === "Fragment") || (name.type === "JSXMemberExpression" && name.property.name === "Fragment");
|
|
57
|
+
};
|
|
58
|
+
/** The nodes an expression renders: elements and text, looked through fragments, conditions, `&&` and `.map`. Other expressions stay as themselves. */
|
|
59
|
+
const rendered = (node, into) => {
|
|
60
|
+
switch (node.type) {
|
|
61
|
+
case "JSXEmptyExpression":
|
|
62
|
+
return;
|
|
63
|
+
case "JSXElement":
|
|
64
|
+
if (isFragmentElement(node)) {
|
|
65
|
+
for (const child of node.children)
|
|
66
|
+
renderedChild(child, into);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
into.push(node);
|
|
70
|
+
return;
|
|
71
|
+
case "JSXFragment":
|
|
72
|
+
for (const child of node.children)
|
|
73
|
+
renderedChild(child, into);
|
|
74
|
+
return;
|
|
75
|
+
case "JSXExpressionContainer":
|
|
76
|
+
rendered(node.expression, into);
|
|
77
|
+
return;
|
|
78
|
+
case "ConditionalExpression":
|
|
79
|
+
rendered(node.consequent, into);
|
|
80
|
+
rendered(node.alternate, into);
|
|
81
|
+
return;
|
|
82
|
+
case "LogicalExpression":
|
|
83
|
+
rendered(node.right, into);
|
|
84
|
+
return;
|
|
85
|
+
case "ArrayExpression":
|
|
86
|
+
for (const element of node.elements)
|
|
87
|
+
if (element !== null && element.type !== "SpreadElement")
|
|
88
|
+
rendered(element, into);
|
|
89
|
+
return;
|
|
90
|
+
case "TSAsExpression":
|
|
91
|
+
case "TSNonNullExpression":
|
|
92
|
+
rendered(node.expression, into);
|
|
93
|
+
return;
|
|
94
|
+
case "CallExpression": {
|
|
95
|
+
const callee = node.callee;
|
|
96
|
+
const mapper = node.arguments[0];
|
|
97
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "map" && mapper !== undefined && (mapper.type === "ArrowFunctionExpression" || mapper.type === "FunctionExpression")) {
|
|
98
|
+
if (mapper.body.type !== "BlockStatement") {
|
|
99
|
+
rendered(mapper.body, into);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
for (const statement of mapper.body.body)
|
|
103
|
+
if (statement.type === "ReturnStatement" && statement.argument)
|
|
104
|
+
rendered(statement.argument, into);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
into.push(node);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
default:
|
|
111
|
+
if (!rendersNothing(node))
|
|
112
|
+
into.push(node);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const renderedChild = (child, into) => {
|
|
116
|
+
if (child.type === "JSXText") {
|
|
117
|
+
if (child.value.trim() !== "")
|
|
118
|
+
into.push(child);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
rendered(child, into);
|
|
122
|
+
};
|
|
123
|
+
/** What an element or fragment renders as children: elements, non-blank text, and opaque expressions. */
|
|
124
|
+
export const renderedChildren = (children) => {
|
|
125
|
+
const found = [];
|
|
126
|
+
for (const child of children)
|
|
127
|
+
renderedChild(child, found);
|
|
128
|
+
return found;
|
|
129
|
+
};
|
|
130
|
+
/** The name a rendered node is drawn by if it is a JSX element (`Stack`, `G.Stack` as `Stack`), else null. */
|
|
131
|
+
export const elementNameOf = (node) => (node.type === "JSXElement" ? nameOf(node.openingElement.name) : null);
|
|
132
|
+
/** The Grammar form controls a field label belongs to. */
|
|
133
|
+
export const FORM_CONTROLS = new Set([
|
|
134
|
+
"Input", "Select", "Textarea", "NumberField", "ComboBox", "Autocomplete", "CheckboxGroup", "RadioGroup", "ChoiceChips",
|
|
135
|
+
"Switch", "DatePicker", "DateField", "TimeField", "SearchField", "Slider", "CopyField",
|
|
136
|
+
]);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SourceView } from "./types.js";
|
|
2
|
+
import type { TSESTree } from "@typescript-eslint/types";
|
|
3
|
+
/** The Grammar export each local name of a file's `@starci/grammar` imports is bound to (an alias maps to the real name). */
|
|
4
|
+
export declare const grammarBindings: (program: TSESTree.Program) => Map<string, string>;
|
|
5
|
+
/** The Grammar component a JSX element is, or its lowercase tag; a local component of the same name is neither. */
|
|
6
|
+
export declare const elementOf: (opening: TSESTree.JSXOpeningElement, bound: ReadonlyMap<string, string>) => {
|
|
7
|
+
readonly grammar: string | null;
|
|
8
|
+
readonly tag: string | null;
|
|
9
|
+
};
|
|
10
|
+
/** The attribute of an opening element by name (a spread is not read). */
|
|
11
|
+
export declare const attributeOf: (opening: TSESTree.JSXOpeningElement, name: string) => TSESTree.JSXAttribute | null;
|
|
12
|
+
/** The string an attribute states literally (`size="figure"`, `size={"figure"}`), or null when absent or computed. */
|
|
13
|
+
export declare const literalAttribute: (opening: TSESTree.JSXOpeningElement, name: string) => string | null;
|
|
14
|
+
/** The name of an object property key (`title`, `"title"`), or null when computed. */
|
|
15
|
+
export declare const keyName: (property: TSESTree.Property) => string | null;
|
|
16
|
+
/** The value of an object literal's property by name. */
|
|
17
|
+
export declare const propertyOf: (object: TSESTree.ObjectExpression, name: string) => TSESTree.Property | null;
|
|
18
|
+
/**
|
|
19
|
+
* True when an object literal is a ListRow data object in the source: it stands in the `items` of a list collection
|
|
20
|
+
* (`{ kind: "list", items }`, or `items` of a `Section` / `SurfaceCard` written `kind="list"`), through any `.map` callback, array,
|
|
21
|
+
* conditional or `as` wrapper, or it is the `row` of a `<ListRow>`. A row list declared elsewhere and passed by name is not followed.
|
|
22
|
+
*/
|
|
23
|
+
export declare const isListRow: (object: TSESTree.ObjectExpression, bound: ReadonlyMap<string, string>) => boolean;
|
|
24
|
+
/** Every node under (and including) a node, in source order; JSX and TypeScript keys come from the parser's visitor keys. */
|
|
25
|
+
export declare function descendants(node: TSESTree.Node, source: SourceView): Generator<TSESTree.Node>;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/** A module that is the Grammar package or one of its entries. */
|
|
2
|
+
const GRAMMAR_SOURCE = /^@starci\/grammar(?:\/|$)/;
|
|
3
|
+
/** The Grammar export each local name of a file's `@starci/grammar` imports is bound to (an alias maps to the real name). */
|
|
4
|
+
export const grammarBindings = (program) => {
|
|
5
|
+
const bound = new Map();
|
|
6
|
+
for (const statement of program.body) {
|
|
7
|
+
if (statement.type !== "ImportDeclaration" || !GRAMMAR_SOURCE.test(String(statement.source.value)))
|
|
8
|
+
continue;
|
|
9
|
+
for (const specifier of statement.specifiers) {
|
|
10
|
+
if (specifier.type === "ImportSpecifier")
|
|
11
|
+
bound.set(specifier.local.name, specifier.imported.type === "Identifier" ? specifier.imported.name : String(specifier.imported.value));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return bound;
|
|
15
|
+
};
|
|
16
|
+
/** The Grammar component a JSX element is, or its lowercase tag; a local component of the same name is neither. */
|
|
17
|
+
export const elementOf = (opening, bound) => {
|
|
18
|
+
const name = opening.name;
|
|
19
|
+
if (name.type === "JSXIdentifier") {
|
|
20
|
+
const grammar = bound.get(name.name);
|
|
21
|
+
if (grammar !== undefined)
|
|
22
|
+
return { grammar, tag: null };
|
|
23
|
+
return { grammar: null, tag: name.name === name.name.toLowerCase() ? name.name : null };
|
|
24
|
+
}
|
|
25
|
+
if (name.type === "JSXMemberExpression" && name.object.type === "JSXIdentifier" && bound.has(name.object.name))
|
|
26
|
+
return { grammar: name.property.name, tag: null };
|
|
27
|
+
return { grammar: null, tag: null };
|
|
28
|
+
};
|
|
29
|
+
/** The attribute of an opening element by name (a spread is not read). */
|
|
30
|
+
export const attributeOf = (opening, name) => {
|
|
31
|
+
for (const entry of opening.attributes) {
|
|
32
|
+
if (entry.type === "JSXAttribute" && entry.name.type === "JSXIdentifier" && entry.name.name === name)
|
|
33
|
+
return entry;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
};
|
|
37
|
+
/** The string an attribute states literally (`size="figure"`, `size={"figure"}`), or null when absent or computed. */
|
|
38
|
+
export const literalAttribute = (opening, name) => {
|
|
39
|
+
const value = attributeOf(opening, name)?.value;
|
|
40
|
+
if (value == null)
|
|
41
|
+
return null;
|
|
42
|
+
if (value.type === "Literal")
|
|
43
|
+
return typeof value.value === "string" ? value.value : null;
|
|
44
|
+
if (value.type === "JSXExpressionContainer") {
|
|
45
|
+
const expression = value.expression;
|
|
46
|
+
if (expression.type === "Literal" && typeof expression.value === "string")
|
|
47
|
+
return expression.value;
|
|
48
|
+
if (expression.type === "TemplateLiteral" && expression.expressions.length === 0)
|
|
49
|
+
return expression.quasis[0]?.value.cooked ?? null;
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
};
|
|
53
|
+
/** The name of an object property key (`title`, `"title"`), or null when computed. */
|
|
54
|
+
export const keyName = (property) => {
|
|
55
|
+
if (property.computed)
|
|
56
|
+
return null;
|
|
57
|
+
if (property.key.type === "Identifier")
|
|
58
|
+
return property.key.name;
|
|
59
|
+
if (property.key.type === "Literal")
|
|
60
|
+
return String(property.key.value);
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
63
|
+
/** The value of an object literal's property by name. */
|
|
64
|
+
export const propertyOf = (object, name) => {
|
|
65
|
+
for (const property of object.properties) {
|
|
66
|
+
if (property.type === "Property" && keyName(property) === name)
|
|
67
|
+
return property;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
};
|
|
71
|
+
const isListKind = (object) => {
|
|
72
|
+
const kind = propertyOf(object, "kind")?.value;
|
|
73
|
+
return kind?.type === "Literal" && kind.value === "list";
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* True when an object literal is a ListRow data object in the source: it stands in the `items` of a list collection
|
|
77
|
+
* (`{ kind: "list", items }`, or `items` of a `Section` / `SurfaceCard` written `kind="list"`), through any `.map` callback, array,
|
|
78
|
+
* conditional or `as` wrapper, or it is the `row` of a `<ListRow>`. A row list declared elsewhere and passed by name is not followed.
|
|
79
|
+
*/
|
|
80
|
+
export const isListRow = (object, bound) => {
|
|
81
|
+
let node = object;
|
|
82
|
+
for (let parent = node.parent; parent != null; node = parent, parent = parent.parent) {
|
|
83
|
+
switch (parent.type) {
|
|
84
|
+
case "ArrayExpression":
|
|
85
|
+
case "SpreadElement":
|
|
86
|
+
case "ConditionalExpression":
|
|
87
|
+
case "LogicalExpression":
|
|
88
|
+
case "ReturnStatement":
|
|
89
|
+
case "BlockStatement":
|
|
90
|
+
case "TSAsExpression":
|
|
91
|
+
case "TSSatisfiesExpression":
|
|
92
|
+
continue;
|
|
93
|
+
case "ArrowFunctionExpression":
|
|
94
|
+
if (parent.body !== node)
|
|
95
|
+
return false;
|
|
96
|
+
continue;
|
|
97
|
+
case "CallExpression":
|
|
98
|
+
if (parent.callee === node || parent.callee.type !== "MemberExpression")
|
|
99
|
+
return false;
|
|
100
|
+
continue;
|
|
101
|
+
case "Property":
|
|
102
|
+
if (parent.value !== node || keyName(parent) !== "items")
|
|
103
|
+
return false;
|
|
104
|
+
return parent.parent.type === "ObjectExpression" && isListKind(parent.parent);
|
|
105
|
+
case "JSXExpressionContainer": {
|
|
106
|
+
const attribute = parent.parent;
|
|
107
|
+
if (attribute.type !== "JSXAttribute" || attribute.name.type !== "JSXIdentifier")
|
|
108
|
+
return false;
|
|
109
|
+
const opening = attribute.parent;
|
|
110
|
+
const { grammar } = elementOf(opening, bound);
|
|
111
|
+
if (attribute.name.name === "row")
|
|
112
|
+
return grammar === "ListRow";
|
|
113
|
+
if (attribute.name.name !== "items")
|
|
114
|
+
return false;
|
|
115
|
+
return literalAttribute(opening, "kind") === "list" && (grammar === "Section" || grammar === "SurfaceCard");
|
|
116
|
+
}
|
|
117
|
+
default:
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
123
|
+
/** Every node under (and including) a node, in source order; JSX and TypeScript keys come from the parser's visitor keys. */
|
|
124
|
+
export function* descendants(node, source) {
|
|
125
|
+
yield node;
|
|
126
|
+
const keys = source.visitorKeys[node.type] ?? [];
|
|
127
|
+
for (const key of keys) {
|
|
128
|
+
const child = node[key];
|
|
129
|
+
for (const item of Array.isArray(child) ? child : [child]) {
|
|
130
|
+
if (item !== null && typeof item === "object" && typeof item.type === "string")
|
|
131
|
+
yield* descendants(item, source);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
package/dist/lib/why.js
CHANGED
|
@@ -9,6 +9,16 @@ export const why = {
|
|
|
9
9
|
en: "A StatusDot or Badge label in `<file>` starts with a lowercase letter.",
|
|
10
10
|
fix: "Start the label with an uppercase letter (`Running`, not `running`); the rule fixes a literal label in place.",
|
|
11
11
|
},
|
|
12
|
+
"tabs-switch-zone": {
|
|
13
|
+
code: "FE_TABS_SWITCH_ZONE",
|
|
14
|
+
en: "A pill (`Tabs appearance=\"primary\"`) is written in JSX in `<file>`, outside the `switch` header zone of a card.",
|
|
15
|
+
fix: "A mode or period switch of one block is the card's zone: `headers={[{ kind: \"switch\", label, items, selectedKey, onSelect }]}` with two to four short options. Views of a page are page tabs (`Page header.tabs`); views or filters inside a region are underline `Tabs`.",
|
|
16
|
+
},
|
|
17
|
+
"tabs-depth": {
|
|
18
|
+
code: "FE_TABS_DEPTH",
|
|
19
|
+
en: "Underline `Tabs` in `<file>` stack past two layers.",
|
|
20
|
+
fix: "A page has the page tabs (`Page header.tabs`) and at most one region set in a tab's panel. Fold a third level into a `switch` zone, a filter zone of a card, a list, or a drawer.",
|
|
21
|
+
},
|
|
12
22
|
"no-primary-tabs-misuse": {
|
|
13
23
|
code: "FE_GROUPED_CHOICE_CONTROL",
|
|
14
24
|
en: "Primary `Tabs` (or a `switch` header zone) in `<file>` hold a count, an href, an attention mark, one item or more than four items.",
|
|
@@ -24,6 +34,16 @@ export const why = {
|
|
|
24
34
|
en: "A ButtonGroup, FilterButtonGroup or SegmentedControl in `<file>` draws a group of attached buttons, which the Grammar no longer has.",
|
|
25
35
|
fix: "Use the component of the meaning: Tabs (kind filter for a list) to switch a view or filter, ChoiceChips or RadioGroup to choose a form value, separate Buttons or a DropdownMenu for actions.",
|
|
26
36
|
},
|
|
37
|
+
"field-label": {
|
|
38
|
+
code: "FE_FIELD_LABEL",
|
|
39
|
+
en: "A Text, Label, label or span in `<file>` stands right before a Grammar form control that has no `label` prop: a hand-made field label.",
|
|
40
|
+
fix: "Give the control its `label` prop. The Field contract spaces label to control 8 and control to help 4; field to field 24 is set by the Form or the field stack.",
|
|
41
|
+
},
|
|
42
|
+
"gap-proximity": {
|
|
43
|
+
code: "FE_GAP_PROXIMITY",
|
|
44
|
+
en: "A Grammar layout element in `<file>` (Stack, Cluster, PaneSection, Section, Grid, Form, Fieldset) stands directly inside another whose gap role is not wider: the child's gap is not at least one step closer than its container's.",
|
|
45
|
+
fix: "Step the child down one role (region -> section -> row -> inline -> identity), or step the parent up. Section is always row and Grid and Form fix their own role: do not nest them where the parent is as close. A field stack of controls inside a Form or Fieldset is exempt.",
|
|
46
|
+
},
|
|
27
47
|
"generated-art-in-mascot-or-media-frame": {
|
|
28
48
|
code: "FE_GENERATED_ART_OUTSIDE_FRAME",
|
|
29
49
|
en: "Generated art (a `.webp` or `.png` under `/images/`) in `<file>` is drawn outside Grammar's `Mascot` and `MediaFrame`, so it takes whatever box the page gives it.",
|
|
@@ -59,6 +79,11 @@ export const why = {
|
|
|
59
79
|
en: "A page head in `<file>` imports PageHeader or Breadcrumbs from the Grammar, so the head is composed by hand.",
|
|
60
80
|
fix: "Pass the head to Page `header` (title, breadcrumbs and the slots it needs: description, meta, status, mark, actions, tabs).",
|
|
61
81
|
},
|
|
82
|
+
"page-breadcrumbs": {
|
|
83
|
+
code: "FE_PAGE_BREADCRUMBS",
|
|
84
|
+
en: "The `Page` header of a console page in `<file>` states no `breadcrumbs`.",
|
|
85
|
+
fix: "Add `breadcrumbs: { label, items }` to the header, or `breadcrumbs: \"root\"` on the root page of the console (it draws no trail).",
|
|
86
|
+
},
|
|
62
87
|
"page-tabs-in-header": {
|
|
63
88
|
code: "FE_PAGE_TABS_OUTSIDE_HEADER",
|
|
64
89
|
en: "A page tab bar in `<file>` is a Tabs at the top of the Page body or right after a PageHeader, outside the header band.",
|
|
@@ -124,6 +149,16 @@ export const why = {
|
|
|
124
149
|
en: "Bottom tab navigation replaces the required mobile sidebar drawer.",
|
|
125
150
|
fix: "Use WorkspaceShell navigationDrawer and Sidebar presentation=\"drawer\".",
|
|
126
151
|
},
|
|
152
|
+
"surface-in-surface": {
|
|
153
|
+
code: "FE_SURFACE_IN_SURFACE",
|
|
154
|
+
en: "A hand-made row list (a Stack, ul, ol or div whose rows are divided by `Divider`) in `<file>` stands inside a SurfaceCard, Drawer, Dialog, PaneSection or main-rail rail.",
|
|
155
|
+
fix: "Use `<Section kind=\"list\">` for rows inside a surface (the Grammar draws them as a nested surface with a border, the card radius and a clip), or a `Section kind=\"list\"` standing alone (it draws its own frame; a card around a lone list is refused by `single-child-card`).",
|
|
156
|
+
},
|
|
157
|
+
"single-child-card": {
|
|
158
|
+
code: "FE_SINGLE_CHILD_CARD",
|
|
159
|
+
en: "A `SurfaceCard` in `<file>` has no zones and holds one table, list or chart (a `DataTable`, `ListBox`, `ItemList`, `DisclosureList`, `Timeline`, `Section kind=\"list\"`, `BarChart`, `ProportionBar`, `BubbleMap`, `FlowGraph`, `MermaidDiagram`, or is `kind=\"table\"` or `kind=\"list\"`): a second frame around the one the Grammar already draws.",
|
|
160
|
+
fix: "Drop the card: put the 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 (`headers`, `footers`) or head actions (`labelEnd`).",
|
|
161
|
+
},
|
|
127
162
|
"surface-card-kind": {
|
|
128
163
|
code: "FE_SURFACE_CARD_KIND",
|
|
129
164
|
en: "A `body` SurfaceCard in `<file>` holds a hand-built list, a Divider, a SectionHeader or Heading as a second title, or a `custom` zone holds a Tabs, SearchField or Divider.",
|
|
@@ -134,4 +169,29 @@ export const why = {
|
|
|
134
169
|
en: "`<file>` colours status content against the status pair rule (`<what>`): content on a `bg-<tone>-soft` tint paints with the solid tone (e.g. `bg-success-soft text-success`), or a mark with no tint behind it paints with the soft foreground (e.g. `text-success-soft-foreground` alone).",
|
|
135
170
|
fix: "Content on a background of the tone takes the pair: `bg-<tone>-soft` with `text-<tone>-soft-foreground`. A mark that stands alone, with no background of the tone, takes the tone itself: `text-<tone>`. A solid fill takes `bg-<tone>` with `text-<tone>-foreground`.",
|
|
136
171
|
},
|
|
172
|
+
"button-arrow": {
|
|
173
|
+
code: "FE_BUTTON_ARROW",
|
|
174
|
+
en: "A Button in `<file>` types an arrow in its label, passes an arrow icon as `endContent`, or sets `endArrow` together with `startGlyph` or `startContent`.",
|
|
175
|
+
fix: "A Button that leads somewhere takes `endArrow` (the Grammar arrow slides on hover and is mirrored in RTL); one that does something takes `startGlyph`; never both.",
|
|
176
|
+
},
|
|
177
|
+
"table-cell-type": {
|
|
178
|
+
code: "FE_TABLE_CELL_TYPE",
|
|
179
|
+
en: "A DataTable cell or ListRow title/description in `<file>` draws a heading-size Text (`size=\"figure\"`), a Heading, a Label or an h1-h6.",
|
|
180
|
+
fix: "The primary of a row or cell is md 14 / 500 (`<Text isStrong>`), the secondary sm 13 muted (`<Text size=\"sm\" tone=\"muted\">`).",
|
|
181
|
+
},
|
|
182
|
+
"same-line-size": {
|
|
183
|
+
code: "FE_SAME_LINE_SIZE",
|
|
184
|
+
en: "The Text children of one Cluster line in `<file>` have different sizes.",
|
|
185
|
+
fix: "One line, one size: give every Text of the line the size of the line's role (md primary, sm secondary). A Badge or chip may stay as it is.",
|
|
186
|
+
},
|
|
187
|
+
"row-title-link": {
|
|
188
|
+
code: "FE_ROW_TITLE_LINK",
|
|
189
|
+
en: "A Link is the title of a row in `<file>` that already opens (onPress, onAction, href, chevron, or a DataTable with onRowAction or row hrefs).",
|
|
190
|
+
fix: "The row is the one pressable: write the title as plain text (`<Text isStrong>`) and keep the destination on the row.",
|
|
191
|
+
},
|
|
192
|
+
"suggestion-chips": {
|
|
193
|
+
code: "FE_SUGGESTION_CHIPS",
|
|
194
|
+
en: "Two or more neutral `size=\"sm\"` Buttons in `<file>` sit in one row (or one in a `.map`) and each press sends its own label.",
|
|
195
|
+
fix: "Quick replies and suggested questions are `SuggestionChips` (pill chips in a labelled toolbar): `<SuggestionChips label items={[{ id, label }]} onSelect={(id) => ...} />`.",
|
|
196
|
+
},
|
|
137
197
|
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rule } from "eslint";
|
|
2
|
+
/** A console page's header states its breadcrumbs. */
|
|
3
|
+
export declare const pageBreadcrumbs: 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 { inSlot, isProductSource, kindOfFile } from "@starci/eslint-canon-fe/lib/scope.mjs";
|
|
2
|
+
import { attributeOf, grammarName } from "./lib/grammar-jsx.js";
|
|
3
|
+
import { mountedOnlyUnderAuthGroup } from "./lib/page-frame.js";
|
|
4
|
+
import { defineFamilyRules, defineRule } from "./lib/rule-contract.js";
|
|
5
|
+
/**
|
|
6
|
+
* The breadcrumbs law (owner, 2026-11-09, PAGE-BREADCRUMBS): every console page states its trail. A `Page` whose
|
|
7
|
+
* `header` has no `breadcrumbs` leaves the reader without a way up; the root page of a console says so with
|
|
8
|
+
* `breadcrumbs: "root"` (the Grammar's `PageHeaderBreadcrumbs | "root"`; "root" draws no trail).
|
|
9
|
+
*
|
|
10
|
+
* WHAT IT READS. A Grammar `Page` whose `header` is an object literal written in place, or an identifier of a `const`
|
|
11
|
+
* of the same file that holds one; the finding is a `header` object with no `breadcrumbs` property. A `header` that is
|
|
12
|
+
* absent (a sign-in card, a landing), not an object literal (another expression, an imported value, a parameter), or
|
|
13
|
+
* holds a spread (`{ ...base }`, the property may arrive through it) is not judged.
|
|
14
|
+
*
|
|
15
|
+
* WHICH PAGES ARE CONSOLE PAGES. The app console is every page of the app that sits inside its shell, so the rule
|
|
16
|
+
* judges a `Page` drawn in:
|
|
17
|
+
* - a page owner or a layout owner (`fe.feature`, kind `pages` or `layouts`), unless it is a segment boundary owner
|
|
18
|
+
* (`*ErrorPage`, `*LoadingPage`, `*NotFoundPage`) or every route that mounts the owner lies under the app's `(auth)`
|
|
19
|
+
* route group (sign-in, sign-up, password: the same reading `route-page-is-one-page` uses);
|
|
20
|
+
* - an app block (`fe.components`, kind `blocks`), because a page owner may hand its `Page` to the one connected block
|
|
21
|
+
* that draws it from client data.
|
|
22
|
+
* A `Page` drawn elsewhere (a script, a story, a library) is not a console page.
|
|
23
|
+
*
|
|
24
|
+
* WHERE IT APPLIES. Product source (`isProductSource`).
|
|
25
|
+
*/
|
|
26
|
+
/** The segment boundary owners of FE-BOUNDARY-1: they stand in for whichever page failed or waits. */
|
|
27
|
+
const BOUNDARY_OWNER = /\/features\/pages\/[^/]*(?:ErrorPage|LoadingPage|NotFoundPage)\//;
|
|
28
|
+
/** True when the file draws console pages (see the header comment). */
|
|
29
|
+
const isConsoleFile = (context) => {
|
|
30
|
+
if (inSlot(context, "fe.components"))
|
|
31
|
+
return kindOfFile(context) === "blocks";
|
|
32
|
+
if (!inSlot(context, "fe.feature"))
|
|
33
|
+
return false;
|
|
34
|
+
const kind = kindOfFile(context);
|
|
35
|
+
if (kind !== "pages" && kind !== "layouts")
|
|
36
|
+
return false;
|
|
37
|
+
if (BOUNDARY_OWNER.test(context.filename.replaceAll("\\", "/")))
|
|
38
|
+
return false;
|
|
39
|
+
return !mountedOnlyUnderAuthGroup(context);
|
|
40
|
+
};
|
|
41
|
+
/** The object literal a `header` attribute passes: in place, or a `const` of the same file; null for anything else. */
|
|
42
|
+
const headerObject = (context, attribute) => {
|
|
43
|
+
const value = attribute.value?.type === "JSXExpressionContainer" ? attribute.value.expression : null;
|
|
44
|
+
if (value === null)
|
|
45
|
+
return null;
|
|
46
|
+
if (value.type === "ObjectExpression")
|
|
47
|
+
return value;
|
|
48
|
+
if (value.type !== "Identifier")
|
|
49
|
+
return null;
|
|
50
|
+
for (let scope = context.sourceCode.getScope(attribute); scope; scope = scope.upper) {
|
|
51
|
+
const definition = scope.set.get(value.name)?.defs[0];
|
|
52
|
+
if (definition === undefined)
|
|
53
|
+
continue;
|
|
54
|
+
const declarator = definition.node;
|
|
55
|
+
return declarator.type === "VariableDeclarator" && declarator.init?.type === "ObjectExpression" ? declarator.init : null;
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
};
|
|
59
|
+
/** True when an object states `breadcrumbs`, or holds a spread that may carry it. */
|
|
60
|
+
const statesBreadcrumbs = (object) => object.properties.some((property) => {
|
|
61
|
+
if (property.type === "SpreadElement")
|
|
62
|
+
return true;
|
|
63
|
+
const key = property.key;
|
|
64
|
+
return (key.type === "Identifier" && !property.computed && key.name === "breadcrumbs") || (key.type === "Literal" && key.value === "breadcrumbs");
|
|
65
|
+
});
|
|
66
|
+
/** A console page's header states its breadcrumbs. */
|
|
67
|
+
export const pageBreadcrumbs = defineRule({
|
|
68
|
+
meta: {
|
|
69
|
+
type: "problem",
|
|
70
|
+
docs: { description: "PAGE-BREADCRUMBS: the header of a console Page states breadcrumbs, or breadcrumbs: \"root\" for the root page." },
|
|
71
|
+
schema: [],
|
|
72
|
+
messages: {
|
|
73
|
+
missing: "This `Page` header states no `breadcrumbs`: every console page gives its trail so the reader can go up. Add `breadcrumbs: { label, items }` (the ancestors, nearest last), or `breadcrumbs: \"root\"` when this is the root page of the console (it draws no trail).",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
create(context) {
|
|
77
|
+
if (!isProductSource(context))
|
|
78
|
+
return {};
|
|
79
|
+
let console = null;
|
|
80
|
+
return {
|
|
81
|
+
JSXOpeningElement(opening) {
|
|
82
|
+
if (grammarName(context, opening) !== "Page")
|
|
83
|
+
return;
|
|
84
|
+
const header = attributeOf(opening, "header");
|
|
85
|
+
if (header === undefined)
|
|
86
|
+
return;
|
|
87
|
+
const object = headerObject(context, header);
|
|
88
|
+
if (object === null || statesBreadcrumbs(object))
|
|
89
|
+
return;
|
|
90
|
+
console ??= isConsoleFile(context);
|
|
91
|
+
if (console)
|
|
92
|
+
context.report({ node: object.range[0] >= opening.range[0] && object.range[1] <= opening.range[1] ? object : header, messageId: "missing" });
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
/** The rules this law contributes to the plugin. */
|
|
98
|
+
export const rules = {
|
|
99
|
+
"page-breadcrumbs": pageBreadcrumbs,
|
|
100
|
+
};
|
|
101
|
+
defineFamilyRules(rules, "knowledge/ui/presentation/grammar-laws.yaml#PAGE-BREADCRUMBS");
|
|
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 rowTitleLink: 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">;
|