@kerf-js/cli 0.0.0-stage → 0.1.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.
Files changed (176) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +554 -2
  3. package/bin/kerf.mjs +15 -0
  4. package/docs/async.md +50 -0
  5. package/docs/browser.md +53 -0
  6. package/docs/config.md +127 -0
  7. package/docs/core.md +30 -0
  8. package/docs/deploy.md +108 -0
  9. package/docs/design.md +106 -0
  10. package/docs/entrypoints.md +83 -0
  11. package/docs/fonts.md +118 -0
  12. package/docs/icons.md +108 -0
  13. package/docs/lists.md +25 -0
  14. package/docs/meta.md +92 -0
  15. package/docs/plugins.md +166 -0
  16. package/docs/props.md +88 -0
  17. package/docs/reactivity.md +26 -0
  18. package/docs/rules.md +39 -0
  19. package/docs/rust.md +253 -0
  20. package/docs/start-short.md +10 -0
  21. package/docs/start.md +642 -0
  22. package/docs/state.md +122 -0
  23. package/docs/style.md +82 -0
  24. package/docs/surface.md +158 -0
  25. package/docs/testing.md +81 -0
  26. package/docs/theme.md +263 -0
  27. package/docs/ui-state.md +105 -0
  28. package/docs/verify.md +61 -0
  29. package/package.json +47 -3
  30. package/src/add/config.mjs +93 -0
  31. package/src/add/errors.mjs +2 -0
  32. package/src/add/setup.mjs +88 -0
  33. package/src/add-rust.mjs +178 -0
  34. package/src/add.mjs +392 -0
  35. package/src/agents-md.mjs +240 -0
  36. package/src/alias.mjs +70 -0
  37. package/src/audit/page.mjs +313 -0
  38. package/src/audit/report.mjs +69 -0
  39. package/src/audit/utils.mjs +5 -0
  40. package/src/audit.mjs +319 -0
  41. package/src/browser/cli.mjs +53 -0
  42. package/src/browser/client.mjs +299 -0
  43. package/src/browser/commands.mjs +258 -0
  44. package/src/capture.mjs +21 -0
  45. package/src/catalog/audit.mjs +129 -0
  46. package/src/catalog/auth.mjs +40 -0
  47. package/src/catalog/compiler.mjs +337 -0
  48. package/src/catalog/contracts.mjs +18 -0
  49. package/src/catalog/database.mjs +52 -0
  50. package/src/catalog/deploy.mjs +117 -0
  51. package/src/catalog/design.mjs +159 -0
  52. package/src/catalog/forms.mjs +25 -0
  53. package/src/catalog/hydration.mjs +29 -0
  54. package/src/catalog/icons.mjs +24 -0
  55. package/src/catalog/media.mjs +163 -0
  56. package/src/catalog/metadata.mjs +116 -0
  57. package/src/catalog/plugins.mjs +228 -0
  58. package/src/catalog/realtime.mjs +195 -0
  59. package/src/catalog/rust.mjs +75 -0
  60. package/src/catalog/server-functions.mjs +100 -0
  61. package/src/catalog/state.mjs +39 -0
  62. package/src/catalog/style-groups.mjs +54 -0
  63. package/src/catalog/tests.mjs +127 -0
  64. package/src/catalog/text.mjs +200 -0
  65. package/src/catalog/ui-state.mjs +42 -0
  66. package/src/catalog.mjs +103 -0
  67. package/src/check/runtime.mjs +280 -0
  68. package/src/check.mjs +321 -0
  69. package/src/cli/authoring.mjs +213 -0
  70. package/src/cli/project.mjs +302 -0
  71. package/src/cli.mjs +340 -0
  72. package/src/config.d.ts +1 -0
  73. package/src/config.mjs +5 -0
  74. package/src/contributions.mjs +119 -0
  75. package/src/db-layer.mjs +41 -0
  76. package/src/deploy/cdn.mjs +142 -0
  77. package/src/deploy/files.mjs +266 -0
  78. package/src/deploy/fit.mjs +166 -0
  79. package/src/deploy/hosts.mjs +126 -0
  80. package/src/deploy/index/cli.mjs +20 -0
  81. package/src/deploy/index/environment.mjs +69 -0
  82. package/src/deploy/index/steps.mjs +87 -0
  83. package/src/deploy/index.mjs +387 -0
  84. package/src/deploy/needs.mjs +149 -0
  85. package/src/design-checks/accessible-names.mjs +62 -0
  86. package/src/design-checks/contrast.mjs +43 -0
  87. package/src/design-checks/csp.mjs +60 -0
  88. package/src/design-checks/files.mjs +59 -0
  89. package/src/design-checks/heading-links.mjs +61 -0
  90. package/src/design-checks/interaction.mjs +65 -0
  91. package/src/design-checks/jsx.mjs +67 -0
  92. package/src/design-checks/layout-values.mjs +68 -0
  93. package/src/design-checks/native-controls.mjs +48 -0
  94. package/src/design-checks/outline.mjs +39 -0
  95. package/src/design-checks/reporter.mjs +37 -0
  96. package/src/design-checks/rules.mjs +274 -0
  97. package/src/design-checks/values.mjs +262 -0
  98. package/src/design-checks.mjs +49 -0
  99. package/src/diag.mjs +98 -0
  100. package/src/docs.mjs +228 -0
  101. package/src/doctor.mjs +42 -0
  102. package/src/files.mjs +60 -0
  103. package/src/fix.mjs +48 -0
  104. package/src/gate/after.mjs +40 -0
  105. package/src/gate/before.mjs +54 -0
  106. package/src/gate/classify.mjs +72 -0
  107. package/src/gate/mutation.mjs +209 -0
  108. package/src/gate/resolve-cases.mjs +22 -0
  109. package/src/gate/result.mjs +63 -0
  110. package/src/gate/run.mjs +75 -0
  111. package/src/gate.mjs +178 -0
  112. package/src/index.mjs +9 -0
  113. package/src/infer/box-props.cjs +140 -0
  114. package/src/infer/core-boxes.cjs +46 -0
  115. package/src/infer/derived-writes.cjs +95 -0
  116. package/src/infer/engine/checks.cjs +193 -0
  117. package/src/infer/engine/flow.cjs +293 -0
  118. package/src/infer/engine/report.cjs +158 -0
  119. package/src/infer/engine/utils.cjs +15 -0
  120. package/src/infer/engine.cjs +265 -0
  121. package/src/infer/facts/sites.cjs +114 -0
  122. package/src/infer/facts.cjs +345 -0
  123. package/src/infer/hosts.cjs +161 -0
  124. package/src/infer/lsp.mjs +240 -0
  125. package/src/infer/session.cjs +161 -0
  126. package/src/infer/shadow.cjs +54 -0
  127. package/src/infer/worker.cjs +145 -0
  128. package/src/jsx-scan.mjs +128 -0
  129. package/src/kerf.mjs +98 -0
  130. package/src/load-config/schema.mjs +300 -0
  131. package/src/load-config.mjs +302 -0
  132. package/src/map.mjs +177 -0
  133. package/src/mcp/browser-tools.mjs +155 -0
  134. package/src/mcp.mjs +273 -0
  135. package/src/meta-table.mjs +63 -0
  136. package/src/mutate.mjs +224 -0
  137. package/src/plugin-checks.mjs +118 -0
  138. package/src/plugins.mjs +258 -0
  139. package/src/project.mjs +310 -0
  140. package/src/props-checks.mjs +64 -0
  141. package/src/props-listener.mjs +56 -0
  142. package/src/react-checks.mjs +67 -0
  143. package/src/server-state-checks.mjs +34 -0
  144. package/src/share-checks.mjs +158 -0
  145. package/src/site.mjs +46 -0
  146. package/src/slots.mjs +181 -0
  147. package/src/start/build.mjs +116 -0
  148. package/src/start/routes.mjs +38 -0
  149. package/src/start.mjs +394 -0
  150. package/src/state-checks/core-state.mjs +223 -0
  151. package/src/state-checks/modules.mjs +17 -0
  152. package/src/state-checks/scan.mjs +232 -0
  153. package/src/state-checks/server-graph.mjs +65 -0
  154. package/src/state-checks.mjs +189 -0
  155. package/src/style/find.mjs +121 -0
  156. package/src/style.mjs +327 -0
  157. package/src/surface-checks.mjs +384 -0
  158. package/src/tailwind.mjs +23 -0
  159. package/src/tasks.mjs +261 -0
  160. package/src/test-rules/context.mjs +109 -0
  161. package/src/test-rules/file-rules.mjs +279 -0
  162. package/src/test-rules/rule-helpers.mjs +244 -0
  163. package/src/test-rules/syntax.mjs +223 -0
  164. package/src/test-rules/test-rules.mjs +127 -0
  165. package/src/testdiff.mjs +219 -0
  166. package/src/tests.mjs +120 -0
  167. package/src/text-checks.mjs +332 -0
  168. package/src/tokens.mjs +33 -0
  169. package/src/trust/scan.mjs +95 -0
  170. package/src/trust.mjs +394 -0
  171. package/src/workspace.mjs +182 -0
  172. package/ts-plugin/context.js +57 -0
  173. package/ts-plugin/index.js +17 -0
  174. package/ts-plugin/mapping.js +65 -0
  175. package/ts-plugin/package.json +1 -0
  176. package/ts-plugin/proxy.js +144 -0
@@ -0,0 +1,332 @@
1
+ // Phase 2's X-codes in `kerf check` (research 11 §8.5, research 26 §7): responsive type and zoom (X005, X006, X008),
2
+ // the font build (X020–X023), media inside query boundaries (X030, X034), and refetches and streams (X033, X035,
3
+ // research 31 §7). X031, X032 and X036 are reported at runtime in dev. Reads .css and .html files and the JSX of the checked files; X022 also reads the font build's
4
+ // report in `.kerf/fonts.json`.
5
+
6
+ import { existsSync, readFileSync } from 'node:fs'
7
+ import { basename, join } from 'node:path'
8
+ import { diagnostic, positions } from './diag.mjs'
9
+ import { discover } from './files.mjs'
10
+ import { scanJsx } from './jsx-scan.mjs'
11
+
12
+ const FLUID = /(-?[\d.]+)(vw|vi|cqi|cqw|vmin|vmax)\b/
13
+ const MAX_RATIO = 2.5
14
+
15
+ /** Comments blanked out, so offsets stay those of the file. */
16
+ const stripComments = (css) => css.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
17
+
18
+ /** `clamp(rem, rem + cqi, rem)` for a viewport/container-only size, from its px at 375 and 1280 (ratio ≤ 2.5). */
19
+ export function fluidFor(n) {
20
+ const at = (w) => (n * w) / 100
21
+ const min = at(375)
22
+ const max = Math.min(at(1280), min * MAX_RATIO)
23
+ const slope = (max - min) / (1280 - 375)
24
+ const r = (x) => Math.round(x * 1000) / 1000
25
+ return `clamp(${r(min / 16)}rem, calc(${r((min - slope * 375) / 16)}rem + ${r(slope * 100)}cqi), ${r(max / 16)}rem)`
26
+ }
27
+
28
+ /** X005/X006 for one `font-size` value; `at` is its offset in the file. */
29
+ function fontSize(value, at, report) {
30
+ if (/var\(/.test(value)) return // tokens: checked where they're defined
31
+ const fluid = FLUID.exec(value)
32
+ if (fluid && !/rem\b/.test(value)) {
33
+ const only = new RegExp(`^\\s*${fluid[0].replace(/[.]/g, '\\.')}\\s*$`).test(value)
34
+ report(
35
+ 'X005',
36
+ at,
37
+ at + value.length,
38
+ `\`font-size: ${value.trim()}\` scales with the ${fluid[2].startsWith('c') ? 'container' : 'viewport'} only: browser zoom and the user's font size don't change it (WCAG 1.4.4).`,
39
+ only
40
+ ? {
41
+ description: 'Use a fluid clamp with rem ends',
42
+ safe: false,
43
+ text: fluidFor(Number(fluid[1])),
44
+ }
45
+ : undefined,
46
+ )
47
+ }
48
+ const clamp = /^\s*clamp\(\s*([\d.]+)(rem|px)\s*,[\s\S]*,\s*([\d.]+)(rem|px)\s*\)\s*$/.exec(value)
49
+ if (clamp && clamp[2] === clamp[4] && Number(clamp[1]) > 0) {
50
+ const ratio = Number(clamp[3]) / Number(clamp[1])
51
+ if (ratio > MAX_RATIO)
52
+ report(
53
+ 'X006',
54
+ at,
55
+ at + value.length,
56
+ `This fluid size grows ${ratio.toFixed(2)}× from its minimum to its maximum; above 2.5×, 200% zoom no longer doubles the text (WCAG 1.4.4).`,
57
+ )
58
+ }
59
+ }
60
+
61
+ const faceFamily = (body) => /font-family\s*:\s*(['"]?)([^;'"]+)\1/.exec(body)?.[2].trim()
62
+
63
+ /** Every `@font-face` in a CSS text: family, body and offsets. */
64
+ function faces(css) {
65
+ const out = []
66
+ for (const m of css.matchAll(/@font-face\s*\{([^}]*)\}/g))
67
+ out.push({
68
+ family: faceFamily(m[1]),
69
+ body: m[1],
70
+ start: m.index,
71
+ bodyStart: m.index + m[0].indexOf('{') + 1,
72
+ end: m.index + m[0].length,
73
+ })
74
+ return out
75
+ }
76
+
77
+ /**
78
+ * @param {string} root
79
+ * @param {{ files: { rel: string, text: string }[], exclude?: string[], fontBuild?: boolean }} options files: the checked .tsx/.jsx files
80
+ */
81
+ export function textChecks(root, { files, exclude = [], fontBuild = false }) {
82
+ const out = []
83
+ const sheets = discover(root, { include: ['**/*.css'], exclude }).map((rel) => ({
84
+ rel,
85
+ text: readFileSync(join(root, rel), 'utf8'),
86
+ }))
87
+ const pages = discover(root, { include: ['**/*.html'], exclude }).map((rel) => ({
88
+ rel,
89
+ text: readFileSync(join(root, rel), 'utf8'),
90
+ }))
91
+ const fontsJson = join(root, '.kerf', 'fonts.json')
92
+ const built = existsSync(fontsJson) ? JSON.parse(readFileSync(fontsJson, 'utf8')) : null
93
+
94
+ const reporter = makeReporter(out)
95
+
96
+ const faceUrls = checkSheets({ sheets, built, fontBuild, reporter, out })
97
+
98
+ // -- Markup: viewport meta, font preloads, media in boundaries, pending timers --
99
+ const viewport = (file, start, end, content, valueStart) => {
100
+ const parts = content
101
+ .split(',')
102
+ .map((s) => s.trim())
103
+ .filter(Boolean)
104
+ const bad = parts.filter(
105
+ (p) =>
106
+ /^user-scalable\s*=\s*(no|0)$/i.test(p) ||
107
+ (/^maximum-scale\s*=/i.test(p) && parseFloat(p.split('=')[1]) < 2),
108
+ )
109
+ if (!bad.length) return
110
+ const keep = parts.filter((p) => !bad.includes(p)).join(', ')
111
+ reporter(file)(
112
+ 'X008',
113
+ start,
114
+ end,
115
+ `The viewport meta blocks zoom (\`${bad.join('`, `')}\`): people who need larger text can't pinch to get it (WCAG 1.4.4).`,
116
+ {
117
+ description: 'Remove the zoom limits',
118
+ safe: true,
119
+ text: keep,
120
+ at: [valueStart, valueStart + content.length],
121
+ },
122
+ )
123
+ }
124
+ checkPages(pages, viewport, preload)
125
+
126
+ /** X023: a font preload no `@font-face` uses (the font build's own preloads live under its asset URLs). */
127
+ function preload(file, href, start, end) {
128
+ if (
129
+ href &&
130
+ !faceUrls.has(basename(href.split('?')[0])) &&
131
+ !href.includes('@kerf-assets') &&
132
+ !href.startsWith('/assets/')
133
+ ) {
134
+ reporter(file)(
135
+ 'X023',
136
+ start,
137
+ end,
138
+ `This preloads \`${href}\`, which no \`@font-face\` uses: the bytes compete with the page's own critical requests.`,
139
+ )
140
+ }
141
+ }
142
+
143
+ checkJsx(files, reporter, viewport, preload)
144
+ return out
145
+ }
146
+
147
+ function makeReporter(out) {
148
+ const reporter = (file) => {
149
+ const pos = positions(file.text)
150
+ /** `fix.at`: the replaced range when it isn't the flagged one. */
151
+ return (code, start, end, why, fix) => {
152
+ const [a, b] = fix?.at ?? [start, end]
153
+ out.push(
154
+ diagnostic({
155
+ code,
156
+ file: file.rel,
157
+ span: pos.span(start, end),
158
+ offset: [start, end],
159
+ why,
160
+ fix: fix && {
161
+ description: fix.description,
162
+ safe: fix.safe,
163
+ edits: [{ file: file.rel, span: pos.span(a, b), offset: [a, b], text: fix.text }],
164
+ },
165
+ }),
166
+ )
167
+ }
168
+ }
169
+ return reporter
170
+ }
171
+
172
+ function checkSheets({ sheets, built, fontBuild, reporter, out }) {
173
+ // -- CSS: font sizes and font faces --
174
+ const allFaces = sheets.flatMap((s) =>
175
+ faces(stripComments(s.text)).map((f) => ({ ...f, sheet: s })),
176
+ )
177
+ const families = new Set(allFaces.map((f) => f.family).filter(Boolean))
178
+ const faceUrls = new Set(
179
+ allFaces.flatMap((f) =>
180
+ [...f.body.matchAll(/url\(\s*['"]?([^'")]+)/g)].map((m) => basename(m[1].split('?')[0])),
181
+ ),
182
+ )
183
+ for (const s of sheets) {
184
+ const css = stripComments(s.text)
185
+ const report = reporter(s)
186
+ for (const m of css.matchAll(/(?<![\w-])font-size\s*:\s*([^;}]+)/g))
187
+ fontSize(m[1], m.index + m[0].indexOf(m[1], m[0].indexOf(':')), report)
188
+ for (const f of faces(css)) {
189
+ const display = /font-display\s*:\s*(block|auto)\b/.exec(f.body)
190
+ if (display) {
191
+ const at = f.bodyStart + display.index + display[0].lastIndexOf(display[1])
192
+ report(
193
+ 'X021',
194
+ at,
195
+ at + display[1].length,
196
+ `\`font-display: ${display[1]}\` hides text for up to 3 s while the font loads; with a matched fallback face, \`swap\` shifts nothing.`,
197
+ { description: 'Use swap', safe: true, text: 'swap' },
198
+ )
199
+ }
200
+ const local = /url\(\s*['"]?(?!https?:|data:)/.test(f.body)
201
+ if (!local || !f.family || f.family.endsWith('Fallback')) continue
202
+ if (!families.has(`${f.family} Fallback`)) {
203
+ const how = fontBuild
204
+ ? 'declare it in `text({ fonts })`, which generates the fallback'
205
+ : 'add `@kerf-js/text` and declare it in `text({ fonts })`'
206
+ report(
207
+ 'X020',
208
+ f.start,
209
+ f.start + '@font-face'.length,
210
+ `\`@font-face\` for "${f.family}" has no "${f.family} Fallback" face: the swap from the system font shifts layout. Fix: ${how}.`,
211
+ )
212
+ }
213
+ if (!/unicode-range\s*:/.test(f.body))
214
+ report(
215
+ 'X022',
216
+ f.start,
217
+ f.start + '@font-face'.length,
218
+ `"${f.family}" ships the whole font file: no \`unicode-range\`, so it isn't subset.`,
219
+ )
220
+ }
221
+ }
222
+ for (const [code, why] of built?.report?.overBudget ?? []) {
223
+ if (code === 'X022') out.push(diagnostic({ code: 'X022', file: '.kerf/fonts.json', why }))
224
+ }
225
+
226
+ return faceUrls
227
+ }
228
+
229
+ function checkPages(pages, viewport, preload) {
230
+ for (const p of pages) {
231
+ for (const m of p.text.matchAll(/<meta\b[^>]*name\s*=\s*["']viewport["'][^>]*>/gi)) {
232
+ const c = /content\s*=\s*(["'])([^"']*)\1/i.exec(m[0])
233
+ if (c)
234
+ viewport(p, m.index, m.index + m[0].length, c[2], m.index + c.index + c[0].indexOf(c[2]))
235
+ }
236
+ for (const m of p.text.matchAll(/<link\b[^>]*rel\s*=\s*["']preload["'][^>]*>/gi)) {
237
+ if (/\bas\s*=\s*["']font["']/i.test(m[0]))
238
+ preload(p, /href\s*=\s*["']([^"']+)/i.exec(m[0])?.[1], m.index, m.index + m[0].length)
239
+ }
240
+ }
241
+ }
242
+
243
+ function checkJsx(files, reporter, viewport, preload) {
244
+ for (const f of files) {
245
+ if (!/\.[jt]sx$/.test(f.rel)) continue
246
+ let scan
247
+ try {
248
+ scan = scanJsx(f.text, f.rel)
249
+ } catch {
250
+ continue // syntax errors are W000's
251
+ }
252
+ const report = reporter(f)
253
+ // X033: `x.pending ? <Skeleton /> : …`, with no `x.value` test beside it.
254
+ for (const m of f.text.matchAll(
255
+ /\b([\w$]+)\.pending\s*(?:\?|&&)\s*\(?\s*<\s*(Shape|Skeleton|Spinner|Progress)\b/g,
256
+ )) {
257
+ const line = f.text.slice(f.text.lastIndexOf('\n', m.index) + 1, m.index)
258
+ if (!line.includes(`${m[1]}.value`))
259
+ report(
260
+ 'X033',
261
+ m.index,
262
+ m.index + m[0].length,
263
+ `\`${m[1]}.pending\` is true on every refetch too, so this <${m[2]}> flashes over a value that's already on screen.`,
264
+ )
265
+ }
266
+ for (const el of scan.elements) {
267
+ checkElement({ el, f, scan, report, viewport, preload })
268
+ }
269
+ }
270
+ }
271
+
272
+ function checkElement({ el, f, scan, report, viewport, preload }) {
273
+ const style = el.attr('style')
274
+ if (style) {
275
+ if (style.kind === 'string')
276
+ for (const m of style.value.matchAll(/font-size\s*:\s*([^;]+)/g))
277
+ fontSize(m[1], style.valueStart + 1 + m.index + m[0].indexOf(m[1]), report)
278
+ else
279
+ for (const m of (style.expr ?? '').matchAll(/fontSize\s*:\s*(['"`])([^'"`]+)\1/g))
280
+ fontSize(m[2], style.valueStart + m.index + m[0].indexOf(m[2]), report)
281
+ }
282
+ if (
283
+ el.name === 'meta' &&
284
+ el.attr('name')?.value === 'viewport' &&
285
+ el.attr('content')?.kind === 'string'
286
+ ) {
287
+ const c = el.attr('content')
288
+ viewport(f, el.start, el.end, c.value, c.valueStart + 1)
289
+ }
290
+ if (
291
+ el.name === 'link' &&
292
+ el.attr('rel')?.value === 'preload' &&
293
+ el.attr('as')?.value === 'font'
294
+ ) {
295
+ preload(f, el.attr('href')?.value, el.start, el.end)
296
+ }
297
+ if (['img', 'video', 'iframe'].includes(el.name) && el.ancestors.includes('Boundary')) {
298
+ const sized =
299
+ (el.attr('width') && el.attr('height')) ||
300
+ /aspect-?ratio/i.test(style?.value ?? style?.expr ?? '')
301
+ if (!sized)
302
+ report(
303
+ 'X030',
304
+ el.start,
305
+ el.tagEnd,
306
+ `<${el.name}> inside a <Boundary> has no \`width\`/\`height\` or \`aspect-ratio\`: it renders 0 px tall, then pushes the content down when it loads.`,
307
+ )
308
+ }
309
+ if (/overflow(-[xy]|Y|X)?\s*:\s*['"]?(auto|scroll)/.test(style?.value ?? style?.expr ?? '')) {
310
+ const live = /\{\s*(\w+)\.value\b/.exec(el.children)?.[1]
311
+ if (live && new RegExp(`\\b${live}\\s*=\\s*[^\\n]*\\bstream\\b`).test(f.text))
312
+ report(
313
+ 'X035',
314
+ el.start,
315
+ el.tagEnd,
316
+ `\`${live}\` is streamed straight into a scroll container: as its tail reflows, the lines above it move.`,
317
+ )
318
+ }
319
+ const pending = el.name === 'Boundary' && el.attr('pending')
320
+ if (pending?.kind === 'expr') {
321
+ const tag = /^<\s*([A-Z][\w.]*)/.exec(pending.expr)?.[1]
322
+ const timers = /\b(setTimeout|setInterval)\s*\(/
323
+ if (timers.test(pending.expr) || (tag && timers.test(scan.functions.get(tag) ?? ''))) {
324
+ report(
325
+ 'X034',
326
+ pending.start,
327
+ pending.end,
328
+ `This \`pending\` fallback${tag ? ` (<${tag}>)` : ''} runs its own timers: <Boundary> already waits 150 ms before showing it and holds it 400 ms, and a second clock makes it flash.`,
329
+ )
330
+ }
331
+ }
332
+ }
package/src/tokens.mjs ADDED
@@ -0,0 +1,33 @@
1
+ // `kerf tokens`: veneer's resolved tokens as DTCG 2025.10 (PLAN §6, research 37), for Figma, Style Dictionary and Terrazzo.
2
+ // veneer owns the values, so this asks the app's veneer, with the app's seed and overrides, and writes what it returns.
3
+
4
+ import { mkdirSync, writeFileSync } from 'node:fs'
5
+ import { createRequire } from 'node:module'
6
+ import { join, resolve } from 'node:path'
7
+ import { pathToFileURL } from 'node:url'
8
+ import { loadConfig } from './load-config.mjs'
9
+
10
+ export async function writeTokens(root) {
11
+ root = resolve(root)
12
+ let plugin
13
+ try {
14
+ plugin = await import(
15
+ pathToFileURL(createRequire(join(root, 'package.json')).resolve('@kerf-js/veneer/plugin'))
16
+ .href
17
+ )
18
+ } catch {
19
+ throw Object.assign(
20
+ new Error("`kerf tokens` exports veneer's tokens, and this app has no @kerf-js/veneer"),
21
+ { code: 'E_NO_VENEER' },
22
+ )
23
+ }
24
+ const css =
25
+ (await loadConfig(root)).contributions.find((c) => c.name === '@kerf-js/veneer')?.manifest
26
+ .tokens?.css ?? ''
27
+ const dir = join(root, '.kerf', 'tokens')
28
+ mkdirSync(dir, { recursive: true })
29
+ const files = plugin.dtcg(css)
30
+ for (const [name, content] of Object.entries(files))
31
+ writeFileSync(join(dir, name), `${JSON.stringify(content, null, 2)}\n`)
32
+ return { ok: true, dir: '.kerf/tokens', files: Object.keys(files) }
33
+ }
@@ -0,0 +1,95 @@
1
+ import { builtinModules } from 'node:module'
2
+ import ts from 'typescript'
3
+
4
+ const builtins = new Set(builtinModules)
5
+ const NEEDS = {
6
+ fs: 'fs',
7
+ 'fs/promises': 'fs',
8
+ child_process: 'spawn',
9
+ cluster: 'spawn',
10
+ net: 'net',
11
+ tls: 'net',
12
+ dgram: 'net',
13
+ dns: 'net',
14
+ 'dns/promises': 'net',
15
+ http: 'net',
16
+ https: 'net',
17
+ http2: 'net',
18
+ }
19
+ const NET_GLOBALS = new Set(['fetch', 'WebSocket', 'EventSource'])
20
+ /** Bundlers inline it, and it says nothing about the machine. */
21
+ const AMBIENT_ENV = new Set(['NODE_ENV'])
22
+
23
+ /** What one file does directly, and what it imports. */
24
+ export function scanFile(text, file) {
25
+ const sf = ts.createSourceFile(
26
+ file,
27
+ text,
28
+ ts.ScriptTarget.Latest,
29
+ true,
30
+ /\.[cm]?tsx?$/.test(file) ? ts.ScriptKind.TSX : ts.ScriptKind.JSX,
31
+ )
32
+ const uses = []
33
+ const imports = []
34
+ const use = (need, node) =>
35
+ uses.push({ need, line: sf.getLineAndCharacterOfPosition(node.getStart(sf)).line + 1 })
36
+ const specifier = (name, node) => {
37
+ const bare = name.replace(/^node:/, '')
38
+ if (name.endsWith('.node')) use('native', node)
39
+ else if (builtins.has(bare)) NEEDS[bare] && use(NEEDS[bare], node)
40
+ else imports.push(name)
41
+ }
42
+ const isProcess = (node) => ts.isIdentifier(node) && node.text === 'process'
43
+ const visit = (node) => {
44
+ if (
45
+ (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) &&
46
+ node.moduleSpecifier &&
47
+ ts.isStringLiteralLike(node.moduleSpecifier)
48
+ ) {
49
+ if (!(node.importClause?.isTypeOnly || node.isTypeOnly))
50
+ specifier(node.moduleSpecifier.text, node)
51
+ } else if (ts.isCallExpression(node)) {
52
+ const callee = node.expression
53
+ const [first] = node.arguments
54
+ if (
55
+ (callee.kind === ts.SyntaxKind.ImportKeyword ||
56
+ (ts.isIdentifier(callee) && callee.text === 'require')) &&
57
+ first &&
58
+ ts.isStringLiteralLike(first)
59
+ )
60
+ specifier(first.text, node)
61
+ else if (ts.isIdentifier(callee) && NET_GLOBALS.has(callee.text)) use('net', node)
62
+ else if (
63
+ ts.isPropertyAccessExpression(callee) &&
64
+ isProcess(callee.expression) &&
65
+ callee.name.text === 'dlopen'
66
+ )
67
+ use('native', node)
68
+ } else if (
69
+ ts.isNewExpression(node) &&
70
+ ts.isIdentifier(node.expression) &&
71
+ NET_GLOBALS.has(node.expression.text)
72
+ )
73
+ use('net', node)
74
+ else if (
75
+ ts.isPropertyAccessExpression(node) &&
76
+ isProcess(node.expression) &&
77
+ node.name.text === 'env'
78
+ ) {
79
+ const parent = node.parent
80
+ if (ts.isPropertyAccessExpression(parent) && parent.expression === node)
81
+ AMBIENT_ENV.has(parent.name.text) || use(`env:${parent.name.text}`, node)
82
+ else if (
83
+ ts.isElementAccessExpression(parent) &&
84
+ parent.expression === node &&
85
+ ts.isStringLiteralLike(parent.argumentExpression)
86
+ )
87
+ AMBIENT_ENV.has(parent.argumentExpression.text) ||
88
+ use(`env:${parent.argumentExpression.text}`, node)
89
+ else use('env:*', node)
90
+ }
91
+ ts.forEachChild(node, visit)
92
+ }
93
+ visit(sf)
94
+ return { uses, imports }
95
+ }