@actioneer/ads-lint 0.2.0 → 1.0.61

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.
@@ -0,0 +1,273 @@
1
+ // Collection-screen rules (user-mandated 2026-11-27).
2
+ //
3
+ // A collection screen's control row — tabs leading, search and filter
4
+ // triggers trailing — is the ADS `CollectionToolbar`. It owns the spacing
5
+ // (24px under the header bar comes from BodyPageHeader's own mb-6, 16px to the
6
+ // table below from the toolbar) and the "All N" label when there are no tabs.
7
+ // Products that hand-rolled the row padded the content's top on top of the
8
+ // header's gap (~42px above, 16px below) and left the leading side empty on a
9
+ // search-only screen.
10
+
11
+ import { matchesAny } from "../config.mjs";
12
+ import { classText, elementEnd, jsxChildren, openingTag, topLevelAttr } from "../jsx.mjs";
13
+
14
+ export const COLLECTION_RULES = [
15
+ {
16
+ name: "collection-toolbar-missing",
17
+ severity: "error",
18
+ guidance:
19
+ "Put the tab row and its search/filter triggers in CollectionToolbar; it owns the 24px/16px gaps and the 'All N' label.",
20
+ },
21
+ {
22
+ name: "search-full-width",
23
+ severity: "error",
24
+ guidance:
25
+ "Scope a list with SearchDisclosure (the icon trigger opposite the tabs or title), not a full-width search bar.",
26
+ },
27
+ {
28
+ name: "collection-toolbar-icon-action",
29
+ severity: "error",
30
+ guidance:
31
+ "CollectionToolbar's trailing side holds only tools that act on the list: SearchDisclosure, Filters and TableColumnsMenu. A workspace setting (Caller numbers) is a labelled outline Button in BodyPageHeader actions, beside the primary action.",
32
+ },
33
+ {
34
+ name: "page-content-top-padding",
35
+ severity: "error",
36
+ guidance:
37
+ "BodyPageHeader owns the 24px gap below it; content must not pad its own top.",
38
+ },
39
+ ];
40
+
41
+ const RULE = Object.fromEntries(COLLECTION_RULES.map((r) => [r.name, r]));
42
+
43
+ const lineOf = (source, index) => source.slice(0, index).split("\n").length;
44
+
45
+ /** Skips whitespace and JSX comments (`{/* … *\/}`) from `i`. */
46
+ function skipTrivia(source, i) {
47
+ for (;;) {
48
+ const ws = /^\s*/.exec(source.slice(i))[0].length;
49
+ i += ws;
50
+ const comment = /^\{\s*\/\*[\s\S]*?\*\/\s*\}/.exec(source.slice(i));
51
+ if (!comment) return i;
52
+ i += comment[0].length;
53
+ }
54
+ }
55
+
56
+ /** Unprefixed top spacing utilities (pt-6, py-4, p-8, mt-2, my-3) — `-0` is fine. */
57
+ const TOP_SPACING = /(?:^|[\s"'`])((?:pt|py|p|mt|my)-(?!0(?![\w.]))[\w.[\]()-]+)/;
58
+
59
+ /**
60
+ * A hand-rolled tab + search row: a SearchDisclosure with a TabList shortly
61
+ * before it in the same component (no new function in between).
62
+ */
63
+ function tabsBeforeSearch(source) {
64
+ for (const m of source.matchAll(/<SearchDisclosure\b/g)) {
65
+ const before = source.slice(Math.max(0, m.index - 1500), m.index);
66
+ const tabs = before.lastIndexOf("<TabList");
67
+ if (tabs < 0) continue;
68
+ if (/\bfunction\s|<TabPanel\b/.test(before.slice(tabs))) continue;
69
+ return m;
70
+ }
71
+ return null;
72
+ }
73
+
74
+ /**
75
+ * A page-level tab row over a table: a TabList followed closely by a Table,
76
+ * with no TabPanel (a record page's sections) and no new function (another
77
+ * component) in between. Kept conservative so tabbed panels and drawers pass.
78
+ */
79
+ function pageTabsOverTable(source) {
80
+ for (const m of source.matchAll(/<TabList\b/g)) {
81
+ const after = source.slice(m.index, m.index + 2000);
82
+ const table = /<Table\b/.exec(after);
83
+ if (!table) continue;
84
+ const between = after.slice(0, table.index);
85
+ if (/<TabPanel\b|\bfunction\s|=>\s*\{|<Drawer\b/.test(between)) continue;
86
+ return m;
87
+ }
88
+ return null;
89
+ }
90
+
91
+ /** [start, end) ranges of every element named in `names` (Modal, Drawer, …). */
92
+ function ranges(source, names) {
93
+ const out = [];
94
+ const re = new RegExp(`<(${names.join("|")})(?![\\w.])`, "g");
95
+ for (const m of source.matchAll(re)) out.push([m.index, elementEnd(source, m.index, m[1])]);
96
+ return out;
97
+ }
98
+ const inside = (list, index) => list.some(([a, b]) => index > a && index < b);
99
+
100
+ /** Surfaces whose own search is sanctioned: modals and drawers (their label rows), menus, the history browser. */
101
+ const SCOPED_SURFACES = ["Modal", "ModalBody", "Drawer", "DrawerBody", "ChatHistory", "Popover", "PopoverContent", "DropdownContent", "CommandPalette"];
102
+
103
+ /**
104
+ * A search-only control row: a SearchDisclosure or Filters trigger, outside a
105
+ * modal/drawer/menu, either on a BodyPageHeader page or shortly before a Table
106
+ * (no new function in between).
107
+ */
108
+ function looseSearchRow(source, scoped) {
109
+ const page = /<BodyPageHeader\b/.test(source);
110
+ const cardHeaders = ranges(source, ["CardHeader"]);
111
+ for (const m of source.matchAll(/<(?:SearchDisclosure|Filters)\b/g)) {
112
+ if (inside(scoped, m.index)) continue;
113
+ // A card's own title row may hold its search, opposite the title.
114
+ if (inside(cardHeaders, m.index)) continue;
115
+ if (page) return m;
116
+ const after = source.slice(m.index, m.index + 1500);
117
+ const table = /<Table\b/.exec(after);
118
+ if (table && !/\bfunction\s|<TabPanel\b/.test(after.slice(0, table.index))) return m;
119
+ }
120
+ return null;
121
+ }
122
+
123
+ /** Inline style that sets a top padding or margin (paddingTop: 24, padding: "24px", marginBlock: 8). */
124
+ const TOP_STYLE = /\b(?:padding|paddingTop|paddingBlock|paddingBlockStart|margin|marginTop|marginBlock|marginBlockStart)\s*:\s*(?!0(?![\d.])|["'`]0(?![\d.]))[^,}]+/;
125
+
126
+ /** Top spacing set by a tag: a class (pt-6, p-8, mt-2) or an inline style. */
127
+ function topSpacing(tag) {
128
+ const cls = TOP_SPACING.exec(classText(tag));
129
+ if (cls) return cls[1];
130
+ const style = topLevelAttr(tag, "style");
131
+ const hit = style && TOP_STYLE.exec(style);
132
+ return hit ? hit[0].trim() : null;
133
+ }
134
+
135
+ /** The first child element of the element opening at `start`, or null. */
136
+ function firstChild(source, start, name) {
137
+ const tag = openingTag(source, start);
138
+ if (tag.endsWith("/>")) return null;
139
+ const i = skipTrivia(source, start + tag.length);
140
+ const child = /^<([A-Za-z][\w.]*)/.exec(source.slice(i));
141
+ return child ? { index: i, name: child[1], tag: openingTag(source, i) } : null;
142
+ }
143
+
144
+ const SEARCH_LABEL = /^["'`]?\{?\s*["'`]?Search\b/;
145
+
146
+ /** The list tools a CollectionToolbar may hold (user-mandated 2026-11-28). */
147
+ const LIST_TOOLS = new Set(["SearchDisclosure", "Filters", "TableColumnsMenu"]);
148
+ const BUTTONS = /^(?:Button|IconButton|TooltipIconButton|MessageAction)$/;
149
+
150
+ /** An icon-only button: an icon size, the icon-button components, or glyph-only children. */
151
+ function isIconOnly(el) {
152
+ if (el.name === "TooltipIconButton" || el.name === "IconButton") return true;
153
+ if (/\ssize=["{]\s*["'`]?icon/.test(el.tag)) return true;
154
+ const kids = jsxChildren(el.inner ?? "");
155
+ return kids.length > 0 && kids.every((k) => k.kind === "element" && /^[A-Z]/.test(k.name) && !BUTTONS.test(k.name));
156
+ }
157
+
158
+ /** Icon-only buttons in a JSX value, skipping into wrappers (Tooltip, Dropdown, fragments) but never into the list tools. */
159
+ function iconButtons(body, out = []) {
160
+ for (const child of jsxChildren(body)) {
161
+ if (child.kind === "expression") {
162
+ const inner = child.text.slice(1, -1);
163
+ if (/<[A-Z]/.test(inner)) iconButtons(inner.replace(/^[^<]*/, "").replace(/[^>]*$/, ""), out);
164
+ continue;
165
+ }
166
+ if (child.kind !== "element" || LIST_TOOLS.has(child.name)) continue;
167
+ if (BUTTONS.test(child.name)) {
168
+ if (isIconOnly(child)) out.push(child);
169
+ continue;
170
+ }
171
+ if (child.inner) iconButtons(child.inner, out);
172
+ }
173
+ return out;
174
+ }
175
+
176
+ export function lintCollections(relPath, source, cfg) {
177
+ if (cfg.collections?.enabled === false) return [];
178
+ if (matchesAny(relPath, cfg.collections?.allow ?? [])) return [];
179
+ if (!/\.(tsx|jsx)$/.test(relPath)) return [];
180
+
181
+ const findings = [];
182
+ const push = (rule, index, match, guidance = RULE[rule].guidance) =>
183
+ findings.push({ line: lineOf(source, index), rule, severity: RULE[rule].severity, match, guidance });
184
+ const scoped = ranges(source, SCOPED_SURFACES);
185
+
186
+ // collection-toolbar-missing (1): a page header carries no Search button —
187
+ // the collection's search trigger sits in CollectionToolbar under it.
188
+ for (const m of source.matchAll(/<BodyPageHeader\b/g)) {
189
+ const actions = topLevelAttr(openingTag(source, m.index), "actions");
190
+ const hit = actions && /<(SearchDisclosure|Filters)\b/.exec(actions);
191
+ if (hit) push("collection-toolbar-missing", m.index, `<BodyPageHeader actions={… <${hit[1]} …}>`, "A page header holds the title and ONE primary action — no Search button. Put SearchDisclosure (then Filters) in CollectionToolbar under the header; with no tabs it reads \"All N\".");
192
+ }
193
+
194
+ // collection-toolbar-icon-action: an icon-only button among the toolbar's
195
+ // trailing tools that is not search, filters or the columns menu.
196
+ for (const m of source.matchAll(/<CollectionToolbar(?![\w.])/g)) {
197
+ const actions = topLevelAttr(openingTag(source, m.index), "actions");
198
+ if (!actions || !actions.startsWith("{")) continue;
199
+ for (const hit of iconButtons(actions.slice(1, -1).trim().replace(/^\(|\)$/g, ""))) {
200
+ const label = /aria-label=["{]\s*["'`]?([^"'`}]*)/.exec(hit.tag)?.[1];
201
+ push("collection-toolbar-icon-action", m.index, `<CollectionToolbar actions={… <${hit.name}${label ? ` aria-label="${label}"` : ""} …}>`);
202
+ }
203
+ }
204
+
205
+ // collection-toolbar-missing (2): a Table with a hand-rolled control row.
206
+ const hasTable = /<Table\b/.test(source);
207
+ if (hasTable && !/<CollectionToolbar\b/.test(source)) {
208
+ const hit =
209
+ tabsBeforeSearch(source) ??
210
+ (/<BodyPageHeader\b/.test(source) ? pageTabsOverTable(source) : null) ??
211
+ looseSearchRow(source, scoped);
212
+ if (hit) push("collection-toolbar-missing", hit.index, hit[0]);
213
+ }
214
+
215
+ // search-full-width: a search field left at its default full width (or made
216
+ // w-full / flex-1) over a table or list. A field counts as search when it is
217
+ // typed search, leads with the Search glyph, or its placeholder / label
218
+ // starts with "Search". A fixed-width field (w-64, max-w-sm) is not a bar, a
219
+ // picker that selects on Enter (onKeyDown) is a listbox search, and a field
220
+ // inside a menu or popover is that menu's own search.
221
+ if (/<Table\b|<ChatHistory\b|<SidebarHistory\b|role="grid"|role="list"|<ul\b|<ol\b|\.map\(/.test(source)) {
222
+ for (const m of source.matchAll(/<(?:Input|InputField|SearchBar|SearchField|input)\b/g)) {
223
+ if (inside(scoped.filter(([a]) => !/^<(?:Modal|ModalBody|Drawer|DrawerBody)\b/.test(source.slice(a, a + 12))), m.index)) continue;
224
+ const tag = openingTag(source, m.index);
225
+ const labelled = ["placeholder", "aria-label", "label", "inputLabel"].some((name) => SEARCH_LABEL.test(topLevelAttr(tag, name) ?? ""));
226
+ const isSearch =
227
+ /^<Search(?:Bar|Field)\b/.test(tag) || /\btype="search"/.test(tag) || /\bstartContent=\{\s*<Search\b/.test(tag) || labelled;
228
+ if (!isSearch || /\bonKeyDown=/.test(tag)) continue;
229
+ const classes = classText(tag);
230
+ const fixed = /(?:^|\s)(?:max-)?w-(?!full(?![\w-]))[\w[\]./-]+/.test(classes);
231
+ if (fixed && !/(?:^|\s)(?:w-full|flex-1)(?![\w-])/.test(classes)) continue;
232
+ push("search-full-width", m.index, tag.replace(/\s+/g, " ").slice(0, 80));
233
+ }
234
+ }
235
+
236
+ // page-content-top-padding: the content after a BodyPageHeader pads or
237
+ // margins its own top — on its own tag, its first child, or through style.
238
+ for (const m of source.matchAll(/<BodyPageHeader\b/g)) {
239
+ const tag = openingTag(source, m.index);
240
+ const flush = /(?:^|[\s"'`])mb-0(?![\w-])/.test(classText(tag));
241
+ const next = skipTrivia(source, elementEnd(source, m.index, "BodyPageHeader"));
242
+ const sibling = /^<([A-Za-z][\w.]*)/.exec(source.slice(next));
243
+ if (!sibling) continue;
244
+ const siblingTag = openingTag(source, next);
245
+ const child = firstChild(source, next, sibling[1]);
246
+ if (flush) {
247
+ // An mb-0 header sits over an attached Drawer row: the section under it
248
+ // pads its own top (pt-6). A toolbar straight under it gets 0px.
249
+ const toolbarAt = sibling[1] === "CollectionToolbar" ? { index: next, tag: siblingTag } : child?.name === "CollectionToolbar" ? child : null;
250
+ if (toolbarAt && !topSpacing(siblingTag) && !topSpacing(toolbarAt.tag)) {
251
+ push(
252
+ "page-content-top-padding",
253
+ toolbarAt.index,
254
+ "<BodyPageHeader className=\"mb-0\"> + <CollectionToolbar>",
255
+ "An mb-0 header gives up its 24px gap: the section under it pads its top (pt-6) — or drop mb-0 when no Drawer row sits under the header.",
256
+ );
257
+ }
258
+ continue;
259
+ }
260
+ const own = topSpacing(siblingTag);
261
+ if (own) {
262
+ push("page-content-top-padding", next, `<${sibling[1]} … ${own}>`);
263
+ continue;
264
+ }
265
+ // One level down only a transparent wrapper counts: a margin on any child,
266
+ // padding only on a bare DOM element (a Card's p-5 is inside its own edge).
267
+ const bare = child && /^[a-z]/.test(child.name) && !/(?:^|\s)(?:bg-|border(?:\s|-))/.test(classText(child.tag));
268
+ const nested = child && (bare ? topSpacing(child.tag) : /(?:^|[\s"'`])((?:mt|my)-(?!0(?![\w.]))[\w.[\]()-]+)/.exec(classText(child.tag))?.[1]);
269
+ if (nested) push("page-content-top-padding", child.index, `<${sibling[1]}> <${child.name} … ${nested}>`);
270
+ }
271
+
272
+ return findings;
273
+ }
@@ -0,0 +1,157 @@
1
+ // Control-labelling rules (user-mandated 2026-11-28).
2
+ //
3
+ // - switch-on-off-text: a Switch is named by its label or by the column header
4
+ // above it ("Use for calls"), never by "On"/"Off" printed beside it — the
5
+ // switch already shows its state.
6
+ // - field-lock-icon: a lock is said once, in words, beside the status in the
7
+ // header bar ("Locked while the campaign is active"). A lock glyph in every
8
+ // Field's label row repeats it per field; a read-only screen shows values
9
+ // (DetailList), not locked inputs.
10
+ // - stepper-as-status: a stage or status is one value (a Chip), not a chain.
11
+ // A Stepper inside a table cell or a record header draws one fact about one
12
+ // record as a journey the person never takes in order.
13
+
14
+ import { matchesAny } from "../config.mjs";
15
+ import { elementEnd, jsxChildren, openingTag, topLevelAttr } from "../jsx.mjs";
16
+
17
+ export const CONTROL_RULES = [
18
+ {
19
+ name: "switch-on-off-text",
20
+ severity: "error",
21
+ guidance:
22
+ "Name a Switch with its label or the column header above it (\"Use for calls\"); never print \"On\"/\"Off\" beside it — the switch shows its state.",
23
+ },
24
+ {
25
+ name: "field-lock-icon",
26
+ severity: "error",
27
+ guidance:
28
+ "Say a lock once, in words, beside the status in the header bar (\"Locked while the campaign is active\"); show read-only values in a DetailList, not a lock glyph on each Field.",
29
+ },
30
+ {
31
+ name: "stepper-as-status",
32
+ severity: "error",
33
+ guidance:
34
+ "A status or stage is one value: show it as a filled Chip (and filter it from the column header). Stepper is for a task the user moves through.",
35
+ },
36
+ ];
37
+
38
+ const RULE = Object.fromEntries(CONTROL_RULES.map((r) => [r.name, r]));
39
+ const lineOf = (source, index) => source.slice(0, index).split("\n").length;
40
+
41
+ /** "On" / "Off" as JSX text, a string expression, or a ternary between them. */
42
+ const ON_OFF_TEXT = /^\s*(?:On|Off)\s*$/;
43
+ const ON_OFF_EXPR = /^\{\s*(?:["'`](?:On|Off)["'`]|[^{}]*\?\s*["'`](?:On|Off)["'`]\s*:\s*["'`](?:On|Off)["'`])\s*\}$/;
44
+
45
+ function isOnOff(child) {
46
+ if (child.kind === "text") return ON_OFF_TEXT.test(child.text);
47
+ if (child.kind === "expression") return ON_OFF_EXPR.test(child.text.replace(/\s+/g, " "));
48
+ // A wrapper that holds only the word: <span>{on ? "On" : "Off"}</span>.
49
+ if (child.kind === "element" && child.name !== "Switch" && child.inner) {
50
+ const inner = jsxChildren(child.inner);
51
+ return inner.length === 1 && inner[0].kind !== "element" && isOnOff(inner[0]);
52
+ }
53
+ return false;
54
+ }
55
+
56
+ /** Every element or fragment opening in `source`, with its inner body. */
57
+ function* elements(source) {
58
+ for (const m of source.matchAll(/<([A-Za-z][\w.]*)?(?=[\s>/])/g)) {
59
+ if (source[m.index + 1] === "/") continue;
60
+ const name = m[1] ?? "";
61
+ if (!name) {
62
+ if (source[m.index + 1] !== ">") continue;
63
+ const close = source.indexOf("</>", m.index + 2);
64
+ if (close < 0) continue;
65
+ yield { index: m.index, name, inner: source.slice(m.index + 2, close) };
66
+ continue;
67
+ }
68
+ const tag = openingTag(source, m.index);
69
+ if (tag.endsWith("/>")) continue;
70
+ const end = elementEnd(source, m.index, name);
71
+ yield { index: m.index, name, tag, inner: source.slice(m.index + tag.length, Math.max(m.index + tag.length, end - name.length - 3)) };
72
+ }
73
+ }
74
+
75
+ /** A lock glyph inside a JSX value: <Lock />, <LockSimple />, <LockKey /> … */
76
+ const LOCK_GLYPH = /<Lock[A-Za-z]*\b/;
77
+
78
+ /** The source of `const name = …` up to its end, when `name` is a local binding. */
79
+ function bindingSource(source, name) {
80
+ const decl = new RegExp(`\\b(?:const|let|var)\\s+${name}\\s*=`).exec(source);
81
+ if (!decl) return null;
82
+ const start = decl.index + decl[0].length;
83
+ const end = source.indexOf(";\n", start);
84
+ return source.slice(start, end < 0 ? start + 2000 : end);
85
+ }
86
+
87
+ /** [start, end) ranges of every element named in `names`. */
88
+ function ranges(source, names) {
89
+ const out = [];
90
+ const re = new RegExp(`<(${names.join("|")})(?![\\w.])`, "g");
91
+ for (const m of source.matchAll(re)) out.push([m.index, elementEnd(source, m.index, m[1])]);
92
+ return out;
93
+ }
94
+ const inside = (list, index) => list.some(([a, b]) => index > a && index < b);
95
+
96
+ export function lintControls(relPath, source, cfg) {
97
+ if (cfg.controls?.enabled === false) return [];
98
+ if (matchesAny(relPath, cfg.controls?.allow ?? [])) return [];
99
+ if (!/\.(tsx|jsx)$/.test(relPath)) return [];
100
+
101
+ const findings = [];
102
+ const push = (rule, index, match) =>
103
+ findings.push({ line: lineOf(source, index), rule, severity: RULE[rule].severity, match, guidance: RULE[rule].guidance });
104
+
105
+ // switch-on-off-text: "On"/"Off" beside a Switch (siblings in one parent), or as its own label.
106
+ if (/<Switch\b/.test(source)) {
107
+ const seen = new Set();
108
+ for (const el of elements(source)) {
109
+ if (el.name !== "Switch" && !el.inner.includes("<Switch")) continue;
110
+ const children = jsxChildren(el.inner);
111
+ const switches = children.filter((c) => c.kind === "element" && c.name === "Switch");
112
+ if (el.name === "Switch") {
113
+ const own = children.find(isOnOff);
114
+ if (own && !seen.has(el.index)) {
115
+ seen.add(el.index);
116
+ push("switch-on-off-text", el.index, `<Switch>${own.text.trim().slice(0, 40)}</Switch>`);
117
+ }
118
+ continue;
119
+ }
120
+ if (switches.length === 0) continue;
121
+ const word = children.find(isOnOff);
122
+ if (word && !seen.has(el.index)) {
123
+ seen.add(el.index);
124
+ push("switch-on-off-text", el.index, `${word.text.replace(/\s+/g, " ").trim().slice(0, 50)} beside <Switch>`);
125
+ }
126
+ }
127
+ }
128
+
129
+ // field-lock-icon: a lock glyph in a Field / InputField label or label-row actions.
130
+ for (const m of source.matchAll(/<(Field|InputField)(?![\w.])/g)) {
131
+ const tag = openingTag(source, m.index);
132
+ for (const attr of ["actions", "label", "info"]) {
133
+ const value = topLevelAttr(tag, attr);
134
+ if (!value) continue;
135
+ const ident = /^\{\s*([A-Za-z_$][\w$]*)\s*\}$/.exec(value)?.[1];
136
+ const resolved = ident ? bindingSource(source, ident) ?? "" : value;
137
+ if (LOCK_GLYPH.test(resolved)) {
138
+ push("field-lock-icon", m.index, `<${m[1]} ${attr}={… <Lock …}>`);
139
+ break;
140
+ }
141
+ }
142
+ }
143
+
144
+ // stepper-as-status: a Stepper in a table cell or a record header.
145
+ if (/<Stepper\b/.test(source)) {
146
+ const statusSlots = ranges(source, ["TableCell", "DrawerHeader", "DrawerMeta", "BodyPageHeader"]);
147
+ for (const m of source.matchAll(/<Breadcrumbs(?![\w.])/g)) {
148
+ const meta = topLevelAttr(openingTag(source, m.index), "meta");
149
+ if (meta && /<Stepper\b/.test(meta)) push("stepper-as-status", m.index, "<Breadcrumbs meta={<Stepper …>}>");
150
+ }
151
+ for (const m of source.matchAll(/<Stepper\b/g)) {
152
+ if (inside(statusSlots, m.index)) push("stepper-as-status", m.index, openingTag(source, m.index).replace(/\s+/g, " ").slice(0, 80));
153
+ }
154
+ }
155
+
156
+ return findings;
157
+ }
@@ -0,0 +1,118 @@
1
+ // Elevation rules.
2
+ //
3
+ // - shadow-static-surface (user-mandated 2026-11-28): the soft `shadow-card`
4
+ // lift means "you can press this". A card, tile or section that only holds
5
+ // content is flat: its 1px N4 edge and 14px radius set it off the page.
6
+ // `Card` applies the lift itself when it is pressable or hoverable, so
7
+ // product code never adds it to a static surface.
8
+ // - shadow-ask-misuse (user-mandated 2026-11-29): `shadow-ask` is the deeper
9
+ // teal drop of the AskActioneer floating button, the one control that
10
+ // floats over page content. It never goes on a menu, card or other button.
11
+ // Only class contexts are read (a className, or the strings passed to
12
+ // cn / clsx / cva / twMerge, and `@apply` in CSS), so prose that names the
13
+ // token — docs, principles, comments — is never flagged.
14
+
15
+ import { matchesAny } from "../config.mjs";
16
+ import { openingTag, topLevelAttr } from "../jsx.mjs";
17
+
18
+ export const ELEVATION_RULES = [
19
+ {
20
+ name: "shadow-static-surface",
21
+ severity: "error",
22
+ guidance:
23
+ "Remove shadow-card from a surface nobody can press: a content card is flat (rounded-card + border border-border). Only a pressable or hoverable card lifts — use Card isPressable, which adds the lift itself.",
24
+ },
25
+ {
26
+ name: "shadow-ask-misuse",
27
+ severity: "error",
28
+ guidance:
29
+ "shadow-ask belongs to the AskActioneer floating button only. Use the AskActioneer component; a menu or popover takes shadow-float, a modal or drawer shadow-overlay, a pressable card shadow-card, the solid Button shadow-cta.",
30
+ },
31
+ ];
32
+
33
+ const RULE = ELEVATION_RULES[0];
34
+ const ASK_RULE = ELEVATION_RULES[1];
35
+
36
+ // The `shadow-ask` utility as a class token, bare or behind variants (`hover:`, `!`).
37
+ const ASK = /(?:^|[\s"'`:!])shadow-ask!?(?=$|[\s"'`!;])/;
38
+ const CLASS_CALL = /\b(?:cn|clsx|cx|cva|twMerge|classNames)\(/g;
39
+
40
+ /** The source of a call's argument list, from just after `(` to its matching `)`. */
41
+ function callArgs(source, open) {
42
+ let depth = 0;
43
+ let quote = null;
44
+ for (let i = open; i < source.length; i++) {
45
+ const c = source[i];
46
+ if (quote) {
47
+ if (c === "\\") i++;
48
+ else if (c === quote) quote = null;
49
+ continue;
50
+ }
51
+ if (c === '"' || c === "'" || c === "`") quote = c;
52
+ else if (c === "(") depth++;
53
+ else if (c === ")" && --depth === 0) return source.slice(open + 1, i);
54
+ }
55
+ return source.slice(open + 1);
56
+ }
57
+
58
+ /** [start, end) of each string literal in source[from, to). */
59
+ function stringRanges(source, from, to, out) {
60
+ for (const m of source.slice(from, to).matchAll(/"[^"]*"|'[^']*'|`[^`]*`/g)) out.push([from + m.index, from + m.index + m[0].length]);
61
+ }
62
+
63
+ function lintAskShadow(relPath, source, cfg) {
64
+ if (matchesAny(relPath, cfg.elevation?.askShadowAllow ?? [])) return [];
65
+ if (!source.includes("shadow-ask")) return [];
66
+ // Class contexts: strings in a className attribute or in a cn/clsx/cva call; `@apply` in CSS.
67
+ const contexts = [];
68
+ if (/\.css$/.test(relPath)) {
69
+ for (const m of source.matchAll(/@apply[^;]*;/g)) contexts.push([m.index, m.index + m[0].length]);
70
+ } else if (/\.(tsx|jsx|ts|js)$/.test(relPath)) {
71
+ for (const m of source.matchAll(/<[A-Za-z][\w.]*/g)) {
72
+ const tag = openingTag(source, m.index);
73
+ const value = topLevelAttr(tag, "className");
74
+ if (!value) continue;
75
+ const at = m.index + tag.indexOf(`className=${value}`) + "className=".length;
76
+ stringRanges(source, at, at + value.length, contexts);
77
+ }
78
+ for (const m of source.matchAll(CLASS_CALL)) {
79
+ const open = m.index + m[0].length - 1;
80
+ stringRanges(source, open, open + 1 + callArgs(source, open).length, contexts);
81
+ }
82
+ }
83
+ const findings = [];
84
+ for (const m of source.matchAll(/shadow-ask/g)) {
85
+ const ctx = contexts.find(([a, b]) => m.index > a && m.index < b);
86
+ if (!ctx) continue;
87
+ // Token boundaries inside the class string: start, space, quote or a variant colon / `!` before; end, space, quote or `!` after.
88
+ if (!ASK.test(source.slice(Math.max(ctx[0], m.index - 1), Math.min(ctx[1], m.index + "shadow-ask".length + 1)))) continue;
89
+ findings.push({ line: source.slice(0, m.index).split("\n").length, rule: ASK_RULE.name, severity: ASK_RULE.severity, match: "shadow-ask", guidance: ASK_RULE.guidance });
90
+ }
91
+ return findings;
92
+ }
93
+
94
+ // Static containers that never take the lift on their own.
95
+ const STATIC_TAG = /^<(?:div|section|article|aside|li|ul|ol|span|p|figure|header|footer|main|Card)(?![\w.-])/;
96
+ // Anything that makes the element something you can press or follow.
97
+ const INTERACTIVE = /\b(?:onClick|onPress|onPointerDown|onKeyDown|href|to|isPressable|isHoverable|hoverEffect|tabIndex|asChild)\b|role=["'](?:button|link|option|menuitem|tab)["']/;
98
+ // An unprefixed `shadow-card` utility (a `hover:` / `data-…:` variant is a state, not a resting lift).
99
+ const LIFT = /(?:^|[\s"'`])!?shadow-card!?(?=$|[\s"'`])/;
100
+
101
+ export function lintElevation(relPath, source, cfg) {
102
+ if (cfg.elevation?.enabled === false) return [];
103
+ if (matchesAny(relPath, cfg.elevation?.allow ?? [])) return [];
104
+ const findings = lintAskShadow(relPath, source, cfg);
105
+ if (!/\.(tsx|jsx)$/.test(relPath)) return findings;
106
+ for (const m of source.matchAll(/<[A-Za-z][\w.]*/g)) {
107
+ const tag = openingTag(source, m.index);
108
+ if (!STATIC_TAG.test(tag) || !LIFT.test(tag) || INTERACTIVE.test(tag)) continue;
109
+ findings.push({
110
+ line: source.slice(0, m.index).split("\n").length,
111
+ rule: RULE.name,
112
+ severity: RULE.severity,
113
+ match: `${m[0]} … shadow-card`,
114
+ guidance: RULE.guidance,
115
+ });
116
+ }
117
+ return findings;
118
+ }
@@ -0,0 +1,99 @@
1
+ // Empty-state rules (user-mandated 2026-11-27).
2
+ //
3
+ // Every empty state reads the same way, top to bottom: the illustration (or a
4
+ // bare icon), the title, a description saying what belongs here and how to
5
+ // start, then the call to action. EmptyState owns that order and the type
6
+ // scale, so a missing description or a restyled title is the bug.
7
+
8
+ //
9
+ // - empty-state-table-header (user-mandated 2026-11-29): with nothing to list,
10
+ // the table is not drawn at all. An EmptyState inside a Table / TableBody /
11
+ // TableCell leaves an empty column header over the message; render the
12
+ // EmptyState instead of the Table, filling the content area under the
13
+ // control row.
14
+
15
+ import { matchesAny } from "../config.mjs";
16
+ import { elementEnd } from "../jsx.mjs";
17
+
18
+ export const EMPTY_STATE_RULES = [
19
+ {
20
+ name: "empty-state-no-description",
21
+ severity: "error",
22
+ guidance:
23
+ "Give every EmptyState a `description` — one or two short sentences under the title saying what belongs here and how to start. EmptyState stacks illustration → title → description → action.",
24
+ },
25
+ {
26
+ name: "empty-state-title-styled",
27
+ severity: "error",
28
+ guidance:
29
+ "Pass the EmptyState title as plain text. EmptyState sets its size and weight (18px Medium on a page, 14px in a panel) — never wrap it in a styled span or a hand-made heading.",
30
+ },
31
+ {
32
+ name: "empty-state-table-header",
33
+ severity: "error",
34
+ guidance:
35
+ "With nothing to list, do not draw the table: render the EmptyState in place of the Table so the message takes the whole content area under the control row — no empty column header above it.",
36
+ },
37
+ ];
38
+
39
+ const RULE = Object.fromEntries(EMPTY_STATE_RULES.map((r) => [r.name, r]));
40
+
41
+ /** Finds each `<EmptyState …>` opening tag, attribute expressions included. */
42
+ function* emptyStateTags(source) {
43
+ const re = /<EmptyState\b/g;
44
+ for (const m of source.matchAll(re)) {
45
+ let i = m.index + m[0].length;
46
+ let depth = 0;
47
+ let quote = null;
48
+ for (; i < source.length; i++) {
49
+ const c = source[i];
50
+ if (quote) {
51
+ if (c === quote) quote = null;
52
+ continue;
53
+ }
54
+ if (c === '"' || c === "'" || c === "`") quote = c;
55
+ else if (c === "{") depth++;
56
+ else if (c === "}") depth--;
57
+ else if (c === ">" && depth === 0) break;
58
+ }
59
+ yield { index: m.index, tag: source.slice(m.index, i + 1) };
60
+ }
61
+ }
62
+
63
+ export function lintEmptyStates(relPath, source, cfg) {
64
+ if (cfg.emptyStates?.enabled === false) return [];
65
+ if (matchesAny(relPath, cfg.emptyStates?.allow ?? [])) return [];
66
+ if (!/\.(tsx|jsx)$/.test(relPath)) return [];
67
+ if (!/<EmptyState\b/.test(source)) return [];
68
+
69
+ const findings = [];
70
+ const push = (name, index, match) =>
71
+ findings.push({
72
+ line: source.slice(0, index).split("\n").length,
73
+ rule: name,
74
+ severity: RULE[name].severity,
75
+ match: match.length > 80 ? `${match.slice(0, 77)}...` : match.replace(/\s+/g, " "),
76
+ guidance: RULE[name].guidance,
77
+ });
78
+
79
+ for (const { index, tag } of emptyStateTags(source)) {
80
+ // `<EmptyState />` with no title is the Thread slot, not the ADS component.
81
+ if (!/\btitle=/.test(tag)) continue;
82
+ // A present-but-empty description ("" / {""} / {``} / {null} / {undefined}) is no description.
83
+ const description = /\bdescription=(""|''|\{\s*(?:""|''|``|null|undefined)\s*\})/.exec(tag);
84
+ if (description || (!/\bdescription=/.test(tag) && !/\{\.\.\./.test(tag))) push("empty-state-no-description", index, tag);
85
+ if (/\btitle=\{\s*<[^>]*(className=|role="heading")/.test(tag)) push("empty-state-title-styled", index, tag);
86
+ }
87
+
88
+ // empty-state-table-header: an EmptyState drawn inside the table it replaces.
89
+ if (/<Table(?:Body|Cell)?(?![\w.])/.test(source) && !matchesAny(relPath, cfg.emptyStates?.tableAllow ?? [])) {
90
+ const tables = [];
91
+ for (const m of source.matchAll(/<(Table|TableBody|TableCell)(?![\w.])/g)) tables.push([m.index, elementEnd(source, m.index, m[1]), m[1]]);
92
+ for (const { index, tag } of emptyStateTags(source)) {
93
+ if (!/\btitle=/.test(tag)) continue;
94
+ const host = tables.filter(([a, b]) => index > a && index < b).pop();
95
+ if (host) push("empty-state-table-header", index, `<${host[2]}> … <EmptyState …>`);
96
+ }
97
+ }
98
+ return findings;
99
+ }