@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
package/src/mcp.mjs
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// `kerf mcp`: an MCP server over stdio (newline-delimited JSON-RPC 2.0) with the tools `docs`, `check`, `routes_meta`, `tasks`,
|
|
2
|
+
// `style_find`, `style_explain`, `test_gate` and the `browser_*` tools.
|
|
3
|
+
// Every tool returns the same JSON as the CLI's `--json`, as text and as structuredContent.
|
|
4
|
+
|
|
5
|
+
import { audit } from './audit.mjs'
|
|
6
|
+
import { agentMap } from './map.mjs'
|
|
7
|
+
import { once } from 'node:events'
|
|
8
|
+
import { createInterface } from 'node:readline'
|
|
9
|
+
import { check } from './check.mjs'
|
|
10
|
+
import { docs, topics } from './docs.mjs'
|
|
11
|
+
import { listSurfaces } from './surface-checks.mjs'
|
|
12
|
+
import { routesCommand } from './start.mjs'
|
|
13
|
+
import { tasks } from './tasks.mjs'
|
|
14
|
+
import { browser } from './browser/commands.mjs'
|
|
15
|
+
import { browserTools } from './mcp/browser-tools.mjs'
|
|
16
|
+
import { gate } from './gate.mjs'
|
|
17
|
+
import { explainTarget, findGroups, styleMatrix } from './style.mjs'
|
|
18
|
+
|
|
19
|
+
const versions = ['2025-11-25', '2025-06-18', '2025-03-26', '2024-11-05']
|
|
20
|
+
|
|
21
|
+
/** What `kerf mcp` offers an agent: each tool's name, its description and the arguments it takes. */
|
|
22
|
+
export const tools = [
|
|
23
|
+
{
|
|
24
|
+
name: 'docs',
|
|
25
|
+
description: `kerf docs for agents. Topics: ${Object.keys(topics).join(', ')}; or a diagnostic code such as W014. No topic: the index.`,
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: { topic: { type: 'string', description: 'Topic name or diagnostic code' } },
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: 'check',
|
|
33
|
+
description:
|
|
34
|
+
'Run `kerf check`: every W- and P-diagnostic with code, file, span, why and fix (edits when an autofix exists), plus slot bindings.',
|
|
35
|
+
inputSchema: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
files: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
items: { type: 'string' },
|
|
41
|
+
description: 'Files relative to the project root; default: all',
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'routes_meta',
|
|
48
|
+
description:
|
|
49
|
+
"Every route's metadata as a crawler gets it (`kerf routes --meta --json`): title, description, image, every tag, where each came from (`route`, `layout`, `config`, `auto`, `card`), real samples of param routes, and H-diagnostics with text to apply. Every page sets `meta` from its own context as part of building it: call this after adding or changing a route.",
|
|
50
|
+
inputSchema: {
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties: {
|
|
53
|
+
preset: {
|
|
54
|
+
type: 'string',
|
|
55
|
+
description: "`static`: no server at runtime, so a param route's card needs `prerender`",
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'audit',
|
|
62
|
+
description:
|
|
63
|
+
"The pages as a browser renders them (`kerf audit`): a clean Chrome at a phone's and a desktop's width, axe-core's WCAG rules (what Lighthouse grades), console errors, deprecations, the head a crawler reads, sideways scroll and layout shift, as L-diagnostics with fixes. Static checks read source; this catches what CSS and a breakpoint do to it. Default: the last build's pages.",
|
|
64
|
+
inputSchema: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
url: {
|
|
68
|
+
type: 'string',
|
|
69
|
+
description: 'A running site to audit instead (a deploy, `kerf start`, the dev server)',
|
|
70
|
+
},
|
|
71
|
+
indexing: {
|
|
72
|
+
type: 'string',
|
|
73
|
+
enum: ['auto', 'public', 'private'],
|
|
74
|
+
description:
|
|
75
|
+
'Public makes noindex in markup or HTTP headers an error. Private preserves intentional noindex. Default inherits build.audit.indexing.',
|
|
76
|
+
},
|
|
77
|
+
routes: {
|
|
78
|
+
type: 'array',
|
|
79
|
+
items: { type: 'string' },
|
|
80
|
+
description: 'Paths; default one page of each kind',
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'map',
|
|
87
|
+
description:
|
|
88
|
+
'What this app has and what to call for each part: routes, server functions, data, styles, plugins',
|
|
89
|
+
inputSchema: { type: 'object', properties: {} },
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'tasks',
|
|
93
|
+
description:
|
|
94
|
+
"List the project's commands and tasks: what each does, its args schema, side effects and allowed environments.",
|
|
95
|
+
inputSchema: { type: 'object', properties: {} },
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'style_find',
|
|
99
|
+
description:
|
|
100
|
+
"Before styling anything, find the style groups that already fit (`kerf style find`): name, doc, axes and values, parts, states, how often it's used, and similar groups. Reuse or add an axis value; don't write a new group or class list for the same thing.",
|
|
101
|
+
inputSchema: {
|
|
102
|
+
type: 'object',
|
|
103
|
+
properties: {
|
|
104
|
+
query: {
|
|
105
|
+
type: 'string',
|
|
106
|
+
description:
|
|
107
|
+
'What the thing is: `card`, `status badge`, `nav`, or a token such as `critical`',
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
required: ['query'],
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
name: 'style_explain',
|
|
115
|
+
description:
|
|
116
|
+
'Explain one variation of a style group (`kerf style explain`): every declaration on its root and parts in cascade order with the token, axis, state, compound or accent it came from, the attributes it renders, the CSS it emits, and the call sites that produce it.',
|
|
117
|
+
inputSchema: {
|
|
118
|
+
type: 'object',
|
|
119
|
+
properties: {
|
|
120
|
+
target: {
|
|
121
|
+
type: 'string',
|
|
122
|
+
description:
|
|
123
|
+
'A group (`order-card`), a part (`order-card.title`), `order-card[status=late][selected]`, or a DOM selector (`[data-g="order-card"][data-status="late"]`)',
|
|
124
|
+
},
|
|
125
|
+
axes: {
|
|
126
|
+
type: 'object',
|
|
127
|
+
additionalProperties: { type: 'string' },
|
|
128
|
+
description: 'Axis values, e.g. { "status": "late" } (defaults fill the rest)',
|
|
129
|
+
},
|
|
130
|
+
states: {
|
|
131
|
+
type: 'array',
|
|
132
|
+
items: { type: 'string' },
|
|
133
|
+
description: 'States to apply: selected, hover…',
|
|
134
|
+
},
|
|
135
|
+
accent: {
|
|
136
|
+
type: 'object',
|
|
137
|
+
additionalProperties: { type: 'string' },
|
|
138
|
+
description: 'A call-site accent, intent → token: { "radius": "round" }',
|
|
139
|
+
},
|
|
140
|
+
part: { type: 'string', description: 'Only this part (and the root)' },
|
|
141
|
+
},
|
|
142
|
+
required: ['target'],
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'surfaces',
|
|
147
|
+
description:
|
|
148
|
+
'List the project\'s canvas and WebGL surfaces (`kerf surfaces`): each `defineSurface` with its name, kind, props and file, ready to drop in as `<Surface use={X} label="…" />`.',
|
|
149
|
+
inputSchema: { type: 'object', properties: {} },
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: 'test_gate',
|
|
153
|
+
description:
|
|
154
|
+
'Run the test gate on the diff (`kerf test --gate`): each new or changed test must fail on the old code (T010), kill a mutant of the changed lines (T011), and no existing test may get weaker (T020). Returns per-test results, surviving mutants with what to assert, and diagnostics.',
|
|
155
|
+
inputSchema: {
|
|
156
|
+
type: 'object',
|
|
157
|
+
properties: {
|
|
158
|
+
base: {
|
|
159
|
+
type: 'string',
|
|
160
|
+
description: 'Git ref to diff against (default HEAD: the uncommitted change)',
|
|
161
|
+
},
|
|
162
|
+
budgetSec: { type: 'number' },
|
|
163
|
+
maxMutants: { type: 'number' },
|
|
164
|
+
refactor: { type: 'boolean' },
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
...browserTools(),
|
|
169
|
+
]
|
|
170
|
+
|
|
171
|
+
async function callBrowser(root, name, args) {
|
|
172
|
+
const o = { url: args.url, tab: args.tab }
|
|
173
|
+
const cmd = name.slice('browser_'.length)
|
|
174
|
+
if (cmd === 'state')
|
|
175
|
+
return args.set
|
|
176
|
+
? browser(root, 'set', { name: args.set, value: args.value }, o)
|
|
177
|
+
: browser(root, 'graph', { filter: args.filter }, o)
|
|
178
|
+
return browser(root, cmd, args, o)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Handle one JSON-RPC message. Returns the response, or undefined for notifications. */
|
|
182
|
+
export async function handle(root, msg) {
|
|
183
|
+
const reply = (result) => ({ jsonrpc: '2.0', id: msg.id, result })
|
|
184
|
+
const fail = (code, message) => ({ jsonrpc: '2.0', id: msg.id ?? null, error: { code, message } })
|
|
185
|
+
if (msg?.jsonrpc !== '2.0' || typeof msg.method !== 'string')
|
|
186
|
+
return fail(-32600, 'Invalid Request')
|
|
187
|
+
const notification = msg.id === undefined
|
|
188
|
+
switch (msg.method) {
|
|
189
|
+
case 'initialize': {
|
|
190
|
+
const asked = msg.params?.protocolVersion
|
|
191
|
+
return reply({
|
|
192
|
+
protocolVersion: versions.includes(asked) ? asked : versions[0],
|
|
193
|
+
capabilities: { tools: { listChanged: false } },
|
|
194
|
+
serverInfo: { name: 'kerf', version: '0.1.4' },
|
|
195
|
+
instructions:
|
|
196
|
+
'kerf is a fine-grained reactive TSX framework, not React. Read `docs` topic `rules` first, and run `check` after every edit. Every page sets `meta` from its own context as part of building it (`docs` topic `meta`). Chrome shared across route sections (a sidebar) goes in one layout above them that follows `currentPath()`, never a layout per section (`docs` topic `start`). Style with groups from @kerf-js/core/style, not class lists: `style_find` before you write one. Verify behavior in the running app with the browser_* tools (or `__kerf__.agent` in your own browser), and run `test_gate` before you call a change done.',
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
case 'ping':
|
|
200
|
+
return reply({})
|
|
201
|
+
case 'tools/list':
|
|
202
|
+
return reply({ tools })
|
|
203
|
+
case 'tools/call': {
|
|
204
|
+
const { name, arguments: args = {} } = msg.params ?? {}
|
|
205
|
+
try {
|
|
206
|
+
let data
|
|
207
|
+
if (name === 'docs') {
|
|
208
|
+
const page = await docs(root, args.topic)
|
|
209
|
+
return reply({ content: [{ type: 'text', text: page.text }] })
|
|
210
|
+
}
|
|
211
|
+
if (name === 'check') data = await check(root, { files: args.files })
|
|
212
|
+
else if (name === 'tasks') data = await tasks(root)
|
|
213
|
+
else if (name === 'routes_meta')
|
|
214
|
+
data = await routesCommand(root, { meta: true, preset: args.preset })
|
|
215
|
+
else if (name === 'audit')
|
|
216
|
+
data = await audit(root, { url: args.url, routes: args.routes, indexing: args.indexing })
|
|
217
|
+
else if (name === 'map') data = await agentMap(root)
|
|
218
|
+
else if (name === 'style_find')
|
|
219
|
+
data = findGroups((await styleMatrix(root)).matrix, args.query)
|
|
220
|
+
else if (name === 'style_explain')
|
|
221
|
+
data = explainTarget(await styleMatrix(root), args.target, {
|
|
222
|
+
axes: args.axes,
|
|
223
|
+
states: args.states,
|
|
224
|
+
accent: args.accent,
|
|
225
|
+
part: args.part,
|
|
226
|
+
})
|
|
227
|
+
else if (name === 'surfaces') data = { surfaces: listSurfaces(root) }
|
|
228
|
+
else if (name === 'test_gate')
|
|
229
|
+
data = await gate(root, {
|
|
230
|
+
base: args.base,
|
|
231
|
+
budgetSec: args.budgetSec,
|
|
232
|
+
maxMutants: args.maxMutants,
|
|
233
|
+
refactor: args.refactor,
|
|
234
|
+
})
|
|
235
|
+
else if (name.startsWith('browser_')) data = await callBrowser(root, name, args)
|
|
236
|
+
else return fail(-32602, `Unknown tool: ${name}`)
|
|
237
|
+
return reply({
|
|
238
|
+
content: [{ type: 'text', text: JSON.stringify(data) }],
|
|
239
|
+
structuredContent: data,
|
|
240
|
+
isError: false,
|
|
241
|
+
})
|
|
242
|
+
} catch (error) {
|
|
243
|
+
return reply({
|
|
244
|
+
content: [{ type: 'text', text: `${error.code ?? 'E_INTERNAL'}: ${error.message}` }],
|
|
245
|
+
isError: true,
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
default:
|
|
250
|
+
return notification ? undefined : fail(-32601, `Method not found: ${msg.method}`)
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Answer newline-delimited JSON-RPC from `input`, one message at a time; resolves once `input` has closed and the last answer is out. */
|
|
255
|
+
export function serve(root, input = process.stdin, output = process.stdout) {
|
|
256
|
+
const rl = createInterface({ input, crlfDelay: Infinity })
|
|
257
|
+
const write = (obj) => output.write(`${JSON.stringify(obj)}\n`)
|
|
258
|
+
let queue = Promise.resolve()
|
|
259
|
+
rl.on('line', (line) => {
|
|
260
|
+
if (!line.trim()) return
|
|
261
|
+
queue = queue.then(async () => {
|
|
262
|
+
let msg
|
|
263
|
+
try {
|
|
264
|
+
msg = JSON.parse(line)
|
|
265
|
+
} catch {
|
|
266
|
+
return write({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } })
|
|
267
|
+
}
|
|
268
|
+
const res = await handle(root, msg)
|
|
269
|
+
if (res) write(res)
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
return once(rl, 'close').then(() => queue)
|
|
273
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The human form of `kerf routes --meta`: one line per page, and one per sampled page of a param route, with where each
|
|
2
|
+
// value came from. `--json` has everything else (every tag, the merged object).
|
|
3
|
+
|
|
4
|
+
const short = (s, n) => {
|
|
5
|
+
const text = s ?? ''
|
|
6
|
+
return text.length > n ? text.slice(0, n - 1) + '…' : text
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const cell = (s, n) => short(s, n).padEnd(n)
|
|
10
|
+
|
|
11
|
+
/** `/_kerf/og/users/42.png?v=abc` → `card: users/42.png`; an explicit image as it is. */
|
|
12
|
+
const imageOf = (url) =>
|
|
13
|
+
!url
|
|
14
|
+
? 'none'
|
|
15
|
+
: url
|
|
16
|
+
.replace(/^https?:\/\/[^/]+/, '')
|
|
17
|
+
.replace(/^\/_kerf\/og\//, '/_kerf/og/')
|
|
18
|
+
.replace(/\?v=.*/, '')
|
|
19
|
+
|
|
20
|
+
export function metaTable(result) {
|
|
21
|
+
const rows = []
|
|
22
|
+
for (const r of result.routes) {
|
|
23
|
+
if (r.kind !== 'page' || r.redirect !== undefined) continue
|
|
24
|
+
const add = (path, title, description, image, source) =>
|
|
25
|
+
rows.push({
|
|
26
|
+
path,
|
|
27
|
+
title,
|
|
28
|
+
description,
|
|
29
|
+
image: imageOf(image),
|
|
30
|
+
source: source ? `${source.title}/${source.description}/${source.image}` : 'source only',
|
|
31
|
+
})
|
|
32
|
+
if (r.samples?.length) {
|
|
33
|
+
r.samples.slice(0, 3).forEach((s) => add(s.url, s.title, s.description, s.image, s.source))
|
|
34
|
+
if (r.samples.length > 3)
|
|
35
|
+
rows.push({
|
|
36
|
+
path: ` … ${r.samples.length - 3} more of ${r.path}`,
|
|
37
|
+
title: '',
|
|
38
|
+
description: '',
|
|
39
|
+
image: '',
|
|
40
|
+
source: '',
|
|
41
|
+
})
|
|
42
|
+
} else
|
|
43
|
+
add(r.url ?? r.path, r.meta?.title, r.meta?.description, r.meta?.og?.image?.url, r.source)
|
|
44
|
+
}
|
|
45
|
+
const width = {
|
|
46
|
+
path: Math.min(30, Math.max(5, ...rows.map((r) => r.path.length))),
|
|
47
|
+
image: Math.min(34, Math.max(5, ...rows.map((r) => r.image.length))),
|
|
48
|
+
}
|
|
49
|
+
const line = (r) =>
|
|
50
|
+
`${cell(r.path, width.path)} ${cell(r.title, 36)} ${cell(r.description, 48)} ${cell(r.image, width.image)} ${r.source}`
|
|
51
|
+
const url = result.site?.url ? `site.url ${result.site.url}` : 'site.url not set'
|
|
52
|
+
return [
|
|
53
|
+
`${url}. source: title/description/image, each route, layout, config, auto or card.`,
|
|
54
|
+
line({
|
|
55
|
+
path: 'route',
|
|
56
|
+
title: 'title',
|
|
57
|
+
description: 'description',
|
|
58
|
+
image: 'image',
|
|
59
|
+
source: 'source',
|
|
60
|
+
}),
|
|
61
|
+
...rows.map(line),
|
|
62
|
+
].join('\n')
|
|
63
|
+
}
|
package/src/mutate.mjs
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// Mutants on the changed lines of a source file (the gate's T011). kerf mutates source text, not compiled output, so
|
|
2
|
+
// each mutant goes through the kerf compiler like any edit and its W-codes still apply. Operators follow Stryker's
|
|
3
|
+
// common set: equality, boundaries, logic, arithmetic, negation, literals, conditions, removed calls, JSX text.
|
|
4
|
+
|
|
5
|
+
import ts from 'typescript'
|
|
6
|
+
import { positions } from './diag.mjs'
|
|
7
|
+
import { parse } from './tests.mjs'
|
|
8
|
+
|
|
9
|
+
const K = ts.SyntaxKind
|
|
10
|
+
const swaps = new Map([
|
|
11
|
+
[K.EqualsEqualsEqualsToken, ['!==']],
|
|
12
|
+
[K.ExclamationEqualsEqualsToken, ['===']],
|
|
13
|
+
[K.EqualsEqualsToken, ['!=']],
|
|
14
|
+
[K.ExclamationEqualsToken, ['==']],
|
|
15
|
+
[K.LessThanToken, ['<=', '>=']],
|
|
16
|
+
[K.LessThanEqualsToken, ['<', '>']],
|
|
17
|
+
[K.GreaterThanToken, ['>=', '<=']],
|
|
18
|
+
[K.GreaterThanEqualsToken, ['>', '<']],
|
|
19
|
+
[K.AmpersandAmpersandToken, ['||']],
|
|
20
|
+
[K.BarBarToken, ['&&']],
|
|
21
|
+
[K.QuestionQuestionToken, ['&&']],
|
|
22
|
+
[K.PlusToken, ['-']],
|
|
23
|
+
[K.MinusToken, ['+']],
|
|
24
|
+
[K.AsteriskToken, ['/']],
|
|
25
|
+
[K.SlashToken, ['*']],
|
|
26
|
+
[K.PercentToken, ['*']],
|
|
27
|
+
])
|
|
28
|
+
|
|
29
|
+
/** Lines (1-based) of `after` that are new or changed since `before` (null: every line). LCS over lines. */
|
|
30
|
+
export function changedLines(before, after) {
|
|
31
|
+
const b = after.split('\n')
|
|
32
|
+
if (before == null) return new Set(b.map((_, i) => i + 1))
|
|
33
|
+
const a = before.split('\n')
|
|
34
|
+
// Trim the common head and tail; the middle is usually small.
|
|
35
|
+
let start = 0
|
|
36
|
+
while (start < a.length && start < b.length && a[start] === b[start]) start++
|
|
37
|
+
let ea = a.length,
|
|
38
|
+
eb = b.length
|
|
39
|
+
while (ea > start && eb > start && a[ea - 1] === b[eb - 1]) {
|
|
40
|
+
ea--
|
|
41
|
+
eb--
|
|
42
|
+
}
|
|
43
|
+
const A = a.slice(start, ea),
|
|
44
|
+
B = b.slice(start, eb)
|
|
45
|
+
const out = new Set()
|
|
46
|
+
if (A.length * B.length > 4e6) {
|
|
47
|
+
for (let i = start; i < eb; i++) out.add(i + 1)
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
const dp = Array.from({ length: A.length + 1 }, () => new Uint32Array(B.length + 1))
|
|
51
|
+
for (let i = A.length - 1; i >= 0; i--)
|
|
52
|
+
for (let j = B.length - 1; j >= 0; j--)
|
|
53
|
+
dp[i][j] = A[i] === B[j] ? dp[i + 1][j + 1] + 1 : Math.max(dp[i + 1][j], dp[i][j + 1])
|
|
54
|
+
let i = 0,
|
|
55
|
+
j = 0
|
|
56
|
+
while (j < B.length) {
|
|
57
|
+
if (i < A.length && A[i] === B[j]) {
|
|
58
|
+
i++
|
|
59
|
+
j++
|
|
60
|
+
} else if (i < A.length && dp[i + 1][j] >= dp[i][j + 1]) i++
|
|
61
|
+
else out.add(start + ++j)
|
|
62
|
+
}
|
|
63
|
+
return out
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Mutants whose code starts on one of `lines`.
|
|
68
|
+
* @returns {{ id: string, file: string, line: number, column: number, op: string, start: number, end: number, original: string, replacement: string }[]}
|
|
69
|
+
*/
|
|
70
|
+
export function mutants(text, rel, lines) {
|
|
71
|
+
const sf = parse(text, rel)
|
|
72
|
+
const pos = positions(text)
|
|
73
|
+
const out = []
|
|
74
|
+
const add = (node, op, replacement, start = node.getStart(sf), end = node.getEnd()) => {
|
|
75
|
+
const [line, column] = pos.lineCol(start)
|
|
76
|
+
if (!lines.has(line)) return
|
|
77
|
+
const original = text.slice(start, end)
|
|
78
|
+
if (original === replacement) return
|
|
79
|
+
out.push({
|
|
80
|
+
id: `${rel}:${line}:${column}:${op}:${out.length}`,
|
|
81
|
+
file: rel,
|
|
82
|
+
line,
|
|
83
|
+
column,
|
|
84
|
+
op,
|
|
85
|
+
start,
|
|
86
|
+
end,
|
|
87
|
+
original,
|
|
88
|
+
replacement,
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
const skip = (n) =>
|
|
92
|
+
ts.isImportDeclaration(n) ||
|
|
93
|
+
ts.isExportDeclaration(n) ||
|
|
94
|
+
ts.isTypeNode(n) ||
|
|
95
|
+
ts.isInterfaceDeclaration(n) ||
|
|
96
|
+
ts.isTypeAliasDeclaration(n) ||
|
|
97
|
+
ts.isEnumDeclaration(n)
|
|
98
|
+
const visit = (n) => {
|
|
99
|
+
if (skip(n)) return
|
|
100
|
+
if (ts.isBinaryExpression(n)) {
|
|
101
|
+
for (const r of swaps.get(n.operatorToken.kind) ?? []) add(n.operatorToken, 'operator', r)
|
|
102
|
+
} else if (ts.isPrefixUnaryExpression(n) && n.operator === K.ExclamationToken) {
|
|
103
|
+
add(n, 'negation', n.operand.getText(sf))
|
|
104
|
+
} else if (n.kind === K.TrueKeyword || n.kind === K.FalseKeyword) {
|
|
105
|
+
add(n, 'boolean', n.kind === K.TrueKeyword ? 'false' : 'true')
|
|
106
|
+
} else if (ts.isNumericLiteral(n)) {
|
|
107
|
+
const v = Number(n.text)
|
|
108
|
+
if (Number.isFinite(v)) add(n, 'number', String(v === 0 ? 1 : v + 1))
|
|
109
|
+
} else if (
|
|
110
|
+
(ts.isStringLiteral(n) || ts.isNoSubstitutionTemplateLiteral(n)) &&
|
|
111
|
+
n.text &&
|
|
112
|
+
!isKey(n)
|
|
113
|
+
) {
|
|
114
|
+
add(n, 'string', '""')
|
|
115
|
+
} else if (ts.isConditionalExpression(n)) {
|
|
116
|
+
add(n.condition, 'condition', 'true')
|
|
117
|
+
add(n.condition, 'condition', 'false')
|
|
118
|
+
} else if (ts.isIfStatement(n)) {
|
|
119
|
+
add(n.expression, 'condition', 'true')
|
|
120
|
+
add(n.expression, 'condition', 'false')
|
|
121
|
+
} else if (
|
|
122
|
+
ts.isExpressionStatement(n) &&
|
|
123
|
+
ts.isCallExpression(unparen(n.expression)) &&
|
|
124
|
+
!isDirective(n)
|
|
125
|
+
) {
|
|
126
|
+
add(n, 'call', ';')
|
|
127
|
+
} else if (ts.isJsxText(n) && n.text.trim()) {
|
|
128
|
+
const start = n.pos + n.text.indexOf(n.text.trim()) // JSX text: `text` starts at `pos`, before any trivia
|
|
129
|
+
add(n, 'jsx-text', '', start, start + n.text.trim().length)
|
|
130
|
+
} else if (
|
|
131
|
+
ts.isReturnStatement(n) &&
|
|
132
|
+
n.expression &&
|
|
133
|
+
isFn(n.parent?.parent) &&
|
|
134
|
+
!ts.isJsxElement(unparen(n.expression)) &&
|
|
135
|
+
!ts.isJsxFragment(unparen(n.expression))
|
|
136
|
+
) {
|
|
137
|
+
add(n.expression, 'return', 'undefined')
|
|
138
|
+
}
|
|
139
|
+
ts.forEachChild(n, visit)
|
|
140
|
+
}
|
|
141
|
+
visit(sf)
|
|
142
|
+
return out
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const unparen = (n) => {
|
|
146
|
+
while (ts.isParenthesizedExpression(n)) n = n.expression
|
|
147
|
+
return n
|
|
148
|
+
}
|
|
149
|
+
const isFn = (n) =>
|
|
150
|
+
n &&
|
|
151
|
+
(ts.isFunctionDeclaration(n) ||
|
|
152
|
+
ts.isFunctionExpression(n) ||
|
|
153
|
+
ts.isArrowFunction(n) ||
|
|
154
|
+
ts.isMethodDeclaration(n))
|
|
155
|
+
const isDirective = (n) => ts.isStringLiteral(n.expression)
|
|
156
|
+
/** Strings that name things rather than being data: property keys, element-access keys, `import()` specifiers. */
|
|
157
|
+
const isKey = (n) => {
|
|
158
|
+
const p = n.parent
|
|
159
|
+
return (
|
|
160
|
+
(ts.isPropertyAssignment(p) && p.name === n) ||
|
|
161
|
+
ts.isElementAccessExpression(p) ||
|
|
162
|
+
ts.isLiteralTypeNode(p) ||
|
|
163
|
+
ts.isExternalModuleReference(p) ||
|
|
164
|
+
(ts.isCallExpression(p) && p.expression.kind === K.ImportKeyword) ||
|
|
165
|
+
(ts.isJsxAttribute(p) &&
|
|
166
|
+
/^(key|class|className|style|id|type|role|for|htmlFor|name)$/.test(p.name.getText()))
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export const apply = (text, m) => text.slice(0, m.start) + m.replacement + text.slice(m.end)
|
|
171
|
+
|
|
172
|
+
/** Spread a sample over lines: one mutant per line in turn, deterministic. */
|
|
173
|
+
export function sample(list, max) {
|
|
174
|
+
if (list.length <= max) return list
|
|
175
|
+
const byLine = new Map()
|
|
176
|
+
for (const m of list) {
|
|
177
|
+
const k = `${m.file}:${m.line}`
|
|
178
|
+
if (!byLine.has(k)) byLine.set(k, [])
|
|
179
|
+
byLine.get(k).push(m)
|
|
180
|
+
}
|
|
181
|
+
const queues = [...byLine.values()]
|
|
182
|
+
const out = []
|
|
183
|
+
for (let round = 0; out.length < max; round++) {
|
|
184
|
+
let any = false
|
|
185
|
+
for (const q of queues)
|
|
186
|
+
if (q[round]) {
|
|
187
|
+
out.push(q[round])
|
|
188
|
+
any = true
|
|
189
|
+
if (out.length >= max) break
|
|
190
|
+
}
|
|
191
|
+
if (!any) break
|
|
192
|
+
}
|
|
193
|
+
return out
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** What a test that kills this mutant would check: the hint surviving mutants come back with (MUTGEN/ACH loop). */
|
|
197
|
+
export function suggest(m) {
|
|
198
|
+
const at = `${m.file}:${m.line}`
|
|
199
|
+
switch (m.op) {
|
|
200
|
+
case 'operator':
|
|
201
|
+
if (/^[<>]=?$/.test(m.original))
|
|
202
|
+
return `${at}: \`${m.original}\` → \`${m.replacement}\` survives. Assert the boundary: the value exactly at the limit.`
|
|
203
|
+
if (/^(&&|\|\||\?\?)$/.test(m.original))
|
|
204
|
+
return `${at}: \`${m.original}\` → \`${m.replacement}\` survives. Assert a case where only one side of the condition holds.`
|
|
205
|
+
if (/^[!=]=/.test(m.original))
|
|
206
|
+
return `${at}: \`${m.original}\` → \`${m.replacement}\` survives. Assert both the matching and the non-matching case.`
|
|
207
|
+
return `${at}: \`${m.original}\` → \`${m.replacement}\` survives. Assert the computed value, not only that something rendered.`
|
|
208
|
+
case 'boolean':
|
|
209
|
+
case 'condition':
|
|
210
|
+
case 'negation':
|
|
211
|
+
return `${at}: \`${m.original}\` → \`${m.replacement}\` survives. Assert what the user sees on the branch this condition guards.`
|
|
212
|
+
case 'number':
|
|
213
|
+
return `${at}: ${m.original} → ${m.replacement} survives. Assert the exact value this number produces.`
|
|
214
|
+
case 'string':
|
|
215
|
+
case 'jsx-text':
|
|
216
|
+
return `${at}: the text ${JSON.stringify(m.original.slice(0, 40))} can be blanked. Assert the text the user sees.`
|
|
217
|
+
case 'call':
|
|
218
|
+
return `${at}: removing \`${m.original.slice(0, 60)}\` survives. Assert the effect of that call.`
|
|
219
|
+
case 'return':
|
|
220
|
+
return `${at}: returning undefined survives. Assert what this function's result shows.`
|
|
221
|
+
default:
|
|
222
|
+
return `${at}: ${m.op} survives`
|
|
223
|
+
}
|
|
224
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Plugin `check` hooks (research 13 §2.3 `CheckHook`, research 26 §2.5): a plugin's manifest may carry
|
|
2
|
+
// `check: { include: string[], run(ctx) }`. `kerf check` runs it once over the matching files, never on HMR. The
|
|
3
|
+
// hook reads through `ctx` (the files, a JSX view, other project files) and returns diagnostics in its namespace.
|
|
4
|
+
|
|
5
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { entry } from './catalog.mjs'
|
|
8
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
9
|
+
import { discover, matcher } from './files.mjs'
|
|
10
|
+
import { importOf, scanJsx } from './jsx-scan.mjs'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {string} root
|
|
14
|
+
* @param {{ contributions: { name: string, namespace: string, manifest: any }[] }} loaded
|
|
15
|
+
* @param {{ files: { rel: string, text: string }[], exclude?: string[] }} options the files `kerf check` compiled
|
|
16
|
+
*/
|
|
17
|
+
export async function pluginChecks(root, loaded, { files, exclude = [] }) {
|
|
18
|
+
const out = []
|
|
19
|
+
const texts = new Map(files.map((f) => [f.rel, f.text]))
|
|
20
|
+
const read = (rel) => {
|
|
21
|
+
if (!texts.has(rel))
|
|
22
|
+
texts.set(rel, existsSync(join(root, rel)) ? readFileSync(join(root, rel), 'utf8') : '')
|
|
23
|
+
return texts.get(rel)
|
|
24
|
+
}
|
|
25
|
+
const scans = new Map()
|
|
26
|
+
const jsx = (rel) => {
|
|
27
|
+
if (!scans.has(rel)) {
|
|
28
|
+
try {
|
|
29
|
+
scans.set(rel, scanJsx(read(rel), rel))
|
|
30
|
+
} catch {
|
|
31
|
+
scans.set(rel, { imports: [], elements: [], functions: new Map() })
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return scans.get(rel)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const c of loaded.contributions) {
|
|
38
|
+
const hook = c.manifest.check
|
|
39
|
+
if (!hook || typeof hook.run !== 'function') continue
|
|
40
|
+
const include = hook.include ?? ['**/*.{tsx,jsx}']
|
|
41
|
+
const wanted = matcher(include)
|
|
42
|
+
/** @type {Set<string>} */
|
|
43
|
+
const chosen = new Set([
|
|
44
|
+
...files.map((f) => f.rel).filter(wanted),
|
|
45
|
+
...discover(root, { include, exclude }),
|
|
46
|
+
])
|
|
47
|
+
const rels = [...chosen].sort()
|
|
48
|
+
const declared = new Map((c.manifest.diagnostics ?? []).map((d) => [d.code, d]))
|
|
49
|
+
const ctx = Object.freeze({
|
|
50
|
+
root,
|
|
51
|
+
files: rels,
|
|
52
|
+
read,
|
|
53
|
+
jsx,
|
|
54
|
+
importOf,
|
|
55
|
+
slots: loaded.slots ?? {},
|
|
56
|
+
exists: (rel) => existsSync(join(root, rel)),
|
|
57
|
+
bytes: (rel) =>
|
|
58
|
+
existsSync(join(root, rel)) ? new Uint8Array(readFileSync(join(root, rel))) : undefined,
|
|
59
|
+
})
|
|
60
|
+
let found
|
|
61
|
+
try {
|
|
62
|
+
found = (await hook.run(ctx)) ?? []
|
|
63
|
+
} catch (error) {
|
|
64
|
+
out.push(
|
|
65
|
+
diagnostic({
|
|
66
|
+
code: 'P119',
|
|
67
|
+
owner: c.name,
|
|
68
|
+
file: 'kerf.config.ts',
|
|
69
|
+
why: `\`${c.name}\`'s check hook threw: ${String(error?.message ?? error).split('\n')[0]}`,
|
|
70
|
+
}),
|
|
71
|
+
)
|
|
72
|
+
continue
|
|
73
|
+
}
|
|
74
|
+
appendFindings({ found, c, out, declared, read })
|
|
75
|
+
}
|
|
76
|
+
return out
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function appendFindings({ found, c, out, declared, read }) {
|
|
80
|
+
for (const d of found) {
|
|
81
|
+
if (!d.code.startsWith(c.namespace)) {
|
|
82
|
+
out.push(
|
|
83
|
+
diagnostic({
|
|
84
|
+
code: 'P111',
|
|
85
|
+
file: 'kerf.config.ts',
|
|
86
|
+
why: `\`${c.name}\`'s check reported \`${d.code}\`, outside its namespace \`${c.namespace}\`.`,
|
|
87
|
+
}),
|
|
88
|
+
)
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
const text = read(d.file)
|
|
92
|
+
const pos = positions(text)
|
|
93
|
+
const [start, end] = [d.start ?? 0, d.end ?? d.start ?? 0]
|
|
94
|
+
const meta = declared.get(d.code)
|
|
95
|
+
out.push(
|
|
96
|
+
diagnostic({
|
|
97
|
+
code: d.code,
|
|
98
|
+
owner: c.name,
|
|
99
|
+
severity: d.severity ?? meta?.severity ?? 'error',
|
|
100
|
+
file: d.file,
|
|
101
|
+
span: pos.span(start, end),
|
|
102
|
+
offset: [start, end],
|
|
103
|
+
why: d.why,
|
|
104
|
+
help: d.help ?? (entry(d.code) ? undefined : meta?.fix), // a catalog code keeps the catalog's fix text
|
|
105
|
+
fix: d.fix && {
|
|
106
|
+
description: d.fix.description,
|
|
107
|
+
safe: d.fix.safe === true,
|
|
108
|
+
edits: d.fix.edits.map((e) => ({
|
|
109
|
+
file: e.file ?? d.file,
|
|
110
|
+
span: positions(read(e.file ?? d.file)).span(e.start, e.end),
|
|
111
|
+
offset: [e.start, e.end],
|
|
112
|
+
text: e.text,
|
|
113
|
+
})),
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
}
|