@actioneer/ads-lint 0.1.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,253 @@
1
+ // Table rules (user-mandated 2026-11-26).
2
+ //
3
+ // Tables fit their records and scroll (ADS has no pagination — not as a
4
+ // component and not hand-rolled under a table). Headers are text-only
5
+ // (user-mandated 2026-11-27): no `icon` prop and no glyph child before the
6
+ // label. A cell holds one value on one line. The actions column is pinned and square: a final
7
+ // `sticky="right"` column turns that on, so an Actions header without it is
8
+ // the bug, and so is switching the elevation off.
9
+ //
10
+ // Row actions (user-mandated 2026-11-27): actions are icon-only
11
+ // (TableRowAction — the label is the tooltip), an unavailable action stays in
12
+ // place disabled with a tooltip saying why (`disabledReason`), never hidden or
13
+ // swapped for another glyph, and the record name opens the detail panel
14
+ // (TableRecordName onOpen).
15
+
16
+ import { matchesAny } from "../config.mjs";
17
+ import { classText, jsxChildren, openingTag, topLevelAttr } from "../jsx.mjs";
18
+
19
+ const COLUMN_OPEN = /<TableColumn\b(?:=>|[^<>])*?(?<!=)\/?>/g;
20
+ const TABLE_OPEN = /<Table\s(?:=>|[^<>])*?(?<!=)>/g;
21
+
22
+ export const TABLE_RULES = [
23
+ {
24
+ name: "table-column-icon",
25
+ severity: "error",
26
+ guidance:
27
+ "Table headers are text-only (user-mandated 2026-11-27): no glyph before a column label. Drop `icon` from the TableColumn.",
28
+ },
29
+ {
30
+ name: "table-actions-not-pinned",
31
+ severity: "error",
32
+ guidance:
33
+ 'The actions column is the final TableColumn with sticky="right" (and its cells too): Table then pins it, elevates it and makes it square.',
34
+ },
35
+ {
36
+ name: "table-cell-repeated-unit",
37
+ severity: "error",
38
+ guidance:
39
+ 'Name a shared unit once in the header (TableColumn unit="people") and pass the number as <TableCell value={n} /> — it right-aligns and compacts it ("5.02k"). Never repeat the unit in every row ("229 people").',
40
+ },
41
+ {
42
+ name: "table-actions-unelevated",
43
+ severity: "error",
44
+ guidance:
45
+ "Leave elevatedActionColumn / stickyActionHeader at their defaults: the actions column stays pinned and square while the table scrolls.",
46
+ },
47
+ {
48
+ name: "table-action-labelled",
49
+ severity: "error",
50
+ guidance:
51
+ 'Row actions are icon-only: <TableRowAction icon={<Pencil />} label="Edit entry" /> — the label becomes the tooltip and accessible name. Never a text label ("Edit") in the actions column.',
52
+ },
53
+ {
54
+ name: "table-action-hidden",
55
+ severity: "error",
56
+ guidance:
57
+ "Keep every row's actions in place. When an action is unavailable for a row, render it disabled with a reason — <TableRowAction disabledReason=\"Documents can't be edited\" … /> — never hide it or swap in another glyph.",
58
+ },
59
+ {
60
+ name: "table-action-disabled-no-reason",
61
+ severity: "error",
62
+ guidance:
63
+ "A disabled row action must say why: use TableRowAction disabledReason (dims the glyph, keeps it hoverable, shows the reason as a tooltip) instead of a bare disabled Button.",
64
+ },
65
+ {
66
+ name: "table-cell-stacked",
67
+ severity: "error",
68
+ guidance:
69
+ "A table cell carries one value on one line. Do not stack a description or metadata under it: give the second fact its own TableColumn (e.g. \"Description\") and a single-line cell.",
70
+ },
71
+ {
72
+ name: "table-record-name-no-open",
73
+ severity: "error",
74
+ guidance:
75
+ "The record name opens the record: pass TableRecordName onOpen (it opens the record's page when it has one, or its side panel when it does not) and say which with opens.",
76
+ },
77
+ {
78
+ name: "table-record-name-no-destination",
79
+ severity: "warn",
80
+ guidance:
81
+ 'Say where the record opens so the open button shows it before the click: <TableRecordName opens="page" …> for the record\'s own page (corners glyph, "Open campaign page") or opens="panel" for a side panel beside the list (panel glyph, "Open call in side panel").',
82
+ },
83
+ {
84
+ name: "table-pagination",
85
+ severity: "error",
86
+ guidance:
87
+ "ADS has no pagination: a table fits its records and scrolls (Table fitContainer, or hasMore + onLoadMore for long lists). Remove the page counter, Previous/Next pager and rows-per-page control.",
88
+ },
89
+ {
90
+ name: "table-record-name-missing",
91
+ severity: "error",
92
+ guidance:
93
+ "This table opens a detail panel, so its record column is <TableRecordName onOpen={…}> — clicking the name opens the panel.",
94
+ },
95
+ ];
96
+
97
+ const RULE = Object.fromEntries(TABLE_RULES.map((r) => [r.name, r]));
98
+
99
+ const BLOCK_TAGS = /^(?:div|p|h[1-6]|ul|ol|li|section|header|footer|dl|table|pre|blockquote)$/;
100
+ const isRow = (cls) => /(?:^|\s)(?:inline-)?flex(?![\w-])/.test(cls) && !/(?:^|\s)flex-col(?![\w-])/.test(cls);
101
+ const isStackClass = (cls) =>
102
+ /(?:^|\s)flex-col(?![\w-])/.test(cls) ||
103
+ /(?:^|\s)space-y-(?!0(?![\w.]))/.test(cls) ||
104
+ (/(?:^|\s)(?:inline-)?grid(?![\w-])/.test(cls) && !/(?:^|\s)(?:grid-cols-|grid-flow-col)/.test(cls));
105
+
106
+ /** Does a child force a new line among its siblings? */
107
+ function isBlock(child) {
108
+ if (child.kind === "element") {
109
+ const cls = classText(child.tag);
110
+ if (/(?:^|\s)(?:inline|inline-block|inline-flex|inline-grid|hidden|sr-only)(?![\w-])/.test(cls)) return false;
111
+ return BLOCK_TAGS.test(child.name) || /(?:^|\s)(?:block|flex|grid)(?![\w-])/.test(cls);
112
+ }
113
+ if (child.kind === "expression") {
114
+ const el = /<([A-Za-z][\w.]*)\b/.exec(child.text);
115
+ if (!el) return false;
116
+ const block = isBlock({ kind: "element", name: el[1], tag: openingTag(child.text, el.index) });
117
+ if (block) child.name = el[1];
118
+ return block;
119
+ }
120
+ return false;
121
+ }
122
+
123
+ /**
124
+ * The first place in a cell body where two values stack: a column/grid/space-y
125
+ * container with two or more children, or two or more children one of which
126
+ * is block-level outside a flex row. `cls` is the container's own classes.
127
+ */
128
+ function stackedIn(body, cls) {
129
+ const kids = jsxChildren(body);
130
+ if (kids.length >= 2) {
131
+ if (isStackClass(cls)) return { at: kids[0].text, why: cls.trim().split(/\s+/).find((c) => /flex-col|space-y|grid/.test(c)) ?? "stack" };
132
+ if (!isRow(cls)) {
133
+ const block = kids.find(isBlock);
134
+ if (block) return { at: block.text, why: `two values, one a block (<${block.name ?? "…"}>)` };
135
+ }
136
+ }
137
+ for (const kid of kids) {
138
+ if (kid.kind !== "element" || !kid.inner) continue;
139
+ const hit = stackedIn(kid.inner, classText(kid.tag));
140
+ if (hit) return hit;
141
+ }
142
+ return null;
143
+ }
144
+
145
+ export function lintTables(relPath, source, cfg) {
146
+ if (cfg.tables?.enabled === false) return [];
147
+ if (matchesAny(relPath, cfg.tables?.allow ?? [])) return [];
148
+ if (!/\.(tsx|jsx)$/.test(relPath)) return [];
149
+ if (!/<Table[\s>]/.test(source)) return [];
150
+
151
+ const findings = [];
152
+ const push = (name, index, match) =>
153
+ findings.push({
154
+ line: source.slice(0, index).split("\n").length,
155
+ rule: name,
156
+ severity: RULE[name].severity,
157
+ match: match.length > 80 ? `${match.slice(0, 77)}...` : match,
158
+ guidance: RULE[name].guidance,
159
+ });
160
+
161
+
162
+ // Header glyphs: any `icon=` other than {null}/{undefined} (a variable counts),
163
+ // or a glyph as the column's first child (<TableColumn><User /> Owner</TableColumn>).
164
+ for (const m of source.matchAll(/<TableColumn\b/g)) {
165
+ const tag = openingTag(source, m.index);
166
+ const icon = topLevelAttr(tag, "icon");
167
+ if (icon !== null && !/^\{\s*(?:null|undefined)\s*\}$/.test(icon)) {
168
+ push("table-column-icon", m.index, tag.replace(/\s+/g, " "));
169
+ continue;
170
+ }
171
+ if (tag.endsWith("/>")) continue;
172
+ const first = /^\s*(?:\{\s*\/\*[\s\S]*?\*\/\s*\}\s*)*<([A-Za-z][\w.]*)\b/.exec(source.slice(m.index + tag.length));
173
+ if (first && (/^[A-Z]/.test(first[1]) || first[1] === "svg") && openingTag(source, m.index + tag.length + first[0].length - first[1].length - 1).endsWith("/>")) {
174
+ push("table-column-icon", m.index, `${tag} <${first[1]} />`.replace(/\s+/g, " "));
175
+ }
176
+ }
177
+
178
+ COLUMN_OPEN.lastIndex = 0;
179
+ for (const m of source.matchAll(COLUMN_OPEN)) {
180
+ const tag = m[0];
181
+ const isActions = /aria-label=["{]?["'`]?Actions/.test(tag) || />\s*Actions\s*</.test(source.slice(m.index, m.index + tag.length + 20));
182
+ if (isActions && !/sticky="right"/.test(tag)) push("table-actions-not-pinned", m.index, tag);
183
+ }
184
+
185
+ // A number followed by the same word in every row: "{n} people", `${n} calls`.
186
+ const UNIT = /<TableCell\b[^>]*>\s*(?:\{[^{}]+\}\s+[A-Za-z]+|\{`\$\{[^}]+\}\s+[A-Za-z]+`\})\s*<\/TableCell>/g;
187
+ // A summary row states one count ("15 leads") — it is not a per-row repeat.
188
+ const rowsOnly = source.replace(/<TableSummary[\s\S]*?<\/TableSummary>/g, (block) => block.replace(/[^\n]/g, " "));
189
+ for (const m of rowsOnly.matchAll(UNIT)) push("table-cell-repeated-unit", m.index, source.slice(m.index, m.index + m[0].length));
190
+
191
+ TABLE_OPEN.lastIndex = 0;
192
+ for (const m of source.matchAll(TABLE_OPEN)) {
193
+ if (/(elevatedActionColumn|stickyActionHeader)=\{false\}/.test(m[0])) push("table-actions-unelevated", m.index, m[0]);
194
+ }
195
+
196
+ // The pinned actions cells.
197
+ const ACTION_CELL = /<TableCell\b(?:=>|[^<>])*?sticky="right"(?:=>|[^<>])*?>([\s\S]*?)<\/TableCell>/g;
198
+ for (const m of source.matchAll(ACTION_CELL)) {
199
+ const body = m[1];
200
+ const bodyAt = m.index + m[0].indexOf(body);
201
+ for (const b of body.matchAll(/<Button\b((?:=>|[^<>])*?)>([\s\S]*?)<\/Button>/g)) {
202
+ const text = b[2].replace(/<[^>]*>/g, "").replace(/\{[^{}]*\}/g, "").trim();
203
+ if (/[A-Za-z]/.test(text)) push("table-action-labelled", bodyAt + b.index, b[0]);
204
+ if (/\bdisabled(?:=\{|\s|>|$)/.test(b[1])) push("table-action-disabled-no-reason", bodyAt + b.index, b[0]);
205
+ }
206
+ for (const h of body.matchAll(/(?:&&\s*\(?\s*<(?:Button|TableRowAction)\b|:\s*null\s*\}|\?\s*null\s*:)/g)) {
207
+ push("table-action-hidden", bodyAt + h.index, h[0]);
208
+ }
209
+ }
210
+
211
+ for (const m of source.matchAll(/<TableRecordName\b(?:=>|[^<>])*?>/g)) {
212
+ if (!/\bonOpen=/.test(m[0])) push("table-record-name-no-open", m.index, m[0]);
213
+ else if (!/\bopens=/.test(m[0])) push("table-record-name-no-destination", m.index, m[0]);
214
+ if (/\bdescription=/.test(m[0])) push("table-cell-stacked", m.index, m[0]);
215
+ }
216
+ // One value per cell: a line break, a vertical stack of two children, two
217
+ // block-level children, or an avatar beside the value is more than one value.
218
+ for (const m of source.matchAll(/<TableCell\b(?:=>|[^<>])*?>((?:(?!<TableCell\b|<\/TableRow\b|<\/Table\b)[\s\S])*?)<\/TableCell>/g)) {
219
+ const bodyAt = m.index + m[0].indexOf(m[1]);
220
+ const avatar = /<Avatar\b/.exec(m[1]);
221
+ if (avatar) {
222
+ push("table-cell-stacked", bodyAt + avatar.index, "<Avatar> in a TableCell");
223
+ continue;
224
+ }
225
+ const br = /<br\s*\/?>/.exec(m[1]);
226
+ if (br) {
227
+ push("table-cell-stacked", bodyAt + br.index, br[0]);
228
+ continue;
229
+ }
230
+ const stack = stackedIn(m[1], "");
231
+ if (stack) push("table-cell-stacked", bodyAt + Math.max(0, m[1].indexOf(stack.at)), stack.why);
232
+ }
233
+
234
+ // Hand-rolled pagination under a table: a page counter, a rows-per-page
235
+ // control, a "Next page"/"Previous page" control, or a Previous + Next pair.
236
+ const firstTable = source.search(/<Table[\s>]/);
237
+ if (firstTable >= 0) {
238
+ const tail = source.slice(firstTable);
239
+ const pager =
240
+ /\bPage\s+(?:\{[^{}]*\}|\d+)\s+of\b/.exec(tail) ??
241
+ /\bRows per page\b/i.exec(tail) ??
242
+ /aria-label=["'`{]*\s*["'`]?(?:Next|Previous|Prev) page\b/i.exec(tail) ??
243
+ (/>\s*(?:Previous|Prev)\s*</.test(tail) && />\s*Next\s*</.test(tail) ? />\s*(?:Previous|Prev)\s*</.exec(tail) : null);
244
+ if (pager) push("table-pagination", firstTable + pager.index, pager[0].trim());
245
+ }
246
+ // A row that opens a detail panel: the name must be the visible way in.
247
+ const opensPanel = /<TableRow\b(?:=>|[^<>])*?\bonClick=/.test(source) && /<(?:Drawer|InlineDrawer)\b/.test(source);
248
+ if (opensPanel && !/<TableRecordName\b/.test(source)) {
249
+ const t = source.search(/<TableRow\b(?:=>|[^<>])*?\bonClick=/);
250
+ push("table-record-name-missing", t, source.slice(t, t + 40));
251
+ }
252
+ return findings;
253
+ }
package/src/runner.mjs CHANGED
@@ -7,8 +7,18 @@ import { matchesAny } from "./config.mjs";
7
7
  import { lintImports } from "./rules/imports.mjs";
8
8
  import { lintMotion } from "./rules/motion.mjs";
9
9
  import { lintLiterals } from "./rules/literals.mjs";
10
+ import { lintModals } from "./rules/modals.mjs";
11
+ import { lintTables } from "./rules/tables.mjs";
12
+ import { lintEmptyStates } from "./rules/empty-states.mjs";
13
+ import { lintButtons } from "./rules/buttons.mjs";
14
+ import { lintCharts } from "./rules/charts.mjs";
15
+ import { lintCollections } from "./rules/collections.mjs";
16
+ import { lintSelection } from "./rules/selection.mjs";
17
+ import { lintElevation } from "./rules/elevation.mjs";
18
+ import { lintControls } from "./rules/controls.mjs";
19
+ import { lintRecipes } from "./rules/recipes.mjs";
10
20
 
11
- const RULE_FNS = [lintImports, lintMotion, lintLiterals];
21
+ const RULE_FNS = [lintImports, lintMotion, lintLiterals, lintModals, lintTables, lintEmptyStates, lintCharts, lintButtons, lintCollections, lintSelection, lintElevation, lintControls, lintRecipes];
12
22
 
13
23
  function toPosix(p) {
14
24
  return p.split(path.sep).join("/");
@@ -0,0 +1,76 @@
1
+ // ads-lint — is this linter as new as the ADS it is checking?
2
+ //
3
+ // ads-lint ships in lockstep with @actioneer/ads: the same version number, the
4
+ // rules for that release. A product that upgrades @actioneer/ads but keeps an
5
+ // older ads-lint runs an old rule set and gets a clean report for patterns the
6
+ // installed ADS forbids. That is how a product on ADS 1.0.60 kept passing a
7
+ // July 0.2.0 linter with 16 of 52 rules (owner-reported 2026-11-29: script,
8
+ // call logs, empty states, table filters and filter chips were missed when ADS
9
+ // was applied). So the run itself reports it, as an error.
10
+
11
+ import fs from "node:fs";
12
+ import path from "node:path";
13
+ import { fileURLToPath } from "node:url";
14
+
15
+ export const VERSION_RULE = {
16
+ name: "ads-lint-stale",
17
+ severity: "error",
18
+ guidance:
19
+ "ads-lint is older than the installed @actioneer/ads, so this run skips every rule added since. Install @actioneer/ads-lint at the same version as @actioneer/ads (they ship together).",
20
+ };
21
+
22
+ /** The version in this package's own package.json. */
23
+ export function ownVersion() {
24
+ const here = path.dirname(fileURLToPath(import.meta.url));
25
+ const pkg = JSON.parse(fs.readFileSync(path.join(here, "..", "package.json"), "utf8"));
26
+ return pkg.version;
27
+ }
28
+
29
+ /** Walk up from `cwd` to find the installed package.json of `name`, or null. */
30
+ export function installedVersion(cwd, name) {
31
+ let dir = path.resolve(cwd);
32
+ for (;;) {
33
+ const candidate = path.join(dir, "node_modules", ...name.split("/"), "package.json");
34
+ if (fs.existsSync(candidate)) {
35
+ try {
36
+ return JSON.parse(fs.readFileSync(candidate, "utf8")).version ?? null;
37
+ } catch {
38
+ return null;
39
+ }
40
+ }
41
+ const parent = path.dirname(dir);
42
+ if (parent === dir) return null;
43
+ dir = parent;
44
+ }
45
+ }
46
+
47
+ /** -1 / 0 / 1 for two "x.y.z" versions; pre-release tags are ignored. */
48
+ export function compareVersions(a, b) {
49
+ const parse = (v) => String(v).split("-")[0].split(".").map((n) => Number.parseInt(n, 10) || 0);
50
+ const [x, y] = [parse(a), parse(b)];
51
+ for (let i = 0; i < 3; i++) {
52
+ if ((x[i] ?? 0) !== (y[i] ?? 0)) return (x[i] ?? 0) < (y[i] ?? 0) ? -1 : 1;
53
+ }
54
+ return 0;
55
+ }
56
+
57
+ /**
58
+ * A finding when the installed ADS is newer than this linter, else null.
59
+ * `adsVersion` and `lintVersion` are injectable for tests.
60
+ */
61
+ export function staleLinterFinding(cwd, cfg, { adsVersion, lintVersion } = {}) {
62
+ if (cfg?.versionCheck === false) return null;
63
+ const pkgName = cfg?.imports?.libraryPackage ?? "@actioneer/ads";
64
+ if (!pkgName.startsWith("@actioneer/")) return null; // library-dev mode: "@/components"
65
+ const ads = adsVersion ?? installedVersion(cwd, pkgName);
66
+ const lint = lintVersion ?? ownVersion();
67
+ if (!ads || !lint || compareVersions(lint, ads) >= 0) return null;
68
+ return {
69
+ file: "package.json",
70
+ line: 1,
71
+ rule: VERSION_RULE.name,
72
+ severity: VERSION_RULE.severity,
73
+ match: `@actioneer/ads-lint ${lint} < ${pkgName} ${ads}`,
74
+ guidance: VERSION_RULE.guidance,
75
+ };
76
+ }