@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,232 @@
|
|
|
1
|
+
import ts from 'typescript'
|
|
2
|
+
|
|
3
|
+
const KEEPS_OBJECTS = new Set([
|
|
4
|
+
'map',
|
|
5
|
+
'filter',
|
|
6
|
+
'flatMap',
|
|
7
|
+
'slice',
|
|
8
|
+
'sort',
|
|
9
|
+
'toSorted',
|
|
10
|
+
'reverse',
|
|
11
|
+
'concat',
|
|
12
|
+
'find',
|
|
13
|
+
'at',
|
|
14
|
+
'structuredClone',
|
|
15
|
+
])
|
|
16
|
+
const ESCAPES = new Set(['untrack', 'optimistic'])
|
|
17
|
+
|
|
18
|
+
export function scan(rel, text, queries, report) {
|
|
19
|
+
const sf = ts.createSourceFile(
|
|
20
|
+
rel,
|
|
21
|
+
text,
|
|
22
|
+
ts.ScriptTarget.Latest,
|
|
23
|
+
true,
|
|
24
|
+
/\.[jt]sx$/.test(rel) ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
25
|
+
)
|
|
26
|
+
const frames = [{ handles: new Set(), values: new Set() }]
|
|
27
|
+
const { peel, isQueryCall, isFetch, isHandle, serverValue } = analysis(queries, frames)
|
|
28
|
+
|
|
29
|
+
const returned = (fn) => {
|
|
30
|
+
if (!ts.isArrowFunction(fn) && !ts.isFunctionExpression(fn)) return undefined
|
|
31
|
+
if (!ts.isBlock(fn.body)) return fn.body
|
|
32
|
+
const r = fn.body.statements.find(ts.isReturnStatement)
|
|
33
|
+
return r?.expression
|
|
34
|
+
}
|
|
35
|
+
/** The first handle a function's body reads: the source `state.linked` follows. */
|
|
36
|
+
function handleIn(node) {
|
|
37
|
+
/** @type {string | undefined} */
|
|
38
|
+
let found
|
|
39
|
+
const walk = (n) => {
|
|
40
|
+
if (found) return
|
|
41
|
+
if (ts.isPropertyAccessExpression(n) && n.name.text === 'value' && isHandle(n.expression))
|
|
42
|
+
found = n.expression.getText(sf)
|
|
43
|
+
else ts.forEachChild(n, walk)
|
|
44
|
+
}
|
|
45
|
+
walk(node)
|
|
46
|
+
return found
|
|
47
|
+
}
|
|
48
|
+
const flag = (node, why) => report('S001', node.getStart(sf), node.end, why)
|
|
49
|
+
|
|
50
|
+
const context = {
|
|
51
|
+
sf,
|
|
52
|
+
frames,
|
|
53
|
+
report,
|
|
54
|
+
peel,
|
|
55
|
+
isQueryCall,
|
|
56
|
+
isFetch,
|
|
57
|
+
serverValue,
|
|
58
|
+
returned,
|
|
59
|
+
handleIn,
|
|
60
|
+
flag,
|
|
61
|
+
}
|
|
62
|
+
const visit = makeVisitor(context)
|
|
63
|
+
visit(sf)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function analysis(queries, frames) {
|
|
67
|
+
const has = (kind, name) => frames.some((f) => f[kind].has(name))
|
|
68
|
+
const peel = (e) => {
|
|
69
|
+
while (
|
|
70
|
+
ts.isParenthesizedExpression(e) ||
|
|
71
|
+
ts.isAsExpression(e) ||
|
|
72
|
+
ts.isNonNullExpression(e) ||
|
|
73
|
+
ts.isSatisfiesExpression(e)
|
|
74
|
+
)
|
|
75
|
+
e = e.expression
|
|
76
|
+
return e
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const isQueryCall = (e) =>
|
|
80
|
+
ts.isCallExpression(e) &&
|
|
81
|
+
(ts.isIdentifier(e.expression)
|
|
82
|
+
? queries.has(e.expression.text)
|
|
83
|
+
: ts.isPropertyAccessExpression(e.expression) &&
|
|
84
|
+
e.expression.name.text === 'live' &&
|
|
85
|
+
ts.isIdentifier(e.expression.expression) &&
|
|
86
|
+
queries.has(e.expression.expression.text))
|
|
87
|
+
const isFetch = (e) =>
|
|
88
|
+
ts.isCallExpression(e) &&
|
|
89
|
+
ts.isPropertyAccessExpression(e.expression) &&
|
|
90
|
+
e.expression.name.text === 'fetch' &&
|
|
91
|
+
ts.isIdentifier(e.expression.expression) &&
|
|
92
|
+
queries.has(e.expression.expression.text)
|
|
93
|
+
const isHandle = (e) => {
|
|
94
|
+
e = peel(e)
|
|
95
|
+
return isQueryCall(e) || (ts.isIdentifier(e) && has('handles', e.text))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The expression holds a whole server object or array, or a derived copy of one. A picked field isn't one. */
|
|
99
|
+
function serverValue(e) {
|
|
100
|
+
e = peel(e)
|
|
101
|
+
if (ts.isIdentifier(e)) return has('values', e.text)
|
|
102
|
+
if (ts.isAwaitExpression(e))
|
|
103
|
+
return isFetch(peel(e.expression)) || isQueryCall(peel(e.expression))
|
|
104
|
+
if (ts.isPropertyAccessExpression(e)) return e.name.text === 'value' && isHandle(e.expression)
|
|
105
|
+
if (ts.isElementAccessExpression(e)) return serverValue(e.expression)
|
|
106
|
+
if (
|
|
107
|
+
ts.isBinaryExpression(e) &&
|
|
108
|
+
[ts.SyntaxKind.QuestionQuestionToken, ts.SyntaxKind.BarBarToken].includes(
|
|
109
|
+
e.operatorToken.kind,
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
return serverValue(e.left) || serverValue(e.right)
|
|
113
|
+
if (ts.isConditionalExpression(e)) return serverValue(e.whenTrue) || serverValue(e.whenFalse)
|
|
114
|
+
if (ts.isCallExpression(e)) {
|
|
115
|
+
const c = e.expression
|
|
116
|
+
return (
|
|
117
|
+
ts.isPropertyAccessExpression(c) &&
|
|
118
|
+
KEEPS_OBJECTS.has(c.name.text) &&
|
|
119
|
+
serverValue(c.expression)
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
if (ts.isArrayLiteralExpression(e))
|
|
123
|
+
return e.elements.some((x) => ts.isSpreadElement(x) && serverValue(x.expression))
|
|
124
|
+
if (ts.isObjectLiteralExpression(e))
|
|
125
|
+
return e.properties.some((p) => ts.isSpreadAssignment(p) && serverValue(p.expression))
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { peel, isQueryCall, isFetch, isHandle, serverValue }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function makeVisitor(context) {
|
|
133
|
+
const { sf, frames, peel, isQueryCall, serverValue, flag } = context
|
|
134
|
+
function visit(node) {
|
|
135
|
+
if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name) && ESCAPES.has(node.name.text))
|
|
136
|
+
return
|
|
137
|
+
if (ts.isMethodDeclaration(node) && ts.isIdentifier(node.name) && ESCAPES.has(node.name.text))
|
|
138
|
+
return
|
|
139
|
+
const fn = ts.isFunctionLike(node)
|
|
140
|
+
if (fn) frames.push({ handles: new Set(), values: new Set() })
|
|
141
|
+
try {
|
|
142
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
143
|
+
const init = peel(node.initializer)
|
|
144
|
+
if (isQueryCall(init)) frames.at(-1).handles.add(node.name.text)
|
|
145
|
+
else if (serverValue(init)) frames.at(-1).values.add(node.name.text)
|
|
146
|
+
}
|
|
147
|
+
if (
|
|
148
|
+
ts.isBinaryExpression(node) &&
|
|
149
|
+
node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
150
|
+
ts.isPropertyAccessExpression(node.left) &&
|
|
151
|
+
node.left.name.text === 'value' &&
|
|
152
|
+
serverValue(node.right)
|
|
153
|
+
)
|
|
154
|
+
flag(
|
|
155
|
+
node,
|
|
156
|
+
`A server value is copied into \`${node.left.expression.getText(sf)}\`: the copy goes stale, and its rows lose their identity.`,
|
|
157
|
+
)
|
|
158
|
+
if (ts.isCallExpression(node) && visitCall(node, context, visit)) return
|
|
159
|
+
ts.forEachChild(node, visit)
|
|
160
|
+
} finally {
|
|
161
|
+
if (fn) frames.pop()
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return visit
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function visitCall(node, context, visit) {
|
|
168
|
+
const { sf, frames, report, peel, isFetch, serverValue, returned, handleIn, flag } = context
|
|
169
|
+
const c = node.expression
|
|
170
|
+
if (ts.isIdentifier(c) && ESCAPES.has(c.text)) return true
|
|
171
|
+
if (
|
|
172
|
+
ts.isPropertyAccessExpression(c) &&
|
|
173
|
+
ts.isIdentifier(c.expression) &&
|
|
174
|
+
c.expression.text === 'state' &&
|
|
175
|
+
c.name.text === 'linked'
|
|
176
|
+
) {
|
|
177
|
+
const source = handleIn(node.arguments[0] ?? node)
|
|
178
|
+
if (source && node.arguments.length < 2) {
|
|
179
|
+
const at = node.arguments[0].end
|
|
180
|
+
report(
|
|
181
|
+
'S012',
|
|
182
|
+
node.getStart(sf),
|
|
183
|
+
node.end,
|
|
184
|
+
`\`state.linked\` over \`${source}\` has no \`key\`: a refetch resets what someone is typing.`,
|
|
185
|
+
{
|
|
186
|
+
description: `Key it by the row: \`{ key: () => ${source}.value?.id }\``,
|
|
187
|
+
safe: false,
|
|
188
|
+
edits: [[at, at, `, { key: () => ${source}.value?.id }`]],
|
|
189
|
+
},
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
return true // the declared editing path: not S001
|
|
193
|
+
}
|
|
194
|
+
if (
|
|
195
|
+
ts.isIdentifier(c) &&
|
|
196
|
+
c.text === 'state' &&
|
|
197
|
+
node.arguments[0] &&
|
|
198
|
+
serverValue(node.arguments[0])
|
|
199
|
+
)
|
|
200
|
+
flag(
|
|
201
|
+
node,
|
|
202
|
+
'A server value is copied into `state`: the copy goes stale, and its rows lose their identity.',
|
|
203
|
+
)
|
|
204
|
+
if (ts.isPropertyAccessExpression(c) && c.name.text === 'set' && node.arguments.length === 1) {
|
|
205
|
+
const r = returned(node.arguments[0])
|
|
206
|
+
if (r && serverValue(r))
|
|
207
|
+
flag(
|
|
208
|
+
node,
|
|
209
|
+
`A server value is copied into \`${c.expression.getText(sf)}\`: the copy goes stale, and its rows lose their identity.`,
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
// The value a callback gets from a query: `watch(() => q.value, v => …)`, `q.fetch().then(v => …)`.
|
|
213
|
+
const fed =
|
|
214
|
+
ts.isIdentifier(c) &&
|
|
215
|
+
c.text === 'watch' &&
|
|
216
|
+
returned(node.arguments[0]) &&
|
|
217
|
+
serverValue(returned(node.arguments[0]))
|
|
218
|
+
? node.arguments[1]
|
|
219
|
+
: ts.isPropertyAccessExpression(c) && c.name.text === 'then' && isFetch(peel(c.expression))
|
|
220
|
+
? node.arguments[0]
|
|
221
|
+
: undefined
|
|
222
|
+
if (
|
|
223
|
+
fed &&
|
|
224
|
+
(ts.isArrowFunction(fed) || ts.isFunctionExpression(fed)) &&
|
|
225
|
+
ts.isIdentifier(fed.parameters[0]?.name)
|
|
226
|
+
) {
|
|
227
|
+
frames.push({ handles: new Set(), values: new Set([fed.parameters[0].name.text]) })
|
|
228
|
+
ts.forEachChild(node, visit)
|
|
229
|
+
frames.pop()
|
|
230
|
+
return true
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Server entry reachability follows local runtime ESM edges, including reexports.
|
|
2
|
+
// Type-only edges do not evaluate modules. Package/dynamic imports stay outside this graph.
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import ts from 'typescript'
|
|
6
|
+
import { discover } from '../files.mjs'
|
|
7
|
+
import { isServerModule, moduleFile } from './modules.mjs'
|
|
8
|
+
|
|
9
|
+
const source = /\.[cm]?[jt]sx?$/
|
|
10
|
+
const ignored = /(?:\.d\.[cm]?ts$|\.(?:test|spec)\.[cm]?[jt]sx?$|\/(?:__tests__|test)\/)/
|
|
11
|
+
const entry = (file) =>
|
|
12
|
+
isServerModule(file) ||
|
|
13
|
+
/^src\/(?:entry-server|server)\.[cm]?[jt]sx?$/.test(file) ||
|
|
14
|
+
/^src\/routes\/.*\.[jt]sx$/.test(file)
|
|
15
|
+
|
|
16
|
+
function runtime(node) {
|
|
17
|
+
if (ts.isImportDeclaration(node)) {
|
|
18
|
+
const clause = node.importClause
|
|
19
|
+
if (!clause) return true
|
|
20
|
+
if (clause.isTypeOnly) return false
|
|
21
|
+
const named = clause.namedBindings
|
|
22
|
+
return (
|
|
23
|
+
!!clause.name ||
|
|
24
|
+
!named ||
|
|
25
|
+
!ts.isNamedImports(named) ||
|
|
26
|
+
!named.elements.length ||
|
|
27
|
+
named.elements.some((element) => !element.isTypeOnly)
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
if (!ts.isExportDeclaration(node) || node.isTypeOnly) return false
|
|
31
|
+
const named = node.exportClause
|
|
32
|
+
return (
|
|
33
|
+
!named ||
|
|
34
|
+
!ts.isNamedExports(named) ||
|
|
35
|
+
!named.elements.length ||
|
|
36
|
+
named.elements.some((element) => !element.isTypeOnly)
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Source snapshots reachable from conventional server modules, entries and SSR route pages. */
|
|
41
|
+
export function serverGraph(root, { files, exclude = [] }) {
|
|
42
|
+
const texts = new Map(
|
|
43
|
+
discover(root, {
|
|
44
|
+
include: ['src/**/*.{ts,tsx,js,jsx,mts,cts,mjs,cjs}', '*.server.{ts,js,mts,mjs,cts,cjs}'],
|
|
45
|
+
exclude,
|
|
46
|
+
}).map((file) => [file, readFileSync(join(root, file), 'utf8')]),
|
|
47
|
+
)
|
|
48
|
+
for (const file of files) texts.set(file.rel, file.text)
|
|
49
|
+
for (const file of texts.keys()) if (!source.test(file) || ignored.test(file)) texts.delete(file)
|
|
50
|
+
const graph = new Map()
|
|
51
|
+
const visit = (file, from) => {
|
|
52
|
+
if (graph.has(file) || !texts.has(file)) return
|
|
53
|
+
const text = texts.get(file)
|
|
54
|
+
const sf = ts.createSourceFile(join(root, file), text, ts.ScriptTarget.Latest, true)
|
|
55
|
+
graph.set(file, { sf, entry: from })
|
|
56
|
+
for (const node of sf.statements) {
|
|
57
|
+
if (!runtime(node) || !node.moduleSpecifier || !ts.isStringLiteralLike(node.moduleSpecifier))
|
|
58
|
+
continue
|
|
59
|
+
const target = moduleFile(root, file, node.moduleSpecifier.text)
|
|
60
|
+
if (target) visit(target, from)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const file of [...texts.keys()].filter(entry).sort()) visit(file, file)
|
|
64
|
+
return graph
|
|
65
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// S001 and S012 in `kerf check` (research 14 §3.1, §3.4, research 29 §4): server values copied into state, and
|
|
2
|
+
// `state.linked` over one with no `key`. X037: a resource or query made beside the `<Boundary>` meant to wait for it.
|
|
3
|
+
// These need to know which imports are queries, so they read the server modules; the compiler sees one file at a time. TypeScript's parser only, no type checking, and a name's taint lasts for the
|
|
4
|
+
// function it was declared in, so a same-named primitive elsewhere isn't flagged.
|
|
5
|
+
|
|
6
|
+
import { readFileSync } from 'node:fs'
|
|
7
|
+
import { join } from 'node:path'
|
|
8
|
+
import ts from 'typescript'
|
|
9
|
+
import { diagnostic, positions } from './diag.mjs'
|
|
10
|
+
import { discover } from './files.mjs'
|
|
11
|
+
import { scan } from './state-checks/scan.mjs'
|
|
12
|
+
import { isServerModule, moduleFile } from './state-checks/modules.mjs'
|
|
13
|
+
|
|
14
|
+
/** `export const name = query(` in a module: the names of its queries. */
|
|
15
|
+
const queriesOf = (text) =>
|
|
16
|
+
new Set(
|
|
17
|
+
[...text.matchAll(/export\s+const\s+([A-Za-z_$][\w$]*)\s*=\s*query\s*\(/g)].map((m) => m[1]),
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
/** Local names of the queries `sf` imports from server modules. */
|
|
21
|
+
function importedQueries(root, rel, sf, cache) {
|
|
22
|
+
const out = new Set()
|
|
23
|
+
for (const n of sf.statements) {
|
|
24
|
+
if (
|
|
25
|
+
!ts.isImportDeclaration(n) ||
|
|
26
|
+
!n.importClause?.namedBindings ||
|
|
27
|
+
!ts.isNamedImports(n.importClause.namedBindings)
|
|
28
|
+
)
|
|
29
|
+
continue
|
|
30
|
+
const file = moduleFile(root, rel, n.moduleSpecifier.text)
|
|
31
|
+
if (!file || !isServerModule(file)) continue
|
|
32
|
+
if (!cache.has(file)) cache.set(file, queriesOf(readFileSync(join(root, file), 'utf8')))
|
|
33
|
+
for (const s of n.importClause.namedBindings.elements)
|
|
34
|
+
if (cache.get(file).has((s.propertyName ?? s.name).text)) out.add(s.name.text)
|
|
35
|
+
}
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The local names `resource` is imported as from @kerf-js/core. */
|
|
40
|
+
const resourceNames = (sf) =>
|
|
41
|
+
new Set(
|
|
42
|
+
sf.statements.flatMap((n) =>
|
|
43
|
+
ts.isImportDeclaration(n) &&
|
|
44
|
+
n.moduleSpecifier.text === '@kerf-js/core' &&
|
|
45
|
+
n.importClause?.namedBindings &&
|
|
46
|
+
ts.isNamedImports(n.importClause.namedBindings)
|
|
47
|
+
? n.importClause.namedBindings.elements
|
|
48
|
+
.filter((s) => (s.propertyName ?? s.name).text === 'resource')
|
|
49
|
+
.map((s) => s.name.text)
|
|
50
|
+
: [],
|
|
51
|
+
),
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* X037: a `resource` or query made in a component's own body and read inside a `<Boundary>` that component renders. A
|
|
56
|
+
* Boundary waits for what is made while its children are built, so it never sees this one: no fallback shows, and the
|
|
57
|
+
* content renders with nothing in it. Only a read inside the Boundary's children counts: reading it elsewhere is fine.
|
|
58
|
+
* @param {ts.SourceFile} sf @param {Set<string>} makers names whose call makes one (`resource` and the queries)
|
|
59
|
+
*/
|
|
60
|
+
function madeBesideBoundary(sf, makers, report) {
|
|
61
|
+
const peel = (e) => {
|
|
62
|
+
while (
|
|
63
|
+
ts.isParenthesizedExpression(e) ||
|
|
64
|
+
ts.isAsExpression(e) ||
|
|
65
|
+
ts.isNonNullExpression(e) ||
|
|
66
|
+
ts.isSatisfiesExpression(e)
|
|
67
|
+
)
|
|
68
|
+
e = e.expression
|
|
69
|
+
return e
|
|
70
|
+
}
|
|
71
|
+
const maker = (e) => {
|
|
72
|
+
e = peel(e)
|
|
73
|
+
if (!ts.isCallExpression(e)) return undefined
|
|
74
|
+
const c = e.expression
|
|
75
|
+
const name = ts.isIdentifier(c)
|
|
76
|
+
? c.text
|
|
77
|
+
: ts.isPropertyAccessExpression(c) && c.name.text === 'live' && ts.isIdentifier(c.expression)
|
|
78
|
+
? c.expression.text
|
|
79
|
+
: undefined
|
|
80
|
+
return name !== undefined && makers.has(name) ? e : undefined
|
|
81
|
+
}
|
|
82
|
+
const nested = (n) =>
|
|
83
|
+
ts.isFunctionDeclaration(n) ||
|
|
84
|
+
(ts.isVariableDeclaration(n) &&
|
|
85
|
+
!!n.initializer &&
|
|
86
|
+
(ts.isArrowFunction(n.initializer) || ts.isFunctionExpression(n.initializer)))
|
|
87
|
+
function component(fn) {
|
|
88
|
+
/** @type {Map<string, ts.CallExpression>} */
|
|
89
|
+
const made = new Map()
|
|
90
|
+
for (const st of fn.body.statements) {
|
|
91
|
+
if (!ts.isVariableStatement(st)) continue
|
|
92
|
+
for (const d of st.declarationList.declarations) {
|
|
93
|
+
const call = ts.isIdentifier(d.name) && d.initializer ? maker(d.initializer) : undefined
|
|
94
|
+
if (call) made.set(d.name.text, call)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!made.size) return
|
|
98
|
+
const done = new Set()
|
|
99
|
+
const read = (n) => {
|
|
100
|
+
if (
|
|
101
|
+
ts.isIdentifier(n) &&
|
|
102
|
+
made.has(n.text) &&
|
|
103
|
+
!done.has(n.text) &&
|
|
104
|
+
!(ts.isPropertyAccessExpression(n.parent) && n.parent.name === n)
|
|
105
|
+
) {
|
|
106
|
+
done.add(n.text)
|
|
107
|
+
const call = made.get(n.text)
|
|
108
|
+
report(
|
|
109
|
+
'X037',
|
|
110
|
+
call.getStart(sf),
|
|
111
|
+
call.end,
|
|
112
|
+
`\`${n.text}\` is made here, beside the <Boundary> that shows it, so that Boundary never waits for it: no fallback shows, and the content renders before the data. Make it in a component rendered inside the Boundary.`,
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
ts.forEachChild(n, read)
|
|
116
|
+
}
|
|
117
|
+
const find = (n) => {
|
|
118
|
+
if (nested(n)) return // another component, with its own body
|
|
119
|
+
if (ts.isJsxElement(n) && n.openingElement.tagName.getText(sf) === 'Boundary')
|
|
120
|
+
n.children.forEach(read)
|
|
121
|
+
else ts.forEachChild(n, find)
|
|
122
|
+
}
|
|
123
|
+
fn.body.statements.forEach(find)
|
|
124
|
+
}
|
|
125
|
+
const visit = (n) => {
|
|
126
|
+
if (
|
|
127
|
+
(ts.isFunctionDeclaration(n) || ts.isArrowFunction(n) || ts.isFunctionExpression(n)) &&
|
|
128
|
+
n.body &&
|
|
129
|
+
ts.isBlock(n.body)
|
|
130
|
+
)
|
|
131
|
+
component(n)
|
|
132
|
+
ts.forEachChild(n, visit)
|
|
133
|
+
}
|
|
134
|
+
visit(sf)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** @param {string} root @param {{ files: { rel: string, text: string }[], exclude?: string[] }} options */
|
|
138
|
+
export function stateChecks(root, { files, exclude = [] }) {
|
|
139
|
+
const out = []
|
|
140
|
+
const cache = new Map()
|
|
141
|
+
const checked = new Set(files.map((f) => f.rel))
|
|
142
|
+
// Scopes usually live in plain .ts files, which the compiler pass doesn't read.
|
|
143
|
+
const extra = discover(root, { include: ['src/**/*.ts'], exclude }).filter(
|
|
144
|
+
(f) => !checked.has(f) && !isServerModule(f) && !/\.(d|test|spec)\.ts$/.test(f),
|
|
145
|
+
)
|
|
146
|
+
const all = [
|
|
147
|
+
...files.map((f) => [f.rel, f.text]),
|
|
148
|
+
...extra.map((f) => [f, readFileSync(join(root, f), 'utf8')]),
|
|
149
|
+
]
|
|
150
|
+
for (const [rel, text] of all) {
|
|
151
|
+
const copies = /\b(state|watch|set)\b/.test(text),
|
|
152
|
+
boundary = /\.[jt]sx$/.test(rel) && text.includes('<Boundary')
|
|
153
|
+
if (isServerModule(rel) || !(copies || boundary)) continue
|
|
154
|
+
const sf = ts.createSourceFile(
|
|
155
|
+
rel,
|
|
156
|
+
text,
|
|
157
|
+
ts.ScriptTarget.Latest,
|
|
158
|
+
boundary,
|
|
159
|
+
/\.[jt]sx$/.test(rel) ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
160
|
+
)
|
|
161
|
+
const queries = importedQueries(root, rel, sf, cache)
|
|
162
|
+
const pos = positions(text)
|
|
163
|
+
const report = (code, start, end, why, fix) => {
|
|
164
|
+
const offset = [start, end]
|
|
165
|
+
out.push(
|
|
166
|
+
diagnostic({
|
|
167
|
+
code,
|
|
168
|
+
file: rel,
|
|
169
|
+
span: pos.span(start, end),
|
|
170
|
+
offset,
|
|
171
|
+
why,
|
|
172
|
+
fix: fix && {
|
|
173
|
+
description: fix.description,
|
|
174
|
+
safe: fix.safe,
|
|
175
|
+
edits: fix.edits.map(([a, b, t]) => ({
|
|
176
|
+
file: rel,
|
|
177
|
+
span: pos.span(a, b),
|
|
178
|
+
offset: [a, b],
|
|
179
|
+
text: t,
|
|
180
|
+
})),
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
if (copies && queries.size) scan(rel, text, queries, report)
|
|
186
|
+
if (boundary) madeBesideBoundary(sf, new Set([...queries, ...resourceNames(sf)]), report)
|
|
187
|
+
}
|
|
188
|
+
return out
|
|
189
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
export const usageError = (message) => Object.assign(new Error(message), { code: 'E_USAGE' })
|
|
2
|
+
|
|
3
|
+
const words = (s) =>
|
|
4
|
+
s
|
|
5
|
+
.toLowerCase()
|
|
6
|
+
.split(/[\s\-_.:/=,]+|(?<=[a-z])(?=[A-Z])/)
|
|
7
|
+
.filter(Boolean)
|
|
8
|
+
|
|
9
|
+
/** Edit distance with adjacent swaps as one edit (`ordre` → `order`). */
|
|
10
|
+
function distance(a, b) {
|
|
11
|
+
const d = Array.from({ length: a.length + 1 }, (_, i) =>
|
|
12
|
+
Array.from({ length: b.length + 1 }, (__, j) => (i ? (j ? 0 : i) : j)),
|
|
13
|
+
)
|
|
14
|
+
for (let i = 1; i <= a.length; i++)
|
|
15
|
+
for (let j = 1; j <= b.length; j++) {
|
|
16
|
+
d[i][j] = Math.min(
|
|
17
|
+
d[i - 1][j] + 1,
|
|
18
|
+
d[i][j - 1] + 1,
|
|
19
|
+
d[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
20
|
+
)
|
|
21
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
22
|
+
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + 1)
|
|
23
|
+
}
|
|
24
|
+
return d[a.length][b.length]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The names closest to `word` (edit distance within a third of its length, or containing it). */
|
|
28
|
+
export function closest(word, names, n = 3) {
|
|
29
|
+
const w = word.toLowerCase()
|
|
30
|
+
const limit = Math.max(1, Math.floor(w.length / 3))
|
|
31
|
+
return names
|
|
32
|
+
.map((x) => [distance(w, x.toLowerCase()), x])
|
|
33
|
+
.filter(([d, x]) => d <= limit || x.toLowerCase().includes(w) || w.includes(x.toLowerCase()))
|
|
34
|
+
.sort((a, b) => a[0] - b[0])
|
|
35
|
+
.slice(0, n)
|
|
36
|
+
.map(([, x]) => x)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `kerf style find <query>`: the groups that fit, best first. Every word of the query must hit the group's name,
|
|
41
|
+
* doc, parts, axes, values, states or tokens (a name word one typo off counts); with no such group, groups that hit
|
|
42
|
+
* any word.
|
|
43
|
+
*/
|
|
44
|
+
export function findGroups(matrix, query) {
|
|
45
|
+
const terms = words(query ?? '')
|
|
46
|
+
if (!terms.length)
|
|
47
|
+
throw usageError(
|
|
48
|
+
'kerf style find <query>: say what the thing is (`card`, `status badge`, `critical`)',
|
|
49
|
+
)
|
|
50
|
+
const score = (name, g) => {
|
|
51
|
+
const fields = [
|
|
52
|
+
[words(name), 30],
|
|
53
|
+
[words(g.doc ?? ''), 10],
|
|
54
|
+
[[...g.parts, ...g.states, ...Object.keys(g.axes)].flatMap(words), 8],
|
|
55
|
+
[
|
|
56
|
+
Object.values(g.axes)
|
|
57
|
+
.flatMap((v) => (Array.isArray(v) ? v : []))
|
|
58
|
+
.flatMap(words),
|
|
59
|
+
6,
|
|
60
|
+
],
|
|
61
|
+
[g.tokens.flatMap(words), 4],
|
|
62
|
+
]
|
|
63
|
+
let total = name === query ? 100 : 0
|
|
64
|
+
const matched = []
|
|
65
|
+
let hits = 0
|
|
66
|
+
for (const t of terms) {
|
|
67
|
+
let best = 0
|
|
68
|
+
let why = null
|
|
69
|
+
for (const [ws, weight] of fields) {
|
|
70
|
+
for (const w of ws) {
|
|
71
|
+
const s =
|
|
72
|
+
w === t
|
|
73
|
+
? weight
|
|
74
|
+
: w.startsWith(t) || (t.length > 3 && w.includes(t))
|
|
75
|
+
? weight * 0.7
|
|
76
|
+
: weight >= 30 && t.length > 3 && distance(w, t) === 1
|
|
77
|
+
? weight * 0.5
|
|
78
|
+
: 0
|
|
79
|
+
if (s > best) [best, why] = [s, w]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (best) {
|
|
83
|
+
hits++
|
|
84
|
+
matched.push(why)
|
|
85
|
+
}
|
|
86
|
+
total += best
|
|
87
|
+
}
|
|
88
|
+
return { total, hits, matched: [...new Set(matched)] }
|
|
89
|
+
}
|
|
90
|
+
const all = Object.entries(matrix.groups)
|
|
91
|
+
.map(([name, g]) => ({ name, g, ...score(name, g) }))
|
|
92
|
+
.filter((x) => x.total > 0)
|
|
93
|
+
const full = all.filter((x) => x.hits === terms.length)
|
|
94
|
+
const ranked = (full.length ? full : all).sort(
|
|
95
|
+
(a, b) => b.total - a.total || a.name.localeCompare(b.name),
|
|
96
|
+
)
|
|
97
|
+
// A name or doc hit outranks groups that only share a token (`row` → product-row, not every `layout: row`).
|
|
98
|
+
const picked = ranked.filter((x) => x.total >= (ranked[0]?.total ?? 0) / 4)
|
|
99
|
+
return {
|
|
100
|
+
query,
|
|
101
|
+
groups: picked.map(({ name, g, total, matched }) => ({
|
|
102
|
+
name,
|
|
103
|
+
score: Math.round(total),
|
|
104
|
+
matched,
|
|
105
|
+
doc: g.doc,
|
|
106
|
+
file: g.file,
|
|
107
|
+
import:
|
|
108
|
+
g.binding && g.exported ? { name: g.binding, from: g.file.replace(/:\d+$/, '') } : null,
|
|
109
|
+
axes: g.axes,
|
|
110
|
+
defaults: g.defaults,
|
|
111
|
+
parts: g.parts,
|
|
112
|
+
states: g.states,
|
|
113
|
+
uses: g.used.length,
|
|
114
|
+
similar: matrix.similar
|
|
115
|
+
.filter((s) => s[0] === name || s[1] === name)
|
|
116
|
+
.map((s) => (s[0] === name ? s[1] : s[0])),
|
|
117
|
+
})),
|
|
118
|
+
partial: !full.length && all.length > 0,
|
|
119
|
+
total: Object.keys(matrix.groups).length,
|
|
120
|
+
}
|
|
121
|
+
}
|