@terpjs/eslint-boundaries 0.25.0 → 0.28.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 +3 -3
- package/src/i18n.test.js +84 -2
- package/src/index.js +157 -30
- package/src/markers.test.js +128 -0
- package/src/spec.js +10 -5
- package/src/surface.test.js +41 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terpjs/eslint-boundaries",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.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",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"typescript-eslint": "^8.69.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@terpjs/spec": "0.
|
|
29
|
-
"eslint": "^10.
|
|
28
|
+
"@terpjs/spec": "0.38.0",
|
|
29
|
+
"eslint": "^10.11.0",
|
|
30
30
|
"vitest": "^5.0.0"
|
|
31
31
|
},
|
|
32
32
|
"license": "Apache-2.0",
|
package/src/i18n.test.js
CHANGED
|
@@ -170,11 +170,57 @@ describe("locale catalog completeness", () => {
|
|
|
170
170
|
});
|
|
171
171
|
|
|
172
172
|
it("does not mistake dynamic business records for UiText descriptors", async () => {
|
|
173
|
+
// A record keeps its id/message shape when it is built from the record: from a member
|
|
174
|
+
// of it, or from a destructured object. Neither is the caller's literal under another
|
|
175
|
+
// name, so neither hides copy from the catalog.
|
|
176
|
+
for (const record of [
|
|
177
|
+
"export const toRecord = (record) => ({ id: record.id, message: record.text });",
|
|
178
|
+
"export const toRecords = (rows) => rows.map(({ id, message }) => ({ id, message }));",
|
|
179
|
+
"export const fromApi = async (fetchOne) => { const { id, message } = await fetchOne(); return { id, message }; };",
|
|
180
|
+
// One parameter is a key into data, not a descriptor's worth of caller literals.
|
|
181
|
+
"export const labelled = (ids, labels) => ids.map((id) => ({ id, message: labels[id] }));",
|
|
182
|
+
"export const lookedUp = (id, labels) => { const message = labels[id]; return { id, message }; };",
|
|
183
|
+
]) {
|
|
184
|
+
const messages = await lintWithCatalog(
|
|
185
|
+
{ sourceLocale: "en", locales: { en: {}, nl: {} } },
|
|
186
|
+
record,
|
|
187
|
+
);
|
|
188
|
+
expect(messages, record).toEqual([]);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it.each([
|
|
193
|
+
["an arrow factory", "export const msg = (id, message) => ({ id, message });"],
|
|
194
|
+
["typed parameters", "export const msg = (id: string, message: string) => ({ id, message });"],
|
|
195
|
+
["a declared function", "export function msg(id, message) { return { id: id, message: message }; }"],
|
|
196
|
+
["a function expression", "export const msg = function (id, message) { return { message, id }; };"],
|
|
197
|
+
["a defaulted parameter", 'export const msg = (id, message = "") => ({ id, message } as const);'],
|
|
198
|
+
])("refuses a descriptor factory written as %s", async (_label, factory) => {
|
|
173
199
|
const messages = await lintWithCatalog(
|
|
174
200
|
{ sourceLocale: "en", locales: { en: {}, nl: {} } },
|
|
175
|
-
|
|
201
|
+
`${factory}\nexport const W = () => <Page title={msg("widgets.title", "Widgets")} />;`,
|
|
176
202
|
);
|
|
177
|
-
|
|
203
|
+
const findings = messages.filter(
|
|
204
|
+
(message) => message.ruleId === "terp/locale-catalogs-complete",
|
|
205
|
+
);
|
|
206
|
+
expect(findings).toHaveLength(1);
|
|
207
|
+
expect(findings[0].message).toContain("built from a function's parameters");
|
|
208
|
+
expect(findings[0].message).toContain("record.id");
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("keeps a helper that takes one descriptor checkable at its call site", async () => {
|
|
212
|
+
const messages = await lintWithCatalog(
|
|
213
|
+
{ sourceLocale: "en", locales: { en: {}, nl: {} } },
|
|
214
|
+
[
|
|
215
|
+
"const withTitle = (descriptor) => ({ title: descriptor });",
|
|
216
|
+
'export const props = withTitle({ id: "widgets.title", message: "Widgets" });',
|
|
217
|
+
].join("\n"),
|
|
218
|
+
);
|
|
219
|
+
// Not refused as a factory, and the literal at the call site is still inventoried:
|
|
220
|
+
// the Dutch entry is missing, and that is what gets reported.
|
|
221
|
+
expect(messages.map((message) => message.message)).toEqual([
|
|
222
|
+
'Translation "widgets.title" is incomplete in frontend/i18n.json (nl: missing).',
|
|
223
|
+
]);
|
|
178
224
|
});
|
|
179
225
|
|
|
180
226
|
it("honours the catalog-derived governed marker for an intentional exception", async () => {
|
|
@@ -212,6 +258,42 @@ describe("untranslated UI coverage", () => {
|
|
|
212
258
|
expect(messages.map((message) => message.ruleId)).toContain("terp/no-untranslated-ui");
|
|
213
259
|
});
|
|
214
260
|
|
|
261
|
+
it("refuses a framework-string reference in app source, and asks no catalog entry for it", async () => {
|
|
262
|
+
// `{ framework: key }` names react-core's own table, for the manifests react-core ships;
|
|
263
|
+
// what it renders is framework copy, which LocaleProvider already requires of every
|
|
264
|
+
// non-English catalog. An app's nav entry is the app's copy and keeps its own descriptor,
|
|
265
|
+
// so here the key reads as a literal under `label`, which is what it is to this rule. The
|
|
266
|
+
// catalog rule stays silent: there is no app id to look up, so nothing to demand.
|
|
267
|
+
const messages = await lintWithCatalog(
|
|
268
|
+
declaration,
|
|
269
|
+
'export const nav = [{ label: { framework: "admin" }, to: "/admin" }];',
|
|
270
|
+
);
|
|
271
|
+
expect(messages.map((message) => message.ruleId)).toEqual(["terp/no-untranslated-ui"]);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it.each([
|
|
275
|
+
['<Grid columns="auto" />', "a layout keyword on Grid"],
|
|
276
|
+
['<DetailList columns="auto" />', "a layout keyword on DetailList"],
|
|
277
|
+
['<DataView columns={[]} loading="eager" empty="none" selected="first" />', "non-text props"],
|
|
278
|
+
['<img loading="lazy" alt={{ id: "w.logo", message: "Logo" }} />', "an image's loading hint"],
|
|
279
|
+
])("does not read %s as copy (%s)", async (jsx) => {
|
|
280
|
+
const messages = await lintWithCatalog(
|
|
281
|
+
{ sourceLocale: "en", locales: { en: {}, nl: { messages: { "w.logo": "Beeldmerk" } } } },
|
|
282
|
+
`export const W = () => ${jsx};`,
|
|
283
|
+
);
|
|
284
|
+
expect(messages.filter((message) => message.ruleId === "terp/no-untranslated-ui"))
|
|
285
|
+
.toEqual([]);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("still reads DataView's own keys as copy inside its strings object", async () => {
|
|
289
|
+
const messages = await lintWithCatalog(
|
|
290
|
+
declaration,
|
|
291
|
+
'export const W = () => <DataView strings={{ columns: "Kolommen", pageOf: "Pagina {page}" }} />;',
|
|
292
|
+
);
|
|
293
|
+
expect(messages.filter((message) => message.ruleId === "terp/no-untranslated-ui"))
|
|
294
|
+
.toHaveLength(2);
|
|
295
|
+
});
|
|
296
|
+
|
|
215
297
|
it("covers UiText-bearing data properties beyond the original short list", async () => {
|
|
216
298
|
const messages = await lintWithCatalog(
|
|
217
299
|
declaration,
|
package/src/index.js
CHANGED
|
@@ -295,6 +295,17 @@ const noEval = {
|
|
|
295
295
|
},
|
|
296
296
|
};
|
|
297
297
|
|
|
298
|
+
/**
|
|
299
|
+
* JSX attributes that carry user-facing text on some component.
|
|
300
|
+
*
|
|
301
|
+
* DataView's own string keys (`columns`, `loading`, `pageOf`, …) are not here. They are text
|
|
302
|
+
* only as keys of a DataView `strings` object, which {@link UI_TEXT_PROPERTIES} covers; as a
|
|
303
|
+
* JSX attribute the same name belongs to a prop that is not text at all — `<Grid
|
|
304
|
+
* columns="auto">`, `<DetailList columns="auto">`, `<img loading="lazy">` — and reporting
|
|
305
|
+
* those left an app only an escape marker to get past a layout keyword. No react-core component declares any of
|
|
306
|
+
* them as a text prop; `searchPlaceholder` (DataView's own) and `actions` (a rendered
|
|
307
|
+
* `ReactNode` slot) do, and stay.
|
|
308
|
+
*/
|
|
298
309
|
const UI_TEXT_ATTRIBUTES = new Set([
|
|
299
310
|
"actions",
|
|
300
311
|
"aria-description",
|
|
@@ -304,55 +315,26 @@ const UI_TEXT_ATTRIBUTES = new Set([
|
|
|
304
315
|
"ariaLabel",
|
|
305
316
|
"broadenedLabel",
|
|
306
317
|
"cancelLabel",
|
|
307
|
-
"cardView",
|
|
308
318
|
"caption",
|
|
309
|
-
"clearFilters",
|
|
310
|
-
"clearSearch",
|
|
311
|
-
"clearSelection",
|
|
312
|
-
"collapseRow",
|
|
313
|
-
"columns",
|
|
314
319
|
"confirmLabel",
|
|
315
320
|
"content",
|
|
316
321
|
"createPlaceholder",
|
|
317
322
|
"description",
|
|
318
|
-
"empty",
|
|
319
323
|
"emptyMessage",
|
|
320
|
-
"errorTitle",
|
|
321
|
-
"expandRow",
|
|
322
|
-
"firstPage",
|
|
323
324
|
"header",
|
|
324
325
|
"hint",
|
|
325
326
|
"label",
|
|
326
|
-
"lastPage",
|
|
327
|
-
"loading",
|
|
328
327
|
"loadingText",
|
|
329
|
-
"moreActions",
|
|
330
|
-
"moveDown",
|
|
331
|
-
"moveUp",
|
|
332
|
-
"nextPage",
|
|
333
328
|
"noOptionsText",
|
|
334
|
-
"openRow",
|
|
335
|
-
"pageOf",
|
|
336
|
-
"pageSize",
|
|
337
329
|
"placeholder",
|
|
338
|
-
"previousPage",
|
|
339
|
-
"refreshing",
|
|
340
330
|
"removeLabel",
|
|
341
|
-
"resizeColumn",
|
|
342
|
-
"resultsRange",
|
|
343
331
|
"searchPlaceholder",
|
|
344
|
-
"selectAllPage",
|
|
345
|
-
"selectAllResults",
|
|
346
|
-
"selected",
|
|
347
|
-
"selectRow",
|
|
348
332
|
"source",
|
|
349
333
|
"stat",
|
|
350
334
|
"subtitle",
|
|
351
|
-
"tableView",
|
|
352
335
|
"title",
|
|
353
336
|
"tooltip",
|
|
354
337
|
"triggerLabel",
|
|
355
|
-
"viewOptions",
|
|
356
338
|
]);
|
|
357
339
|
const UI_TEXT_PROPERTIES = new Set([
|
|
358
340
|
"actions",
|
|
@@ -686,6 +668,58 @@ function objectStringProperty(node, name) {
|
|
|
686
668
|
return property ? staticString(property.value) : null;
|
|
687
669
|
}
|
|
688
670
|
|
|
671
|
+
/** The parameter names of the function nearest *node*, when they are plain identifiers. */
|
|
672
|
+
function enclosingParameterNames(node) {
|
|
673
|
+
let current = node.parent;
|
|
674
|
+
while (current) {
|
|
675
|
+
if (
|
|
676
|
+
current.type === "ArrowFunctionExpression" ||
|
|
677
|
+
current.type === "FunctionExpression" ||
|
|
678
|
+
current.type === "FunctionDeclaration"
|
|
679
|
+
) {
|
|
680
|
+
return new Set(
|
|
681
|
+
current.params
|
|
682
|
+
.map((param) => (param.type === "AssignmentPattern" ? param.left : param))
|
|
683
|
+
.filter((param) => param.type === "Identifier")
|
|
684
|
+
.map((param) => param.name),
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
current = current.parent;
|
|
688
|
+
}
|
|
689
|
+
return new Set();
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Whether *node* is `{ id, message }` built from the enclosing function's own parameters.
|
|
694
|
+
*
|
|
695
|
+
* `(id, message) => ({ id, message })` is a descriptor factory: every call through it —
|
|
696
|
+
* `title={msg("widgets.title", "Widgets")}` — carries its copy in a call's arguments, where
|
|
697
|
+
* the catalog inventory never looks, so a target locale can be missing the entry and the
|
|
698
|
+
* gate stays green. A business record keeps its shape without this form (`(record) => ({
|
|
699
|
+
* id: record.id, message: record.text })`), and a helper that takes one object keeps a
|
|
700
|
+
* literal descriptor at every call site, so only the positional factory is refused.
|
|
701
|
+
*/
|
|
702
|
+
function isDescriptorFactory(node) {
|
|
703
|
+
const valueOf = (name) =>
|
|
704
|
+
unwrapExpression(
|
|
705
|
+
node.properties.find(
|
|
706
|
+
(entry) => entry.type === "Property" && propertyName(entry) === name,
|
|
707
|
+
)?.value,
|
|
708
|
+
);
|
|
709
|
+
const id = valueOf("id");
|
|
710
|
+
const message = valueOf("message");
|
|
711
|
+
if (id?.type !== "Identifier" || message?.type !== "Identifier") return false;
|
|
712
|
+
const parameters = enclosingParameterNames(node);
|
|
713
|
+
return parameters.has(id.name) && parameters.has(message.name);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const descriptorFactoryMessage =
|
|
717
|
+
"A UiText descriptor built from a function's parameters hides every call through it from " +
|
|
718
|
+
"frontend/i18n.json, so a missing translation passes the gate. Write the descriptor where " +
|
|
719
|
+
'the text is used — { id: "…", message: "…" } or <Trans id=… message=… /> — and drop the ' +
|
|
720
|
+
"helper. If this object is business data rather than UI copy, build it from the record " +
|
|
721
|
+
"(record.id, record.text) instead of from bare parameters.";
|
|
722
|
+
|
|
689
723
|
function transDescriptor(node) {
|
|
690
724
|
if (jsxName(node.name) !== "Trans") return null;
|
|
691
725
|
const id = staticStringFromJsxValue(getJsxAttribute(node, "id")?.value);
|
|
@@ -767,12 +801,17 @@ const localeCatalogsComplete = {
|
|
|
767
801
|
(entry) => entry.type === "Property" && propertyName(entry) === "message",
|
|
768
802
|
);
|
|
769
803
|
if (!hasId || !hasMessage) return;
|
|
804
|
+
if (isDescriptorFactory(node)) {
|
|
805
|
+
context.report({ node, message: descriptorFactoryMessage });
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
770
808
|
const id = objectStringProperty(node, "id");
|
|
771
809
|
const message = objectStringProperty(node, "message");
|
|
772
810
|
// The id/message pair is also a common dynamic business-data shape. With
|
|
773
811
|
// no authored literal there is nothing for the static catalog to inventory;
|
|
774
812
|
// if such a value is later used as UiText, LocaleProvider is the runtime
|
|
775
|
-
// backstop.
|
|
813
|
+
// backstop. The one dynamic shape refused above is the positional factory,
|
|
814
|
+
// whose values are the caller's literals under another name. One static half does identify a malformed descriptor and must
|
|
776
815
|
// not let its authored copy escape the catalog.
|
|
777
816
|
if (id === null && message === null) return;
|
|
778
817
|
if (id === null || message === null) {
|
|
@@ -913,6 +952,90 @@ const layoutContract = {
|
|
|
913
952
|
},
|
|
914
953
|
};
|
|
915
954
|
|
|
955
|
+
/**
|
|
956
|
+
* react-core's identity markers: `data-terp` and every `data-terp-*` attribute (ADR 0160).
|
|
957
|
+
*
|
|
958
|
+
* react-core's components stamp `data-terp` on the elements they render, and two controls
|
|
959
|
+
* trust the stamp: the stylesheet selects on it (`[data-terp="card"]`, ADR 0094), and the
|
|
960
|
+
* runtime half of the layout contract identifies a slot's children by it (ADR 0079). So
|
|
961
|
+
* `data-terp="card"` written by an app borrows Card's styling without composing Card, which
|
|
962
|
+
* gets around the `style` / `className` refusal (ADR 0059), and it passes the runtime slot
|
|
963
|
+
* check as a component it is not. The rest of the namespace (`data-terp-preview-pick`) is
|
|
964
|
+
* react-core's too.
|
|
965
|
+
*
|
|
966
|
+
* Case-insensitive because the DOM lowercases an attribute name written on an HTML element:
|
|
967
|
+
* measured in Chromium, `setAttribute("DATA-TERP", "card")` leaves `data-terp="card"`, which
|
|
968
|
+
* `[data-terp="card"]` matches.
|
|
969
|
+
*/
|
|
970
|
+
const FRAMEWORK_MARKER_RE = /^data-terp(?:-|$)/i;
|
|
971
|
+
/** The same namespace as `dataset` keys: `dataset.terp`, `dataset.terpPreviewPick`. */
|
|
972
|
+
const FRAMEWORK_DATASET_KEY_RE = /^terp(?:[A-Z]|$)/;
|
|
973
|
+
/** DOM methods that write an attribute, mapped to the argument that names it. */
|
|
974
|
+
const ATTRIBUTE_WRITERS = new Map([
|
|
975
|
+
["setAttribute", 0],
|
|
976
|
+
["toggleAttribute", 0],
|
|
977
|
+
["setAttributeNS", 1],
|
|
978
|
+
]);
|
|
979
|
+
|
|
980
|
+
const frameworkMarkerMessage = (name) =>
|
|
981
|
+
`${name} is one of react-core's markers, and react-core trusts them: its stylesheet selects ` +
|
|
982
|
+
"on them and the runtime layout contract identifies a slot's children by them, so a marker " +
|
|
983
|
+
"written in app code borrows a component's styling and passes that check without the " +
|
|
984
|
+
"component. Compose the react-core component that renders it (Card, Stack, ...) instead. " +
|
|
985
|
+
"A framework screen the app replaces, such as renderTerpApp({ login }), is the app's own " +
|
|
986
|
+
'screen: test it by the roles and accessible names it renders (getByRole("button", ' +
|
|
987
|
+
"{ name: ... })), not by the framework's markers.";
|
|
988
|
+
|
|
989
|
+
/** The static key of an object-literal property, including a literal computed key. */
|
|
990
|
+
function staticPropertyKey(node) {
|
|
991
|
+
return node.computed ? staticString(node.key) : propertyName(node);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/** The key assigned through `element.dataset.<key>` or `element.dataset["<key>"]`, or null. */
|
|
995
|
+
function datasetKey(node) {
|
|
996
|
+
if (node.type !== "MemberExpression" || memberName(node.object) !== "dataset") return null;
|
|
997
|
+
return node.computed ? staticString(node.property) : node.property.name;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* App code never writes react-core's identity markers — in JSX, as the key of a props object
|
|
1002
|
+
* (an inline spread, a hoisted object, a createElement bag), through a DOM attribute writer, or
|
|
1003
|
+
* through `dataset`. Reading one is not a write and is left alone; so is every other data
|
|
1004
|
+
* attribute (`data-testid`), which an app writes for itself.
|
|
1005
|
+
*/
|
|
1006
|
+
const noFrameworkMarkers = {
|
|
1007
|
+
meta: {
|
|
1008
|
+
type: "problem",
|
|
1009
|
+
docs: { description: "Disallow writing react-core's data-terp identity markers in app code." },
|
|
1010
|
+
schema: [],
|
|
1011
|
+
},
|
|
1012
|
+
create(context) {
|
|
1013
|
+
const report = (node, name) => context.report({ node, message: frameworkMarkerMessage(name) });
|
|
1014
|
+
return {
|
|
1015
|
+
JSXAttribute(node) {
|
|
1016
|
+
const name = jsxName(node.name);
|
|
1017
|
+
if (name !== null && FRAMEWORK_MARKER_RE.test(name)) report(node, name);
|
|
1018
|
+
},
|
|
1019
|
+
Property(node) {
|
|
1020
|
+
// A destructuring pattern reads the key; only an object literal writes it.
|
|
1021
|
+
if (node.parent?.type !== "ObjectExpression") return;
|
|
1022
|
+
const name = staticPropertyKey(node);
|
|
1023
|
+
if (name !== null && FRAMEWORK_MARKER_RE.test(name)) report(node.key, name);
|
|
1024
|
+
},
|
|
1025
|
+
CallExpression(node) {
|
|
1026
|
+
const index = ATTRIBUTE_WRITERS.get(memberName(node.callee));
|
|
1027
|
+
if (index === undefined) return;
|
|
1028
|
+
const name = staticString(node.arguments[index]);
|
|
1029
|
+
if (name !== null && FRAMEWORK_MARKER_RE.test(name)) report(node, name);
|
|
1030
|
+
},
|
|
1031
|
+
AssignmentExpression(node) {
|
|
1032
|
+
const key = datasetKey(node.left);
|
|
1033
|
+
if (key !== null && FRAMEWORK_DATASET_KEY_RE.test(key)) report(node.left, `dataset.${key}`);
|
|
1034
|
+
},
|
|
1035
|
+
};
|
|
1036
|
+
},
|
|
1037
|
+
};
|
|
1038
|
+
|
|
916
1039
|
const terpPlugin = {
|
|
917
1040
|
rules: {
|
|
918
1041
|
"locale-catalogs-complete": localeCatalogsComplete,
|
|
@@ -920,6 +1043,7 @@ const terpPlugin = {
|
|
|
920
1043
|
"no-cross-module-imports": noCrossModuleImports,
|
|
921
1044
|
"no-dom-html-injection": noDomHtmlInjection,
|
|
922
1045
|
"no-eval": noEval,
|
|
1046
|
+
"no-framework-markers": noFrameworkMarkers,
|
|
923
1047
|
"no-untranslated-ui": noUntranslatedUi,
|
|
924
1048
|
"no-unsafe-href": noUnsafeHref,
|
|
925
1049
|
"no-unsafe-target-blank": noUnsafeTargetBlank,
|
|
@@ -1320,6 +1444,9 @@ export function terpBoundaries() {
|
|
|
1320
1444
|
plugins: { terp: terpPlugin },
|
|
1321
1445
|
rules: {
|
|
1322
1446
|
"terp/locale-catalogs-complete": "error",
|
|
1447
|
+
// Across src, not only modules: a replaced framework screen (a custom sign-in passed
|
|
1448
|
+
// to renderTerpApp) can live beside the bootstrap, outside any module.
|
|
1449
|
+
"terp/no-framework-markers": "error",
|
|
1323
1450
|
"terp/no-untranslated-ui": "error",
|
|
1324
1451
|
},
|
|
1325
1452
|
},
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
|
|
3
|
+
import { ESLint } from "eslint";
|
|
4
|
+
import { describe, expect, it } from "vitest";
|
|
5
|
+
|
|
6
|
+
import terpBoundaries from "./index.js";
|
|
7
|
+
|
|
8
|
+
// App code never writes react-core's `data-terp` markers (ADR 0160): the stylesheet selects on
|
|
9
|
+
// them and the runtime layout contract identifies a slot's children by them, so a hand-written
|
|
10
|
+
// one borrows a component's styling and passes that check without the component.
|
|
11
|
+
|
|
12
|
+
const RULE = "terp/no-framework-markers";
|
|
13
|
+
|
|
14
|
+
async function markerFindings(source, relative = "src/modules/widgets/Widget.tsx") {
|
|
15
|
+
const eslint = new ESLint({ overrideConfigFile: true, overrideConfig: terpBoundaries });
|
|
16
|
+
const [result] = await eslint.lintText(source, { filePath: path.resolve(relative) });
|
|
17
|
+
return result.messages.filter((message) => message.ruleId === RULE);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function markerLines(source, relative) {
|
|
21
|
+
return (await markerFindings(source, relative)).map((message) => message.line);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("no-framework-markers", () => {
|
|
25
|
+
it("refuses a data-terp attribute on a raw element and on a react-core component", async () => {
|
|
26
|
+
const source = [
|
|
27
|
+
'import { Card, Stack } from "@terpjs/react-core";',
|
|
28
|
+
"export function Widget() {",
|
|
29
|
+
" return (",
|
|
30
|
+
" <Stack>",
|
|
31
|
+
' <div data-terp="card" />',
|
|
32
|
+
' <Stack data-terp="hubcard" />',
|
|
33
|
+
" <Card data-terp />",
|
|
34
|
+
" </Stack>",
|
|
35
|
+
" );",
|
|
36
|
+
"}",
|
|
37
|
+
].join("\n");
|
|
38
|
+
expect(await markerLines(source)).toEqual([5, 6, 7]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("applies across src, not only inside modules", async () => {
|
|
42
|
+
// A replaced sign-in screen is passed to renderTerpApp from the bootstrap, and nothing
|
|
43
|
+
// requires it to live in a module.
|
|
44
|
+
const source = 'export const SignIn = () => <div data-terp="login-title" />;\n';
|
|
45
|
+
expect(await markerLines(source, "src/SignIn.tsx")).toEqual([1]);
|
|
46
|
+
expect(await markerLines(source, "src/main.tsx")).toEqual([1]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("refuses the whole data-terp- namespace, whatever the case", async () => {
|
|
50
|
+
const source = [
|
|
51
|
+
"export const Picked = () => <div data-terp-preview-pick />;",
|
|
52
|
+
"export const Loud = () => <div DATA-TERP=\"card\" />;",
|
|
53
|
+
].join("\n");
|
|
54
|
+
expect(await markerLines(source)).toEqual([1, 2]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("refuses the marker as the key of a props object", async () => {
|
|
58
|
+
const source = [
|
|
59
|
+
'import { createElement } from "react";',
|
|
60
|
+
'export const Inline = () => <div {...{ "data-terp": "card" }} />;',
|
|
61
|
+
'const props = { "data-terp": "card" };',
|
|
62
|
+
"export const Hoisted = () => <div {...props} />;",
|
|
63
|
+
'export const Computed = () => <div {...{ ["data-terp"]: "card" }} />;',
|
|
64
|
+
'export const Built = () => createElement("div", { "data-terp": "card" });',
|
|
65
|
+
].join("\n");
|
|
66
|
+
expect(await markerLines(source)).toEqual([2, 3, 5, 6]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("refuses the marker written through the DOM", async () => {
|
|
70
|
+
const source = [
|
|
71
|
+
"export function stamp(element: HTMLElement) {",
|
|
72
|
+
' element.setAttribute("data-terp", "card");',
|
|
73
|
+
' element.setAttributeNS(null, "data-terp", "card");',
|
|
74
|
+
' element.toggleAttribute("data-terp-preview-pick");',
|
|
75
|
+
' element.setAttribute("DATA-TERP", "card");',
|
|
76
|
+
' element.dataset.terp = "card";',
|
|
77
|
+
' element.dataset["terp"] = "card";',
|
|
78
|
+
' element.dataset.terpPreviewPick = "";',
|
|
79
|
+
"}",
|
|
80
|
+
].join("\n");
|
|
81
|
+
expect(await markerLines(source)).toEqual([2, 3, 4, 5, 6, 7, 8]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("leaves every other data attribute alone", async () => {
|
|
85
|
+
const source = [
|
|
86
|
+
"export function Widget({ element }: { element: HTMLElement }) {",
|
|
87
|
+
' element.setAttribute("data-testid", "summary");',
|
|
88
|
+
' element.setAttributeNS(null, "data-state", "open");',
|
|
89
|
+
' element.dataset.state = "open";',
|
|
90
|
+
' element.dataset.terpentine = "resin";',
|
|
91
|
+
' const props = { "data-testid": "summary" };',
|
|
92
|
+
' return <div data-testid="summary" data-terpentine="resin" {...props} />;',
|
|
93
|
+
"}",
|
|
94
|
+
].join("\n");
|
|
95
|
+
expect(await markerFindings(source)).toEqual([]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("leaves reading a marker alone", async () => {
|
|
99
|
+
const source = [
|
|
100
|
+
"export function describe(element: HTMLElement, props: Record<string, string>) {",
|
|
101
|
+
' const { "data-terp": marker } = props;',
|
|
102
|
+
' const card = element.querySelector(\'[data-terp="card"]\');',
|
|
103
|
+
' return [marker, element.getAttribute("data-terp"), element.dataset.terp, card];',
|
|
104
|
+
"}",
|
|
105
|
+
].join("\n");
|
|
106
|
+
expect(await markerFindings(source)).toEqual([]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("names the fix: compose the component, and test a replaced screen by its own names", async () => {
|
|
110
|
+
const [finding] = await markerFindings('export const W = () => <div data-terp="card" />;\n');
|
|
111
|
+
expect(finding.message).toContain("data-terp is one of react-core's markers");
|
|
112
|
+
expect(finding.message).toContain("Compose the react-core component");
|
|
113
|
+
expect(finding.message).toContain("roles and accessible names");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("honours the catalog-derived governed marker for an intentional exception", async () => {
|
|
117
|
+
const line = 'export const W = () => <div data-terp="card" />;';
|
|
118
|
+
const eslint = new ESLint({ overrideConfigFile: true, overrideConfig: terpBoundaries });
|
|
119
|
+
const filePath = path.resolve("src/modules/widgets/Widget.tsx");
|
|
120
|
+
const [waived] = await eslint.lintText(
|
|
121
|
+
`// terp-allow-no-framework-markers: recorded parity exception\n${line}\n`,
|
|
122
|
+
{ filePath },
|
|
123
|
+
);
|
|
124
|
+
expect(waived.messages).toEqual([]);
|
|
125
|
+
// ...and the same line without the marker is reported, so the waiver is what cleared it.
|
|
126
|
+
expect(await markerLines(`${line}\n`)).toEqual([1]);
|
|
127
|
+
});
|
|
128
|
+
});
|
package/src/spec.js
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
* ./index.js realises them for the React stack; a future stack (e.g. Svelte) can realise the same
|
|
4
4
|
* spec with its own adapter. The *rules* are shared; only the *enforcement adapter* is per-stack.
|
|
5
5
|
*
|
|
6
|
-
* Structural/security boundaries apply to `src/modules/**`; localization
|
|
7
|
-
* app-authored `src/**`. Framework packages
|
|
8
|
-
* back to and are outside an app's
|
|
6
|
+
* Structural/security boundaries apply to `src/modules/**`; localization and the refusal of
|
|
7
|
+
* react-core's `data-terp` markers apply to all app-authored `src/**`. Framework packages
|
|
8
|
+
* legitimately define the primitives these rules point back to and are outside an app's
|
|
9
|
+
* boundary config.
|
|
9
10
|
*/
|
|
10
11
|
/**
|
|
11
12
|
* The Terp Standard version this adapter is certified against — the `spec_version` a
|
|
@@ -16,10 +17,13 @@
|
|
|
16
17
|
* NOT by this package's own suite, which certification runs against candidate spec releases
|
|
17
18
|
* whose version is allowed to be newer).
|
|
18
19
|
*/
|
|
19
|
-
export const SPEC_VERSION = "0.
|
|
20
|
+
export const SPEC_VERSION = "0.38.0";
|
|
20
21
|
|
|
21
22
|
export const BOUNDARY_SPEC = {
|
|
22
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Every app-authored TypeScript source file: its user-facing copy must be cataloged, and it
|
|
25
|
+
* never writes react-core's markers.
|
|
26
|
+
*/
|
|
23
27
|
appFiles: ["**/src/**/*.{ts,tsx}"],
|
|
24
28
|
/** App module files the boundary + frontend security defaults apply to. */
|
|
25
29
|
moduleFiles: ["**/modules/**/*.{ts,tsx}"],
|
|
@@ -35,6 +39,7 @@ export const BOUNDARY_SPEC = {
|
|
|
35
39
|
table: "DataView",
|
|
36
40
|
dialog: "ConfirmDialog",
|
|
37
41
|
form: 'Stack as="form"',
|
|
42
|
+
meter: "Meter",
|
|
38
43
|
},
|
|
39
44
|
/**
|
|
40
45
|
* JSX attributes an app module must not author — styling lives in the design tokens and the
|
package/src/surface.test.js
CHANGED
|
@@ -37,10 +37,35 @@ async function lintModuleSource(source) {
|
|
|
37
37
|
return result.messages.map((message) => catalogRuleId(message));
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Elements this checker refuses before the pinned Standard lists them.
|
|
42
|
+
*
|
|
43
|
+
* The Standard states the floor, not the ceiling ("widening a detector past the contract
|
|
44
|
+
* is always allowed"), so refusing more is conformant. It is still a deliberate list: every
|
|
45
|
+
* element the map refuses beyond the pinned spec must be named here, so a typo in the map
|
|
46
|
+
* cannot pass as an extra refusal; and an entry the pinned spec already names fails too, so
|
|
47
|
+
* adopting the spec release that lists an element is the step that removes it here.
|
|
48
|
+
*/
|
|
49
|
+
const AHEAD_OF_SPEC = [];
|
|
50
|
+
|
|
51
|
+
describe("structural parity: BOUNDARY_SPEC realises the declared surface", () => {
|
|
52
|
+
it("every element the Standard restricts is restricted here", () => {
|
|
53
|
+
const mapped = Object.keys(BOUNDARY_SPEC.restrictedElements);
|
|
54
|
+
expect(SURFACE.restrictedElements.filter((element) => !mapped.includes(element))).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("anything restricted beyond the Standard is named, and only until it is adopted", () => {
|
|
58
|
+
// Exact, deliberately: an AHEAD_OF_SPEC entry the pinned spec already lists is not in
|
|
59
|
+
// `beyond`, so a stale entry fails here instead of lingering as a silent exemption.
|
|
60
|
+
const beyond = Object.keys(BOUNDARY_SPEC.restrictedElements)
|
|
61
|
+
.filter((element) => !SURFACE.restrictedElements.includes(element))
|
|
62
|
+
.sort();
|
|
63
|
+
expect(beyond).toEqual([...AHEAD_OF_SPEC].sort());
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("a raw <meter> is refused and names Meter", async () => {
|
|
67
|
+
expect(await lintModuleSource("export const W = () => <meter value={0.5} />;\n")).toContain(
|
|
68
|
+
"frontend/token-styled-elements",
|
|
44
69
|
);
|
|
45
70
|
});
|
|
46
71
|
|
|
@@ -171,6 +196,9 @@ const VIOLATION_SNIPPETS = {
|
|
|
171
196
|
"frontend/no-cross-module-imports": 'import { x } from "../other/thing";',
|
|
172
197
|
"frontend/no-dom-html-injection": "export const W = (el, html) => { el.innerHTML = html; };",
|
|
173
198
|
"frontend/no-eval": "export const run = (code) => eval(code);",
|
|
199
|
+
"frontend/no-framework-markers": 'export const W = () => <div data-terp="card" />;',
|
|
200
|
+
"frontend/no-raw-clipboard": "export const copy = (text) => navigator.clipboard.writeText(text);",
|
|
201
|
+
"frontend/no-raw-random-uuid": "export const newId = () => crypto.randomUUID();",
|
|
174
202
|
"frontend/no-unsafe-href":
|
|
175
203
|
'export const W = ({label}) => <a href="javascript:alert(1)">{label}</a>;',
|
|
176
204
|
"frontend/no-unsafe-target-blank":
|
|
@@ -195,11 +223,17 @@ describe(
|
|
|
195
223
|
expect(optOut).toBe(`// terp-allow-${name}: <reason>`);
|
|
196
224
|
});
|
|
197
225
|
const snippet = VIOLATION_SNIPPETS[entry.id];
|
|
198
|
-
if (
|
|
199
|
-
//
|
|
200
|
-
//
|
|
226
|
+
if (entry.id === "frontend/layout-contract") {
|
|
227
|
+
// It needs the opt-in contract config; its marker behaviour is covered by
|
|
228
|
+
// layouts.test.js with the same spelling.
|
|
201
229
|
continue;
|
|
202
230
|
}
|
|
231
|
+
// Any other rule without a snippet used to be skipped here in silence, which is
|
|
232
|
+
// how two rules shipped with their opt-out parity never checked.
|
|
233
|
+
it(`${entry.id} has a violating snippet here`, () => {
|
|
234
|
+
expect(snippet, `add a VIOLATION_SNIPPETS entry for ${entry.id}`).toBeDefined();
|
|
235
|
+
});
|
|
236
|
+
if (snippet === undefined) continue;
|
|
203
237
|
it(`${entry.id}'s declared marker suppresses its violation`, async () => {
|
|
204
238
|
const marker = optOut.replace("<reason>", "recorded parity exception");
|
|
205
239
|
// These snippets violate exactly one rule, so the suppressed result is
|