@terpjs/eslint-boundaries 0.30.0 → 0.31.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@terpjs/eslint-boundaries",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "type": "module",
5
5
  "description": "Terp frontend boundary rules (as data) + the ESLint adapter: no cross-module imports, no package internals, design-token-only styling (no style/className/module stylesheets), token-styled components for raw HTML tags, router-only in-app links, generated-client-only, and browser XSS/navigation sink bans. Strict-only (no modes); governed opt-outs via terp-allow markers + the escape-hatch budget ratchet (terp-boundaries-budget).",
6
6
  "main": "./src/index.js",
@@ -22,12 +22,12 @@
22
22
  "eslint": ">=9"
23
23
  },
24
24
  "dependencies": {
25
- "typescript-eslint": "^8.69.0"
25
+ "typescript-eslint": "^8.71.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@terpjs/spec": "0.39.0",
29
29
  "eslint": "^10.11.0",
30
- "vitest": "^5.0.0"
30
+ "vitest": "^5.0.2"
31
31
  },
32
32
  "license": "Apache-2.0",
33
33
  "repository": {
package/src/index.js CHANGED
@@ -17,7 +17,13 @@ import path from "node:path";
17
17
 
18
18
  import tseslint from "typescript-eslint";
19
19
 
20
- import { LAYOUT_CONTRACTS, LAYOUT_CONTRACT_FILE, slotViolationMessage } from "./layouts.js";
20
+ import {
21
+ LAYOUT_CONTRACTS,
22
+ LAYOUT_CONTRACT_FILE,
23
+ headlineViolationMessage,
24
+ slotViolationMessage,
25
+ summaryViolationMessage,
26
+ } from "./layouts.js";
21
27
  import { BOUNDARY_SPEC } from "./spec.js";
22
28
 
23
29
  /** The app-module name a file/import path belongs to (the segment after `modules/`), or null. */
@@ -332,6 +338,7 @@ const UI_TEXT_ATTRIBUTES = new Set([
332
338
  "source",
333
339
  "stat",
334
340
  "subtitle",
341
+ "summary",
335
342
  "title",
336
343
  "tooltip",
337
344
  "triggerLabel",
@@ -881,6 +888,13 @@ export function activeLayoutContract(dir) {
881
888
  * contract allows in that slot. Dynamic children (`{...}` expressions) are deliberately
882
889
  * not resolved here — the react-core runtime half verifies the rendered DOM and refuses
883
890
  * a non-conforming view, fail closed. Both halves phrase the same directive message.
891
+ *
892
+ * Two rules of the page frame ride on the same rule (ADR 0169 §4), on every page element, the
893
+ * plain `Page` included: a `summary` holds only the contract's summary components, and a page
894
+ * element carries at most one headline figure in its static JSX. The count takes the larger
895
+ * branch of a conditional, since only one branch renders, and stops at a nested page element,
896
+ * which is counted on its own. A headline in a component the page renders from elsewhere is
897
+ * out of this file's sight; the runtime half counts the rendered page.
884
898
  */
885
899
  const layoutContract = {
886
900
  meta: {
@@ -914,18 +928,17 @@ const layoutContract = {
914
928
  },
915
929
  };
916
930
  }
917
- const checkChild = (child, slotOwner, allowed) => {
931
+ // `describe` phrases the message for what was found, so a body slot and the summary band
932
+ // share one walk and differ only in their words.
933
+ const checkChild = (child, allowed, describe) => {
918
934
  if (child.type === "JSXText") {
919
935
  if (child.value.trim() !== "") {
920
- context.report({
921
- node: child,
922
- message: slotViolationMessage(contractId, slotOwner, "raw text"),
923
- });
936
+ context.report({ node: child, message: describe("raw text") });
924
937
  }
925
938
  return;
926
939
  }
927
940
  if (child.type === "JSXFragment") {
928
- child.children.forEach((inner) => checkChild(inner, slotOwner, allowed));
941
+ child.children.forEach((inner) => checkChild(inner, allowed, describe));
929
942
  return;
930
943
  }
931
944
  if (child.type !== "JSXElement") {
@@ -933,20 +946,105 @@ const layoutContract = {
933
946
  }
934
947
  const name = jsxName(child.openingElement.name);
935
948
  if (name !== null && allowed[name] === undefined) {
936
- context.report({
937
- node: child.openingElement,
938
- message: slotViolationMessage(contractId, slotOwner, `<${name}>`),
939
- });
949
+ context.report({ node: child.openingElement, message: describe(`<${name}>`) });
950
+ }
951
+ };
952
+ // Every page element: the governed archetypes, and the plain Page, whose body is free and
953
+ // whose frame is not.
954
+ const pages = new Set([...Object.keys(contract.slots), "Page"]);
955
+ const checkSummary = (openingElement) => {
956
+ const attribute = getJsxAttribute(openingElement, "summary");
957
+ if (attribute === undefined || attribute.value === null) {
958
+ return;
959
+ }
960
+ const describe = (found) => summaryViolationMessage(contractId, found);
961
+ const value =
962
+ attribute.value.type === "JSXExpressionContainer"
963
+ ? attribute.value.expression
964
+ : attribute.value;
965
+ // `summary={null}` and `summary={false}` are the band left out, as the runtime renders
966
+ // them; only a string is text.
967
+ if (
968
+ value.type === "TemplateLiteral" ||
969
+ (value.type === "Literal" && typeof value.value === "string" && value.value.trim() !== "")
970
+ ) {
971
+ context.report({ node: attribute, message: describe("raw text") });
972
+ return;
973
+ }
974
+ if (value.type === "JSXElement" || value.type === "JSXFragment") {
975
+ checkChild(value, contract.summary.components, describe);
976
+ }
977
+ };
978
+ const isHeadline = (node) => {
979
+ if (node.type !== "JSXElement") {
980
+ return false;
981
+ }
982
+ const name = jsxName(node.openingElement.name);
983
+ if (name === null || contract.headline.components[name] === undefined) {
984
+ return false;
940
985
  }
986
+ const attribute = getJsxAttribute(node.openingElement, "headline");
987
+ if (attribute === undefined) {
988
+ return false;
989
+ }
990
+ // Counted only where the file itself says yes: a bare `headline` or `headline={true}`.
991
+ // An expression -- `headline={isMain}` beside `headline={!isMain}` -- is decided at run
992
+ // time, and the page's own count holds it there; counting it here refused code the
993
+ // runtime accepts, and left the author an escape marker as the only way through.
994
+ const value = attribute.value;
995
+ return (
996
+ value === null ||
997
+ (value.type === "JSXExpressionContainer" &&
998
+ value.expression.type === "Literal" &&
999
+ value.expression.value === true)
1000
+ );
1001
+ };
1002
+ const visitorKeys = context.sourceCode.visitorKeys;
1003
+ // The most headline figures that can render at once under `node`.
1004
+ const headlines = (node, page) => {
1005
+ if (node === null || typeof node !== "object" || typeof node.type !== "string") {
1006
+ return 0;
1007
+ }
1008
+ if (node !== page && node.type === "JSXElement" && pages.has(jsxName(node.openingElement.name))) {
1009
+ return 0;
1010
+ }
1011
+ if (node.type === "ConditionalExpression") {
1012
+ return Math.max(headlines(node.consequent, page), headlines(node.alternate, page));
1013
+ }
1014
+ let total = isHeadline(node) ? 1 : 0;
1015
+ for (const key of visitorKeys[node.type] ?? []) {
1016
+ const child = node[key];
1017
+ for (const inner of Array.isArray(child) ? child : [child]) {
1018
+ total += headlines(inner, page);
1019
+ }
1020
+ }
1021
+ return total;
941
1022
  };
942
1023
  return {
943
1024
  JSXElement(node) {
944
1025
  const owner = jsxName(node.openingElement.name);
945
- const slot = owner !== null ? contract.slots[owner] : undefined;
1026
+ if (owner === null) {
1027
+ return;
1028
+ }
1029
+ if (pages.has(owner)) {
1030
+ checkSummary(node.openingElement);
1031
+ const count = headlines(node, node);
1032
+ if (count > 1) {
1033
+ context.report({
1034
+ node: node.openingElement,
1035
+ message: headlineViolationMessage(contractId, `${count} figures marked headline`),
1036
+ });
1037
+ }
1038
+ }
1039
+ const slot = contract.slots[owner];
946
1040
  if (slot === undefined) {
947
1041
  return;
948
1042
  }
949
- node.children.forEach((child) => checkChild(child, owner, slot.components));
1043
+ node.children.forEach((child) =>
1044
+ checkChild(child, slot.components, (found) =>
1045
+ slotViolationMessage(contractId, owner, found),
1046
+ ),
1047
+ );
950
1048
  },
951
1049
  };
952
1050
  },
package/src/layouts.js CHANGED
@@ -42,9 +42,15 @@ export const LAYOUT_CONTRACTS = {
42
42
  "beside those: a form body is a container (Stack) with optional Grid / Card " +
43
43
  "sections and no Field at the top level, so the body is always a container rather " +
44
44
  "than a loose run of controls; a settings body is Card sections and holds no " +
45
- "collection; and a split " +
46
- "body is two SplitPanes and nothing else. A screen that needs no contract " +
47
- "composes the plain Page, which this contract deliberately leaves unconstrained.",
45
+ "collection; a split " +
46
+ "body is two SplitPanes and nothing else; and a dashboard body is its figures and " +
47
+ "charts in Grid sections, the collection they summarise and the framework states. " +
48
+ "A screen that needs no contract " +
49
+ "composes the plain Page, whose body this contract deliberately leaves unconstrained. " +
50
+ "Every page, the plain one included, keeps two rules of its frame: its summary band " +
51
+ "holds the page's own figures (Stat / StatGroup / StatusHistory / Badge / Text), and " +
52
+ "at most one " +
53
+ "figure on the page is its headline.",
48
54
  slots: {
49
55
  HubPage: {
50
56
  components: { HubCard: "hubcard" },
@@ -95,6 +101,28 @@ export const LAYOUT_CONTRACTS = {
95
101
  SplitPage: {
96
102
  components: { SplitPane: "splitpane" },
97
103
  },
104
+ DashboardPage: {
105
+ components: {
106
+ Grid: "grid",
107
+ Stack: "stack",
108
+ Card: "card",
109
+ DataView: "dataview",
110
+ Stat: "stat",
111
+ StatGroup: "stat-group",
112
+ TrendChart: "trend-chart",
113
+ BarChart: "bar-chart",
114
+ ProportionBar: "proportion-bar",
115
+ StatusHistory: "status-history",
116
+ Timeline: "timeline",
117
+ Divider: "divider",
118
+ Text: "text",
119
+ EmptyState: "empty-state",
120
+ ErrorState: "error-state",
121
+ LoadingState: "loading-state",
122
+ Alert: "alert",
123
+ ConfirmDialog: "dialog",
124
+ },
125
+ },
98
126
  DetailPage: {
99
127
  components: {
100
128
  DetailList: "detail-list",
@@ -115,6 +143,23 @@ export const LAYOUT_CONTRACTS = {
115
143
  },
116
144
  },
117
145
  },
146
+ // The band a page renders under its title band when it is given a summary (ADR 0169 §4):
147
+ // the page's own figures. Governed on every page, the plain Page included, because the
148
+ // band belongs to the frame and not to a body.
149
+ summary: {
150
+ components: {
151
+ Stat: "stat",
152
+ StatGroup: "stat-group",
153
+ StatusHistory: "status-history",
154
+ Badge: "badge",
155
+ Text: "text",
156
+ },
157
+ },
158
+ // The components whose `headline` prop (rendered as `data-headline`) marks the page's
159
+ // headline figure, of which a page carries at most one (ADR 0169 §4).
160
+ headline: {
161
+ components: { Stat: "stat" },
162
+ },
118
163
  },
119
164
  };
120
165
 
@@ -133,3 +178,32 @@ export function slotViolationMessage(contractId, slotOwner, found) {
133
178
  "marker (counted by the escape-hatch budget)."
134
179
  );
135
180
  }
181
+
182
+ /**
183
+ * The directive message for a page's summary band (ADR 0169 §4): what it admits, what was
184
+ * found, and the fix.
185
+ */
186
+ export function summaryViolationMessage(contractId, found) {
187
+ const allowed = Object.keys(LAYOUT_CONTRACTS[contractId].summary.components);
188
+ return (
189
+ `Layout contract "${contractId}": a page's summary band accepts only ` +
190
+ `${allowed.join(" / ")}; found ${found}. The band carries the page's own figures, so ` +
191
+ "move anything else into the body (recipe: terp guide layouts), " +
192
+ "or opt out on this line with a justified // terp-allow-layout-contract: <reason> " +
193
+ "marker (counted by the escape-hatch budget)."
194
+ );
195
+ }
196
+
197
+ /**
198
+ * The directive message for a page that carries more than one headline figure (ADR 0169 §4).
199
+ * Scarcity is the rule: an accent means something only while one figure has it.
200
+ */
201
+ export function headlineViolationMessage(contractId, found) {
202
+ return (
203
+ `Layout contract "${contractId}": a page carries at most one headline figure; found ` +
204
+ `${found}. Keep headline on the one figure the page is about and render the others as ` +
205
+ "ordinary figures (recipe: terp guide layouts), " +
206
+ "or opt out on this line with a justified // terp-allow-layout-contract: <reason> " +
207
+ "marker (counted by the escape-hatch budget)."
208
+ );
209
+ }
@@ -4,7 +4,12 @@ import { ESLint } from "eslint";
4
4
  import { describe, expect, it } from "vitest";
5
5
 
6
6
  import terpBoundaries from "./index.js";
7
- import { LAYOUT_CONTRACTS, slotViolationMessage } from "./layouts.js";
7
+ import {
8
+ LAYOUT_CONTRACTS,
9
+ headlineViolationMessage,
10
+ slotViolationMessage,
11
+ summaryViolationMessage,
12
+ } from "./layouts.js";
8
13
 
9
14
  // The build-time half of the slot-typed layout contract control (ADR 0079): prove the
10
15
  // `terp/layout-contract` rule fires on a non-conforming slot child, stays quiet on
@@ -104,3 +109,150 @@ describe("terp/layout-contract", () => {
104
109
  }
105
110
  });
106
111
  });
112
+
113
+ describe("terp/layout-contract — the page frame's two rules (ADR 0169 §4)", () => {
114
+ const imports =
115
+ 'import { Badge, Card, DataView, DetailPage, OverviewPage, Page, Stack, Stat, StatGroup, Text } from "@terpjs/react-core";\n';
116
+
117
+ it("governs a dashboard's body: figures and charts pass, a bare element is refused", async () => {
118
+ const code =
119
+ 'import { BarChart, DashboardPage, Grid, Stat, StatusHistory, TrendChart } from "@terpjs/react-core";\n' +
120
+ "export const D = ({title, points, runs}) => <DashboardPage title={title}><Grid template=\"2:1\"><TrendChart label={title} series={points} /><BarChart label={title} bars={[]} /></Grid><Stat label={title} value={1} /><StatusHistory label={title} runs={runs} /></DashboardPage>;\n" +
121
+ "export const E = ({title}) => <DashboardPage title={title}><div /></DashboardPage>;";
122
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([
123
+ slotViolationMessage("standard", "DashboardPage", "<div>"),
124
+ ]);
125
+ });
126
+
127
+ it("passes a status history in the summary", async () => {
128
+ const code =
129
+ 'import { Page, StatusHistory } from "@terpjs/react-core";\n' +
130
+ "export const P = ({title, runs}) => <Page title={title} summary={<StatusHistory label={title} runs={runs} />}><div /></Page>;";
131
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
132
+ });
133
+
134
+ it("passes a summary of the page's own figures, on an archetype and on the plain Page", async () => {
135
+ const code =
136
+ imports +
137
+ "export const O = ({title, n}) => <OverviewPage title={title} summary={<StatGroup><Stat label={title} value={n} /></StatGroup>}><DataView /></OverviewPage>;\n" +
138
+ "export const D = ({title, n}) => <DetailPage title={title} parents={[]} summary={<><Stat label={title} value={n} /><Badge label={title} /><Text>{title}</Text></>}><Card title={title} /></DetailPage>;\n" +
139
+ "export const P = ({title, n, figures}) => <Page title={title} summary={figures}><div /></Page>;";
140
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
141
+ });
142
+
143
+ it("refuses anything else in a summary, on the plain Page too, with the directive message", async () => {
144
+ // Mutation: drop checkSummary from the visitor, and both pages lint clean.
145
+ const code =
146
+ imports +
147
+ "export const O = ({title}) => <OverviewPage title={title} summary={<DataView />}><DataView /></OverviewPage>;\n" +
148
+ "export const P = ({title}) => <Page title={title} summary={<><Stat label={title} value={1} /><Card title={title} /></>}><div /></Page>;";
149
+ const messages = (await lint(code, configWithContract("standard"))).map((m) => m.message);
150
+ expect(messages).toEqual([
151
+ summaryViolationMessage("standard", "<DataView>"),
152
+ summaryViolationMessage("standard", "<Card>"),
153
+ ]);
154
+ });
155
+
156
+ it("refuses a summary that is a bare string or a template literal", async () => {
157
+ const code =
158
+ imports +
159
+ 'export const A = ({title}) => <Page title={title} summary="Twelve open">{title}</Page>;\n' +
160
+ "export const B = ({title, n}) => <Page title={title} summary={`${n} open`}>{title}</Page>;";
161
+ const messages = await lint(code, configWithContract("standard"));
162
+ expect(
163
+ messages.filter((m) => m.ruleId === "terp/layout-contract").map((m) => m.message),
164
+ ).toEqual([
165
+ summaryViolationMessage("standard", "raw text"),
166
+ summaryViolationMessage("standard", "raw text"),
167
+ ]);
168
+ });
169
+
170
+ it("reads a bare-string summary as copy to translate, with or without a contract", async () => {
171
+ // The band takes rendered nodes, as `actions` does, so a string written there is copy the
172
+ // app's catalog never sees. Mutation: drop "summary" from UI_TEXT_ATTRIBUTES, and an app
173
+ // with no contract ships it untranslated.
174
+ const code =
175
+ imports + 'export const A = ({title}) => <Page title={title} summary="Twelve open">{title}</Page>;';
176
+ expect((await lint(code)).map((m) => m.ruleId)).toEqual(["terp/no-untranslated-ui"]);
177
+ });
178
+
179
+ it("refuses a page that carries two headline figures, wherever in its JSX they sit", async () => {
180
+ // One in the summary and one in the body is still two on the page.
181
+ // Mutation: report only when the count exceeds 2, and this lints clean.
182
+ const code =
183
+ imports +
184
+ "export const D = ({title}) => <DetailPage title={title} parents={[]} summary={<Stat headline label={title} value={1} />}><Stack><Stat headline label={title} value={2} /></Stack></DetailPage>;";
185
+ const messages = (await lint(code, configWithContract("standard"))).map((m) => m.message);
186
+ expect(messages).toEqual([headlineViolationMessage("standard", "2 figures marked headline")]);
187
+ });
188
+
189
+ it("counts one headline per page, so a page with one passes and headline={false} is not one", async () => {
190
+ const code =
191
+ imports +
192
+ "export const D = ({title}) => <DetailPage title={title} parents={[]} summary={<StatGroup><Stat headline label={title} value={1} /><Stat headline={false} label={title} value={2} /></StatGroup>}><Card title={title} /></DetailPage>;";
193
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
194
+ });
195
+
196
+ it("counts the larger branch of a conditional, since only one branch renders", async () => {
197
+ // Mutation: sum both branches, and this exclusive pair is refused.
198
+ const exclusive =
199
+ imports +
200
+ "export const P = ({title, a}) => <Page title={title} summary={a ? <Stat headline label={title} value={1} /> : <Stat headline label={title} value={2} />}><div /></Page>;";
201
+ expect((await lint(exclusive, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
202
+ const both =
203
+ imports +
204
+ "export const P = ({title, a}) => <Page title={title} summary={<>{a && <Stat headline label={title} value={1} />}<Stat headline label={title} value={2} /></>}><div /></Page>;";
205
+ expect((await lint(both, configWithContract("standard"))).map((m) => m.message)).toEqual([
206
+ headlineViolationMessage("standard", "2 figures marked headline"),
207
+ ]);
208
+ });
209
+
210
+ it("reads summary={null} and summary={false} as the band left out", async () => {
211
+ // Mutation: report any Literal as raw text again, and both are refused.
212
+ const code =
213
+ imports +
214
+ "export const A = ({title}) => <Page title={title} summary={null}><div /></Page>;\n" +
215
+ "export const B = ({title}) => <Page title={title} summary={false}><div /></Page>;";
216
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
217
+ });
218
+
219
+ it("leaves a headline decided by an expression to the runtime's count", async () => {
220
+ // `headline={isMain}` beside `headline={!isMain}` is one headline at a time, which no
221
+ // static count can see. Mutation: count expression values again, and this is refused.
222
+ const code =
223
+ imports +
224
+ "export const P = ({title, isMain}) => <Page title={title}><Stat headline={isMain} label={title} value={1} /><Stat headline={!isMain} label={title} value={2} /></Page>;";
225
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
226
+ const literal =
227
+ imports +
228
+ "export const P = ({title}) => <Page title={title}><Stat headline={true} label={title} value={1} /><Stat headline label={title} value={2} /></Page>;";
229
+ expect((await lint(literal, configWithContract("standard"))).map((m) => m.message)).toEqual([
230
+ headlineViolationMessage("standard", "2 figures marked headline"),
231
+ ]);
232
+ });
233
+
234
+ it("counts a nested page on its own rather than twice", async () => {
235
+ const code =
236
+ imports +
237
+ "export const P = ({title}) => <Page title={title}><Page title={title} summary={<Stat headline label={title} value={1} />}><div /></Page><Stat headline label={title} value={2} /></Page>;";
238
+ expect((await lint(code, configWithContract("standard"))).map((m) => m.message)).toEqual([]);
239
+ });
240
+
241
+ it("is inert without an opted-in contract", async () => {
242
+ const code =
243
+ imports +
244
+ "export const P = ({title}) => <Page title={title} summary={<DataView />}><Stat headline label={title} value={1} /><Stat headline label={title} value={2} /></Page>;";
245
+ expect((await lint(code)).map((m) => m.ruleId)).toEqual([]);
246
+ });
247
+
248
+ it("declares a marker-named runtime marker for every summary and headline component (data sanity)", () => {
249
+ for (const contract of Object.values(LAYOUT_CONTRACTS)) {
250
+ for (const table of [contract.summary.components, contract.headline.components]) {
251
+ for (const [name, marker] of Object.entries(table)) {
252
+ expect(name).toMatch(/^[A-Z]/);
253
+ expect(marker).toMatch(/^[a-z][a-z-]*$/);
254
+ }
255
+ }
256
+ }
257
+ });
258
+ });