@starci/eslint-grammar-core 0.30.0 → 0.34.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.
@@ -1,122 +0,0 @@
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"]));
@@ -1,31 +0,0 @@
1
- # Suggestion chips
2
-
3
- Law module: `suggestion-chips.ts`. Finding code: `FE_SUGGESTION_CHIPS`. Knowledge: `knowledge/ui/presentation/grammar-laws.yaml#SUGGESTION-CHIPS`.
4
-
5
- Quick replies and suggested questions (the assistant drawer, chat composer suggestions, empty-state shortcuts) are `SuggestionChips`: pill chips, 28px high, in a labelled toolbar, each chip a button. A row of small neutral Buttons that each send their own label is the same thing drawn without the toolbar label and the chip look, so the law refuses it.
6
-
7
- The rule is an error in `coreGrammarConfig`; it cannot be switched off or suppressed inline.
8
-
9
- ## `starci-grammar/suggestion-chips`
10
-
11
- The rendered children of a Grammar `Cluster` or `Stack`, or a `div` (through fragments, `cond && x`, ternaries and `.map`) are a finding when two or more of them are suggestion Buttons, or one is drawn by a `.map`. A suggestion Button is a Grammar `Button` that:
12
-
13
- - states `size="sm"` literally;
14
- - has no `intent`, or a literal `intent` of `neutral` or `alternative`;
15
- - has an inline arrow `onPress` whose one call passes the same expression as the Button's single child (`{q}` and `send(q)`, `{item.label}` and `ask(item.label)`), or the same string literal as the literal child text.
16
-
17
- A spread, a computed `size` or `intent`, a handler that is not an inline arrow, a label with several children, or a handler that sends something else (an id) is not judged.
18
-
19
- **Invalid**
20
-
21
- ```tsx
22
- <Cluster>{questions.map((q) => <Button key={q} size="sm" onPress={() => send(q)}>{q}</Button>)}</Cluster>
23
- ```
24
-
25
- **Valid**
26
-
27
- ```tsx
28
- <SuggestionChips label="Suggested questions" items={questions.map((q) => ({ id: q, label: q }))} onSelect={send} />
29
- ```
30
-
31
- Rule ownership: [FE-RULE-OWNERSHIP](../../../contexts/code/fe/README.md#the-placement-test-fe-rule-ownership).