@starci/eslint-canon-fe 5.1.2 → 6.0.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/CHANGELOG.md +78 -2
- package/README.md +17 -18
- package/brand-values.mjs +1 -3
- package/class-names.mjs +9 -9
- package/client-boundary.mjs +125 -37
- package/comments.mjs +14 -21
- package/docs/client-boundary.md +26 -1
- package/docs/grammar-boundary.md +33 -0
- package/docs/hygiene.md +15 -4
- package/docs/lint-escape-hatch.md +3 -1
- package/docs/next-conventions.md +26 -0
- package/docs/size-growth.md +29 -0
- package/docs/spec-quality.md +15 -7
- package/docs/status-colors.md +32 -0
- package/docs/translation.md +4 -0
- package/docs/transport.md +36 -9
- package/e2e-shape.mjs +20 -23
- package/env-owner.mjs +1 -1
- package/file-layout.mjs +105 -103
- package/formatting.mjs +2 -4
- package/grammar-boundary.mjs +85 -9
- package/hooks-folder.mjs +17 -16
- package/hygiene.mjs +370 -92
- package/icon.mjs +21 -20
- package/index.mjs +27 -85
- package/landmark.mjs +0 -11
- package/lib/bindings.mjs +144 -0
- package/lib/config.mjs +79 -0
- package/lib/hfs.mjs +48 -0
- package/lib/next.mjs +6 -0
- package/lib/platform.mjs +66 -0
- package/lib/react.mjs +28 -0
- package/lib/scope.mjs +119 -19
- package/lib/status-tones.generated.mjs +9 -0
- package/lib/types.mjs +63 -0
- package/lib/why.mjs +41 -11
- package/lint-escape-hatch.mjs +17 -15
- package/loading.mjs +9 -16
- package/naming.mjs +8 -2
- package/next-conventions.mjs +109 -25
- package/package.json +9 -4
- package/props-and-slots.mjs +19 -13
- package/runtime/engine/plain-object.mjs +5 -0
- package/runtime/engine/runtime-root.mjs +32 -0
- package/runtime/engine/yaml.mjs +161 -0
- package/runtime/knowledge/hfs/slots.yaml +1072 -0
- package/runtime/scripts/lib/git.mjs +53 -0
- package/runtime/scripts/lib/glob.mjs +23 -0
- package/runtime/scripts/lib/hfs-allows.mjs +42 -0
- package/runtime/scripts/lib/hfs-slots.mjs +722 -0
- package/runtime/scripts/lib/hfs-view.mjs +68 -0
- package/runtime/scripts/lib/next-contract.mjs +14 -0
- package/runtime/scripts/lib/path-key.mjs +17 -0
- package/runtime/scripts/lib/recorded-lines.mjs +27 -0
- package/served-locale.mjs +30 -16
- package/shape-slot.mjs +22 -20
- package/size-and-state-budget.mjs +41 -23
- package/size-growth.mjs +58 -0
- package/spec-quality.mjs +95 -16
- package/status-colors.mjs +139 -0
- package/the-split.mjs +13 -13
- package/tokens.mjs +26 -34
- package/translation.mjs +52 -19
- package/transport.mjs +437 -89
- package/type-safety.mjs +8 -17
- package/typography.mjs +7 -11
- package/vendor-boundary.mjs +5 -27
- package/lint-adoption.mjs +0 -79
package/spec-quality.mjs
CHANGED
|
@@ -19,7 +19,11 @@
|
|
|
19
19
|
* entry is invisible until a reader sees `course.titel`.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import {
|
|
22
|
+
import { existsSync, readFileSync } from "node:fs"
|
|
23
|
+
import { dirname, join } from "node:path"
|
|
24
|
+
import ts from "typescript"
|
|
25
|
+
import { hfsOf } from "./lib/hfs.mjs"
|
|
26
|
+
import { baseName, isSpecFile, resolveImport } from "./lib/scope.mjs"
|
|
23
27
|
import { normalizePath } from "./lib/path.mjs"
|
|
24
28
|
|
|
25
29
|
/** The subject a spec sits beside: `Feed.test.tsx` -> `Feed`. */
|
|
@@ -116,20 +120,34 @@ export const specTestsItsNeighbour = {
|
|
|
116
120
|
|
|
117
121
|
// -- SPEC-3 ----------------------------------------------------------------------------------------
|
|
118
122
|
|
|
119
|
-
/**
|
|
123
|
+
/** The siblings `index.spec.ts(x)` can be the spec of. */
|
|
124
|
+
const INDEX_SIBLINGS = ["index.ts", "index.tsx"]
|
|
125
|
+
|
|
126
|
+
/** A statement that holds no behaviour: an import or an export declaration (a re-export, `export {}`, `export type {}`). */
|
|
127
|
+
const isBarrelStatement = (statement) => ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)
|
|
128
|
+
|
|
129
|
+
/** True when the file holds nothing but import and export declarations. */
|
|
130
|
+
const isBarrelSource = (text) => {
|
|
131
|
+
const source = ts.createSourceFile("index.tsx", text, ts.ScriptTarget.Latest, false, ts.ScriptKind.TSX)
|
|
132
|
+
return source.statements.every(isBarrelStatement)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** No spec for a barrel: the spec's `index.ts(x)` sibling holds only imports and re-exports. */
|
|
120
136
|
export const noBarrelSpec = {
|
|
121
137
|
meta: {
|
|
122
138
|
type: "problem",
|
|
123
|
-
docs: { description: "No `index.
|
|
139
|
+
docs: { description: "No `index.spec.ts` beside an `index.ts` that is only a barrel." },
|
|
124
140
|
schema: [],
|
|
125
141
|
messages: {
|
|
126
142
|
barrel:
|
|
127
|
-
"A spec for a barrel. `index.ts` holds re-exports, which have no behaviour; a spec beside one asserts that the language works. Test the units the barrel re-exports, beside each of them.",
|
|
143
|
+
"A spec for a barrel. The sibling `index.ts` holds only imports and re-exports, which have no behaviour; a spec beside one asserts that the language works. Test the units the barrel re-exports, beside each of them. (An `index.ts` that holds implementation - a function, a class or a value - may have this spec.)",
|
|
128
144
|
},
|
|
129
145
|
},
|
|
130
146
|
create(context) {
|
|
131
147
|
const file = normalizePath(context.filename || context.getFilename())
|
|
132
|
-
if (
|
|
148
|
+
if (!isSpecFile(file) || subjectOf(file) !== "index") return {}
|
|
149
|
+
const sibling = INDEX_SIBLINGS.map((name) => join(dirname(file), name)).find((candidate) => existsSync(candidate))
|
|
150
|
+
if (!sibling || !isBarrelSource(readFileSync(sibling, "utf8"))) return {}
|
|
133
151
|
return { Program: (node) => context.report({ node, messageId: "barrel" }) }
|
|
134
152
|
},
|
|
135
153
|
}
|
|
@@ -162,10 +180,18 @@ export const noDoubleCastInSpec = {
|
|
|
162
180
|
|
|
163
181
|
// -- SPEC-5 ----------------------------------------------------------------------------------------
|
|
164
182
|
|
|
165
|
-
/**
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
183
|
+
/**
|
|
184
|
+
* The spec of a connected screen: the spec of the entry (`index.tsx`, role `entry`) of a block or of a feature. The slot answers
|
|
185
|
+
* both: the sibling the spec is named after is classified, and it must be a component-tier entry in the `blocks` layer or a
|
|
186
|
+
* `fe.feature` entry.
|
|
187
|
+
*/
|
|
188
|
+
const isConnectedSpec = (context, file) => {
|
|
189
|
+
const subject = subjectOf(file)
|
|
190
|
+
const hfs = hfsOf(context)
|
|
191
|
+
const entry = hfs.classify(join(dirname(file), `${subject}.tsx`))
|
|
192
|
+
if (entry.role !== "entry") return false
|
|
193
|
+
return (entry.slot === "fe.components" && entry.kind === "blocks") || entry.slot === "fe.feature"
|
|
194
|
+
}
|
|
169
195
|
|
|
170
196
|
/** Names that run an accessibility check. */
|
|
171
197
|
const AXE_CALLS = new Set(["axe", "runAxe", "expectNoAxeViolations", "expectNoA11yViolations", "toHaveNoViolations"])
|
|
@@ -183,7 +209,7 @@ export const connectedSpecHasAxe = {
|
|
|
183
209
|
},
|
|
184
210
|
create(context) {
|
|
185
211
|
const file = normalizePath(context.filename || context.getFilename())
|
|
186
|
-
if (!isConnectedSpec(file)) return {}
|
|
212
|
+
if (!isSpecFile(file) || !isConnectedSpec(context, file)) return {}
|
|
187
213
|
let ran = false
|
|
188
214
|
return {
|
|
189
215
|
CallExpression(node) {
|
|
@@ -199,27 +225,80 @@ export const connectedSpecHasAxe = {
|
|
|
199
225
|
|
|
200
226
|
// -- SPEC-6 ----------------------------------------------------------------------------------------
|
|
201
227
|
|
|
202
|
-
/**
|
|
228
|
+
/** The functions a spec mocks a module with. */
|
|
229
|
+
const MOCK_CALLS = new Set(["mock", "doMock", "unstable_mockModule"])
|
|
230
|
+
|
|
231
|
+
/** Every node under `root` (inclusive), through the parser's visitor keys. */
|
|
232
|
+
const walk = (root, keys, visit) => {
|
|
233
|
+
if (!root || typeof root.type !== "string") return
|
|
234
|
+
visit(root)
|
|
235
|
+
for (const key of keys[root.type] ?? []) {
|
|
236
|
+
const child = root[key]
|
|
237
|
+
for (const item of Array.isArray(child) ? child : [child]) walk(item, keys, visit)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** True when the specifier is the app's message catalogue: a file under `messages/` of the i18n module slot. */
|
|
242
|
+
const isCatalogSpecifier = (context, specifier) => {
|
|
243
|
+
const target = resolveImport(context, specifier)
|
|
244
|
+
if (!target) return false
|
|
245
|
+
const hfs = hfsOf(context)
|
|
246
|
+
const { slot, root } = hfs.classify(target)
|
|
247
|
+
return slot === "fe.modules.i18n" && hfs.relative(target).startsWith(`${root}/messages/`)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** A spec renders with the real catalogue; a server helper's mock serves it. */
|
|
203
251
|
export const noMockedTranslations = {
|
|
204
252
|
meta: {
|
|
205
253
|
type: "problem",
|
|
206
|
-
docs: { description: "A spec renders with the real catalogue; it never mocks `next-intl
|
|
254
|
+
docs: { description: "A spec renders with the real catalogue; it never mocks `next-intl` (a `next-intl/server` mock must serve the real catalogue)." },
|
|
207
255
|
schema: [],
|
|
208
256
|
messages: {
|
|
209
257
|
mocked:
|
|
210
258
|
"A mocked `next-intl`. The spec then passes on the KEY, so a missing or misspelled catalogue entry is invisible until a reader sees `course.titel`. Render inside `NextIntlClientProvider` with the real `messages/<locale>.json`.",
|
|
259
|
+
mockedServer:
|
|
260
|
+
"A `next-intl/server` mock that does not serve the real catalogue. A server helper has no provider to render inside, so the mock is allowed - but its factory must build the translator from the app's real messages (import `modules/i18n/messages/<locale>.json` and pass it to `createTranslator` from `next-intl`); a literal or key-echoing translator passes on the KEY and hides a missing or misspelled entry.",
|
|
211
261
|
},
|
|
212
262
|
},
|
|
213
263
|
create(context) {
|
|
214
|
-
|
|
264
|
+
const file = context.filename || context.getFilename()
|
|
265
|
+
if (!isSpecFile(file)) return {}
|
|
266
|
+
const source = context.sourceCode ?? context.getSourceCode()
|
|
267
|
+
const keys = source.visitorKeys
|
|
268
|
+
const catalogBindings = new Set()
|
|
269
|
+
const mocks = []
|
|
270
|
+
const hoisted = []
|
|
215
271
|
return {
|
|
272
|
+
ImportDeclaration(node) {
|
|
273
|
+
if (!isCatalogSpecifier(context, String(node.source.value))) return
|
|
274
|
+
for (const specifier of node.specifiers) catalogBindings.add(specifier.local.name)
|
|
275
|
+
},
|
|
216
276
|
CallExpression(node) {
|
|
217
277
|
const callee = node.callee
|
|
218
278
|
if (callee.type !== "MemberExpression" || callee.computed) return
|
|
219
|
-
if (
|
|
220
|
-
if (!["vi", "jest"].includes(callee.object.name)) return
|
|
279
|
+
if (["vi", "jest"].includes(callee.object.name) && callee.property.name === "hoisted") hoisted.push(node)
|
|
280
|
+
if (!MOCK_CALLS.has(callee.property.name) || !["vi", "jest"].includes(callee.object.name)) return
|
|
221
281
|
const target = staticString(node.arguments[0])
|
|
222
|
-
if (target !== null && /^next-intl(?:\/|$)/.test(target))
|
|
282
|
+
if (target !== null && /^next-intl(?:\/|$)/.test(target)) mocks.push({ node, target })
|
|
283
|
+
},
|
|
284
|
+
"Program:exit"() {
|
|
285
|
+
// The catalogue is served when a catalogue binding, or a dynamic import of the catalogue, is used by the factory or by a `vi.hoisted` block.
|
|
286
|
+
const serves = (root) => {
|
|
287
|
+
let found = false
|
|
288
|
+
walk(root, keys, (inner) => {
|
|
289
|
+
if (inner.type === "Identifier" && catalogBindings.has(inner.name)) found = true
|
|
290
|
+
const specifier = inner.type === "ImportExpression" ? staticString(inner.source) : null
|
|
291
|
+
if (specifier !== null && isCatalogSpecifier(context, specifier)) found = true
|
|
292
|
+
})
|
|
293
|
+
return found
|
|
294
|
+
}
|
|
295
|
+
for (const { node, target } of mocks) {
|
|
296
|
+
if (target === "next-intl/server") {
|
|
297
|
+
const factory = node.arguments[1]
|
|
298
|
+
if (factory && (serves(factory) || hoisted.some(serves))) continue
|
|
299
|
+
context.report({ node, messageId: "mockedServer" })
|
|
300
|
+
} else context.report({ node, messageId: "mocked" })
|
|
301
|
+
}
|
|
223
302
|
},
|
|
224
303
|
}
|
|
225
304
|
},
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rule that holds `status-colors.md` (HFS R61 `FE_STYLE_TOKEN_ONLY`, the class-name half of the soft-pair contrast
|
|
3
|
+
* obligation; the brand layer's own contrast belongs to `@starci/stylelint-canon` `starci/status-contrast`).
|
|
4
|
+
*
|
|
5
|
+
* A STATUS TONE HAS TWO FACES. The SOLID tone (`--success`) is a fill: a button, a bar, a badge dot's disc, with
|
|
6
|
+
* `--success-foreground` ink on it. A status shown as TEXT or as an ICON uses the SOFT pair, the way a HeroUI flat Chip
|
|
7
|
+
* does: `--success-soft-foreground` ink, on `--success-soft` when it sits on a tint. The brand layer is checked to
|
|
8
|
+
* make that ink readable (3:1 on the tint and on the page); the solid tone is only the mid-luminance hue of the brand
|
|
9
|
+
* and reads 3.8:1 or worse on white, so as a text colour it fails the contrast the soft pair was built to give.
|
|
10
|
+
*
|
|
11
|
+
* WHAT IT READS. Every static class string a component writes: the `className` / `class` attribute of an element (through
|
|
12
|
+
* `cn()`, `clsx()`, conditionals, arrays, template text), and a string in a constant or in a `classes` entry. The tones
|
|
13
|
+
* are the grammar's `STATUS_TONES` (`lib/status-tones.generated.mjs`, generated from the grammar with the stylelint
|
|
14
|
+
* vocabulary), never a list typed here. A class is a finding when it paints text, an icon or a text decoration with a solid
|
|
15
|
+
* tone (`text-success`, `fill-danger`, `stroke-warning`, `decoration-info`, with any variant and opacity).
|
|
16
|
+
*
|
|
17
|
+
* WHAT IT LEAVES ALONE: the soft pair (`text-success-soft-foreground`, `bg-success-soft`), a solid fill with its own ink
|
|
18
|
+
* (`bg-success text-success-foreground`), a solid border, ring or outline (`border-danger`, `ring-warning`: a
|
|
19
|
+
* boundary is not text), and a class that only shares a prefix (`text-successful`).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { attributeValue } from "./lib/ast.mjs"
|
|
23
|
+
import { isSpecFile } from "./lib/scope.mjs"
|
|
24
|
+
import { STATUS_TONES } from "./lib/status-tones.generated.mjs"
|
|
25
|
+
|
|
26
|
+
/** The utility families that paint text, an icon or a text decoration. */
|
|
27
|
+
const TEXT_PAINT = ["text", "fill", "stroke", "decoration"]
|
|
28
|
+
|
|
29
|
+
/** `text-success`, `text-success/60`, `text-success-hover`, with an optional `!` and any variants before it. */
|
|
30
|
+
const SOLID_TEXT = new RegExp(`^!?(${TEXT_PAINT.join("|")})-(${STATUS_TONES.join("|")})(?:-hover)?(?:/(?:\\d+|\\[[^\\]]*\\]))?$`)
|
|
31
|
+
|
|
32
|
+
/** The class after its variants: `md:hover:text-success` and `[&>svg]:text-success` both end at `text-success`. */
|
|
33
|
+
const utilityOf = (token) => {
|
|
34
|
+
let depth = 0
|
|
35
|
+
let cut = -1
|
|
36
|
+
for (let i = 0; i < token.length; i += 1) {
|
|
37
|
+
if (token[i] === "[") depth += 1
|
|
38
|
+
else if (token[i] === "]") depth -= 1
|
|
39
|
+
else if (token[i] === ":" && depth === 0) cut = i
|
|
40
|
+
}
|
|
41
|
+
return token.slice(cut + 1)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Every solid-tone text class in one class string, as `{ token, family, tone }`. */
|
|
45
|
+
export const solidTextClasses = (text) =>
|
|
46
|
+
text
|
|
47
|
+
.split(/\s+/)
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.flatMap((token) => {
|
|
50
|
+
const match = SOLID_TEXT.exec(utilityOf(token))
|
|
51
|
+
return match ? [{ token, family: match[1], tone: match[2] }] : []
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
/** The string nodes inside an expression that builds a class string: literals, template text, keys, and everything a call, array or condition carries. */
|
|
55
|
+
function* stringNodes(node) {
|
|
56
|
+
if (!node || typeof node.type !== "string") return
|
|
57
|
+
switch (node.type) {
|
|
58
|
+
case "Literal":
|
|
59
|
+
if (typeof node.value === "string") yield { node, text: node.value }
|
|
60
|
+
return
|
|
61
|
+
case "TemplateLiteral":
|
|
62
|
+
yield { node, text: node.quasis.map((quasi) => quasi.value.cooked ?? "").join(" ") }
|
|
63
|
+
return
|
|
64
|
+
case "JSXExpressionContainer":
|
|
65
|
+
yield* stringNodes(node.expression)
|
|
66
|
+
return
|
|
67
|
+
case "ConditionalExpression":
|
|
68
|
+
yield* stringNodes(node.consequent)
|
|
69
|
+
yield* stringNodes(node.alternate)
|
|
70
|
+
return
|
|
71
|
+
case "LogicalExpression":
|
|
72
|
+
yield* stringNodes(node.left)
|
|
73
|
+
yield* stringNodes(node.right)
|
|
74
|
+
return
|
|
75
|
+
case "TSAsExpression":
|
|
76
|
+
case "TSSatisfiesExpression":
|
|
77
|
+
case "TSNonNullExpression":
|
|
78
|
+
yield* stringNodes(node.expression)
|
|
79
|
+
return
|
|
80
|
+
case "ArrayExpression":
|
|
81
|
+
for (const element of node.elements) yield* stringNodes(element)
|
|
82
|
+
return
|
|
83
|
+
case "CallExpression":
|
|
84
|
+
for (const argument of node.arguments) yield* stringNodes(argument)
|
|
85
|
+
return
|
|
86
|
+
case "ObjectExpression":
|
|
87
|
+
for (const property of node.properties) {
|
|
88
|
+
if (property.type !== "Property") continue
|
|
89
|
+
if (!property.computed && property.key.type === "Literal") yield* stringNodes(property.key)
|
|
90
|
+
yield* stringNodes(property.value)
|
|
91
|
+
}
|
|
92
|
+
return
|
|
93
|
+
default:
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Text or an icon painted with a solid status tone is not readable at the contrast the soft pair was built for. */
|
|
98
|
+
export const statusTextUsesSoftForeground = {
|
|
99
|
+
meta: {
|
|
100
|
+
type: "problem",
|
|
101
|
+
docs: { description: "Status text and icons take the soft foreground of the tone, never the solid tone." },
|
|
102
|
+
schema: [],
|
|
103
|
+
messages: {
|
|
104
|
+
solid:
|
|
105
|
+
"`{{token}}` paints text or an icon with the solid `{{tone}}` tone, which is a fill and reads under 4.5:1 on the page (3.8:1 on white for a mid-luminance brand). A status shown as text or an icon takes the soft pair: `{{family}}-{{tone}}-soft-foreground`, on `bg-{{tone}}-soft` when it sits on a tint. The solid tone is for a fill with `text-{{tone}}-foreground` on it.",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
create(context) {
|
|
109
|
+
if (isSpecFile(context.filename || context.getFilename())) return {}
|
|
110
|
+
// A `classes` entry inside a constant is reached twice (as part of the constant, and as the entry): one finding per string.
|
|
111
|
+
const seen = new Set()
|
|
112
|
+
const check = (root) => {
|
|
113
|
+
for (const { node, text } of stringNodes(root)) {
|
|
114
|
+
if (seen.has(node)) continue
|
|
115
|
+
seen.add(node)
|
|
116
|
+
for (const { token, family, tone } of solidTextClasses(text)) context.report({ node, messageId: "solid", data: { token, family, tone } })
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
JSXAttribute(node) {
|
|
121
|
+
if (node.name.type === "JSXIdentifier" && (node.name.name === "className" || node.name.name === "class")) check(attributeValue(node))
|
|
122
|
+
},
|
|
123
|
+
VariableDeclarator(node) {
|
|
124
|
+
check(node.init)
|
|
125
|
+
},
|
|
126
|
+
Property(node) {
|
|
127
|
+
if (!node.computed && node.key.type === "Identifier" && node.key.name === "classes") check(node.value)
|
|
128
|
+
},
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The rules this law contributes to the plugin. */
|
|
134
|
+
export const rules = {
|
|
135
|
+
"status-text-uses-soft-foreground": statusTextUsesSoftForeground,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Every rule of this law is an error. */
|
|
139
|
+
export const recommended = Object.fromEntries(Object.keys(rules).map((name) => [`starci-fe/${name}`, "error"]))
|
package/the-split.mjs
CHANGED
|
@@ -7,21 +7,21 @@
|
|
|
7
7
|
* neither. So this rule guards the boundary from the side where a machine can see it, and the other
|
|
8
8
|
* side stays a matter for review.
|
|
9
9
|
*
|
|
10
|
-
* The scope is the
|
|
11
|
-
* that splits, so the
|
|
10
|
+
* The scope is the slot. The role `drawing` of a component owner is the drawing half in every tier
|
|
11
|
+
* that splits, so the slot does the scoping and the rule needs no configuration.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import { hfsOf } from "./lib/hfs.mjs"
|
|
15
|
+
import { fileOf, inSlot, kindOfFile, roleOfFile } from "./lib/scope.mjs"
|
|
15
16
|
|
|
16
|
-
/** The drawing half
|
|
17
|
-
const isDrawingHalf = (
|
|
17
|
+
/** The drawing half: the file the slot gives the role `drawing` (`component.tsx` of a component, feature or shared-package owner). */
|
|
18
|
+
const isDrawingHalf = (context) => roleOfFile(context) === "drawing"
|
|
18
19
|
|
|
19
|
-
/** A connected block entrypoint and the public name its folder fixes. */
|
|
20
|
-
const connectedBlock = (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
)
|
|
24
|
-
return match ? match[1] : null
|
|
20
|
+
/** A connected block entrypoint (the `entry` of a block owner) and the public name its folder fixes. */
|
|
21
|
+
const connectedBlock = (context) => {
|
|
22
|
+
if (!inSlot(context, "fe.components") || kindOfFile(context) !== "blocks" || roleOfFile(context) !== "entry") return null
|
|
23
|
+
const name = hfsOf(context).relative(fileOf(context)).split("/").slice(-2)[0]
|
|
24
|
+
return /^[A-Z][A-Za-z0-9]*$/.test(name) ? name : null
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -47,7 +47,7 @@ export const presentationalPurity = {
|
|
|
47
47
|
},
|
|
48
48
|
},
|
|
49
49
|
create(context) {
|
|
50
|
-
if (!isDrawingHalf(context
|
|
50
|
+
if (!isDrawingHalf(context)) return {}
|
|
51
51
|
return {
|
|
52
52
|
CallExpression(node) {
|
|
53
53
|
const callee = node.callee
|
|
@@ -79,7 +79,7 @@ export const connectedBlockHasPresentationalTwin = {
|
|
|
79
79
|
},
|
|
80
80
|
},
|
|
81
81
|
create(context) {
|
|
82
|
-
const block = connectedBlock(context
|
|
82
|
+
const block = connectedBlock(context)
|
|
83
83
|
if (!block) return {}
|
|
84
84
|
|
|
85
85
|
const twin = `${block}Base`
|
package/tokens.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs"
|
|
2
|
-
import {
|
|
2
|
+
import { join } from "node:path"
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The rules that hold `tokens.md`.
|
|
@@ -16,10 +16,8 @@ import { dirname, join } from "node:path"
|
|
|
16
16
|
* license one.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
/** Product source lives under `src/`; tooling and config are out of scope. */
|
|
22
|
-
const isSourceFile = (filename) => normalizePath(filename).includes("/src/")
|
|
19
|
+
import { hfsOf } from "./lib/hfs.mjs"
|
|
20
|
+
import { classOf, isProductSource } from "./lib/scope.mjs"
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
23
|
* A fractional step, in any family that measures.
|
|
@@ -64,7 +62,8 @@ const staticText = (value) => {
|
|
|
64
62
|
|
|
65
63
|
/** Walk every place a class string can be written: markup, a constant, or an entry's array. */
|
|
66
64
|
const classTextVisitors = (context, report) => {
|
|
67
|
-
|
|
65
|
+
// Product source is a file of a product tier (a slot says so); tooling, config, e2e and specs are out of scope.
|
|
66
|
+
if (!isProductSource(context)) return {}
|
|
68
67
|
return {
|
|
69
68
|
JSXAttribute(node) {
|
|
70
69
|
if (!isClassAttribute(node)) return
|
|
@@ -184,33 +183,27 @@ const TOKEN_CLASS_FAMILIES = [
|
|
|
184
183
|
{ pattern: /^min-h-([a-z][a-z0-9-]*)$/, variable: (name) => `--min-height-${name}` },
|
|
185
184
|
]
|
|
186
185
|
|
|
187
|
-
/**
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
"packages/ui/src/styles/globals.css",
|
|
194
|
-
]
|
|
195
|
-
|
|
196
|
-
/** Every stylesheet found above the linted file, read once and cached for the run. */
|
|
186
|
+
/**
|
|
187
|
+
* The stylesheets an app declares its theme in: the route slot's `globals.css` (`fe.route`, named by its `allows`) and the brand slot's
|
|
188
|
+
* `brand.css` (`fe.modules.brand`), both with `<app>` filled from the linted file's slot. Null when the file belongs to no app
|
|
189
|
+
* (a shared package has no theme of its own).
|
|
190
|
+
*/
|
|
191
|
+
/** Every theme read once per lint run. */
|
|
197
192
|
const themeCache = new Map()
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
}
|
|
211
|
-
directory = dirname(directory)
|
|
193
|
+
const themeText = (context) => {
|
|
194
|
+
const hfs = hfsOf(context)
|
|
195
|
+
const app = classOf(context).bindings?.app
|
|
196
|
+
if (app === undefined) return null
|
|
197
|
+
const candidates = [
|
|
198
|
+
join(hfs.slot("fe.route").path.replace("<app>", app), "globals.css"),
|
|
199
|
+
hfs.slot("fe.modules.brand").path.replace("<app>", app),
|
|
200
|
+
].map((relative) => join(hfs.repoRoot, relative))
|
|
201
|
+
const key = candidates.join("|")
|
|
202
|
+
if (!themeCache.has(key)) {
|
|
203
|
+
const found = candidates.filter((candidate) => existsSync(candidate)).map((candidate) => readFileSync(candidate, "utf8"))
|
|
204
|
+
themeCache.set(key, found.length > 0 ? found.join("\n") : null)
|
|
212
205
|
}
|
|
213
|
-
return
|
|
206
|
+
return themeCache.get(key)
|
|
214
207
|
}
|
|
215
208
|
|
|
216
209
|
/** A class naming a token the theme never defines is a class that draws nothing. */
|
|
@@ -225,9 +218,8 @@ export const noUnresolvedTokenClass = {
|
|
|
225
218
|
},
|
|
226
219
|
},
|
|
227
220
|
create(context) {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
const theme = themeTextFor(file)
|
|
221
|
+
if (!isProductSource(context)) return {}
|
|
222
|
+
const theme = themeText(context)
|
|
231
223
|
// A reader that cannot find the stylesheet stays quiet rather than calling every token dead.
|
|
232
224
|
if (theme === null) return {}
|
|
233
225
|
// The visitor hands (node, text) in that order; reversing them here is how a rule reports the
|
package/translation.mjs
CHANGED
|
@@ -29,16 +29,10 @@
|
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
31
|
import { SECOND_LANGUAGE_LETTER, isContentFile } from "./comments.mjs"
|
|
32
|
-
import {
|
|
32
|
+
import { isComponentFile, kindOfFile } from "./lib/scope.mjs"
|
|
33
33
|
|
|
34
|
-
/**
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
/** True when a file sits in a tier that must not resolve copy. */
|
|
38
|
-
const isVocabularyFile = (filename) => {
|
|
39
|
-
const file = normalizePath(filename)
|
|
40
|
-
return VOCABULARY_DIRS.some((dir) => file.includes(`/src/components/${dir}/`))
|
|
41
|
-
}
|
|
34
|
+
/** True when a file sits in a component layer that must not resolve copy: every layer but `blocks` receives every word it renders, so it can know no domain and no sentence. */
|
|
35
|
+
const isVocabularyFile = (context) => isComponentFile(context) && kindOfFile(context) !== "blocks"
|
|
42
36
|
|
|
43
37
|
/** Attributes a reader sees or hears: any word in them is copy. */
|
|
44
38
|
const STRICT_ATTRS = new Set([
|
|
@@ -119,6 +113,36 @@ const staticString = (node) => {
|
|
|
119
113
|
return null
|
|
120
114
|
}
|
|
121
115
|
|
|
116
|
+
/**
|
|
117
|
+
* The static text of a template literal that has substitutions, each substitution standing as `#`
|
|
118
|
+
* (a template of "the count, then installed" reads "# installed"); null for anything that is not such a template.
|
|
119
|
+
*/
|
|
120
|
+
const templateText = (node) => {
|
|
121
|
+
if (!node || node.type !== "TemplateLiteral" || node.expressions.length === 0) return null
|
|
122
|
+
return node.quasis.map((quasi) => quasi.value.cooked ?? "").join("#")
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** True when the static parts of a template with substitutions contain a word (a count then "installed", not two ids joined by a dash). */
|
|
126
|
+
const templateHasWord = (node) => {
|
|
127
|
+
const text = templateText(node)
|
|
128
|
+
return text !== null && hasLetter(text.replaceAll("#", " "))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A sentence rather than a token, for a property whose key does not say it is copy: two or more words made of
|
|
133
|
+
* letters and sentence punctuation only (so a class list, a URL, a media type or an id never qualifies), and it starts
|
|
134
|
+
* like a sentence, ends like one, or carries a non-ASCII letter.
|
|
135
|
+
*/
|
|
136
|
+
const looksLikeSentence = (text) => {
|
|
137
|
+
const tokens = text.trim().split(/\s+/)
|
|
138
|
+
if (tokens.length < 2) return false
|
|
139
|
+
// a word has punctuation only at its edges or as a hyphen between letters; an all-capitals hyphen chain (YYYY-MM-DD) is a format
|
|
140
|
+
const isWord = (token) => /^["'(]*[\p{L}\p{N}#'’]+(?:-[\p{L}\p{N}#]+)*[.,!?:;")]*$/u.test(token) && !/^\p{Lu}{2,}(?:-\p{Lu}{2,})+$/u.test(token)
|
|
141
|
+
if (!tokens.every(isWord)) return false
|
|
142
|
+
if (tokens.filter((token) => /\p{L}{2,}/u.test(token)).length < 2) return false
|
|
143
|
+
return /^\p{Lu}/u.test(text.trim()) || /[^\x00-\x7F]/.test(text) || /[.!?]$/.test(text.trim())
|
|
144
|
+
}
|
|
145
|
+
|
|
122
146
|
/** Static string carried by a JSX attribute value, else null. */
|
|
123
147
|
const attributeText = (node) => {
|
|
124
148
|
const value = node && node.value
|
|
@@ -153,7 +177,7 @@ export const noCopyResolutionBelowBlock = {
|
|
|
153
177
|
},
|
|
154
178
|
},
|
|
155
179
|
create(context) {
|
|
156
|
-
if (!isVocabularyFile(context
|
|
180
|
+
if (!isVocabularyFile(context)) return {}
|
|
157
181
|
return {
|
|
158
182
|
CallExpression(node) {
|
|
159
183
|
const callee = node.callee
|
|
@@ -186,7 +210,7 @@ export const noHardcodedCopy = {
|
|
|
186
210
|
},
|
|
187
211
|
},
|
|
188
212
|
create(context) {
|
|
189
|
-
if (isContentFile(context
|
|
213
|
+
if (isContentFile(context)) return {}
|
|
190
214
|
const source = context.sourceCode || context.getSourceCode()
|
|
191
215
|
/** Value nodes already reported through their owner (attribute, property), so a string reports once. */
|
|
192
216
|
const claimed = new WeakSet()
|
|
@@ -205,6 +229,10 @@ export const noHardcodedCopy = {
|
|
|
205
229
|
// `<p>{"Hello"}</p>`: a literal in child position is JSX text with braces around it.
|
|
206
230
|
const parent = node.parent
|
|
207
231
|
if (!parent || (parent.type !== "JSXElement" && parent.type !== "JSXFragment")) return
|
|
232
|
+
if (templateHasWord(node.expression)) {
|
|
233
|
+
claimed.add(node.expression)
|
|
234
|
+
return context.report({ node, messageId: "text", data: { text: source.getText(node.expression) } })
|
|
235
|
+
}
|
|
208
236
|
const text = staticString(node.expression)
|
|
209
237
|
if (text === null || !hasLetter(text.trim())) return
|
|
210
238
|
claimed.add(node.expression)
|
|
@@ -215,19 +243,24 @@ export const noHardcodedCopy = {
|
|
|
215
243
|
if (!attr) return
|
|
216
244
|
const strict = STRICT_ATTRS.has(attr)
|
|
217
245
|
if (!strict && !PROSE_ATTRS.has(attr)) return
|
|
218
|
-
const
|
|
246
|
+
const value = attributeValueNode(node)
|
|
247
|
+
const template = templateText(value)
|
|
248
|
+
const text = template ?? attributeText(node)
|
|
219
249
|
if (text === null) return
|
|
220
|
-
if (strict ? !hasLetter(text) : !looksLikeProse(text)) return
|
|
221
|
-
claimed.add(
|
|
222
|
-
context.report({ node, messageId: "attribute", data: { attr, text } })
|
|
250
|
+
if (strict ? !hasLetter(text.replaceAll("#", " ")) : !looksLikeProse(text)) return
|
|
251
|
+
claimed.add(value)
|
|
252
|
+
context.report({ node, messageId: "attribute", data: { attr, text: template === null ? text : source.getText(value) } })
|
|
223
253
|
},
|
|
224
254
|
Property(node) {
|
|
225
255
|
const key = keyName(node)
|
|
226
|
-
if (!key ||
|
|
227
|
-
const
|
|
228
|
-
|
|
256
|
+
if (!key || node.parent.type !== "ObjectExpression") return
|
|
257
|
+
const template = templateText(node.value)
|
|
258
|
+
const text = template ?? staticString(node.value)
|
|
259
|
+
if (text === null) return
|
|
260
|
+
// a copy key needs prose; any other key needs a whole sentence: { ready: "Your course is ready" } in a hook is copy
|
|
261
|
+
if (!(COPY_KEYS.has(key) ? looksLikeProse(text) : looksLikeSentence(text))) return
|
|
229
262
|
claimed.add(node.value)
|
|
230
|
-
context.report({ node, messageId: "property", data: { key, text } })
|
|
263
|
+
context.report({ node, messageId: "property", data: { key, text: template === null ? text : source.getText(node.value) } })
|
|
231
264
|
},
|
|
232
265
|
Literal(node) {
|
|
233
266
|
if (claimed.has(node) || typeof node.value !== "string") return
|