@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.
- package/LICENSE +16 -0
- package/README.md +554 -2
- package/bin/kerf.mjs +15 -0
- package/docs/async.md +50 -0
- package/docs/browser.md +53 -0
- package/docs/config.md +127 -0
- package/docs/core.md +30 -0
- package/docs/deploy.md +108 -0
- package/docs/design.md +106 -0
- package/docs/entrypoints.md +83 -0
- package/docs/fonts.md +118 -0
- package/docs/icons.md +108 -0
- package/docs/lists.md +25 -0
- package/docs/meta.md +92 -0
- package/docs/plugins.md +166 -0
- package/docs/props.md +88 -0
- package/docs/reactivity.md +26 -0
- package/docs/rules.md +39 -0
- package/docs/rust.md +253 -0
- package/docs/start-short.md +10 -0
- package/docs/start.md +642 -0
- package/docs/state.md +122 -0
- package/docs/style.md +82 -0
- package/docs/surface.md +158 -0
- package/docs/testing.md +81 -0
- package/docs/theme.md +263 -0
- package/docs/ui-state.md +105 -0
- package/docs/verify.md +61 -0
- package/package.json +47 -3
- package/src/add/config.mjs +93 -0
- package/src/add/errors.mjs +2 -0
- package/src/add/setup.mjs +88 -0
- package/src/add-rust.mjs +178 -0
- package/src/add.mjs +392 -0
- package/src/agents-md.mjs +240 -0
- package/src/alias.mjs +70 -0
- package/src/audit/page.mjs +313 -0
- package/src/audit/report.mjs +69 -0
- package/src/audit/utils.mjs +5 -0
- package/src/audit.mjs +319 -0
- package/src/browser/cli.mjs +53 -0
- package/src/browser/client.mjs +299 -0
- package/src/browser/commands.mjs +258 -0
- package/src/capture.mjs +21 -0
- package/src/catalog/audit.mjs +129 -0
- package/src/catalog/auth.mjs +40 -0
- package/src/catalog/compiler.mjs +337 -0
- package/src/catalog/contracts.mjs +18 -0
- package/src/catalog/database.mjs +52 -0
- package/src/catalog/deploy.mjs +117 -0
- package/src/catalog/design.mjs +159 -0
- package/src/catalog/forms.mjs +25 -0
- package/src/catalog/hydration.mjs +29 -0
- package/src/catalog/icons.mjs +24 -0
- package/src/catalog/media.mjs +163 -0
- package/src/catalog/metadata.mjs +116 -0
- package/src/catalog/plugins.mjs +228 -0
- package/src/catalog/realtime.mjs +195 -0
- package/src/catalog/rust.mjs +75 -0
- package/src/catalog/server-functions.mjs +100 -0
- package/src/catalog/state.mjs +39 -0
- package/src/catalog/style-groups.mjs +54 -0
- package/src/catalog/tests.mjs +127 -0
- package/src/catalog/text.mjs +200 -0
- package/src/catalog/ui-state.mjs +42 -0
- package/src/catalog.mjs +103 -0
- package/src/check/runtime.mjs +280 -0
- package/src/check.mjs +321 -0
- package/src/cli/authoring.mjs +213 -0
- package/src/cli/project.mjs +302 -0
- package/src/cli.mjs +340 -0
- package/src/config.d.ts +1 -0
- package/src/config.mjs +5 -0
- package/src/contributions.mjs +119 -0
- package/src/db-layer.mjs +41 -0
- package/src/deploy/cdn.mjs +142 -0
- package/src/deploy/files.mjs +266 -0
- package/src/deploy/fit.mjs +166 -0
- package/src/deploy/hosts.mjs +126 -0
- package/src/deploy/index/cli.mjs +20 -0
- package/src/deploy/index/environment.mjs +69 -0
- package/src/deploy/index/steps.mjs +87 -0
- package/src/deploy/index.mjs +387 -0
- package/src/deploy/needs.mjs +149 -0
- package/src/design-checks/accessible-names.mjs +62 -0
- package/src/design-checks/contrast.mjs +43 -0
- package/src/design-checks/csp.mjs +60 -0
- package/src/design-checks/files.mjs +59 -0
- package/src/design-checks/heading-links.mjs +61 -0
- package/src/design-checks/interaction.mjs +65 -0
- package/src/design-checks/jsx.mjs +67 -0
- package/src/design-checks/layout-values.mjs +68 -0
- package/src/design-checks/native-controls.mjs +48 -0
- package/src/design-checks/outline.mjs +39 -0
- package/src/design-checks/reporter.mjs +37 -0
- package/src/design-checks/rules.mjs +274 -0
- package/src/design-checks/values.mjs +262 -0
- package/src/design-checks.mjs +49 -0
- package/src/diag.mjs +98 -0
- package/src/docs.mjs +228 -0
- package/src/doctor.mjs +42 -0
- package/src/files.mjs +60 -0
- package/src/fix.mjs +48 -0
- package/src/gate/after.mjs +40 -0
- package/src/gate/before.mjs +54 -0
- package/src/gate/classify.mjs +72 -0
- package/src/gate/mutation.mjs +209 -0
- package/src/gate/resolve-cases.mjs +22 -0
- package/src/gate/result.mjs +63 -0
- package/src/gate/run.mjs +75 -0
- package/src/gate.mjs +178 -0
- package/src/index.mjs +9 -0
- package/src/infer/box-props.cjs +140 -0
- package/src/infer/core-boxes.cjs +46 -0
- package/src/infer/derived-writes.cjs +95 -0
- package/src/infer/engine/checks.cjs +193 -0
- package/src/infer/engine/flow.cjs +293 -0
- package/src/infer/engine/report.cjs +158 -0
- package/src/infer/engine/utils.cjs +15 -0
- package/src/infer/engine.cjs +265 -0
- package/src/infer/facts/sites.cjs +114 -0
- package/src/infer/facts.cjs +345 -0
- package/src/infer/hosts.cjs +161 -0
- package/src/infer/lsp.mjs +240 -0
- package/src/infer/session.cjs +161 -0
- package/src/infer/shadow.cjs +54 -0
- package/src/infer/worker.cjs +145 -0
- package/src/jsx-scan.mjs +128 -0
- package/src/kerf.mjs +98 -0
- package/src/load-config/schema.mjs +300 -0
- package/src/load-config.mjs +302 -0
- package/src/map.mjs +177 -0
- package/src/mcp/browser-tools.mjs +155 -0
- package/src/mcp.mjs +273 -0
- package/src/meta-table.mjs +63 -0
- package/src/mutate.mjs +224 -0
- package/src/plugin-checks.mjs +118 -0
- package/src/plugins.mjs +258 -0
- package/src/project.mjs +310 -0
- package/src/props-checks.mjs +64 -0
- package/src/props-listener.mjs +56 -0
- package/src/react-checks.mjs +67 -0
- package/src/server-state-checks.mjs +34 -0
- package/src/share-checks.mjs +158 -0
- package/src/site.mjs +46 -0
- package/src/slots.mjs +181 -0
- package/src/start/build.mjs +116 -0
- package/src/start/routes.mjs +38 -0
- package/src/start.mjs +394 -0
- package/src/state-checks/core-state.mjs +223 -0
- package/src/state-checks/modules.mjs +17 -0
- package/src/state-checks/scan.mjs +232 -0
- package/src/state-checks/server-graph.mjs +65 -0
- package/src/state-checks.mjs +189 -0
- package/src/style/find.mjs +121 -0
- package/src/style.mjs +327 -0
- package/src/surface-checks.mjs +384 -0
- package/src/tailwind.mjs +23 -0
- package/src/tasks.mjs +261 -0
- package/src/test-rules/context.mjs +109 -0
- package/src/test-rules/file-rules.mjs +279 -0
- package/src/test-rules/rule-helpers.mjs +244 -0
- package/src/test-rules/syntax.mjs +223 -0
- package/src/test-rules/test-rules.mjs +127 -0
- package/src/testdiff.mjs +219 -0
- package/src/tests.mjs +120 -0
- package/src/text-checks.mjs +332 -0
- package/src/tokens.mjs +33 -0
- package/src/trust/scan.mjs +95 -0
- package/src/trust.mjs +394 -0
- package/src/workspace.mjs +182 -0
- package/ts-plugin/context.js +57 -0
- package/ts-plugin/index.js +17 -0
- package/ts-plugin/mapping.js +65 -0
- package/ts-plugin/package.json +1 -0
- package/ts-plugin/proxy.js +144 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// Surfaces in `kerf check` (research 33 §8): X002 and X009–X012 for surfaces and their text, X040 and X041 for per-glyph
|
|
2
|
+
// markup, and P101 for a surface that needs a slot nothing provides. X001, X003 and X004 need the live mirror, so
|
|
3
|
+
// @kerf-js/core/surface reports those in dev. `listSurfaces` is the catalog behind `kerf surfaces` and the MCP tool.
|
|
4
|
+
|
|
5
|
+
import { readFileSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import ts from 'typescript'
|
|
8
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
9
|
+
import { discover } from './files.mjs'
|
|
10
|
+
import { scanJsx } from './jsx-scan.mjs'
|
|
11
|
+
|
|
12
|
+
const CODE = /\.[cm]?[jt]sx?$/
|
|
13
|
+
const SYSTEM = /(^|,)\s*["']?(system-ui|-apple-system|BlinkMacSystemFont)["']?\s*(,|$)/i
|
|
14
|
+
const GENERIC_ONLY = /^\s*(serif|sans-serif|monospace|cursive|fantasy)\s*$/i
|
|
15
|
+
const GLYPHS = 200
|
|
16
|
+
|
|
17
|
+
const parse = (rel, text) =>
|
|
18
|
+
ts.createSourceFile(
|
|
19
|
+
rel,
|
|
20
|
+
text,
|
|
21
|
+
ts.ScriptTarget.Latest,
|
|
22
|
+
true,
|
|
23
|
+
/\.[jt]sx$/.test(rel) ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
const prop = (object, name) =>
|
|
27
|
+
object.properties.find(
|
|
28
|
+
(p) =>
|
|
29
|
+
(ts.isPropertyAssignment(p) ||
|
|
30
|
+
ts.isMethodDeclaration(p) ||
|
|
31
|
+
ts.isShorthandPropertyAssignment(p)) &&
|
|
32
|
+
p.name.getText() === name,
|
|
33
|
+
)
|
|
34
|
+
const stringOf = (node) =>
|
|
35
|
+
node && (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node))
|
|
36
|
+
? node.text
|
|
37
|
+
: undefined
|
|
38
|
+
|
|
39
|
+
function walk(node, visit) {
|
|
40
|
+
visit(node)
|
|
41
|
+
ts.forEachChild(node, (c) => walk(c, visit))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Every `defineSurface({ ... })` call in a file: the call, its options and the name of `setup`'s `s`. */
|
|
45
|
+
function surfaceCalls(sf) {
|
|
46
|
+
const out = []
|
|
47
|
+
walk(sf, (node) => {
|
|
48
|
+
if (!ts.isCallExpression(node) || node.expression.getText() !== 'defineSurface') return
|
|
49
|
+
const def = node.arguments[0]
|
|
50
|
+
if (!def || !ts.isObjectLiteralExpression(def)) return
|
|
51
|
+
const setup = prop(def, 'setup')
|
|
52
|
+
const fn = setup && (ts.isMethodDeclaration(setup) ? setup : setup.initializer)
|
|
53
|
+
out.push({ node, def, fn, param: fn?.parameters?.[0]?.name.getText() })
|
|
54
|
+
})
|
|
55
|
+
return out
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function propNames(call) {
|
|
59
|
+
const schema = prop(call.def, 'props')?.initializer
|
|
60
|
+
if (
|
|
61
|
+
schema &&
|
|
62
|
+
ts.isCallExpression(schema) &&
|
|
63
|
+
schema.expression.getText().endsWith('.object') &&
|
|
64
|
+
ts.isObjectLiteralExpression(schema.arguments[0] ?? schema)
|
|
65
|
+
)
|
|
66
|
+
return schema.arguments[0].properties.map((p) => p.name.getText())
|
|
67
|
+
const type = call.node.typeArguments?.[0]
|
|
68
|
+
if (type && ts.isTypeLiteralNode(type))
|
|
69
|
+
return type.members.map((m) => m.name?.getText()).filter(Boolean)
|
|
70
|
+
return []
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What the project's `defineSurface` calls declare, read statically: nothing needs to build. */
|
|
74
|
+
export function listSurfaces(root, { exclude = [] } = {}) {
|
|
75
|
+
const out = []
|
|
76
|
+
for (const rel of discover(root, { include: ['src/**/*.{ts,tsx,js,jsx}'], exclude })) {
|
|
77
|
+
const text = readFileSync(join(root, rel), 'utf8')
|
|
78
|
+
if (!text.includes('defineSurface')) continue
|
|
79
|
+
const sf = parse(rel, text)
|
|
80
|
+
for (const call of surfaceCalls(sf)) {
|
|
81
|
+
const line = sf.getLineAndCharacterOfPosition(call.node.getStart(sf)).line + 1
|
|
82
|
+
out.push({
|
|
83
|
+
name: stringOf(prop(call.def, 'name')?.initializer) ?? null,
|
|
84
|
+
kind: stringOf(prop(call.def, 'kind')?.initializer) ?? null,
|
|
85
|
+
props: propNames(call),
|
|
86
|
+
file: rel,
|
|
87
|
+
line,
|
|
88
|
+
...(prop(call.def, 'pixel') ? { pixel: true } : {}),
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return out.sort((a, b) => String(a.name).localeCompare(String(b.name)))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** `[...x]`, `x.split('')` or `Array.from(x)`: the string being spread, or undefined. */
|
|
96
|
+
function glyphSource(node) {
|
|
97
|
+
if (
|
|
98
|
+
ts.isArrayLiteralExpression(node) &&
|
|
99
|
+
node.elements.length === 1 &&
|
|
100
|
+
ts.isSpreadElement(node.elements[0])
|
|
101
|
+
)
|
|
102
|
+
return node.elements[0].expression
|
|
103
|
+
if (!ts.isCallExpression(node)) return undefined
|
|
104
|
+
const callee = node.expression.getText()
|
|
105
|
+
if (
|
|
106
|
+
callee === 'Array.from' &&
|
|
107
|
+
node.arguments.length === 1 &&
|
|
108
|
+
!ts.isObjectLiteralExpression(node.arguments[0])
|
|
109
|
+
)
|
|
110
|
+
return node.arguments[0]
|
|
111
|
+
if (
|
|
112
|
+
ts.isPropertyAccessExpression(node.expression) &&
|
|
113
|
+
node.expression.name.text === 'split' &&
|
|
114
|
+
stringOf(node.arguments[0]) === ''
|
|
115
|
+
)
|
|
116
|
+
return node.expression.expression
|
|
117
|
+
return undefined
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The first `<span>` a `.map` callback returns. */
|
|
121
|
+
function returnedSpan(fn) {
|
|
122
|
+
let span
|
|
123
|
+
walk(fn.body, (n) => {
|
|
124
|
+
if (span) return
|
|
125
|
+
const open = ts.isJsxElement(n)
|
|
126
|
+
? n.openingElement
|
|
127
|
+
: ts.isJsxSelfClosingElement(n)
|
|
128
|
+
? n
|
|
129
|
+
: undefined
|
|
130
|
+
if (open?.tagName.getText() === 'span') span = open
|
|
131
|
+
})
|
|
132
|
+
return span
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const attrOf = (open, name) =>
|
|
136
|
+
open.attributes.properties.some((p) => !ts.isJsxSpreadAttribute(p) && p.name.getText() === name)
|
|
137
|
+
const spreads = (open) => open.attributes.properties.some(ts.isJsxSpreadAttribute)
|
|
138
|
+
|
|
139
|
+
/** The JSX element a child expression sits in. */
|
|
140
|
+
function jsxParent(node) {
|
|
141
|
+
for (let n = node.parent; n; n = n.parent) if (ts.isJsxElement(n)) return n.openingElement
|
|
142
|
+
return undefined
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A string literal, or a `const` in this file holding one. */
|
|
146
|
+
function knownString(sf, node) {
|
|
147
|
+
const direct = stringOf(node)
|
|
148
|
+
if (direct !== undefined) return direct
|
|
149
|
+
if (!ts.isIdentifier(node)) return undefined
|
|
150
|
+
let found
|
|
151
|
+
walk(sf, (n) => {
|
|
152
|
+
if (
|
|
153
|
+
ts.isVariableDeclaration(n) &&
|
|
154
|
+
n.name.getText() === node.text &&
|
|
155
|
+
ts.isVariableDeclarationList(n.parent) &&
|
|
156
|
+
n.parent.flags & ts.NodeFlags.Const
|
|
157
|
+
)
|
|
158
|
+
found ??= stringOf(n.initializer)
|
|
159
|
+
})
|
|
160
|
+
return found
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function glyphChecks(sf, report) {
|
|
164
|
+
walk(sf, (node) => {
|
|
165
|
+
if (
|
|
166
|
+
!ts.isCallExpression(node) ||
|
|
167
|
+
!ts.isPropertyAccessExpression(node.expression) ||
|
|
168
|
+
node.expression.name.text !== 'map'
|
|
169
|
+
)
|
|
170
|
+
return
|
|
171
|
+
const source = glyphSource(node.expression.expression)
|
|
172
|
+
const fn = node.arguments[0]
|
|
173
|
+
if (!source || !fn || !(ts.isArrowFunction(fn) || ts.isFunctionExpression(fn))) return
|
|
174
|
+
const span = returnedSpan(fn)
|
|
175
|
+
if (!span) return
|
|
176
|
+
const text = knownString(sf, source)
|
|
177
|
+
const count = text === undefined ? 0 : Array.from(text).length
|
|
178
|
+
if (count > GLYPHS)
|
|
179
|
+
report(
|
|
180
|
+
'X040',
|
|
181
|
+
node,
|
|
182
|
+
`Per-grapheme spans over ${count} graphemes: each is a DOM node, a style and a composited layer once it animates. Use word-level motion or a Surface.`,
|
|
183
|
+
)
|
|
184
|
+
const hidden = attrOf(span, 'aria-hidden') || spreads(span)
|
|
185
|
+
const parent = jsxParent(node)
|
|
186
|
+
const named =
|
|
187
|
+
!parent ||
|
|
188
|
+
attrOf(parent, 'aria-label') ||
|
|
189
|
+
attrOf(parent, 'aria-labelledby') ||
|
|
190
|
+
spreads(parent)
|
|
191
|
+
if (!hidden || !named)
|
|
192
|
+
report(
|
|
193
|
+
'X041',
|
|
194
|
+
span,
|
|
195
|
+
`Per-character spans read as separate letters to a screen reader${hidden ? '' : ': add `aria-hidden` to each span'}${named ? '' : `${hidden ? ':' : ', and'} give the parent an \`aria-label\` with the whole text`}.`,
|
|
196
|
+
)
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function surfaceFileChecks(sf, slots, report) {
|
|
201
|
+
const calls = surfaceCalls(sf)
|
|
202
|
+
if (!calls.length) return
|
|
203
|
+
const decorative = new Set()
|
|
204
|
+
walk(sf, (n) => {
|
|
205
|
+
if (
|
|
206
|
+
ts.isCallExpression(n) &&
|
|
207
|
+
ts.isPropertyAccessExpression(n.expression) &&
|
|
208
|
+
n.expression.name.text === 'decorative'
|
|
209
|
+
)
|
|
210
|
+
decorative.add(n)
|
|
211
|
+
})
|
|
212
|
+
const inside = (n) => {
|
|
213
|
+
for (let p = n.parent; p; p = p.parent) if (decorative.has(p)) return true
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
walk(sf, (n) => {
|
|
217
|
+
if (!ts.isCallExpression(n) || !ts.isPropertyAccessExpression(n.expression)) return
|
|
218
|
+
const method = n.expression.name.text
|
|
219
|
+
if ((method === 'fillText' || method === 'strokeText') && !inside(n))
|
|
220
|
+
report(
|
|
221
|
+
'X010',
|
|
222
|
+
n,
|
|
223
|
+
`\`${method}\` in a surface draws text only the canvas knows about: no selection, find, translation or screen reader. Use \`s.text()\`, or \`s.decorative()\` for text that is truly decoration.`,
|
|
224
|
+
)
|
|
225
|
+
if (
|
|
226
|
+
method === 'setAttribute' &&
|
|
227
|
+
stringOf(n.arguments[0]) === 'aria-hidden' &&
|
|
228
|
+
n.expression.expression.getText().endsWith('.el')
|
|
229
|
+
)
|
|
230
|
+
report(
|
|
231
|
+
'X011',
|
|
232
|
+
n,
|
|
233
|
+
'This hides the surface or its text mirror from assistive tech. Leave the mirror to kerf.',
|
|
234
|
+
)
|
|
235
|
+
})
|
|
236
|
+
for (const call of calls) {
|
|
237
|
+
const kindNode = prop(call.def, 'kind')?.initializer
|
|
238
|
+
const kind = stringOf(kindNode)
|
|
239
|
+
if (kind && slots && slots['surface.renderer'] && !slots['surface.renderer'][kind])
|
|
240
|
+
report(
|
|
241
|
+
'P101',
|
|
242
|
+
kindNode,
|
|
243
|
+
`Surface kind \`${kind}\` has no provider: add the plugin that provides it to \`plugins\` in kerf.config.ts.`,
|
|
244
|
+
)
|
|
245
|
+
if (!call.param || !call.fn) continue
|
|
246
|
+
walk(call.fn, (n) => {
|
|
247
|
+
if (!ts.isCallExpression(n) || n.expression.getText() !== `${call.param}.text`) return
|
|
248
|
+
const options = n.arguments[1]
|
|
249
|
+
const font =
|
|
250
|
+
options && ts.isObjectLiteralExpression(options)
|
|
251
|
+
? stringOf(prop(options, 'font')?.initializer)
|
|
252
|
+
: undefined
|
|
253
|
+
const family = font?.replace(/^.*?\d[\d.]*px(\/\S+)?\s*/, '')
|
|
254
|
+
if (family !== undefined && (SYSTEM.test(family) || GENERIC_ONLY.test(family)))
|
|
255
|
+
report(
|
|
256
|
+
'X002',
|
|
257
|
+
n.arguments[1],
|
|
258
|
+
`\`${font}\` can't be measured safely: \`system-ui\`, \`-apple-system\` and generic-only families resolve differently in canvas and CSS. Name a family.`,
|
|
259
|
+
)
|
|
260
|
+
if (kind === 'canvas2d' && slots && slots['text.measure'] === null)
|
|
261
|
+
report(
|
|
262
|
+
'P101',
|
|
263
|
+
n,
|
|
264
|
+
'Canvas `s.text()` needs `text.measure`, and nothing provides it: add `text()` from `@kerf-js/text` to `plugins` in kerf.config.ts.',
|
|
265
|
+
)
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const HIDES = /(?:display\s*:\s*none|visibility\s*:\s*hidden)/
|
|
271
|
+
|
|
272
|
+
/** X011/X012 in a stylesheet: rules aimed at the host or the mirror. */
|
|
273
|
+
function cssChecks(file, report) {
|
|
274
|
+
const css = file.text.replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
|
|
275
|
+
const rule = /([^{}]*\[data-surface(-text)?\b[^{}]*)\{([^}]*)\}/g
|
|
276
|
+
for (const m of css.matchAll(rule)) {
|
|
277
|
+
const at = m.index + m[0].indexOf('[data-surface')
|
|
278
|
+
const before = css.slice(0, m.index)
|
|
279
|
+
const open = before.lastIndexOf('@media')
|
|
280
|
+
const inPrint =
|
|
281
|
+
open >= 0 &&
|
|
282
|
+
/^@media[^{]*\bprint\b/.test(before.slice(open)) &&
|
|
283
|
+
(before.slice(open).match(/\{/g) ?? []).length >
|
|
284
|
+
(before.slice(open).match(/\}/g) ?? []).length
|
|
285
|
+
if (/forced-color-adjust\s*:\s*none/.test(m[3]))
|
|
286
|
+
report(
|
|
287
|
+
'X012',
|
|
288
|
+
at,
|
|
289
|
+
at + 13,
|
|
290
|
+
'`forced-color-adjust: none` on a surface keeps its author colours in forced-colours mode, where the mirror must take over.',
|
|
291
|
+
)
|
|
292
|
+
else if (inPrint && HIDES.test(m[3]))
|
|
293
|
+
report(
|
|
294
|
+
'X012',
|
|
295
|
+
at,
|
|
296
|
+
at + 13,
|
|
297
|
+
'This hides surface text when printing, where the mirror is the only copy that prints sharply.',
|
|
298
|
+
)
|
|
299
|
+
else if (m[2] && HIDES.test(m[3]))
|
|
300
|
+
report(
|
|
301
|
+
'X011',
|
|
302
|
+
at,
|
|
303
|
+
at + 18,
|
|
304
|
+
'This hides the text mirror from assistive tech, and it is the only copy a screen reader can read. Leave the mirror to kerf.',
|
|
305
|
+
)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {string} root
|
|
311
|
+
* @param {{ files: { rel: string, text: string }[], exclude?: string[], slots?: Record<string, any> }} options
|
|
312
|
+
*/
|
|
313
|
+
export function surfaceChecks(root, { files, exclude = [], slots }) {
|
|
314
|
+
const out = []
|
|
315
|
+
const reporter = (file) => {
|
|
316
|
+
const pos = positions(file.text)
|
|
317
|
+
return (code, start, end, why) =>
|
|
318
|
+
out.push(
|
|
319
|
+
diagnostic({ code, file: file.rel, span: pos.span(start, end), offset: [start, end], why }),
|
|
320
|
+
)
|
|
321
|
+
}
|
|
322
|
+
const sources = new Map(files.filter((f) => CODE.test(f.rel)).map((f) => [f.rel, f]))
|
|
323
|
+
for (const rel of discover(root, { include: ['src/**/*.{ts,tsx,js,jsx}'], exclude })) {
|
|
324
|
+
if (!sources.has(rel)) sources.set(rel, { rel, text: readFileSync(join(root, rel), 'utf8') })
|
|
325
|
+
}
|
|
326
|
+
for (const f of sources.values()) {
|
|
327
|
+
const glyphs = /\.[jt]sx$/.test(f.rel) && /\.map\(/.test(f.text) && /<span\b/.test(f.text)
|
|
328
|
+
const surfaces = f.text.includes('@kerf-js/core/surface')
|
|
329
|
+
if (!glyphs && !surfaces) continue
|
|
330
|
+
let sf
|
|
331
|
+
try {
|
|
332
|
+
sf = parse(f.rel, f.text)
|
|
333
|
+
} catch {
|
|
334
|
+
continue
|
|
335
|
+
}
|
|
336
|
+
const raw = reporter(f)
|
|
337
|
+
const report = (code, node, why) => raw(code, node.getStart(sf), node.end, why)
|
|
338
|
+
if (glyphs) glyphChecks(sf, report)
|
|
339
|
+
if (!surfaces) continue
|
|
340
|
+
surfaceFileChecks(sf, slots, report)
|
|
341
|
+
if (!/\.[jt]sx$/.test(f.rel)) continue
|
|
342
|
+
const scan = scanJsx(f.text, f.rel)
|
|
343
|
+
const local = scan.imports.find(
|
|
344
|
+
(i) => i.source === '@kerf-js/core/surface' && i.imported === 'Surface',
|
|
345
|
+
)?.local
|
|
346
|
+
for (const el of scan.elements) {
|
|
347
|
+
if (el.name !== local) continue
|
|
348
|
+
const spread = el.attrs.some((a) => a.kind === 'spread')
|
|
349
|
+
if (!el.attr('label') && !el.attr('decorative') && !spread)
|
|
350
|
+
raw(
|
|
351
|
+
'X009',
|
|
352
|
+
el.start,
|
|
353
|
+
el.tagEnd,
|
|
354
|
+
`<${el.name}> has no accessible name: a screen reader finds an unnamed image. Add \`label\`, or \`decorative\` if it shows nothing a reader needs.`,
|
|
355
|
+
)
|
|
356
|
+
const hidden = el.attr('aria-hidden')
|
|
357
|
+
if (hidden && hidden.value !== 'false')
|
|
358
|
+
raw(
|
|
359
|
+
'X011',
|
|
360
|
+
hidden.start,
|
|
361
|
+
hidden.end,
|
|
362
|
+
'This hides the surface and its text mirror from assistive tech. For a surface with nothing to read use `decorative`, which leaves mirrors alone.',
|
|
363
|
+
)
|
|
364
|
+
const style = el.attr('style')
|
|
365
|
+
if (
|
|
366
|
+
style &&
|
|
367
|
+
/forced-color-adjust\s*:\s*none|forcedColorAdjust\s*:\s*['"]none/.test(
|
|
368
|
+
style.value ?? style.expr ?? '',
|
|
369
|
+
)
|
|
370
|
+
)
|
|
371
|
+
raw(
|
|
372
|
+
'X012',
|
|
373
|
+
style.start,
|
|
374
|
+
style.end,
|
|
375
|
+
'`forced-color-adjust: none` on a surface keeps its author colours in forced-colours mode, where the mirror must take over.',
|
|
376
|
+
)
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
for (const rel of discover(root, { include: ['**/*.css'], exclude })) {
|
|
380
|
+
const text = readFileSync(join(root, rel), 'utf8')
|
|
381
|
+
if (text.includes('data-surface')) cssChecks({ rel, text }, reporter({ rel, text }))
|
|
382
|
+
}
|
|
383
|
+
return out
|
|
384
|
+
}
|
package/src/tailwind.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Tailwind v4 for a kerf app (research 34 §5). `create-kerf --ui tailwind` and `kerf add tailwind` write the same files,
|
|
2
|
+
// and a create-kerf test holds them equal, because create-kerf can't depend on the CLI.
|
|
3
|
+
|
|
4
|
+
export const packages = { tailwindcss: '4.3.3', '@tailwindcss/vite': '4.3.3' }
|
|
5
|
+
|
|
6
|
+
export const viteConfig = `import tailwindcss from '@tailwindcss/vite'
|
|
7
|
+
|
|
8
|
+
export default { plugins: [tailwindcss()] }
|
|
9
|
+
`
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The top of the app's CSS. With veneer, Tailwind's reset stays out: its `base` layer comes after `veneer`, so it would
|
|
13
|
+
* beat every veneer component. veneer brings its own base. Layer order has to be declared before any import.
|
|
14
|
+
*/
|
|
15
|
+
export const cssEntry = (veneer) =>
|
|
16
|
+
veneer
|
|
17
|
+
? `@layer veneer, theme, base, components, utilities;
|
|
18
|
+
@import "tailwindcss/theme.css" layer(theme);
|
|
19
|
+
@import "tailwindcss/utilities.css" layer(utilities);
|
|
20
|
+
@import "@kerf-js/veneer/tailwind.css";
|
|
21
|
+
`
|
|
22
|
+
: `@import "tailwindcss";
|
|
23
|
+
`
|
package/src/tasks.mjs
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// `kerf tasks`: the single list of what can be run (PLAN §7b). Built-ins first, then the project's `tasks`.
|
|
2
|
+
|
|
3
|
+
import { spawn } from 'node:child_process'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { join, relative, resolve } from 'node:path'
|
|
6
|
+
import { z } from 'zod'
|
|
7
|
+
import { loadConfig } from './load-config.mjs'
|
|
8
|
+
|
|
9
|
+
const files = {
|
|
10
|
+
type: 'array',
|
|
11
|
+
items: { type: 'string' },
|
|
12
|
+
description: 'Files relative to the project root; default: the whole project',
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** `available: false` marks commands PLAN names that this CLI does not ship yet; `stack` ones ship for apps with src/routes. */
|
|
16
|
+
export const builtins = [
|
|
17
|
+
{
|
|
18
|
+
name: 'check',
|
|
19
|
+
description: 'Compile every component in check mode and report W- and P-diagnostics with fixes',
|
|
20
|
+
args: { type: 'object', properties: { files } },
|
|
21
|
+
sideEffects: [],
|
|
22
|
+
available: true,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'tasks',
|
|
26
|
+
description: 'List every command and project task with args, side effects and environments',
|
|
27
|
+
args: { type: 'object', properties: {} },
|
|
28
|
+
sideEffects: [],
|
|
29
|
+
available: true,
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'docs',
|
|
33
|
+
description: 'Print a docs topic or a diagnostic code (`kerf docs W014`)',
|
|
34
|
+
args: { type: 'object', properties: { topic: { type: 'string' } } },
|
|
35
|
+
sideEffects: [],
|
|
36
|
+
available: true,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'agents-md',
|
|
40
|
+
description: 'Regenerate AGENTS.md (`--check` fails if stale)',
|
|
41
|
+
args: { type: 'object', properties: { check: { type: 'boolean' } } },
|
|
42
|
+
sideEffects: ['fs:AGENTS.md'],
|
|
43
|
+
available: true,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'mcp',
|
|
47
|
+
description: 'These commands as MCP tools (stdio)',
|
|
48
|
+
args: { type: 'object', properties: {} },
|
|
49
|
+
sideEffects: [],
|
|
50
|
+
available: true,
|
|
51
|
+
longRunning: true,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'style',
|
|
55
|
+
description:
|
|
56
|
+
'Style groups: `find <query>`, `explain <group|selector>`, `matrix` (writes .kerf/styles.json)',
|
|
57
|
+
args: {
|
|
58
|
+
type: 'object',
|
|
59
|
+
properties: {
|
|
60
|
+
subcommand: { type: 'string', enum: ['find', 'explain', 'matrix'] },
|
|
61
|
+
query: { type: 'string' },
|
|
62
|
+
target: { type: 'string' },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
sideEffects: ['fs:.kerf/styles.json'],
|
|
66
|
+
available: true,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: 'map',
|
|
70
|
+
description: 'Write `.kerf/map.md`: a matrix of what to call in this app',
|
|
71
|
+
args: { type: 'object', properties: {} },
|
|
72
|
+
sideEffects: ['fs:.kerf/map.*'],
|
|
73
|
+
available: true,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: 'add',
|
|
77
|
+
description: 'Install a plugin and wire it into the config (`--dry-run` shows the changes)',
|
|
78
|
+
args: {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: { plugin: { type: 'string' }, 'dry-run': { type: 'boolean' } },
|
|
81
|
+
},
|
|
82
|
+
sideEffects: ['fs:kerf.config.*', 'fs:package.json', 'network:install'],
|
|
83
|
+
available: true,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'dev',
|
|
87
|
+
description: 'Dev server',
|
|
88
|
+
sideEffects: ['network:listen'],
|
|
89
|
+
available: false,
|
|
90
|
+
stack: true,
|
|
91
|
+
longRunning: true,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
name: 'build',
|
|
95
|
+
description: 'Production build and contract',
|
|
96
|
+
sideEffects: ['fs:.output/**'],
|
|
97
|
+
available: false,
|
|
98
|
+
stack: true,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'start',
|
|
102
|
+
description: 'Serve the build',
|
|
103
|
+
sideEffects: ['network:listen'],
|
|
104
|
+
available: false,
|
|
105
|
+
stack: true,
|
|
106
|
+
longRunning: true,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'db',
|
|
110
|
+
description: '`generate`: new migrations',
|
|
111
|
+
sideEffects: ['fs:drizzle/**', 'fs:migrations/**', 'fs:prisma/**'],
|
|
112
|
+
available: false,
|
|
113
|
+
stack: true,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'test',
|
|
117
|
+
description: 'The test gate on the diff (`--gate`)',
|
|
118
|
+
args: {
|
|
119
|
+
type: 'object',
|
|
120
|
+
properties: {
|
|
121
|
+
gate: { type: 'boolean' },
|
|
122
|
+
base: { type: 'string' },
|
|
123
|
+
budget: { type: 'number' },
|
|
124
|
+
refactor: { type: 'boolean' },
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
sideEffects: ['fs:.kerf/gate'],
|
|
128
|
+
available: true,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: 'fix',
|
|
132
|
+
description: 'Apply the safe autofixes from `kerf check`',
|
|
133
|
+
args: { type: 'object', properties: { files } },
|
|
134
|
+
sideEffects: ['fs:scope.write'],
|
|
135
|
+
available: true,
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: 'tokens',
|
|
139
|
+
description:
|
|
140
|
+
"veneer's resolved tokens as DTCG 2025.10 in `.kerf/tokens/` (Figma, Style Dictionary, Terrazzo)",
|
|
141
|
+
args: { type: 'object', properties: {} },
|
|
142
|
+
sideEffects: ['fs:.kerf/tokens/**'],
|
|
143
|
+
available: true,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'browser',
|
|
147
|
+
description:
|
|
148
|
+
'Drive the running app in any open browser by role/label/text (`kerf browser help`)',
|
|
149
|
+
args: { type: 'object', properties: { command: { type: 'string' } } },
|
|
150
|
+
sideEffects: ['network:dev-server', 'fs:.kerf/browser'],
|
|
151
|
+
available: true,
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
name: 'run',
|
|
155
|
+
description: 'Run a project task by name',
|
|
156
|
+
args: { type: 'object', properties: { name: { type: 'string' } } },
|
|
157
|
+
sideEffects: ['task'],
|
|
158
|
+
available: true,
|
|
159
|
+
},
|
|
160
|
+
{ name: 'create', description: 'Scaffold a kerf app', sideEffects: ['fs'], available: false },
|
|
161
|
+
{
|
|
162
|
+
name: 'surfaces',
|
|
163
|
+
description: "List the project's surfaces: name, kind, props and file",
|
|
164
|
+
sideEffects: [],
|
|
165
|
+
available: true,
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: 'plugins',
|
|
169
|
+
description:
|
|
170
|
+
'List and explain plugins and slot bindings; `sync` regenerates .kerf/slots and plugin tokens; `audit` and `approve` check and write the permissions lock',
|
|
171
|
+
args: {
|
|
172
|
+
type: 'object',
|
|
173
|
+
properties: {
|
|
174
|
+
subcommand: { type: 'string', enum: ['list', 'explain', 'sync', 'audit', 'approve'] },
|
|
175
|
+
slot: { type: 'string' },
|
|
176
|
+
all: { type: 'boolean' },
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
sideEffects: ['fs:.kerf/**', 'fs:.gitignore'],
|
|
180
|
+
available: true,
|
|
181
|
+
},
|
|
182
|
+
]
|
|
183
|
+
|
|
184
|
+
function jsonSchema(args) {
|
|
185
|
+
if (!args) return { type: 'object', properties: {} }
|
|
186
|
+
if ('_zod' in args || '~standard' in args) {
|
|
187
|
+
try {
|
|
188
|
+
return z.toJSONSchema(args)
|
|
189
|
+
} catch {
|
|
190
|
+
return { type: 'object', description: 'Zod schema that has no JSON Schema form' }
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return args
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** `kerf run <name>`: the task's command in a shell at the project root, its output yours. Resolves with its exit code. */
|
|
197
|
+
export async function runTask(root, name) {
|
|
198
|
+
root = resolve(root)
|
|
199
|
+
const { config } = await loadConfig(root)
|
|
200
|
+
const task = config.tasks?.[name]
|
|
201
|
+
if (!task)
|
|
202
|
+
throw Object.assign(
|
|
203
|
+
new Error(
|
|
204
|
+
`no task "${name}" (tasks: ${Object.keys(config.tasks ?? {}).join(', ') || 'none'})`,
|
|
205
|
+
),
|
|
206
|
+
{ code: 'E_USAGE' },
|
|
207
|
+
)
|
|
208
|
+
return new Promise((ok, fail) => {
|
|
209
|
+
const child = spawn(task.run, { cwd: root, shell: true, stdio: 'inherit' })
|
|
210
|
+
child.on('error', fail)
|
|
211
|
+
child.on('close', (code) => ok(code ?? 1))
|
|
212
|
+
})
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Everything `kerf` can run here: the built-ins and the project's own tasks, with the config's diagnostics. */
|
|
216
|
+
export async function tasks(root) {
|
|
217
|
+
root = resolve(root)
|
|
218
|
+
const { file, config, diagnostics } = await loadConfig(root)
|
|
219
|
+
const environments = Object.keys(config.environments ?? {})
|
|
220
|
+
const stack = existsSync(join(root, 'src', 'routes'))
|
|
221
|
+
const list = builtins.map((b) => ({
|
|
222
|
+
name: b.name,
|
|
223
|
+
kind: 'builtin',
|
|
224
|
+
command:
|
|
225
|
+
{ check: 'kerf check --json', tasks: 'kerf tasks --json', docs: 'kerf docs <topic>' }[
|
|
226
|
+
b.name
|
|
227
|
+
] ?? `kerf ${b.name}`,
|
|
228
|
+
description: b.description,
|
|
229
|
+
args: b.args ?? { type: 'object', properties: {} },
|
|
230
|
+
sideEffects: b.sideEffects,
|
|
231
|
+
env: environments,
|
|
232
|
+
available: b.available || (b.stack && stack),
|
|
233
|
+
...(b.longRunning && { longRunning: true }),
|
|
234
|
+
}))
|
|
235
|
+
for (const [name, t] of Object.entries(config.tasks ?? {})) {
|
|
236
|
+
list.push({
|
|
237
|
+
name,
|
|
238
|
+
kind: 'project',
|
|
239
|
+
command: `kerf run ${name}`,
|
|
240
|
+
description: t.description ?? null,
|
|
241
|
+
args: jsonSchema(t.args),
|
|
242
|
+
sideEffects: t.sideEffects ?? null,
|
|
243
|
+
env: t.env ?? environments,
|
|
244
|
+
available: true,
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
version: 1,
|
|
249
|
+
ok: !diagnostics.some((d) => d.severity === 'error'),
|
|
250
|
+
root,
|
|
251
|
+
config: file ? relative(root, file).split('\\').join('/') : null,
|
|
252
|
+
tasks: list,
|
|
253
|
+
scope: {
|
|
254
|
+
write: config.scope?.write ?? null,
|
|
255
|
+
read: config.scope?.read ?? null,
|
|
256
|
+
forbid: config.scope?.forbid ?? [],
|
|
257
|
+
},
|
|
258
|
+
environments,
|
|
259
|
+
diagnostics: diagnostics.filter((d) => ['P003', 'P004', 'P007'].includes(d.code)),
|
|
260
|
+
}
|
|
261
|
+
}
|