@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,46 @@
|
|
|
1
|
+
// Core box provenance shared by TypeScript diagnostic mappings.
|
|
2
|
+
const programs = new WeakMap()
|
|
3
|
+
|
|
4
|
+
/** Resolved core exports and member declarations for one checker program. */
|
|
5
|
+
function coreMembers(ts, program, checker) {
|
|
6
|
+
if (programs.has(program)) return programs.get(program)
|
|
7
|
+
const roots = new Set(),
|
|
8
|
+
shapes = [],
|
|
9
|
+
derivedRoots = new Set()
|
|
10
|
+
for (const file of program.getSourceFiles()) {
|
|
11
|
+
for (const node of file.statements) {
|
|
12
|
+
if (
|
|
13
|
+
(!ts.isImportDeclaration(node) && !ts.isExportDeclaration(node)) ||
|
|
14
|
+
node.moduleSpecifier?.text !== '@kerf-js/core'
|
|
15
|
+
)
|
|
16
|
+
continue
|
|
17
|
+
const module = checker.getSymbolAtLocation(node.moduleSpecifier)
|
|
18
|
+
if (!module) continue
|
|
19
|
+
for (let symbol of checker.getExportsOfModule(module)) {
|
|
20
|
+
if (!['State', 'Derived'].includes(symbol.name)) continue
|
|
21
|
+
const derived = symbol.name === 'Derived'
|
|
22
|
+
if (symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol)
|
|
23
|
+
const type = checker.getDeclaredTypeOfSymbol(symbol)
|
|
24
|
+
const value = checker.getPropertyOfType(type, 'value')
|
|
25
|
+
roots.add(symbol)
|
|
26
|
+
if (derived) derivedRoots.add(symbol)
|
|
27
|
+
shapes.push({
|
|
28
|
+
names: checker.getPropertiesOfType(type).map((property) => property.name),
|
|
29
|
+
declarations: new Set(value?.getDeclarations() ?? []),
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const core = { roots, shapes, derivedRoots }
|
|
35
|
+
programs.set(program, core)
|
|
36
|
+
return core
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Whether a type avoids any, unknown and unresolved type parameters. */
|
|
40
|
+
function known(ts, type) {
|
|
41
|
+
if (!type || type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter))
|
|
42
|
+
return false
|
|
43
|
+
return !type.isUnion() || type.types.every((part) => known(ts, part))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
module.exports = { coreMembers, known }
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// W007 maps only TypeScript's readonly write error on a proven core Derived.value.
|
|
2
|
+
const { coreMembers, known } = require('./core-boxes.cjs')
|
|
3
|
+
|
|
4
|
+
function derivedSymbol(ts, checker, symbol, roots, seen = new Set()) {
|
|
5
|
+
if (!symbol || seen.has(symbol)) return false
|
|
6
|
+
seen.add(symbol)
|
|
7
|
+
if (symbol.flags & ts.SymbolFlags.Alias) symbol = checker.getAliasedSymbol(symbol)
|
|
8
|
+
if (roots.has(symbol)) return true
|
|
9
|
+
return (symbol.getDeclarations() ?? []).some((declaration) => {
|
|
10
|
+
if (!ts.isTypeAliasDeclaration(declaration)) return false
|
|
11
|
+
let type = declaration.type
|
|
12
|
+
while (ts.isParenthesizedTypeNode(type)) type = type.type
|
|
13
|
+
return (
|
|
14
|
+
ts.isTypeReferenceNode(type) &&
|
|
15
|
+
derivedSymbol(ts, checker, checker.getSymbolAtLocation(type.typeName), roots, seen)
|
|
16
|
+
)
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function derivedType(ts, checker, type, roots) {
|
|
21
|
+
if (!known(ts, type)) return false
|
|
22
|
+
if (type.isUnion()) return type.types.every((part) => derivedType(ts, checker, part, roots))
|
|
23
|
+
if (type.isIntersection()) return type.types.some((part) => derivedType(ts, checker, part, roots))
|
|
24
|
+
if (
|
|
25
|
+
derivedSymbol(ts, checker, type.aliasSymbol, roots) ||
|
|
26
|
+
derivedSymbol(ts, checker, type.getSymbol(), roots)
|
|
27
|
+
)
|
|
28
|
+
return true
|
|
29
|
+
return (
|
|
30
|
+
!!type.isClassOrInterface?.() &&
|
|
31
|
+
checker.getBaseTypes(type).some((base) => derivedType(ts, checker, base, roots))
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function writeTarget(ts, node) {
|
|
36
|
+
for (let parent = node.parent; parent; node = parent, parent = parent.parent) {
|
|
37
|
+
if (ts.isBinaryExpression(parent) && parent.left === node) {
|
|
38
|
+
const operator = parent.operatorToken.kind
|
|
39
|
+
return (
|
|
40
|
+
operator === ts.SyntaxKind.EqualsToken ||
|
|
41
|
+
(operator >= ts.SyntaxKind.FirstCompoundAssignment &&
|
|
42
|
+
operator <= ts.SyntaxKind.LastCompoundAssignment)
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
if (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent))
|
|
46
|
+
return (
|
|
47
|
+
parent.operand === node &&
|
|
48
|
+
[ts.SyntaxKind.PlusPlusToken, ts.SyntaxKind.MinusMinusToken].includes(parent.operator)
|
|
49
|
+
)
|
|
50
|
+
if (ts.isForOfStatement(parent) || ts.isForInStatement(parent))
|
|
51
|
+
return parent.initializer === node
|
|
52
|
+
const wrapped =
|
|
53
|
+
ts.isParenthesizedExpression(parent) ||
|
|
54
|
+
ts.isNonNullExpression(parent) ||
|
|
55
|
+
ts.isAsExpression(parent) ||
|
|
56
|
+
ts.isTypeAssertionExpression(parent)
|
|
57
|
+
const wrapper = wrapped && parent.expression === node
|
|
58
|
+
const pattern =
|
|
59
|
+
ts.isArrayLiteralExpression(parent) ||
|
|
60
|
+
ts.isObjectLiteralExpression(parent) ||
|
|
61
|
+
(ts.isPropertyAssignment(parent) && parent.initializer === node) ||
|
|
62
|
+
(ts.isSpreadElement(parent) && parent.expression === node) ||
|
|
63
|
+
(ts.isSpreadAssignment(parent) && parent.expression === node)
|
|
64
|
+
if (!wrapper && !pattern) return false
|
|
65
|
+
}
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Remap actual readonly assignments; ambiguous receivers and projections keep TS2540. */
|
|
70
|
+
function derivedWrite(ts, program, diagnostic) {
|
|
71
|
+
if (diagnostic.code !== 2540 || !diagnostic.file || diagnostic.start === undefined) return null
|
|
72
|
+
let node = ts.getTokenAtPosition(diagnostic.file, diagnostic.start)
|
|
73
|
+
while (node && !ts.isPropertyAccessExpression(node) && !ts.isElementAccessExpression(node))
|
|
74
|
+
node = node.parent
|
|
75
|
+
if (!node || !writeTarget(ts, node)) return null
|
|
76
|
+
const name = ts.isPropertyAccessExpression(node)
|
|
77
|
+
? node.name
|
|
78
|
+
: node.argumentExpression && ts.isStringLiteralLike(node.argumentExpression)
|
|
79
|
+
? node.argumentExpression
|
|
80
|
+
: null
|
|
81
|
+
if (name?.text !== 'value') return null
|
|
82
|
+
const checker = program.getTypeChecker()
|
|
83
|
+
const roots = coreMembers(ts, program, checker).derivedRoots
|
|
84
|
+
if (!derivedType(ts, checker, checker.getTypeAtLocation(node.expression), roots)) return null
|
|
85
|
+
return {
|
|
86
|
+
code: 'W007',
|
|
87
|
+
severity: 'error',
|
|
88
|
+
why: 'This writes a core Derived.value; derived values are readonly computations.',
|
|
89
|
+
help:
|
|
90
|
+
'Use `state(initial)` or `state.linked(() => source)` when independent writes are intended. ' +
|
|
91
|
+
'Otherwise keep `derived(() => expression)` pure and update its source state instead.',
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = { derivedWrite }
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
const { inGenerated, toShadow, toSource } = require('../shadow.cjs')
|
|
2
|
+
const { ROUNDS, quote, union, overlaps } = require('./utils.cjs')
|
|
3
|
+
|
|
4
|
+
function conflicts(host, comps) {
|
|
5
|
+
for (const c of comps) {
|
|
6
|
+
const st = this.state.get(c.id)
|
|
7
|
+
if (!st?.keys) continue
|
|
8
|
+
for (const [key, k] of st.keys) {
|
|
9
|
+
if (k.tally.size < 2 || st.pins.has(key)) continue
|
|
10
|
+
const map = this.maps.get(c.file)
|
|
11
|
+
const uses = k.uses.map((u) => u.span.map((p) => toShadow(map, p)))
|
|
12
|
+
const hurt = () =>
|
|
13
|
+
this.diag(host, c.file).filter((d) =>
|
|
14
|
+
uses.some((u) => overlaps(u, [d.start, d.start + d.length])),
|
|
15
|
+
)
|
|
16
|
+
if (!hurt().length) continue
|
|
17
|
+
const shape = this.shapes(st)
|
|
18
|
+
if (shape) {
|
|
19
|
+
st.shape = shape
|
|
20
|
+
this.state.set(c.id, this.infer(c, this.lastSites.get(c.id), host))
|
|
21
|
+
this.publish(host, new Set([c.file]))
|
|
22
|
+
const m2 = this.maps.get(c.file)
|
|
23
|
+
const u2 = k.uses.map((u) => u.span.map((p) => toShadow(m2, p)))
|
|
24
|
+
if (
|
|
25
|
+
!this.diag(host, c.file).some((d) =>
|
|
26
|
+
u2.some((u) => overlaps(u, [d.start, d.start + d.length])),
|
|
27
|
+
)
|
|
28
|
+
) {
|
|
29
|
+
st.shape = shape
|
|
30
|
+
break
|
|
31
|
+
}
|
|
32
|
+
st.shape = undefined
|
|
33
|
+
this.state.set(c.id, this.infer(c, this.lastSites.get(c.id), host))
|
|
34
|
+
this.publish(host, new Set([c.file]))
|
|
35
|
+
}
|
|
36
|
+
const order = [...k.tally].sort((a, b) => b[1] - a[1]).map(([t]) => t)
|
|
37
|
+
let chosen = null
|
|
38
|
+
for (const t of order) {
|
|
39
|
+
st.pins.set(key, t)
|
|
40
|
+
this.state.set(c.id, this.infer(c, this.lastSites.get(c.id), host))
|
|
41
|
+
this.publish(host, new Set([c.file]))
|
|
42
|
+
const m2 = this.maps.get(c.file)
|
|
43
|
+
const u2 = k.uses.map((u) => u.span.map((p) => toShadow(m2, p)))
|
|
44
|
+
if (
|
|
45
|
+
!this.diag(host, c.file).some((d) =>
|
|
46
|
+
u2.some((u) => overlaps(u, [d.start, d.start + d.length])),
|
|
47
|
+
)
|
|
48
|
+
) {
|
|
49
|
+
chosen = t
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (chosen) {
|
|
54
|
+
st.pins.set(key, chosen)
|
|
55
|
+
const next = this.infer(c, this.lastSites.get(c.id), host)
|
|
56
|
+
this.state.set(c.id, next)
|
|
57
|
+
this.publish(host, new Set([c.file]))
|
|
58
|
+
next.odd = [
|
|
59
|
+
...(next.odd ?? []),
|
|
60
|
+
...k.given
|
|
61
|
+
.filter((v) => !v.any && v.text !== chosen)
|
|
62
|
+
.map((v) => ({
|
|
63
|
+
key,
|
|
64
|
+
type: v.text,
|
|
65
|
+
want: chosen,
|
|
66
|
+
...v.at,
|
|
67
|
+
use: k.uses.find((u) => u.as !== 'read') ?? k.uses[0],
|
|
68
|
+
})),
|
|
69
|
+
]
|
|
70
|
+
} else {
|
|
71
|
+
st.pins.delete(key)
|
|
72
|
+
this.state.set(c.id, this.infer(c, this.lastSites.get(c.id), host))
|
|
73
|
+
this.publish(host, new Set([c.file]))
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function shapes(st) {
|
|
80
|
+
const keys = [...st.keys]
|
|
81
|
+
const tag = keys.find(([, k]) => {
|
|
82
|
+
const lits = k.given.map((v) => v.exact)
|
|
83
|
+
return lits.length && lits.every((l) => l?.startsWith('"')) && new Set(lits).size > 1
|
|
84
|
+
})
|
|
85
|
+
if (!tag) return null
|
|
86
|
+
const groups = new Map()
|
|
87
|
+
for (const v of tag[1].given) {
|
|
88
|
+
if (!groups.has(v.exact)) groups.set(v.exact, new Set())
|
|
89
|
+
groups.get(v.exact).add(`${v.at.file}:${v.at.whole?.[0]}`)
|
|
90
|
+
}
|
|
91
|
+
return [...groups]
|
|
92
|
+
.map(([lit]) => {
|
|
93
|
+
const parts = [`${quote(tag[0])}: ${lit}`]
|
|
94
|
+
for (const [key, k] of keys) {
|
|
95
|
+
if (key === tag[0]) continue
|
|
96
|
+
const near = tag[1].given.filter((v) => v.exact === lit).map((v) => v.at)
|
|
97
|
+
const mine = k.given.filter((v) =>
|
|
98
|
+
near.some((n) => n.tagAt === v.at.tagAt && n.file === v.at.file),
|
|
99
|
+
)
|
|
100
|
+
if (mine.length)
|
|
101
|
+
parts.push(
|
|
102
|
+
`${quote(key)}${mine.length < near.length ? '?' : ''}: ${union(mine.map((v) => v.text))}`,
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
return `{ ${parts.join('; ')} }`
|
|
106
|
+
})
|
|
107
|
+
.join(' | ')
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function unprintable(host, comps) {
|
|
111
|
+
for (const c of comps) {
|
|
112
|
+
const map = this.maps.get(c.file)
|
|
113
|
+
if (!map || !this.state.get(c.id)?.keys) continue
|
|
114
|
+
for (const d of this.diag(host, c.file)) {
|
|
115
|
+
const at = toSource(map, d.start)
|
|
116
|
+
if (!inGenerated(map, d.start, d.length) || at !== c.nameEnd) continue
|
|
117
|
+
const st = this.state.get(c.id)
|
|
118
|
+
st.unnamed = d.text
|
|
119
|
+
st.type = 'any'
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function errors(host, file) {
|
|
125
|
+
const map = this.maps.get(file)
|
|
126
|
+
const said = (this.findings.get(file) ?? []).filter((f) => f.code === 'W084')
|
|
127
|
+
return this.diag(host, file).flatMap((d) => {
|
|
128
|
+
if (map && inGenerated(map, d.start, d.length)) return []
|
|
129
|
+
const start = map ? toSource(map, d.start) : d.start
|
|
130
|
+
const end = map ? toSource(map, d.start + d.length) : d.start + d.length
|
|
131
|
+
if (said.some((f) => overlaps([f.start, f.end], [start, Math.max(end, start + 1)]))) return []
|
|
132
|
+
return [{ code: `TS${d.code}`, start, end, why: d.text, help: 'Fix the type error', ...d.kerf }]
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function affected() {
|
|
137
|
+
const files = new Set(this.maps.keys())
|
|
138
|
+
const derived = this.derivedWritesNeeded()
|
|
139
|
+
if (derived || this.boxPropsNeeded())
|
|
140
|
+
for (const facts of this.facts.values())
|
|
141
|
+
if (derived || facts.sites.length) files.add(facts.file)
|
|
142
|
+
for (const sites of this.lastSites?.values() ?? []) for (const s of sites) files.add(s.file)
|
|
143
|
+
return files
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function run(host, changed) {
|
|
147
|
+
if (this.typedChecksNeeded()) this.diags.clear()
|
|
148
|
+
this.findings = new Map()
|
|
149
|
+
const comps = this.comps()
|
|
150
|
+
if (!this.needed()) {
|
|
151
|
+
this.publish(host)
|
|
152
|
+
return this.findings
|
|
153
|
+
}
|
|
154
|
+
const first = this.publish(host)
|
|
155
|
+
let force = changed ? new Set(changed) : undefined
|
|
156
|
+
if (force) {
|
|
157
|
+
for (const f of changed) for (const d of this.dependents(f)) force.add(d)
|
|
158
|
+
for (const f of first.keys()) force.add(f)
|
|
159
|
+
}
|
|
160
|
+
const touched = new Set(force)
|
|
161
|
+
const sitesOf = (id) => (this.lastSites?.get(id) ?? []).map((s) => s.file)
|
|
162
|
+
let dirty = comps
|
|
163
|
+
for (let round = 0; round < ROUNDS; round++) {
|
|
164
|
+
this.stats.rounds++
|
|
165
|
+
const sites = this.collect(host, force)
|
|
166
|
+
this.lastSites = sites
|
|
167
|
+
if (force)
|
|
168
|
+
dirty = comps.filter(
|
|
169
|
+
(c) =>
|
|
170
|
+
!this.state.has(c.id) ||
|
|
171
|
+
force.has(c.file) ||
|
|
172
|
+
sites.get(c.id).some((s) => force.has(s.file)),
|
|
173
|
+
)
|
|
174
|
+
const moved = []
|
|
175
|
+
for (const c of dirty) {
|
|
176
|
+
const st = this.infer(c, sites.get(c.id), host)
|
|
177
|
+
if (st.type !== this.state.get(c.id)?.type) moved.push(c)
|
|
178
|
+
this.state.set(c.id, st)
|
|
179
|
+
}
|
|
180
|
+
if (!moved.length) break
|
|
181
|
+
const out = this.publish(host)
|
|
182
|
+
force = new Set([...out.keys(), ...moved.flatMap((c) => [c.file, ...sitesOf(c.id)])])
|
|
183
|
+
for (const f of force) touched.add(f)
|
|
184
|
+
}
|
|
185
|
+
this.conflicts(host, dirty)
|
|
186
|
+
this.unprintable(host, dirty)
|
|
187
|
+
this.publish(host)
|
|
188
|
+
for (const c of comps) this.report(c)
|
|
189
|
+
this.touched = touched
|
|
190
|
+
return this.findings
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
module.exports = { conflicts, shapes, unprintable, errors, affected, run }
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
const { toShadow } = require('../shadow.cjs')
|
|
2
|
+
const { MAX_LITERALS, quote, unique, union, sameFile } = require('./utils.cjs')
|
|
3
|
+
|
|
4
|
+
function findFlows(host, force) {
|
|
5
|
+
const out = new Map() // "file:start" → comps
|
|
6
|
+
const comps = this.comps()
|
|
7
|
+
const open = comps.filter(
|
|
8
|
+
(c) =>
|
|
9
|
+
c.exported ||
|
|
10
|
+
this.facts.get(c.file).escaped.has(c.name) ||
|
|
11
|
+
[...this.facts.values()].some((f) => [...f.wraps.values()].some((l) => l.includes(c.name))),
|
|
12
|
+
)
|
|
13
|
+
if (!open.length) return out
|
|
14
|
+
const names = new Set(open.map((c) => c.name))
|
|
15
|
+
for (const [file, facts] of this.facts) {
|
|
16
|
+
const cross = (s) => {
|
|
17
|
+
const head = s.tag.split('.')[0]
|
|
18
|
+
const imported = facts.imports.get(head)
|
|
19
|
+
if (imported)
|
|
20
|
+
return names.has(imported.name) || imported.name === 'default' || names.has(head)
|
|
21
|
+
return !facts.comps.some((c) => c.name === s.tag)
|
|
22
|
+
}
|
|
23
|
+
const held = this.flowCache.get(file)
|
|
24
|
+
if (force && !force.has(file) && held?.facts === facts) {
|
|
25
|
+
for (const [k, v] of held.hits) out.set(k, v)
|
|
26
|
+
continue
|
|
27
|
+
}
|
|
28
|
+
const sites = facts.sites.filter((s) => !this.direct(facts, s).length && cross(s))
|
|
29
|
+
const hits = new Map()
|
|
30
|
+
this.flowCache.set(file, { facts, hits })
|
|
31
|
+
if (!sites.length) continue
|
|
32
|
+
const res = this.ask(
|
|
33
|
+
host,
|
|
34
|
+
file,
|
|
35
|
+
sites.map((s) => ({ kind: 'decls', span: s.tagSpan })),
|
|
36
|
+
)
|
|
37
|
+
sites.forEach((s, i) => {
|
|
38
|
+
const found = []
|
|
39
|
+
for (const d of res[i].decls ?? []) {
|
|
40
|
+
for (const c of open) {
|
|
41
|
+
if (!sameFile(c.file, d.file)) continue
|
|
42
|
+
const map = this.maps.get(c.file)
|
|
43
|
+
if ((map ? toShadow(map, c.bodySpan[1]) : c.bodySpan[1]) === d.end && !found.includes(c))
|
|
44
|
+
found.push(c)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (found.length) {
|
|
48
|
+
hits.set(`${file}:${s.span[0]}`, found)
|
|
49
|
+
out.set(`${file}:${s.span[0]}`, found)
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function collect(host, force) {
|
|
57
|
+
const flows = this.flows(host, force)
|
|
58
|
+
const sites = new Map(this.comps().map((c) => [c.id, []]))
|
|
59
|
+
for (const [file, facts] of this.facts) {
|
|
60
|
+
const targets = facts.sites.map((s) => {
|
|
61
|
+
const d = this.direct(facts, s)
|
|
62
|
+
return d.length ? d : (flows.get(`${file}:${s.span[0]}`) ?? [])
|
|
63
|
+
})
|
|
64
|
+
let held = this.typed.get(file)
|
|
65
|
+
if (!held || held.facts !== facts || !force || force.has(file)) {
|
|
66
|
+
const reqs = [],
|
|
67
|
+
back = []
|
|
68
|
+
facts.sites.forEach((s, i) => {
|
|
69
|
+
if (!targets[i].length) return
|
|
70
|
+
for (const a of s.attrs)
|
|
71
|
+
if (a.span) {
|
|
72
|
+
back.push(a)
|
|
73
|
+
reqs.push({ kind: 'type', span: a.span })
|
|
74
|
+
}
|
|
75
|
+
for (const sp of s.spreads) {
|
|
76
|
+
back.push(sp)
|
|
77
|
+
reqs.push({ kind: 'spread', span: sp.span })
|
|
78
|
+
}
|
|
79
|
+
if (s.children?.span) {
|
|
80
|
+
back.push(s.children)
|
|
81
|
+
reqs.push({ kind: 'type', span: s.children.span })
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
const res = this.ask(host, file, reqs)
|
|
85
|
+
held = { facts, typed: new Map(back.map((a, i) => [a, res[i]])) }
|
|
86
|
+
this.typed.set(file, held)
|
|
87
|
+
}
|
|
88
|
+
const { typed } = held
|
|
89
|
+
facts.sites.forEach((s, i) => {
|
|
90
|
+
for (const c of targets[i]) sites.get(c.id).push({ file, site: s, typed })
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
return sites
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function fromBody(c, key, entry, cache) {
|
|
97
|
+
const q = (kind) => (span) => cache(kind, span)
|
|
98
|
+
const typeOf = q('type'),
|
|
99
|
+
ctx = q('context')
|
|
100
|
+
let member = null
|
|
101
|
+
for (const u of entry.uses) {
|
|
102
|
+
if (u.as === 'number') return 'number'
|
|
103
|
+
if (u.as === 'call')
|
|
104
|
+
return `(${u.args.map((a, i) => `arg${i}: ${typeOf(a).text}`).join(', ')}) => any`
|
|
105
|
+
if (u.as === 'context') {
|
|
106
|
+
const t = ctx(u.span)
|
|
107
|
+
if (t && !t.any && t.text !== 'unknown') return t.text.replace(/ \| undefined$/, '')
|
|
108
|
+
}
|
|
109
|
+
if (u.as === 'write') {
|
|
110
|
+
const t = typeOf(u.value)
|
|
111
|
+
if (!t.any) return t.text
|
|
112
|
+
}
|
|
113
|
+
if (u.as === 'member') member = u.member
|
|
114
|
+
}
|
|
115
|
+
if (member)
|
|
116
|
+
return /^(length|map|filter|forEach|slice|find|some|every|join|reduce)$/.test(member)
|
|
117
|
+
? 'any[]'
|
|
118
|
+
: `{ ${member}: any }`
|
|
119
|
+
if (entry.fallback) {
|
|
120
|
+
const t = typeOf(entry.fallback)
|
|
121
|
+
if (!t.any) return t.text
|
|
122
|
+
}
|
|
123
|
+
return null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function siteValues(engine, c, sites) {
|
|
127
|
+
const seen = new Map() // key → [{ text, exact, any, untypedFn, at }]
|
|
128
|
+
const add = (key, v) => {
|
|
129
|
+
if (!seen.has(key)) seen.set(key, [])
|
|
130
|
+
seen.get(key).push(v)
|
|
131
|
+
}
|
|
132
|
+
for (const { file, site, typed } of sites) {
|
|
133
|
+
const self = site.in === c.name && file === c.file
|
|
134
|
+
for (const a of site.attrs) {
|
|
135
|
+
const r = a.span ? typed.get(a) : null
|
|
136
|
+
const held = a.value && engine.facts.get(file).comps.some((x) => x.name === a.value)
|
|
137
|
+
const v = held
|
|
138
|
+
? { text: `typeof ${a.value}` }
|
|
139
|
+
: a.literal === 'true'
|
|
140
|
+
? { text: 'boolean' }
|
|
141
|
+
: a.literal === 'string'
|
|
142
|
+
? { text: 'string', exact: a.exact }
|
|
143
|
+
: (r ?? { text: 'any', any: true })
|
|
144
|
+
if (self) continue // a component rendering itself says nothing new about its props
|
|
145
|
+
add(a.name, {
|
|
146
|
+
...v,
|
|
147
|
+
untypedFn: a.untypedFn,
|
|
148
|
+
at: {
|
|
149
|
+
file,
|
|
150
|
+
span: a.span ?? a.whole ?? site.tagSpan,
|
|
151
|
+
whole: a.whole ?? site.tagSpan,
|
|
152
|
+
tag: site.tag,
|
|
153
|
+
tagAt: site.tagSpan[0],
|
|
154
|
+
},
|
|
155
|
+
self,
|
|
156
|
+
literal: a.literal,
|
|
157
|
+
})
|
|
158
|
+
}
|
|
159
|
+
for (const sp of site.spreads) {
|
|
160
|
+
const r = typed.get(sp)
|
|
161
|
+
if (!self)
|
|
162
|
+
for (const p of r?.props ?? [])
|
|
163
|
+
add(p.name, {
|
|
164
|
+
text: p.text,
|
|
165
|
+
any: p.any,
|
|
166
|
+
at: { file, span: sp.span, tag: site.tag },
|
|
167
|
+
self,
|
|
168
|
+
optional: p.optional,
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
if (site.children && !self) {
|
|
172
|
+
const r = site.children.span ? typed.get(site.children) : null
|
|
173
|
+
add('children', {
|
|
174
|
+
...(r ?? { text: 'unknown' }),
|
|
175
|
+
untypedFn: site.children.untypedFn,
|
|
176
|
+
at: { file, span: site.tagSpan, tag: site.tag },
|
|
177
|
+
self,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return seen
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function inferKeys({ engine, c, fwd, outer, st, seen, cache }) {
|
|
186
|
+
const named = new Set(c.info.keys.keys())
|
|
187
|
+
const all = fwd.length ? named : new Set([...seen.keys(), ...named])
|
|
188
|
+
const parts = []
|
|
189
|
+
for (const key of all) {
|
|
190
|
+
const entry = c.info.keys.get(key) ?? { uses: [] }
|
|
191
|
+
const given = seen.get(key) ?? []
|
|
192
|
+
const real = given.filter((v) => !v.any)
|
|
193
|
+
const body =
|
|
194
|
+
given.every((v) => v.any || v.untypedFn) || !given.length
|
|
195
|
+
? engine.fromBody(c, key, entry, cache)
|
|
196
|
+
: null
|
|
197
|
+
let text
|
|
198
|
+
const lits = real.filter((v) => v.exact?.startsWith('"'))
|
|
199
|
+
if (
|
|
200
|
+
real.length &&
|
|
201
|
+
lits.length === real.length &&
|
|
202
|
+
unique(lits.map((v) => v.exact)).length > 1 &&
|
|
203
|
+
unique(lits.map((v) => v.exact)).length <= MAX_LITERALS
|
|
204
|
+
)
|
|
205
|
+
text = union(lits.map((v) => v.exact))
|
|
206
|
+
else text = body ?? union(real.map((v) => v.text))
|
|
207
|
+
const distinct = unique(real.map((v) => v.text))
|
|
208
|
+
const tally = new Map(distinct.map((t) => [t, real.filter((v) => v.text === t).length]))
|
|
209
|
+
const number =
|
|
210
|
+
entry.uses.some((u) => u.as === 'number') && !tally.has('number') && distinct.length
|
|
211
|
+
? 'number'
|
|
212
|
+
: null
|
|
213
|
+
if (number) tally.set(number, 0)
|
|
214
|
+
if (st.pins.has(key)) text = st.pins.get(key)
|
|
215
|
+
else if (number) text = union([text, number])
|
|
216
|
+
if (given.some((v) => v.any) && !real.length)
|
|
217
|
+
st.anyFrom.push(...given.filter((v) => v.any).map((v) => ({ key, ...v.at, body: !!body })))
|
|
218
|
+
if (!text) {
|
|
219
|
+
if (given.length) text = 'any'
|
|
220
|
+
else if (entry.fallback) text = 'any'
|
|
221
|
+
else if (outer.length) text = 'undefined'
|
|
222
|
+
else text = 'any'
|
|
223
|
+
}
|
|
224
|
+
const passed = outer.filter(
|
|
225
|
+
(s) =>
|
|
226
|
+
s.site.attrs.some((a) => a.name === key) ||
|
|
227
|
+
(key === 'children' && s.site.children) ||
|
|
228
|
+
s.site.spreads.some((sp) =>
|
|
229
|
+
s.typed.get(sp)?.props?.some((p) => p.name === key && !p.optional),
|
|
230
|
+
),
|
|
231
|
+
)
|
|
232
|
+
const opaque = outer.some((s) => s.site.spreads.some((sp) => s.typed.get(sp)?.any))
|
|
233
|
+
const required =
|
|
234
|
+
outer.length &&
|
|
235
|
+
passed.length === outer.length &&
|
|
236
|
+
!entry.fallback &&
|
|
237
|
+
!opaque &&
|
|
238
|
+
text !== 'undefined'
|
|
239
|
+
const onlySelf = !outer.length
|
|
240
|
+
const optional = onlySelf ? !!entry.fallback : !required
|
|
241
|
+
st.keys.set(key, { text, optional, given, tally, uses: entry.uses })
|
|
242
|
+
parts.push(`${quote(key)}${optional ? '?' : ''}: ${text}`)
|
|
243
|
+
}
|
|
244
|
+
return { named, parts }
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function infer(c, sites, host) {
|
|
248
|
+
const facts = this.facts.get(c.file)
|
|
249
|
+
const prev = this.state.get(c.id) ?? {}
|
|
250
|
+
const st = {
|
|
251
|
+
keys: new Map(),
|
|
252
|
+
sites: sites.length,
|
|
253
|
+
pins: prev.pins ?? new Map(),
|
|
254
|
+
shape: prev.shape,
|
|
255
|
+
anyFrom: [],
|
|
256
|
+
noCallers: false,
|
|
257
|
+
}
|
|
258
|
+
if (this.routes(c.file) && c.isDefault) {
|
|
259
|
+
st.type = `import('@kerf-js/start').RouteProps${facts.exportsLoad ? '<typeof load>' : ''}`
|
|
260
|
+
st.route = true
|
|
261
|
+
return st
|
|
262
|
+
}
|
|
263
|
+
const outer = sites.filter((s) => !(s.site.in === c.name && s.file === c.file))
|
|
264
|
+
if (!outer.length) st.noCallers = true
|
|
265
|
+
|
|
266
|
+
const memo = new Map()
|
|
267
|
+
const cache = (kind, span) => {
|
|
268
|
+
const k = `${kind}:${span}`
|
|
269
|
+
if (!memo.has(k)) memo.set(k, this.ask(host, c.file, [{ kind, span }])[0])
|
|
270
|
+
return memo.get(k)
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const seen = siteValues(this, c, sites)
|
|
274
|
+
|
|
275
|
+
const fwd = c.forwards
|
|
276
|
+
const { named, parts } = inferKeys({ engine: this, c, fwd, outer, st, seen, cache })
|
|
277
|
+
const own = parts.length ? `{ ${parts.join('; ')} }` : '{}'
|
|
278
|
+
if (fwd.length) {
|
|
279
|
+
const f = fwd[0]
|
|
280
|
+
const base = `Parameters<typeof ${f.tag}>[0]`
|
|
281
|
+
const names = unique([...named, ...f.given])
|
|
282
|
+
const taken = names.map((n) => JSON.stringify(n)).join(' | ')
|
|
283
|
+
const left = names.length ? `Omit<${base}, ${taken}>` : base
|
|
284
|
+
const loose = f.given.filter((n) => !named.has(n))
|
|
285
|
+
const given = loose.length
|
|
286
|
+
? ` & Partial<Pick<${base}, Extract<keyof ${base}, ${loose.map((n) => JSON.stringify(n)).join(' | ')}>>>`
|
|
287
|
+
: ''
|
|
288
|
+
st.type = parts.length ? `${left}${given} & ${own}` : `${left}${given}`
|
|
289
|
+
} else st.type = c.info.opaque ? 'any' : (st.shape ?? own)
|
|
290
|
+
return st
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
module.exports = { flows: findFlows, collect, fromBody, infer }
|