@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,345 @@
|
|
|
1
|
+
// What one file says about its components, from syntax alone (research 44 §4). No checker here: this runs on every save
|
|
2
|
+
// and is cached by content, so the common file, with nothing to infer, costs a parse. CommonJS because the tsserver
|
|
3
|
+
// plugin loads it synchronously; `ts` is passed in because a plugin must use the TypeScript that hosts it.
|
|
4
|
+
|
|
5
|
+
const { createHash } = require('node:crypto')
|
|
6
|
+
|
|
7
|
+
const hashOf = (text) => createHash('sha1').update(text).digest('base64url').slice(0, 16)
|
|
8
|
+
const { isCap, jsxSite, escapes, holds, isOpening } = require('./facts/sites.cjs')
|
|
9
|
+
/** The comment a generated annotation starts with. It keeps the type regenerable; deleting it makes the type yours. */
|
|
10
|
+
const MARK = '/*inferred*/'
|
|
11
|
+
|
|
12
|
+
function isExported(ts, node) {
|
|
13
|
+
return (
|
|
14
|
+
!!ts.canHaveModifiers(node) &&
|
|
15
|
+
(ts.getModifiers(node) ?? []).some((m) => m.kind === ts.SyntaxKind.ExportKeyword)
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const walk = (ts, node, visit) => {
|
|
20
|
+
const go = (n) => {
|
|
21
|
+
if (visit(n) !== false) ts.forEachChild(n, go)
|
|
22
|
+
}
|
|
23
|
+
ts.forEachChild(node, go)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A function a component could be: the node, its name, and the parameter it would type. */
|
|
27
|
+
function componentAt(ts, node) {
|
|
28
|
+
if (ts.isFunctionDeclaration(node) && node.name && isCap(node.name.text))
|
|
29
|
+
return { fn: node, name: node.name.text, nameNode: node.name, holder: node }
|
|
30
|
+
if (
|
|
31
|
+
ts.isVariableDeclaration(node) &&
|
|
32
|
+
ts.isIdentifier(node.name) &&
|
|
33
|
+
isCap(node.name.text) &&
|
|
34
|
+
!node.type &&
|
|
35
|
+
node.initializer
|
|
36
|
+
) {
|
|
37
|
+
const init = node.initializer
|
|
38
|
+
if (ts.isArrowFunction(init) || ts.isFunctionExpression(init))
|
|
39
|
+
return { fn: init, name: node.name.text, nameNode: node.name, holder: node.parent.parent }
|
|
40
|
+
}
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const hasJsx = (ts, node) => {
|
|
45
|
+
let found = false
|
|
46
|
+
walk(ts, node, (n) => {
|
|
47
|
+
if (found) return false
|
|
48
|
+
if (ts.isJsxElement(n) || ts.isJsxSelfClosingElement(n) || ts.isJsxFragment(n)) found = true
|
|
49
|
+
return !found
|
|
50
|
+
})
|
|
51
|
+
return found
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A parameter pattern's keys: `{ a, b = 1, c: d, ...rest }`. */
|
|
55
|
+
function patternKeys(ts, sf, pattern, out) {
|
|
56
|
+
for (const el of pattern.elements) {
|
|
57
|
+
if (el.dotDotDotToken) {
|
|
58
|
+
if (ts.isIdentifier(el.name)) out.rest = el.name.text
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
61
|
+
const key =
|
|
62
|
+
el.propertyName && ts.isIdentifier(el.propertyName)
|
|
63
|
+
? el.propertyName.text
|
|
64
|
+
: ts.isIdentifier(el.name)
|
|
65
|
+
? el.name.text
|
|
66
|
+
: null
|
|
67
|
+
if (!key) {
|
|
68
|
+
out.opaque = true
|
|
69
|
+
continue
|
|
70
|
+
}
|
|
71
|
+
if (ts.isIdentifier(el.name)) out.aliases.set(el.name.text, key)
|
|
72
|
+
const entry = out.keys.get(key) ?? { uses: [] }
|
|
73
|
+
if (el.initializer) entry.fallback = [el.initializer.getStart(sf), el.initializer.end]
|
|
74
|
+
out.keys.set(key, entry)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Reads and writes of the prop `key` at `node`, the expression that names it. */
|
|
79
|
+
function classify(ts, sf, node) {
|
|
80
|
+
const up = node.parent
|
|
81
|
+
const span = (n) => [n.getStart(sf), n.end]
|
|
82
|
+
if (
|
|
83
|
+
(ts.isPostfixUnaryExpression(up) || ts.isPrefixUnaryExpression(up)) &&
|
|
84
|
+
(up.operator === ts.SyntaxKind.PlusPlusToken || up.operator === ts.SyntaxKind.MinusMinusToken)
|
|
85
|
+
)
|
|
86
|
+
return { as: 'number', span: span(node) }
|
|
87
|
+
if (ts.isBinaryExpression(up) && up.left === node) {
|
|
88
|
+
const op = up.operatorToken.kind
|
|
89
|
+
if (op === ts.SyntaxKind.EqualsToken || op === ts.SyntaxKind.PlusEqualsToken)
|
|
90
|
+
return { as: 'write', span: span(node), value: span(up.right) }
|
|
91
|
+
if (op >= ts.SyntaxKind.FirstCompoundAssignment && op <= ts.SyntaxKind.LastCompoundAssignment)
|
|
92
|
+
return { as: 'number', span: span(node) }
|
|
93
|
+
}
|
|
94
|
+
if (ts.isCallExpression(up) && up.expression === node)
|
|
95
|
+
return { as: 'call', span: span(node), args: up.arguments.map(span) }
|
|
96
|
+
if ((ts.isPropertyAccessExpression(up) && up.expression === node) || ts.isNonNullExpression(up)) {
|
|
97
|
+
const member = ts.isPropertyAccessExpression(up) ? up.name.text : null
|
|
98
|
+
if (member) return { as: 'member', span: span(up), member }
|
|
99
|
+
}
|
|
100
|
+
if (ts.isJsxExpression(up) && ts.isJsxAttribute(up.parent))
|
|
101
|
+
return { as: 'context', span: span(node) }
|
|
102
|
+
return { as: 'read', span: span(node) }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Every use of the props in a component's body. */
|
|
106
|
+
function bodyUses(ts, sf, comp) {
|
|
107
|
+
const { fn, param, info } = comp
|
|
108
|
+
const names = new Set(ts.isIdentifier(param.name) ? [param.name.text] : [])
|
|
109
|
+
const use = (key, node) => {
|
|
110
|
+
const entry = info.keys.get(key) ?? { uses: [] }
|
|
111
|
+
entry.uses.push(classify(ts, sf, node))
|
|
112
|
+
info.keys.set(key, entry)
|
|
113
|
+
}
|
|
114
|
+
const shadows = (node, name) =>
|
|
115
|
+
node.parameters?.some((p) => ts.isIdentifier(p.name) && p.name.text === name)
|
|
116
|
+
const visit = (node, scope) => {
|
|
117
|
+
// `const { a } = props`, `let n = props.n`: another name for the prop (research 43). Only the first level.
|
|
118
|
+
if (ts.isVariableDeclaration(node) && node.initializer) {
|
|
119
|
+
const init = node.initializer
|
|
120
|
+
if (ts.isIdentifier(init) && scope.has(init.text) && ts.isObjectBindingPattern(node.name))
|
|
121
|
+
patternKeys(ts, sf, node.name, info)
|
|
122
|
+
else if (
|
|
123
|
+
ts.isPropertyAccessExpression(init) &&
|
|
124
|
+
ts.isIdentifier(init.expression) &&
|
|
125
|
+
scope.has(init.expression.text) &&
|
|
126
|
+
ts.isIdentifier(node.name)
|
|
127
|
+
) {
|
|
128
|
+
info.aliases.set(node.name.text, init.name.text)
|
|
129
|
+
if (!info.keys.has(init.name.text)) info.keys.set(init.name.text, { uses: [] })
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (
|
|
134
|
+
ts.isPropertyAccessExpression(node) &&
|
|
135
|
+
ts.isIdentifier(node.expression) &&
|
|
136
|
+
scope.has(node.expression.text)
|
|
137
|
+
)
|
|
138
|
+
return use(node.name.text, node)
|
|
139
|
+
if (
|
|
140
|
+
ts.isElementAccessExpression(node) &&
|
|
141
|
+
ts.isIdentifier(node.expression) &&
|
|
142
|
+
scope.has(node.expression.text)
|
|
143
|
+
) {
|
|
144
|
+
if (ts.isStringLiteralLike(node.argumentExpression))
|
|
145
|
+
return use(node.argumentExpression.text, node)
|
|
146
|
+
info.opaque = true
|
|
147
|
+
}
|
|
148
|
+
if (
|
|
149
|
+
ts.isIdentifier(node) &&
|
|
150
|
+
info.aliases.has(node.text) &&
|
|
151
|
+
ts.isExpressionNode?.(node) !== false
|
|
152
|
+
) {
|
|
153
|
+
const up = node.parent
|
|
154
|
+
const named =
|
|
155
|
+
(ts.isPropertyAccessExpression(up) && up.name === node) ||
|
|
156
|
+
(ts.isPropertyAssignment(up) && up.name === node) ||
|
|
157
|
+
ts.isBindingElement(up) ||
|
|
158
|
+
(ts.isVariableDeclaration(up) && up.name === node) ||
|
|
159
|
+
ts.isJsxAttribute(up)
|
|
160
|
+
if (!named) use(info.aliases.get(node.text), node)
|
|
161
|
+
}
|
|
162
|
+
if (ts.isFunctionLike(node)) {
|
|
163
|
+
let inner = scope
|
|
164
|
+
for (const n of scope)
|
|
165
|
+
if (shadows(node, n)) {
|
|
166
|
+
inner = new Set(scope)
|
|
167
|
+
inner.delete(n)
|
|
168
|
+
}
|
|
169
|
+
ts.forEachChild(node, (c) => visit(c, inner))
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
ts.forEachChild(node, (c) => visit(c, scope))
|
|
173
|
+
}
|
|
174
|
+
if (fn.body) visit(fn.body, names)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function scanImports(ts, sf, imports, exported) {
|
|
178
|
+
let exportsLoad = false
|
|
179
|
+
|
|
180
|
+
for (const st of sf.statements) {
|
|
181
|
+
if (ts.isImportDeclaration(st) && st.importClause && ts.isStringLiteral(st.moduleSpecifier)) {
|
|
182
|
+
const from = st.moduleSpecifier.text
|
|
183
|
+
const { name, namedBindings } = st.importClause
|
|
184
|
+
if (name) imports.set(name.text, { from, name: 'default' })
|
|
185
|
+
if (namedBindings && ts.isNamedImports(namedBindings))
|
|
186
|
+
for (const el of namedBindings.elements)
|
|
187
|
+
imports.set(el.name.text, { from, name: (el.propertyName ?? el.name).text })
|
|
188
|
+
}
|
|
189
|
+
if (ts.isExportAssignment(st) && ts.isIdentifier(st.expression))
|
|
190
|
+
exported.add(st.expression.text)
|
|
191
|
+
if (
|
|
192
|
+
ts.isExportDeclaration(st) &&
|
|
193
|
+
st.exportClause &&
|
|
194
|
+
ts.isNamedExports(st.exportClause) &&
|
|
195
|
+
!st.moduleSpecifier
|
|
196
|
+
)
|
|
197
|
+
for (const el of st.exportClause.elements) exported.add((el.propertyName ?? el.name).text)
|
|
198
|
+
if (isExported(ts, st) && ts.isVariableStatement(st))
|
|
199
|
+
for (const d of st.declarationList.declarations)
|
|
200
|
+
if (ts.isIdentifier(d.name) && d.name.text === 'load') exportsLoad = true
|
|
201
|
+
if (isExported(ts, st) && ts.isFunctionDeclaration(st) && st.name?.text === 'load')
|
|
202
|
+
exportsLoad = true
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return exportsLoad
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function visitNodes({ ts, sf, file, text, comps, sites, wraps, escaped }) {
|
|
209
|
+
const enclosing = []
|
|
210
|
+
const visit = (node) => {
|
|
211
|
+
const found = componentAt(ts, node)
|
|
212
|
+
if (found) {
|
|
213
|
+
const { fn, name } = found
|
|
214
|
+
const param = fn.parameters?.[0]
|
|
215
|
+
const typed = param?.type
|
|
216
|
+
const generated = typed && text.slice(param.name.end, typed.pos + 64).includes(MARK)
|
|
217
|
+
if (
|
|
218
|
+
param &&
|
|
219
|
+
fn.parameters.length === 1 &&
|
|
220
|
+
(!typed || generated) &&
|
|
221
|
+
!param.questionToken &&
|
|
222
|
+
!param.dotDotDotToken &&
|
|
223
|
+
!fn.typeParameters &&
|
|
224
|
+
hasJsx(ts, fn)
|
|
225
|
+
) {
|
|
226
|
+
const info = { keys: new Map(), aliases: new Map(), rest: null, opaque: false }
|
|
227
|
+
if (ts.isObjectBindingPattern(param.name)) patternKeys(ts, sf, param.name, info)
|
|
228
|
+
else if (!ts.isIdentifier(param.name)) info.opaque = true
|
|
229
|
+
const comp = {
|
|
230
|
+
id: `${file}#${name}`,
|
|
231
|
+
file,
|
|
232
|
+
name,
|
|
233
|
+
param,
|
|
234
|
+
fn,
|
|
235
|
+
info,
|
|
236
|
+
nameSpan: [found.nameNode.getStart(sf), found.nameNode.end],
|
|
237
|
+
bodySpan: [fn.getStart(sf), fn.end],
|
|
238
|
+
exported: isExported(ts, found.holder),
|
|
239
|
+
isDefault: (ts.canHaveModifiers(found.holder)
|
|
240
|
+
? (ts.getModifiers(found.holder) ?? [])
|
|
241
|
+
: []
|
|
242
|
+
).some((m) => m.kind === ts.SyntaxKind.DefaultKeyword),
|
|
243
|
+
paramSpan: [param.getStart(sf), param.end],
|
|
244
|
+
nameEnd: param.name.end,
|
|
245
|
+
parens: !!fn.getChildren?.(sf).some((c) => c.kind === ts.SyntaxKind.OpenParenToken),
|
|
246
|
+
written: generated ? [param.name.end, typed.end] : null,
|
|
247
|
+
writtenType: generated ? text.slice(typed.getStart(sf), typed.end).trim() : null,
|
|
248
|
+
forwards: [],
|
|
249
|
+
local: enclosing.length > 0,
|
|
250
|
+
async: !!(ts.getModifiers?.(fn) ?? []).some((m) => m.kind === ts.SyntaxKind.AsyncKeyword),
|
|
251
|
+
}
|
|
252
|
+
bodyUses(ts, sf, comp)
|
|
253
|
+
comps.push(comp)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
|
|
257
|
+
const held = holds(ts, node.initializer)
|
|
258
|
+
if (held.length) wraps.set(node.name.text, held)
|
|
259
|
+
}
|
|
260
|
+
if (isOpening(ts, node)) {
|
|
261
|
+
const site = jsxSite(ts, sf, node, enclosing[enclosing.length - 1])
|
|
262
|
+
if (site) sites.push(site)
|
|
263
|
+
}
|
|
264
|
+
if (
|
|
265
|
+
ts.isIdentifier(node) &&
|
|
266
|
+
isCap(node.text) &&
|
|
267
|
+
node.parent &&
|
|
268
|
+
!ts.isTypeReferenceNode(node.parent) &&
|
|
269
|
+
escapes(ts, node)
|
|
270
|
+
)
|
|
271
|
+
escaped.add(node.text)
|
|
272
|
+
const push = found && found.fn
|
|
273
|
+
if (push) enclosing.push(found.name)
|
|
274
|
+
ts.forEachChild(node, visit)
|
|
275
|
+
if (push) enclosing.pop()
|
|
276
|
+
}
|
|
277
|
+
ts.forEachChild(sf, visit)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* @returns the file's facts: `comps` (untyped or generated), `sites` (capitalised JSX tags), `imports`, `wraps`
|
|
282
|
+
* (`const W = hoc(Inner)`), `escaped` (component names used as values), `exports` (local names).
|
|
283
|
+
*/
|
|
284
|
+
function fileFacts(ts, file, text, options = {}) {
|
|
285
|
+
const hash = hashOf(text)
|
|
286
|
+
if (!file.endsWith('.tsx'))
|
|
287
|
+
return {
|
|
288
|
+
file,
|
|
289
|
+
hash,
|
|
290
|
+
comps: [],
|
|
291
|
+
sites: [],
|
|
292
|
+
imports: new Map(),
|
|
293
|
+
wraps: new Map(),
|
|
294
|
+
escaped: new Set(),
|
|
295
|
+
exported: new Set(),
|
|
296
|
+
parse: null,
|
|
297
|
+
}
|
|
298
|
+
const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX)
|
|
299
|
+
const comps = [],
|
|
300
|
+
sites = []
|
|
301
|
+
const imports = new Map(),
|
|
302
|
+
wraps = new Map(),
|
|
303
|
+
escaped = new Set(),
|
|
304
|
+
exported = new Set()
|
|
305
|
+
const exportsLoad = scanImports(ts, sf, imports, exported)
|
|
306
|
+
|
|
307
|
+
visitNodes({ ts, sf, file, text, comps, sites, wraps, escaped })
|
|
308
|
+
|
|
309
|
+
for (const c of comps)
|
|
310
|
+
if (exported.has(c.name) || c.isDefault) {
|
|
311
|
+
c.exported = true
|
|
312
|
+
c.isDefault ||= false
|
|
313
|
+
}
|
|
314
|
+
// Spreads and attributes forward from a component to whatever it renders.
|
|
315
|
+
for (const c of comps) {
|
|
316
|
+
const own = new Set([
|
|
317
|
+
...(ts.isIdentifier(c.param.name) ? [c.param.name.text] : []),
|
|
318
|
+
...(c.info.rest ? [c.info.rest] : []),
|
|
319
|
+
])
|
|
320
|
+
for (const s of sites) {
|
|
321
|
+
if (s.span[0] < c.fn.getStart(sf) || s.span[1] > c.fn.end) continue
|
|
322
|
+
for (const sp of s.spreads)
|
|
323
|
+
if (own.has(sp.text))
|
|
324
|
+
c.forwards.push({ tag: s.tag, tagSpan: s.tagSpan, given: s.attrs.map((a) => a.name) })
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
for (const c of comps) {
|
|
328
|
+
delete c.fn
|
|
329
|
+
delete c.param
|
|
330
|
+
}
|
|
331
|
+
return {
|
|
332
|
+
file,
|
|
333
|
+
hash,
|
|
334
|
+
comps,
|
|
335
|
+
sites,
|
|
336
|
+
imports,
|
|
337
|
+
wraps,
|
|
338
|
+
escaped,
|
|
339
|
+
exported,
|
|
340
|
+
exportsLoad,
|
|
341
|
+
parse: options.keep ? sf : null,
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
module.exports = { fileFacts, hashOf, MARK }
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// The two ways to ask a checker about a shadow. Both answer the same four questions about a span (`query`), so the
|
|
2
|
+
// engine doesn't know which compiler it is on. `tsgoHost` drives TypeScript 7's process through its unstable API with a
|
|
3
|
+
// file overlay: `tsc --noEmit` of the source tree, with no files on disk and no plugin. `languageHost` reuses a language
|
|
4
|
+
// service, which is what a tsserver plugin has, and what a project on TypeScript 6 gets.
|
|
5
|
+
|
|
6
|
+
const { boxMismatch } = require('./box-props.cjs')
|
|
7
|
+
const { derivedWrite } = require('./derived-writes.cjs')
|
|
8
|
+
|
|
9
|
+
const NO_TRUNCATE = 1
|
|
10
|
+
|
|
11
|
+
/** The node exactly on [start, end]: the deepest one, because `x` and `x;` can share an end but not a start. */
|
|
12
|
+
function nodeAt(each, root, start, end) {
|
|
13
|
+
let best = null
|
|
14
|
+
const go = (n) => {
|
|
15
|
+
if (n.pos <= start && n.end >= end) {
|
|
16
|
+
if (n.end === end) best = n
|
|
17
|
+
each(n, go)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
each(root, go)
|
|
21
|
+
return best
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The flags the engine reads. `lit`: the widened text differs from the exact one. */
|
|
25
|
+
function describe(checker, type, flagsAny, node) {
|
|
26
|
+
if (!type) return { text: 'any', any: true }
|
|
27
|
+
const widened =
|
|
28
|
+
checker.getWidenedType?.(checker.getBaseTypeOfLiteralType(type)) ??
|
|
29
|
+
checker.getBaseTypeOfLiteralType(type)
|
|
30
|
+
const text = checker.typeToString(widened, node, NO_TRUNCATE)
|
|
31
|
+
const exact = checker.typeToString(type, node, NO_TRUNCATE)
|
|
32
|
+
return { text, exact: exact === text ? undefined : exact, any: !!(type.flags & flagsAny) }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function answer(checker, node, req, env) {
|
|
36
|
+
if (!node) return { text: 'any', any: true, missing: true }
|
|
37
|
+
switch (req.kind) {
|
|
38
|
+
case 'type':
|
|
39
|
+
return describe(checker, checker.getTypeAtLocation(node), env.Any, node)
|
|
40
|
+
case 'context':
|
|
41
|
+
return describe(checker, checker.getContextualType(node), env.Any, node)
|
|
42
|
+
case 'spread': {
|
|
43
|
+
const type = checker.getTypeAtLocation(node)
|
|
44
|
+
if (!type || type.flags & env.Any) return { any: true, props: [] }
|
|
45
|
+
return {
|
|
46
|
+
props: checker.getPropertiesOfType(type).map((s) => ({
|
|
47
|
+
name: s.name,
|
|
48
|
+
optional: !!(s.flags & env.Optional),
|
|
49
|
+
...describe(checker, checker.getTypeOfSymbol(s), env.Any, node),
|
|
50
|
+
})),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
case 'decls': {
|
|
54
|
+
const type = checker.getTypeAtLocation(node)
|
|
55
|
+
if (!type) return { decls: [] }
|
|
56
|
+
const members = env.union(type) ?? [type]
|
|
57
|
+
return {
|
|
58
|
+
decls: members
|
|
59
|
+
.flatMap((t) => checker.getSignaturesOfType(t, env.Call).map((s) => env.place(s)))
|
|
60
|
+
.filter(Boolean),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Over TypeScript 7's API. `typescript/unstable/*` is a moving name, so this is the one file that touches it. */
|
|
67
|
+
async function tsgoHost({ root, config, typescriptDir, overlay }) {
|
|
68
|
+
const load = (name) =>
|
|
69
|
+
import(require('node:url').pathToFileURL(require('node:path').join(typescriptDir, name)).href)
|
|
70
|
+
const { API, SignatureKind, SymbolFlags, TypeFlags } = await load('dist/api/sync/api.js')
|
|
71
|
+
const each = (n, f) => n.forEachChild(f)
|
|
72
|
+
const api = new API({ cwd: root, fs: { readFile: (f) => overlay.get(f) } })
|
|
73
|
+
let snap = api.updateSnapshot({ openProject: config })
|
|
74
|
+
let project = snap.getProject(config) ?? snap.getProjects()[0]
|
|
75
|
+
const env = {
|
|
76
|
+
Any: TypeFlags.Any,
|
|
77
|
+
Optional: SymbolFlags.Optional,
|
|
78
|
+
Call: SignatureKind.Call,
|
|
79
|
+
union: (t) => (t.isUnionType() ? t.getTypes() : null),
|
|
80
|
+
place(s) {
|
|
81
|
+
const n = s.declaration?.resolve(project)
|
|
82
|
+
return n && { file: s.declaration.path, pos: n.pos, end: n.end }
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
kind: 'tsgo',
|
|
87
|
+
update(files, touched = []) {
|
|
88
|
+
for (const [f, text] of files) overlay.set(f, text)
|
|
89
|
+
const next = api.updateSnapshot({ fileChanges: { changed: [...files.keys(), ...touched] } })
|
|
90
|
+
snap.dispose?.()
|
|
91
|
+
snap = next
|
|
92
|
+
project = snap.getProject(config) ?? snap.getProjects()[0]
|
|
93
|
+
},
|
|
94
|
+
query(file, reqs) {
|
|
95
|
+
const sf = project.program.getSourceFile(file)
|
|
96
|
+
if (!sf) return reqs.map(() => ({ text: 'any', any: true, missing: true }))
|
|
97
|
+
return reqs.map((r) => answer(project.checker, nodeAt(each, sf, r.start, r.end), r, env))
|
|
98
|
+
},
|
|
99
|
+
diagnostics(file) {
|
|
100
|
+
return [
|
|
101
|
+
...project.program.getSyntacticDiagnostics(file),
|
|
102
|
+
...project.program.getSemanticDiagnostics(file),
|
|
103
|
+
].map((d) => ({ start: d.pos, length: d.end - d.pos, code: d.code, text: flatten(d) }))
|
|
104
|
+
},
|
|
105
|
+
allDiagnostics() {
|
|
106
|
+
return project.program
|
|
107
|
+
.getSourceFileNames()
|
|
108
|
+
.filter((f) => !f.includes('/node_modules/'))
|
|
109
|
+
.flatMap((f) => this.diagnostics(f).map((d) => ({ ...d, file: f })))
|
|
110
|
+
},
|
|
111
|
+
close() {
|
|
112
|
+
snap.dispose?.()
|
|
113
|
+
api.close()
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const flatten = (d) =>
|
|
119
|
+
(typeof d.text === 'string' ? d.text : (d.message ?? d.messageText ?? String(d.text))) +
|
|
120
|
+
(d.messageChain?.length ? `\n ${d.messageChain.map(flatten).join('\n ')}` : '')
|
|
121
|
+
|
|
122
|
+
/** Over a TypeScript language service: `service()` returns it, `sync()` makes it see what `update` was told. */
|
|
123
|
+
function languageHost(ts, service, sync = () => {}) {
|
|
124
|
+
const env = {
|
|
125
|
+
Any: ts.TypeFlags.Any,
|
|
126
|
+
Optional: ts.SymbolFlags.Optional,
|
|
127
|
+
Call: ts.SignatureKind.Call,
|
|
128
|
+
union: (t) => (t.isUnion() ? t.types : null),
|
|
129
|
+
place(s) {
|
|
130
|
+
const n = s.declaration
|
|
131
|
+
return n && { file: n.getSourceFile().fileName, pos: n.pos, end: n.end }
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
const each = (n, f) => ts.forEachChild(n, f)
|
|
135
|
+
return {
|
|
136
|
+
kind: 'language-service',
|
|
137
|
+
update(files, touched = []) {
|
|
138
|
+
sync(files, touched)
|
|
139
|
+
},
|
|
140
|
+
query(file, reqs) {
|
|
141
|
+
const program = service().getProgram()
|
|
142
|
+
const sf = program.getSourceFile(file)
|
|
143
|
+
if (!sf) return reqs.map(() => ({ text: 'any', any: true, missing: true }))
|
|
144
|
+
const checker = program.getTypeChecker()
|
|
145
|
+
return reqs.map((r) => answer(checker, nodeAt(each, sf, r.start, r.end), r, env))
|
|
146
|
+
},
|
|
147
|
+
diagnostics(file) {
|
|
148
|
+
const s = service()
|
|
149
|
+
return [...s.getSyntacticDiagnostics(file), ...s.getSemanticDiagnostics(file)].map((d) => ({
|
|
150
|
+
start: d.start ?? 0,
|
|
151
|
+
length: d.length ?? 0,
|
|
152
|
+
code: d.code,
|
|
153
|
+
text: ts.flattenDiagnosticMessageText(d.messageText, '\n'),
|
|
154
|
+
kerf: derivedWrite(ts, s.getProgram(), d) ?? boxMismatch(ts, s.getProgram(), d),
|
|
155
|
+
}))
|
|
156
|
+
},
|
|
157
|
+
close() {},
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
module.exports = { tsgoHost, languageHost, nodeAt }
|