@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.
Files changed (176) hide show
  1. package/LICENSE +16 -0
  2. package/README.md +554 -2
  3. package/bin/kerf.mjs +15 -0
  4. package/docs/async.md +50 -0
  5. package/docs/browser.md +53 -0
  6. package/docs/config.md +127 -0
  7. package/docs/core.md +30 -0
  8. package/docs/deploy.md +108 -0
  9. package/docs/design.md +106 -0
  10. package/docs/entrypoints.md +83 -0
  11. package/docs/fonts.md +118 -0
  12. package/docs/icons.md +108 -0
  13. package/docs/lists.md +25 -0
  14. package/docs/meta.md +92 -0
  15. package/docs/plugins.md +166 -0
  16. package/docs/props.md +88 -0
  17. package/docs/reactivity.md +26 -0
  18. package/docs/rules.md +39 -0
  19. package/docs/rust.md +253 -0
  20. package/docs/start-short.md +10 -0
  21. package/docs/start.md +642 -0
  22. package/docs/state.md +122 -0
  23. package/docs/style.md +82 -0
  24. package/docs/surface.md +158 -0
  25. package/docs/testing.md +81 -0
  26. package/docs/theme.md +263 -0
  27. package/docs/ui-state.md +105 -0
  28. package/docs/verify.md +61 -0
  29. package/package.json +47 -3
  30. package/src/add/config.mjs +93 -0
  31. package/src/add/errors.mjs +2 -0
  32. package/src/add/setup.mjs +88 -0
  33. package/src/add-rust.mjs +178 -0
  34. package/src/add.mjs +392 -0
  35. package/src/agents-md.mjs +240 -0
  36. package/src/alias.mjs +70 -0
  37. package/src/audit/page.mjs +313 -0
  38. package/src/audit/report.mjs +69 -0
  39. package/src/audit/utils.mjs +5 -0
  40. package/src/audit.mjs +319 -0
  41. package/src/browser/cli.mjs +53 -0
  42. package/src/browser/client.mjs +299 -0
  43. package/src/browser/commands.mjs +258 -0
  44. package/src/capture.mjs +21 -0
  45. package/src/catalog/audit.mjs +129 -0
  46. package/src/catalog/auth.mjs +40 -0
  47. package/src/catalog/compiler.mjs +337 -0
  48. package/src/catalog/contracts.mjs +18 -0
  49. package/src/catalog/database.mjs +52 -0
  50. package/src/catalog/deploy.mjs +117 -0
  51. package/src/catalog/design.mjs +159 -0
  52. package/src/catalog/forms.mjs +25 -0
  53. package/src/catalog/hydration.mjs +29 -0
  54. package/src/catalog/icons.mjs +24 -0
  55. package/src/catalog/media.mjs +163 -0
  56. package/src/catalog/metadata.mjs +116 -0
  57. package/src/catalog/plugins.mjs +228 -0
  58. package/src/catalog/realtime.mjs +195 -0
  59. package/src/catalog/rust.mjs +75 -0
  60. package/src/catalog/server-functions.mjs +100 -0
  61. package/src/catalog/state.mjs +39 -0
  62. package/src/catalog/style-groups.mjs +54 -0
  63. package/src/catalog/tests.mjs +127 -0
  64. package/src/catalog/text.mjs +200 -0
  65. package/src/catalog/ui-state.mjs +42 -0
  66. package/src/catalog.mjs +103 -0
  67. package/src/check/runtime.mjs +280 -0
  68. package/src/check.mjs +321 -0
  69. package/src/cli/authoring.mjs +213 -0
  70. package/src/cli/project.mjs +302 -0
  71. package/src/cli.mjs +340 -0
  72. package/src/config.d.ts +1 -0
  73. package/src/config.mjs +5 -0
  74. package/src/contributions.mjs +119 -0
  75. package/src/db-layer.mjs +41 -0
  76. package/src/deploy/cdn.mjs +142 -0
  77. package/src/deploy/files.mjs +266 -0
  78. package/src/deploy/fit.mjs +166 -0
  79. package/src/deploy/hosts.mjs +126 -0
  80. package/src/deploy/index/cli.mjs +20 -0
  81. package/src/deploy/index/environment.mjs +69 -0
  82. package/src/deploy/index/steps.mjs +87 -0
  83. package/src/deploy/index.mjs +387 -0
  84. package/src/deploy/needs.mjs +149 -0
  85. package/src/design-checks/accessible-names.mjs +62 -0
  86. package/src/design-checks/contrast.mjs +43 -0
  87. package/src/design-checks/csp.mjs +60 -0
  88. package/src/design-checks/files.mjs +59 -0
  89. package/src/design-checks/heading-links.mjs +61 -0
  90. package/src/design-checks/interaction.mjs +65 -0
  91. package/src/design-checks/jsx.mjs +67 -0
  92. package/src/design-checks/layout-values.mjs +68 -0
  93. package/src/design-checks/native-controls.mjs +48 -0
  94. package/src/design-checks/outline.mjs +39 -0
  95. package/src/design-checks/reporter.mjs +37 -0
  96. package/src/design-checks/rules.mjs +274 -0
  97. package/src/design-checks/values.mjs +262 -0
  98. package/src/design-checks.mjs +49 -0
  99. package/src/diag.mjs +98 -0
  100. package/src/docs.mjs +228 -0
  101. package/src/doctor.mjs +42 -0
  102. package/src/files.mjs +60 -0
  103. package/src/fix.mjs +48 -0
  104. package/src/gate/after.mjs +40 -0
  105. package/src/gate/before.mjs +54 -0
  106. package/src/gate/classify.mjs +72 -0
  107. package/src/gate/mutation.mjs +209 -0
  108. package/src/gate/resolve-cases.mjs +22 -0
  109. package/src/gate/result.mjs +63 -0
  110. package/src/gate/run.mjs +75 -0
  111. package/src/gate.mjs +178 -0
  112. package/src/index.mjs +9 -0
  113. package/src/infer/box-props.cjs +140 -0
  114. package/src/infer/core-boxes.cjs +46 -0
  115. package/src/infer/derived-writes.cjs +95 -0
  116. package/src/infer/engine/checks.cjs +193 -0
  117. package/src/infer/engine/flow.cjs +293 -0
  118. package/src/infer/engine/report.cjs +158 -0
  119. package/src/infer/engine/utils.cjs +15 -0
  120. package/src/infer/engine.cjs +265 -0
  121. package/src/infer/facts/sites.cjs +114 -0
  122. package/src/infer/facts.cjs +345 -0
  123. package/src/infer/hosts.cjs +161 -0
  124. package/src/infer/lsp.mjs +240 -0
  125. package/src/infer/session.cjs +161 -0
  126. package/src/infer/shadow.cjs +54 -0
  127. package/src/infer/worker.cjs +145 -0
  128. package/src/jsx-scan.mjs +128 -0
  129. package/src/kerf.mjs +98 -0
  130. package/src/load-config/schema.mjs +300 -0
  131. package/src/load-config.mjs +302 -0
  132. package/src/map.mjs +177 -0
  133. package/src/mcp/browser-tools.mjs +155 -0
  134. package/src/mcp.mjs +273 -0
  135. package/src/meta-table.mjs +63 -0
  136. package/src/mutate.mjs +224 -0
  137. package/src/plugin-checks.mjs +118 -0
  138. package/src/plugins.mjs +258 -0
  139. package/src/project.mjs +310 -0
  140. package/src/props-checks.mjs +64 -0
  141. package/src/props-listener.mjs +56 -0
  142. package/src/react-checks.mjs +67 -0
  143. package/src/server-state-checks.mjs +34 -0
  144. package/src/share-checks.mjs +158 -0
  145. package/src/site.mjs +46 -0
  146. package/src/slots.mjs +181 -0
  147. package/src/start/build.mjs +116 -0
  148. package/src/start/routes.mjs +38 -0
  149. package/src/start.mjs +394 -0
  150. package/src/state-checks/core-state.mjs +223 -0
  151. package/src/state-checks/modules.mjs +17 -0
  152. package/src/state-checks/scan.mjs +232 -0
  153. package/src/state-checks/server-graph.mjs +65 -0
  154. package/src/state-checks.mjs +189 -0
  155. package/src/style/find.mjs +121 -0
  156. package/src/style.mjs +327 -0
  157. package/src/surface-checks.mjs +384 -0
  158. package/src/tailwind.mjs +23 -0
  159. package/src/tasks.mjs +261 -0
  160. package/src/test-rules/context.mjs +109 -0
  161. package/src/test-rules/file-rules.mjs +279 -0
  162. package/src/test-rules/rule-helpers.mjs +244 -0
  163. package/src/test-rules/syntax.mjs +223 -0
  164. package/src/test-rules/test-rules.mjs +127 -0
  165. package/src/testdiff.mjs +219 -0
  166. package/src/tests.mjs +120 -0
  167. package/src/text-checks.mjs +332 -0
  168. package/src/tokens.mjs +33 -0
  169. package/src/trust/scan.mjs +95 -0
  170. package/src/trust.mjs +394 -0
  171. package/src/workspace.mjs +182 -0
  172. package/ts-plugin/context.js +57 -0
  173. package/ts-plugin/index.js +17 -0
  174. package/ts-plugin/mapping.js +65 -0
  175. package/ts-plugin/package.json +1 -0
  176. package/ts-plugin/proxy.js +144 -0
@@ -0,0 +1,158 @@
1
+ const { MARK } = require('../facts.cjs')
2
+
3
+ function note(file, finding) {
4
+ if (!this.findings.has(file)) this.findings.set(file, [])
5
+ this.findings.get(file).push(finding)
6
+ }
7
+
8
+ function annotation(c, type) {
9
+ const text = `: ${MARK} ${type}`
10
+ if (c.written) return [{ start: c.written[0], end: c.written[1], text }]
11
+ if (c.parens) return [{ start: c.nameEnd, end: c.nameEnd, text }]
12
+ return [
13
+ { start: c.paramSpan[0], end: c.paramSpan[0], text: '(' },
14
+ { start: c.nameEnd, end: c.nameEnd, text: `${text})` },
15
+ ]
16
+ }
17
+
18
+ function oddFindings(c, st, line) {
19
+ for (const o of st.odd ?? []) {
20
+ const use = o.use?.span ? this.texts.get(c.file).slice(...o.use.span) : o.key
21
+ this.note(o.file, {
22
+ code: 'W084',
23
+ start: o.whole[0],
24
+ end: o.whole[1],
25
+ why: `\`${o.key}\` here is ${o.type}, but \`${c.name}\` uses it as ${o.want} (\`${use}\`, line ${line(c.file, o.use?.span?.[0] ?? c.nameSpan[0])}), and its other callers agree`,
26
+ help: `Pass ${o.want} here, or change \`${c.name}\` to handle ${o.type} (a type parameter, if the two props must agree)`,
27
+ })
28
+ }
29
+ }
30
+
31
+ function report(c) {
32
+ const st = this.state.get(c.id)
33
+ if (!st || st.route) return
34
+ const [start, end] = c.nameSpan
35
+ const line = (file, pos) => this.texts.get(file).slice(0, pos).split('\n').length
36
+ if (st.unnamed)
37
+ this.note(c.file, {
38
+ code: 'W085',
39
+ severity: 'warning',
40
+ start,
41
+ end,
42
+ why: `\`${c.name}\`'s inferred props name a type this scope can't see, so they fell back to \`any\`: ${st.unnamed}`,
43
+ help: 'Move the type to module scope or export it, or annotate the parameter',
44
+ })
45
+ const exposed = c.exported
46
+ if (st.noCallers && !exposed) {
47
+ const escaped = this.facts.get(c.file).escaped.has(c.name)
48
+ this.note(
49
+ c.file,
50
+ escaped
51
+ ? {
52
+ code: 'W083',
53
+ severity: 'warning',
54
+ start,
55
+ end,
56
+ why: `\`${c.name}\` is used as a value and no JSX site for it could be traced, so its props are \`any\``,
57
+ help: `Annotate its parameter: function ${c.name}(props: { ... })`,
58
+ }
59
+ : {
60
+ code: 'W080',
61
+ start,
62
+ end,
63
+ why: `\`${c.name}\` has an untyped parameter and nothing renders it, so its props have nothing to be inferred from`,
64
+ help: `Render it (\`<${c.name} ... />\`), delete it, or annotate the parameter: function ${c.name}(props: { ... })`,
65
+ },
66
+ )
67
+ }
68
+ if (exposed) {
69
+ const type = st.type
70
+ const edits = this.annotation(c, type)
71
+ const bad = st.noCallers
72
+ if (!c.written)
73
+ this.note(c.file, {
74
+ code: 'W081',
75
+ start,
76
+ end,
77
+ why: `\`${c.name}\` is exported with no prop type${bad ? ' and no caller to infer one from' : ''}. An export's type is written down, so a caller's edit can't change a public API unseen`,
78
+ help: `\`kerf fix\` writes the inferred type: ${c.name}(props: ${type})`,
79
+ edits: bad ? undefined : edits,
80
+ safe: !bad,
81
+ })
82
+ else if (c.writtenType.replace(MARK, '').trim() !== type && !bad)
83
+ this.note(c.file, {
84
+ code: 'W082',
85
+ start,
86
+ end,
87
+ why: `\`${c.name}\`'s written prop type no longer matches its callers: it says ${c.writtenType.replace(MARK, '').trim()}, they need ${type}`,
88
+ help: '`kerf fix` rewrites it (a diff to review), or delete the /*inferred*/ comment to keep the type you have',
89
+ edits,
90
+ safe: true,
91
+ })
92
+ }
93
+ for (const a of st.anyFrom) {
94
+ const origin = this.origin(a.file, a.span)
95
+ this.note(c.file, {
96
+ code: 'W083',
97
+ severity: 'warning',
98
+ start,
99
+ end,
100
+ why: `\`${c.name}\`'s prop \`${a.key}\` is ${a.body ? 'typed from its use in the body, because' : '`any`, because'} the value at ${a.tag} (line ${line(a.file, a.span[0])}) is \`any\`${origin ? `: it comes from ${origin}` : ''}`,
101
+ help: 'Give that source a type, or annotate the prop',
102
+ })
103
+ }
104
+ oddFindings.call(this, c, st, line)
105
+ }
106
+
107
+ function originOf(file, span) {
108
+ const ts = this.ts
109
+ const text = this.texts.get(file)
110
+ const sf = ts.createSourceFile(file, text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX)
111
+ const lineOf = (n) => sf.getLineAndCharacterOfPosition(n.getStart(sf)).line + 1
112
+ let node = null
113
+ const find = (n) => {
114
+ if (n.getStart(sf) <= span[0] && n.end >= span[1]) {
115
+ node = n
116
+ ts.forEachChild(n, find)
117
+ }
118
+ }
119
+ ts.forEachChild(sf, find)
120
+ for (let i = 0; node && i < 6; i++) {
121
+ if (ts.isPropertyAccessExpression(node)) node = node.expression
122
+ if (
123
+ ts.isAsExpression(node) ||
124
+ ts.isParenthesizedExpression(node) ||
125
+ ts.isNonNullExpression(node)
126
+ ) {
127
+ node = node.expression
128
+ continue
129
+ }
130
+ if (ts.isCallExpression(node))
131
+ return `\`${node.getText(sf).slice(0, 48)}\` (line ${lineOf(node)})`
132
+ if (!ts.isIdentifier(node)) return `\`${node.getText(sf).slice(0, 48)}\` (line ${lineOf(node)})`
133
+ let decl = null
134
+ const look = (n) => {
135
+ if (
136
+ (ts.isVariableDeclaration(n) ||
137
+ ts.isParameter(n) ||
138
+ ts.isImportSpecifier(n) ||
139
+ ts.isImportClause(n)) &&
140
+ n.name?.getText(sf) === node.text &&
141
+ n.pos < node.pos
142
+ )
143
+ decl = n
144
+ ts.forEachChild(n, look)
145
+ }
146
+ ts.forEachChild(sf, look)
147
+ if (!decl) return `\`${node.text}\``
148
+ if (ts.isParameter(decl))
149
+ return `the parameter \`${node.text}\`, which has no type (line ${lineOf(decl)})`
150
+ if (ts.isImportSpecifier(decl) || ts.isImportClause(decl))
151
+ return `\`${node.text}\`, imported at line ${lineOf(decl)}`
152
+ if (!decl.initializer) return `\`${node.text}\` (line ${lineOf(decl)})`
153
+ node = decl.initializer
154
+ }
155
+ return null
156
+ }
157
+
158
+ module.exports = { note, annotation, report, origin: originOf }
@@ -0,0 +1,15 @@
1
+ const ROUNDS = 5
2
+ const MAX_LITERALS = 8
3
+
4
+ const quote = (key) => (/^[A-Za-z_$][\w$]*$/.test(key) ? key : JSON.stringify(key))
5
+ const unique = (list) => [...new Set(list)]
6
+ const union = (list) => {
7
+ const parts = unique(list.filter(Boolean))
8
+ return parts.length
9
+ ? parts.map((t) => (/=>/.test(t) && !/^\(.*\)$/.test(t) ? `(${t})` : t)).join(' | ')
10
+ : null
11
+ }
12
+ const sameFile = (a, b) => a === b || a.toLowerCase() === b.toLowerCase()
13
+ const overlaps = (a, b) => a[0] < b[1] && b[0] < a[1]
14
+
15
+ module.exports = { ROUNDS, MAX_LITERALS, quote, unique, union, sameFile, overlaps }
@@ -0,0 +1,265 @@
1
+ // Prop inference (research 44). Facts come from syntax; the types come from whichever checker the host wraps, asked
2
+ // about the call sites in the program's own terms. A round writes each component's annotation into its file's shadow;
3
+ // the next round sees the types that made possible (a prop forwarded to another inferred component, a component passed
4
+ // as a prop). Rounds stop when nothing changes. A checker starts for inference or core box diagnostics.
5
+
6
+ const { fileFacts, hashOf } = require('./facts.cjs')
7
+ const CORE_IMPORT = /['"]@kerf-js\/core['"]/
8
+ const { shadow, toShadow } = require('./shadow.cjs')
9
+
10
+ const { flows, collect, fromBody, infer } = require('./engine/flow.cjs')
11
+ const { conflicts, shapes, unprintable, errors, affected, run } = require('./engine/checks.cjs')
12
+ const { note: noteFinding, annotation, report, origin } = require('./engine/report.cjs')
13
+
14
+ class Engine {
15
+ /** @param {{ ts: any, read: (file: string) => string | undefined, routes?: (file: string) => boolean }} options */
16
+ constructor({ ts, read, routes }) {
17
+ this.ts = ts
18
+ this.read = read
19
+ this.routes = routes ?? ((f) => /\/src\/routes\//.test(f))
20
+ this.facts = new Map()
21
+ this.texts = new Map()
22
+ this.maps = new Map() // file → shadow, for files that differ from their source
23
+ this.state = new Map() // comp id → { type, keys, sites, ... }
24
+ this.findings = new Map() // file → engine diagnostics
25
+ this.typed = new Map() // file → what the checker said about its sites, until the file or a shadow it reads moves
26
+ this.flowCache = new Map()
27
+ this.diags = new Map() // file → the checker's errors for one shadow text
28
+ this.touched = new Set() // files whose errors the last run may have moved
29
+ this.stats = { rounds: 0, queries: 0, parses: 0 }
30
+ }
31
+
32
+ /** New text for a file. @returns whether anything about it changed */
33
+ set(file, text = this.read(file)) {
34
+ if (text === undefined) {
35
+ const had = this.facts.delete(file)
36
+ this.texts.delete(file)
37
+ this.maps.delete(file)
38
+ this.typed.delete(file)
39
+ this.flowCache.delete(file)
40
+ return had
41
+ }
42
+ const before = this.facts.get(file)
43
+ this.texts.set(file, text)
44
+ const hash = hashOf(text)
45
+ if (before?.hash === hash) return false
46
+ this.stats.parses++
47
+ this.facts.set(file, fileFacts(this.ts, file, text))
48
+ return true
49
+ }
50
+
51
+ comps() {
52
+ return [...this.facts.values()].flatMap((f) => f.comps)
53
+ }
54
+ /** Facts that need a checker: inferred props, bindings or possible core box errors. */
55
+ needed() {
56
+ return (
57
+ this.typedChecksNeeded() ||
58
+ [...this.facts.values()].some(
59
+ (f) => f.comps.length || f.sites.some((s) => s.attrs.some((a) => a.bind)),
60
+ )
61
+ )
62
+ }
63
+
64
+ /** Syntax only gates startup; the checker proves every W008 finding. */
65
+ boxPropsNeeded() {
66
+ const sites = [...this.facts.values()].some((facts) =>
67
+ facts.sites.some((site) => site.attrs.length),
68
+ )
69
+ return sites && [...this.texts.values()].some((text) => CORE_IMPORT.test(text))
70
+ }
71
+
72
+ /** Syntax only gates startup; TS2540 and resolved core symbols prove readonly writes. */
73
+ derivedWritesNeeded() {
74
+ return (
75
+ [...this.texts.values()].some((text) => CORE_IMPORT.test(text)) &&
76
+ [...this.texts.values()].some((text) => /\bvalue\b/.test(text))
77
+ )
78
+ }
79
+
80
+ /** Whether either core box diagnostic family needs the shared checker. */
81
+ typedChecksNeeded() {
82
+ return this.boxPropsNeeded() || this.derivedWritesNeeded()
83
+ }
84
+
85
+ /** Files with a component or a site of one that import `file`: what they pass may have changed type. */
86
+ dependents(file) {
87
+ const base = file.replace(/^.*[\\/]/, '').replace(/\.[^.]*$/, '')
88
+ const names = new Set(this.comps().map((c) => c.name))
89
+ const out = new Set()
90
+ for (const f of this.facts.values()) {
91
+ if (!f.comps.length && !f.sites.some((s) => names.has(s.tag))) continue
92
+ for (const { from } of f.imports.values())
93
+ if (from === base || from.endsWith(`/${base}`)) {
94
+ out.add(f.file)
95
+ break
96
+ }
97
+ }
98
+ return out
99
+ }
100
+
101
+ /** Whether a saved file can move anything: it holds a component or a site of one, or such a file imports it. */
102
+ relevant(file) {
103
+ if (!this.needed()) return false
104
+ if (this.typedChecksNeeded()) return true
105
+ const facts = this.facts.get(file)
106
+ if (!facts || facts.comps.length || facts.sites.some((s) => s.attrs.some((a) => a.bind)))
107
+ return true
108
+ const names = new Set(this.comps().map((c) => c.name))
109
+ return (
110
+ facts.sites.some((s) => names.has(s.tag) || (this.flowing?.has(file) ?? false)) ||
111
+ this.dependents(file).size > 0
112
+ )
113
+ }
114
+
115
+ /** `comp` for a tag that needs no checker: a name in the same file, or a local wrap of one. */
116
+ direct(facts, site) {
117
+ const names = new Set(),
118
+ todo = [site.tag.split('.')[0]]
119
+ while (todo.length) {
120
+ const n = todo.pop()
121
+ if (names.has(n)) continue
122
+ names.add(n)
123
+ todo.push(...(facts.wraps.get(n) ?? []))
124
+ }
125
+ return facts.comps.filter((c) => names.has(c.name))
126
+ }
127
+
128
+ shadowOf(file) {
129
+ return this.maps.get(file)
130
+ }
131
+
132
+ edits(facts) {
133
+ const edits = []
134
+ for (const site of facts.sites)
135
+ for (const a of site.attrs) {
136
+ if (!a.bind) continue
137
+ if (a.raw)
138
+ edits.push(
139
+ { start: a.raw[0], end: a.raw[0] + 5, text: '' },
140
+ { start: a.span[1], end: a.span[1], text: '.value' },
141
+ )
142
+ }
143
+ for (const c of facts.comps) {
144
+ const type = this.state.get(c.id)?.type ?? 'any'
145
+ const note = `: ${type}`
146
+ if (c.written) edits.push({ start: c.written[0], end: c.written[1], text: note })
147
+ else if (c.parens) edits.push({ start: c.nameEnd, end: c.nameEnd, text: note })
148
+ else
149
+ edits.push(
150
+ { start: c.paramSpan[0], end: c.paramSpan[0], text: '(' },
151
+ { start: c.nameEnd, end: c.nameEnd, text: `${note})` },
152
+ )
153
+ }
154
+ return edits
155
+ }
156
+
157
+ /** Rewrite the files whose shadow moved, and tell the host. */
158
+ publish(host, only) {
159
+ const changed = new Map()
160
+ for (const [file, facts] of this.facts) {
161
+ if (only && !only.has(file)) continue
162
+ if (!facts.comps.length && !facts.sites.some((s) => s.attrs.some((a) => a.bind))) {
163
+ if (this.maps.delete(file)) changed.set(file, this.texts.get(file))
164
+ continue
165
+ }
166
+ const map = shadow(this.texts.get(file), this.edits(facts))
167
+ if (this.maps.get(file)?.text !== map.text) {
168
+ this.maps.set(file, map)
169
+ changed.set(file, map.text)
170
+ }
171
+ }
172
+ if (changed.size) host.update(changed)
173
+ return changed
174
+ }
175
+
176
+ /** The checker's errors, once per shadow text: conflicts, naming and reporting all want them. */
177
+ diag(host, file) {
178
+ const text = this.maps.get(file)?.text ?? this.texts.get(file)
179
+ const hit = this.diags.get(file)
180
+ if (hit?.text === text) return hit.list
181
+ const list = host.diagnostics(file)
182
+ this.diags.set(file, { text, list })
183
+ return list
184
+ }
185
+
186
+ ask(host, file, reqs) {
187
+ if (!reqs.length) return []
188
+ this.stats.queries += reqs.length
189
+ const map = this.maps.get(file)
190
+ const at = (p) => (map ? toShadow(map, p) : p)
191
+ return host.query(
192
+ file,
193
+ reqs.map((r) => ({ kind: r.kind, start: at(r.span[0]), end: at(r.span[1]) })),
194
+ )
195
+ }
196
+
197
+ /** Who a tag renders when its name doesn't say: a component held in a variable, a map, a prop, an import. */
198
+ flows(host, force) {
199
+ return flows.call(this, host, force)
200
+ }
201
+
202
+ /** Every call site of every component, with what the checker says each attribute is. */
203
+ collect(host, force) {
204
+ return collect.call(this, host, force)
205
+ }
206
+
207
+ /** What the body alone says about `key`: what's left when no caller says, or a caller says `any`. */
208
+ fromBody(c, key, entry, cache) {
209
+ return fromBody.call(this, c, key, entry, cache)
210
+ }
211
+
212
+ infer(c, sites, host) {
213
+ return infer.call(this, c, sites, host)
214
+ }
215
+
216
+ /** The body can't take the union: try each caller's type alone, and leave the rest as errors at those callers. */
217
+ conflicts(host, comps) {
218
+ return conflicts.call(this, host, comps)
219
+ }
220
+
221
+ /** Callers that differ by a string prop: one object type per value, so the body can narrow on it. */
222
+ shapes(st) {
223
+ return shapes.call(this, st)
224
+ }
225
+
226
+ /** A type the checker printed in another scope may not be nameable here: then this component is `any`, and says so. */
227
+ unprintable(host, comps) {
228
+ return unprintable.call(this, host, comps)
229
+ }
230
+
231
+ /** The checker's errors in a file, in source positions, without what the engine already said better. */
232
+ errors(host, file) {
233
+ return errors.call(this, host, file)
234
+ }
235
+
236
+ /** Files whose types depend on inference: where a component is, and where it's rendered. */
237
+ affected() {
238
+ return affected.call(this)
239
+ }
240
+
241
+ /** @returns {Map<string, {code: string, severity?: string, start: number, end: number, why: string, help?: string, edit?: object}[]>} */
242
+ run(host, changed) {
243
+ return run.call(this, host, changed)
244
+ }
245
+
246
+ note(file, finding) {
247
+ return noteFinding.call(this, file, finding)
248
+ }
249
+
250
+ /** The source edits that write a type down. */
251
+ annotation(c, type) {
252
+ return annotation.call(this, c, type)
253
+ }
254
+
255
+ report(c) {
256
+ return report.call(this, c)
257
+ }
258
+
259
+ /** Where an `any` started, by following names in the file: `JSON.parse(raw)`, an untyped parameter, an import. */
260
+ origin(file, span) {
261
+ return origin.call(this, file, span)
262
+ }
263
+ }
264
+
265
+ module.exports = { Engine }
@@ -0,0 +1,114 @@
1
+ const isCap = (name) => /^[A-Z]/.test(name)
2
+
3
+ function jsxSite(ts, sf, node, enclosing) {
4
+ const tag = node.tagName
5
+ if (!ts.isIdentifier(tag) && !ts.isPropertyAccessExpression(tag)) return null
6
+ if (ts.isIdentifier(tag) && !isCap(tag.text)) return null
7
+ const span = (n) => [n.getStart(sf), n.end]
8
+ const site = {
9
+ tag: tag.getText(sf),
10
+ tagSpan: span(tag),
11
+ span: span(node),
12
+ attrs: [],
13
+ spreads: [],
14
+ children: null,
15
+ in: enclosing ?? null,
16
+ }
17
+ for (const a of node.attributes.properties) {
18
+ if (ts.isJsxSpreadAttribute(a)) {
19
+ site.spreads.push({ span: span(a.expression), text: a.expression.getText(sf) })
20
+ continue
21
+ }
22
+ const raw = a.name.getText(sf)
23
+ const bind = raw.startsWith('bind:')
24
+ const name = bind ? raw.slice(5) : raw
25
+ if (name === 'key') continue
26
+ const value = a.initializer
27
+ if (!value) {
28
+ site.attrs.push({ name, literal: 'true', bind })
29
+ continue
30
+ }
31
+ if (ts.isStringLiteral(value)) {
32
+ site.attrs.push({
33
+ name,
34
+ literal: 'string',
35
+ exact: JSON.stringify(value.text),
36
+ whole: span(a),
37
+ bind,
38
+ })
39
+ continue
40
+ }
41
+ const expr = value.expression
42
+ if (!expr) continue
43
+ const lambda = ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)
44
+ site.attrs.push({
45
+ name,
46
+ span: span(expr),
47
+ whole: span(a),
48
+ bind,
49
+ raw: bind ? span(a.name) : undefined,
50
+ untypedFn: (lambda && expr.parameters.some((p) => !p.type)) || undefined,
51
+ value: ts.isIdentifier(expr) && isCap(expr.text) ? expr.text : undefined,
52
+ })
53
+ }
54
+ if (ts.isJsxOpeningElement(node)) {
55
+ const kids = node.parent.children.filter((c) => !(ts.isJsxText(c) && !c.text.trim()))
56
+ if (kids.length) {
57
+ const only = kids.length === 1 ? kids[0] : null
58
+ const expr = only && ts.isJsxExpression(only) ? only.expression : null
59
+ const lambda = expr && (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr))
60
+ site.children = lambda
61
+ ? {
62
+ fn: true,
63
+ span: span(expr),
64
+ untypedFn: expr.parameters.some((p) => !p.type) || undefined,
65
+ }
66
+ : { fn: false }
67
+ }
68
+ }
69
+ return site
70
+ }
71
+
72
+ /** `const views = { a: A }`, `pick(A, B)`, `<X icon={A} />`: an identifier that leaves JSX tag position. */
73
+ function escapes(ts, node) {
74
+ const up = node.parent
75
+ if (ts.isJsxOpeningLikeElement?.(up) && up.tagName === node) return false
76
+ if (ts.isJsxClosingElement(up)) return false
77
+ if (ts.isFunctionDeclaration(up) || (ts.isVariableDeclaration(up) && up.name === node))
78
+ return false
79
+ if (ts.isPropertyAccessExpression(up) && up.name === node) return false
80
+ if (
81
+ ts.isExportSpecifier(up) ||
82
+ ts.isExportAssignment(up) ||
83
+ ts.isImportSpecifier(up) ||
84
+ ts.isImportClause(up)
85
+ )
86
+ return false
87
+ if (ts.isTypeQueryNode(up)) return false
88
+ return true
89
+ }
90
+
91
+ /** The names an initializer can hand back: `pick(A, B)`, `{ a: A, b: B }`, `views[k]`, `views.a`, `hoc(A)`, `x ? A : B`. */
92
+ function holds(ts, init) {
93
+ const name = (e) => (ts.isIdentifier(e) ? [e.text] : [])
94
+ if (ts.isCallExpression(init)) return init.arguments.flatMap((a) => name(a)).filter(isCap)
95
+ if (ts.isConditionalExpression(init))
96
+ return [...name(init.whenTrue), ...name(init.whenFalse)].filter(isCap)
97
+ if (ts.isObjectLiteralExpression(init))
98
+ return init.properties
99
+ .flatMap((p) =>
100
+ ts.isPropertyAssignment(p)
101
+ ? name(p.initializer)
102
+ : ts.isShorthandPropertyAssignment(p)
103
+ ? [p.name.text]
104
+ : [],
105
+ )
106
+ .filter(isCap)
107
+ if (ts.isElementAccessExpression(init) || ts.isPropertyAccessExpression(init))
108
+ return name(init.expression)
109
+ return []
110
+ }
111
+
112
+ const isOpening = (ts, n) => ts.isJsxOpeningElement(n) || ts.isJsxSelfClosingElement(n)
113
+
114
+ module.exports = { isCap, jsxSite, escapes, holds, isOpening }