@kayjs/kay 1.106.0 → 1.108.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 (254) hide show
  1. package/README.md +2 -2
  2. package/dist/eslint.js +477 -204
  3. package/editors/zed/extension.toml +2 -2
  4. package/llms/action-protection.md +1 -1
  5. package/llms/actions-from-islands.md +1 -1
  6. package/llms/actions-refused-fields.md +22 -0
  7. package/llms/actions.md +2 -22
  8. package/llms/browser-only.md +1 -1
  9. package/llms/compiler-api.md +1 -1
  10. package/llms/component-libraries.md +1 -1
  11. package/llms/components.md +1 -1
  12. package/llms/deprecated-lints.md +2 -4
  13. package/llms/island-and-form-lints.md +1 -0
  14. package/llms/islands.md +11 -2
  15. package/llms/project-new-and-add.md +1 -1
  16. package/llms/project.md +2 -2
  17. package/llms/schemas.md +6 -4
  18. package/llms/upgrading.md +25 -1
  19. package/llms/whats-new-archive.md +36 -1
  20. package/llms/whats-new.md +18 -35
  21. package/llms.txt +1 -1
  22. package/package.json +15 -6
  23. package/src/build/client.ts +23 -26
  24. package/src/build/entry-files.ts +19 -0
  25. package/src/build/icon-slots.ts +140 -0
  26. package/src/build/lazy-components.ts +130 -0
  27. package/src/build/lazy-manifest.ts +56 -0
  28. package/src/build/lazy-stubs.ts +31 -0
  29. package/src/build/libraries.ts +24 -2
  30. package/src/build/plugin.ts +13 -1
  31. package/src/build/runtime-chunk.ts +3 -3
  32. package/src/build/source-files.ts +8 -3
  33. package/src/build/transform.ts +17 -7
  34. package/src/build/variants.ts +97 -5
  35. package/src/check/action-options.ts +56 -0
  36. package/src/check/project.ts +4 -4
  37. package/src/check/renamed-packages.ts +3 -63
  38. package/src/check/schema-json.ts +226 -0
  39. package/src/check/shadow-types.ts +15 -27
  40. package/src/compiler/codegen.ts +7 -17
  41. package/src/compiler/dead-code.ts +22 -0
  42. package/src/compiler/deprecated-lints-1-105.ts +41 -0
  43. package/src/compiler/deprecated-lints-1-106.ts +1 -1
  44. package/src/compiler/deprecated-lints-1-107.ts +14 -0
  45. package/src/compiler/deprecated-lints.ts +8 -65
  46. package/src/compiler/dom-property.ts +23 -0
  47. package/src/compiler/dom.ts +15 -15
  48. package/src/compiler/import-edits.ts +15 -0
  49. package/src/compiler/index.ts +8 -3
  50. package/src/compiler/island-declaration.ts +15 -0
  51. package/src/compiler/kay-exports.ts +1 -0
  52. package/src/compiler/lazy-imports.ts +357 -0
  53. package/src/compiler/parser.ts +2 -2
  54. package/src/compiler/route-lints.ts +2 -0
  55. package/src/compiler/schema-lints.ts +13 -0
  56. package/src/compiler/script-server.ts +6 -3
  57. package/src/content/validate.ts +1 -1
  58. package/src/new.ts +6 -2
  59. package/src/package-names.ts +1 -1
  60. package/src/runtime/action-call.ts +39 -327
  61. package/src/runtime/action-run.ts +357 -0
  62. package/src/runtime/action.ts +47 -4
  63. package/src/runtime/client.ts +10 -0
  64. package/src/runtime/dom-lazy.ts +276 -0
  65. package/src/runtime/field-errors.ts +58 -0
  66. package/src/runtime/field-refusal.ts +179 -0
  67. package/src/runtime/form.ts +3 -2
  68. package/src/runtime/html.ts +6 -1
  69. package/src/runtime/index.ts +2 -0
  70. package/src/runtime/island.server.ts +36 -2
  71. package/src/runtime/schema-record.ts +2 -1
  72. package/src/runtime/schema.client.ts +104 -14
  73. package/src/runtime/schema.ts +9 -2
  74. package/src/runtime/standard-schema.ts +32 -0
  75. package/src/runtime/validate-run.client.ts +179 -0
  76. package/src/runtime/validate.client.ts +11 -0
  77. package/src/runtime/validate.ts +4 -137
  78. package/src/server/actions.ts +48 -43
  79. package/src/server/answer.ts +102 -0
  80. package/src/server/api-input.ts +166 -0
  81. package/src/server/api.ts +12 -44
  82. package/src/server/app-stages.ts +15 -8
  83. package/src/server/app.ts +5 -2
  84. package/src/server/client.ts +2 -0
  85. package/src/server/dev.ts +7 -9
  86. package/src/server/file-limit.ts +7 -1
  87. package/src/server/forms.ts +1 -1
  88. package/src/server/host-allowlist.ts +7 -7
  89. package/src/server/index.ts +2 -0
  90. package/src/server/kay-routes.ts +221 -0
  91. package/src/server/kay-server.ts +228 -0
  92. package/src/server/kept-exports.ts +1 -0
  93. package/src/server/library.ts +0 -2
  94. package/src/server/mcp.ts +1 -1
  95. package/src/server/openapi.ts +12 -3
  96. package/src/server/phases.ts +249 -0
  97. package/src/server/protection.ts +6 -6
  98. package/src/server/proxy-key.ts +8 -8
  99. package/src/server/query.ts +4 -25
  100. package/src/server/receive.ts +43 -0
  101. package/src/server/render-document.ts +13 -3
  102. package/src/server/request-context.ts +137 -0
  103. package/src/server/schema-check.ts +1 -1
  104. package/src/server/socket.ts +19 -47
  105. package/src/server/stream.ts +1 -1
  106. package/src/server/validate.ts +346 -0
  107. package/src/test/mount.ts +6 -2
  108. package/types/build/client.d.ts +3 -2
  109. package/types/build/client.d.ts.map +1 -1
  110. package/types/build/entry-files.d.ts +4 -0
  111. package/types/build/entry-files.d.ts.map +1 -0
  112. package/types/build/icon-slots.d.ts +7 -0
  113. package/types/build/icon-slots.d.ts.map +1 -0
  114. package/types/build/lazy-components.d.ts +6 -0
  115. package/types/build/lazy-components.d.ts.map +1 -0
  116. package/types/build/lazy-manifest.d.ts +19 -0
  117. package/types/build/lazy-manifest.d.ts.map +1 -0
  118. package/types/build/lazy-stubs.d.ts +5 -0
  119. package/types/build/lazy-stubs.d.ts.map +1 -0
  120. package/types/build/libraries.d.ts +2 -0
  121. package/types/build/libraries.d.ts.map +1 -1
  122. package/types/build/plugin.d.ts +2 -0
  123. package/types/build/plugin.d.ts.map +1 -1
  124. package/types/build/source-files.d.ts.map +1 -1
  125. package/types/build/transform.d.ts +3 -1
  126. package/types/build/transform.d.ts.map +1 -1
  127. package/types/build/variants.d.ts.map +1 -1
  128. package/types/check/action-options.d.ts +3 -0
  129. package/types/check/action-options.d.ts.map +1 -0
  130. package/types/check/renamed-packages.d.ts +0 -3
  131. package/types/check/renamed-packages.d.ts.map +1 -1
  132. package/types/check/schema-json.d.ts +3 -0
  133. package/types/check/schema-json.d.ts.map +1 -0
  134. package/types/check/shadow-types.d.ts.map +1 -1
  135. package/types/compiler/codegen.d.ts +2 -0
  136. package/types/compiler/codegen.d.ts.map +1 -1
  137. package/types/compiler/dead-code.d.ts +9 -0
  138. package/types/compiler/dead-code.d.ts.map +1 -0
  139. package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
  140. package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
  141. package/types/compiler/deprecated-lints-1-106.d.ts +1 -1
  142. package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -1
  143. package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
  144. package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
  145. package/types/compiler/deprecated-lints.d.ts +36 -53
  146. package/types/compiler/deprecated-lints.d.ts.map +1 -1
  147. package/types/compiler/dom-property.d.ts +9 -0
  148. package/types/compiler/dom-property.d.ts.map +1 -0
  149. package/types/compiler/dom.d.ts +1 -0
  150. package/types/compiler/dom.d.ts.map +1 -1
  151. package/types/compiler/import-edits.d.ts +1 -0
  152. package/types/compiler/import-edits.d.ts.map +1 -1
  153. package/types/compiler/index.d.ts +2 -0
  154. package/types/compiler/index.d.ts.map +1 -1
  155. package/types/compiler/island-declaration.d.ts +5 -0
  156. package/types/compiler/island-declaration.d.ts.map +1 -1
  157. package/types/compiler/kay-exports.d.ts.map +1 -1
  158. package/types/compiler/lazy-imports.d.ts +33 -0
  159. package/types/compiler/lazy-imports.d.ts.map +1 -0
  160. package/types/compiler/lints.d.ts +44 -53
  161. package/types/compiler/lints.d.ts.map +1 -1
  162. package/types/compiler/route-lints.d.ts +8 -0
  163. package/types/compiler/route-lints.d.ts.map +1 -1
  164. package/types/compiler/schema-lints.d.ts +11 -0
  165. package/types/compiler/schema-lints.d.ts.map +1 -0
  166. package/types/compiler/script-server.d.ts.map +1 -1
  167. package/types/new.d.ts +1 -0
  168. package/types/new.d.ts.map +1 -1
  169. package/types/runtime/action-call.d.ts.map +1 -1
  170. package/types/runtime/action-run.d.ts +23 -0
  171. package/types/runtime/action-run.d.ts.map +1 -0
  172. package/types/runtime/action.d.ts +26 -1
  173. package/types/runtime/action.d.ts.map +1 -1
  174. package/types/runtime/client.d.ts.map +1 -1
  175. package/types/runtime/dom-lazy.d.ts +6 -0
  176. package/types/runtime/dom-lazy.d.ts.map +1 -0
  177. package/types/runtime/field-errors.d.ts +4 -0
  178. package/types/runtime/field-errors.d.ts.map +1 -0
  179. package/types/runtime/field-refusal.d.ts +37 -0
  180. package/types/runtime/field-refusal.d.ts.map +1 -0
  181. package/types/runtime/form.d.ts +1 -1
  182. package/types/runtime/form.d.ts.map +1 -1
  183. package/types/runtime/html.d.ts.map +1 -1
  184. package/types/runtime/index.d.ts +2 -0
  185. package/types/runtime/index.d.ts.map +1 -1
  186. package/types/runtime/island.server.d.ts +2 -0
  187. package/types/runtime/island.server.d.ts.map +1 -1
  188. package/types/runtime/schema-record.d.ts.map +1 -1
  189. package/types/runtime/schema.client.d.ts.map +1 -1
  190. package/types/runtime/schema.d.ts +6 -1
  191. package/types/runtime/schema.d.ts.map +1 -1
  192. package/types/runtime/standard-schema.d.ts +37 -0
  193. package/types/runtime/standard-schema.d.ts.map +1 -0
  194. package/types/runtime/validate-run.client.d.ts +4 -0
  195. package/types/runtime/validate-run.client.d.ts.map +1 -0
  196. package/types/runtime/validate.client.d.ts +4 -0
  197. package/types/runtime/validate.client.d.ts.map +1 -0
  198. package/types/runtime/validate.d.ts +1 -21
  199. package/types/runtime/validate.d.ts.map +1 -1
  200. package/types/server/actions.d.ts.map +1 -1
  201. package/types/server/answer.d.ts +6 -0
  202. package/types/server/answer.d.ts.map +1 -0
  203. package/types/server/api-input.d.ts +19 -0
  204. package/types/server/api-input.d.ts.map +1 -0
  205. package/types/server/api.d.ts.map +1 -1
  206. package/types/server/app-stages.d.ts +4 -1
  207. package/types/server/app-stages.d.ts.map +1 -1
  208. package/types/server/app.d.ts +2 -0
  209. package/types/server/app.d.ts.map +1 -1
  210. package/types/server/client.d.ts +1 -0
  211. package/types/server/client.d.ts.map +1 -1
  212. package/types/server/dev.d.ts +1 -1
  213. package/types/server/dev.d.ts.map +1 -1
  214. package/types/server/file-limit.d.ts.map +1 -1
  215. package/types/server/host-allowlist.d.ts +3 -3
  216. package/types/server/host-allowlist.d.ts.map +1 -1
  217. package/types/server/index.d.ts +2 -0
  218. package/types/server/index.d.ts.map +1 -1
  219. package/types/server/kay-routes.d.ts +47 -0
  220. package/types/server/kay-routes.d.ts.map +1 -0
  221. package/types/server/kay-server.d.ts +25 -0
  222. package/types/server/kay-server.d.ts.map +1 -0
  223. package/types/server/kept-exports.d.ts.map +1 -1
  224. package/types/server/library.d.ts +0 -1
  225. package/types/server/library.d.ts.map +1 -1
  226. package/types/server/openapi.d.ts.map +1 -1
  227. package/types/server/phases.d.ts +36 -0
  228. package/types/server/phases.d.ts.map +1 -0
  229. package/types/server/protection.d.ts +3 -2
  230. package/types/server/protection.d.ts.map +1 -1
  231. package/types/server/proxy-key.d.ts +3 -3
  232. package/types/server/proxy-key.d.ts.map +1 -1
  233. package/types/server/query.d.ts.map +1 -1
  234. package/types/server/receive.d.ts +9 -0
  235. package/types/server/receive.d.ts.map +1 -0
  236. package/types/server/render-document.d.ts.map +1 -1
  237. package/types/server/request-context.d.ts +29 -0
  238. package/types/server/request-context.d.ts.map +1 -0
  239. package/types/server/schema-check.d.ts +1 -0
  240. package/types/server/schema-check.d.ts.map +1 -1
  241. package/types/server/socket.d.ts +3 -5
  242. package/types/server/socket.d.ts.map +1 -1
  243. package/types/server/validate.d.ts +26 -0
  244. package/types/server/validate.d.ts.map +1 -0
  245. package/types/test/mount.d.ts.map +1 -1
  246. package/src/check/schema-type.ts +0 -9
  247. package/src/compiler/deprecated-script-fence.ts +0 -26
  248. package/src/server/field-errors.ts +0 -136
  249. package/types/check/schema-type.d.ts +0 -3
  250. package/types/check/schema-type.d.ts.map +0 -1
  251. package/types/compiler/deprecated-script-fence.d.ts +0 -3
  252. package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
  253. package/types/server/field-errors.d.ts +0 -6
  254. package/types/server/field-errors.d.ts.map +0 -1
@@ -0,0 +1,357 @@
1
+ import type { ElementNode, KayFile, Node, Span } from './ast'
2
+ import type { ImportBinding } from './script-shared'
3
+ import { serverIslandOf, visitNodes } from './ast'
4
+ import { helperName } from './markup'
5
+
6
+ // What the build knows of a `.kay` module a component imports: the key its chunk is named by, and the props it draws only inside a branch that may not render, as a closed dialog does its children.
7
+ export interface ImportedComponent {
8
+ key: string
9
+ // `children` for the lines between its tags, else the name of a `@block`.
10
+ deferred: readonly string[]
11
+ // The size of its source, as a component under `LAZY_FLOOR` costs more as a chunk than it weighs.
12
+ size: number
13
+ // The keys of the components loaded with it: its own, and those its imports draw outside any branch that may not render, which a module importing it already holds.
14
+ reach: readonly string[]
15
+ }
16
+
17
+ // The imported components of a file, by the specifier it imports them with.
18
+ export type ImportedComponents = Readonly<Record<string, ImportedComponent>>
19
+
20
+ // A component loaded the first time it draws, with the key that names its chunk.
21
+ export interface LazyImport {
22
+ key: string
23
+ specifier: string
24
+ declaration: { start: number, end: number }
25
+ }
26
+
27
+ // The smallest source a component loads lazily: below it the request and the stub cost more than the code saves.
28
+ export const LAZY_FLOOR = 2000
29
+ const CHILDREN = 'children'
30
+ const LINE_COMMENT = /^[ \t]*\/\/.*$/
31
+ const PLAIN_STRING = /'(?:[^'\\\n$`]|\\.)*'|"(?:[^"\\\n$`]|\\.)*"/g
32
+ // A line where a quote may open something other than a string: a template literal, or a regular expression, which starts where a value is expected.
33
+ const UNSURE_LINE = /`|[(,=:?!&|{;[]\s*\/(?![/*])/
34
+ // An import of a component's default export on a line of its own, the one form a lazy import can replace.
35
+ const PLAIN_IMPORT = /^import\s+[\w$]+\s+from\s*(['"])[^'"]+\1\s*;?$/
36
+
37
+ // How often a name is written as a word of the text, as `codeOf` leaves it: a count the tag uses do not account for is another reference to it.
38
+ function occurrences(text: string, name: string): number {
39
+ return text.match(new RegExp(`(?<![\\w$.])${name.replaceAll('$', '\\$')}(?![\\w$])`, 'g'))?.length ?? 0
40
+ }
41
+
42
+ // Whether the script names a prop other than as a string or a type's optional member, as `Omit<Props, 'children'>` or `children?: Snippet` do: any other mention may read it.
43
+ function reads(code: string, name: string): boolean {
44
+ return new RegExp(`(?<![\\w$'"])${name}(?![\\w$'"])(?!\\?:)`).test(code)
45
+ }
46
+
47
+ // Walks a template noting where each component call and each `slot` stands: in a branch that may not render (an `if` or `match` branch, a `catch`) or in a block a component draws only inside such a branch, which is `deferred`. The lines of a `snippet` stay where they are written, as it is drawn where it is called.
48
+ class Positions {
49
+ // How many repeated blocks, a loop body or a block of a function of a row, enclose the node being walked: a branch inside one is drawn by most pages, so it is no reason to split a component off.
50
+ private repeating = 0
51
+
52
+ constructor(private readonly source: string, private readonly deferring: (element: ElementNode) => readonly string[], private visit: { use?: (element: ElementNode, deferred: boolean) => void, outlet?: (element: ElementNode, deferred: boolean) => void }) {}
53
+
54
+ // An `if` with an `else` or a `match` with a default always draws one of its branches: a name written in each of them is drawn at first paint, one missing from some may not be.
55
+ private exhaustive(branches: readonly (readonly Node[])[], deferred: boolean): void {
56
+ const real = this.visit
57
+ const calls = branches.map((children) => {
58
+ const seen: { name: string, element: ElementNode, deferred: boolean, outlet: boolean }[] = []
59
+
60
+ this.visit = {
61
+ use: (element, inner) => seen.push({ name: element.name, element, deferred: inner, outlet: false }),
62
+ outlet: (element, inner) => seen.push({ name: element.outlet ? this.source.slice(element.outlet.name.start, element.outlet.name.end) : CHILDREN, element, deferred: inner, outlet: true }),
63
+ }
64
+ this.walk(children, deferred)
65
+
66
+ return seen
67
+ })
68
+
69
+ this.visit = real
70
+
71
+ const keyOf = (call: { name: string, outlet: boolean }) => `${call.outlet}:${call.name}`
72
+ const everywhere = calls.map(seen => new Set(seen.map(keyOf)))
73
+
74
+ for (const call of calls.flat()) {
75
+ const always = everywhere.every(names => names.has(keyOf(call)))
76
+
77
+ if (call.outlet)
78
+ real.outlet?.(call.element, call.deferred || (!always && this.repeating === 0))
79
+ else
80
+ real.use?.(call.element, call.deferred || (!always && this.repeating === 0))
81
+ }
82
+ }
83
+
84
+ walk(nodes: readonly Node[], deferred: boolean): void {
85
+ for (const node of nodes) {
86
+ switch (node.kind) {
87
+ case 'element':
88
+ this.element(node, deferred)
89
+
90
+ break
91
+ case 'if':
92
+ if (node.fallback) {
93
+ this.exhaustive([...node.branches.map(branch => branch.children), node.fallback.children], deferred)
94
+
95
+ break
96
+ }
97
+
98
+ for (const branch of node.branches)
99
+ this.walk(branch.children, deferred || this.repeating === 0)
100
+
101
+ break
102
+ case 'match':
103
+ if (node.fallback) {
104
+ this.exhaustive([...node.cases.map(branch => branch.children), node.fallback.children], deferred)
105
+
106
+ break
107
+ }
108
+
109
+ for (const branch of node.cases)
110
+ this.walk(branch.children, deferred || this.repeating === 0)
111
+
112
+ break
113
+ case 'for':
114
+ this.repeating++
115
+ this.walk(node.children, deferred)
116
+ this.repeating--
117
+ this.walk(node.fallback ?? [], deferred)
118
+
119
+ break
120
+ case 'await':
121
+ this.walk(node.pending, deferred)
122
+ this.walk(node.resolved?.children ?? [], deferred)
123
+ this.walk(node.rejected?.children ?? [], true)
124
+
125
+ break
126
+ case 'try':
127
+ this.walk(node.children, deferred)
128
+ this.walk(node.rejected?.children ?? [], true)
129
+
130
+ break
131
+ case 'text':
132
+ this.walk(node.parts.filter(part => part.kind === 'element'), deferred)
133
+
134
+ break
135
+ case 'slot':
136
+ case 'snippet':
137
+ case 'pending':
138
+ this.walk(node.children, deferred)
139
+
140
+ break
141
+ case 'const':
142
+ case 'error':
143
+ break
144
+ }
145
+ }
146
+ }
147
+
148
+ private element(element: ElementNode, deferred: boolean): void {
149
+ if (element.component)
150
+ this.visit.use?.(element, deferred)
151
+ else if (element.name === 'slot')
152
+ this.visit.outlet?.(element, deferred)
153
+
154
+ const props = element.component ? this.deferring(element) : []
155
+
156
+ for (const child of element.children) {
157
+ const own = child.kind === 'slot' ? this.source.slice(child.name.start, child.name.end) : CHILDREN
158
+
159
+ const repeated = child.kind === 'slot' && child.params !== undefined
160
+
161
+ this.repeating += repeated ? 1 : 0
162
+ this.walk(child.kind === 'slot' ? child.children : [child], deferred || props.includes(own))
163
+ this.repeating -= repeated ? 1 : 0
164
+ }
165
+ }
166
+ }
167
+
168
+ // The file with the text and the quoted attribute values of its template blanked, so what is left is code: a word naming a component is no reference when it is only written in a sentence.
169
+ function codeOf(file: KayFile): string {
170
+ const blanks: Span[] = []
171
+
172
+ visitNodes(file.template, (node) => {
173
+ if (node.kind === 'text')
174
+ blanks.push(...node.parts.filter(part => part.kind === 'chunk'))
175
+ else if (node.kind === 'element')
176
+ blanks.push(...node.attributes.flatMap(attribute => attribute.kind === 'static' ? [attribute.value] : []))
177
+ })
178
+
179
+ let code = file.source
180
+
181
+ for (const { start, end } of blanks)
182
+ code = `${code.slice(0, start)}${' '.repeat(end - start)}${code.slice(end)}`
183
+
184
+ return blankLiterals(code)
185
+ }
186
+
187
+ // The whole-line comments and the one-line strings that hold no `${`, blanked: a word in them, as the `Select all` of a label, is text no code reads, so it is no reference to a component of that name. A line that may hold a template literal or a regular expression is left as it is, since a quote there may pair with another that is no string's, and a word hidden by mistake would load a component lazily that code reads.
188
+ function blankLiterals(code: string): string {
189
+ const blank = (text: string) => ' '.repeat(text.length)
190
+
191
+ return code.split('\n').map((line) => {
192
+ if (LINE_COMMENT.test(line))
193
+ return blank(line)
194
+
195
+ return UNSURE_LINE.test(line) ? line : line.replace(PLAIN_STRING, blank)
196
+ }).join('\n')
197
+ }
198
+
199
+ // The text before a file's template, which is all of its script.
200
+ function fences(file: KayFile): string {
201
+ return file.source.slice(0, file.templateStart)
202
+ }
203
+
204
+ // The props of a component it draws only where some of its own branches may not render, so what a use hands it for them is not drawn when they do not.
205
+ // A name its script mentions is left out, as it may read the prop itself.
206
+ export function deferredProps(file: KayFile, imports: readonly ImportBinding[], known: (specifier: string) => ImportedComponent | undefined): string[] {
207
+ const specifiers = new Map(imports.filter(binding => binding.imported === 'default' && !binding.typeOnly).map(binding => [binding.local, binding.source]))
208
+ const drawn = new Map<string, boolean>()
209
+ const code = fences(file)
210
+
211
+ new Positions(file.source, (element) => {
212
+ const specifier = specifiers.get(element.name)
213
+
214
+ return specifier === undefined ? [] : known(specifier)?.deferred ?? []
215
+ }, {
216
+ outlet(element, deferred) {
217
+ const name = element.outlet ? file.source.slice(element.outlet.name.start, element.outlet.name.end) : CHILDREN
218
+
219
+ drawn.set(name, (drawn.get(name) ?? true) && deferred)
220
+ },
221
+ }).walk(file.template, false)
222
+
223
+ return [...drawn].filter(([name, deferred]) => deferred && !reads(code, name)).map(([name]) => name)
224
+ }
225
+
226
+ // The default imports of a component's script that draw only in branches that may not render, so their code loads when one does.
227
+ // An import is left alone when anything but a tag names it, as its value is then read at once, and when a call of it is a server island or a dynamic tag.
228
+ export function lazyImports(file: KayFile, imports: readonly ImportBinding[], known: ImportedComponents): Map<string, LazyImport> {
229
+ const found = positionsLazy(file, imports, known)
230
+
231
+ // A component another import of the file brings along as it loads is no chunk worth a request: dropping one adds what it brings, so the check goes on until nothing changes.
232
+ for (let changed = found.size > 0; changed;) {
233
+ const held = new Set(eagerReach(imports, known, found))
234
+
235
+ changed = false
236
+
237
+ for (const [local, lazy] of found) {
238
+ if (held.has(lazy.key)) {
239
+ found.delete(local)
240
+ changed = true
241
+ }
242
+ }
243
+ }
244
+
245
+ return found
246
+ }
247
+
248
+ // The keys the file loads as it loads: those of the components it imports other than lazily, and what each of them brings.
249
+ export function eagerReach(imports: readonly ImportBinding[], known: ImportedComponents, lazy: ReadonlyMap<string, LazyImport>): string[] {
250
+ const reach = new Set<string>()
251
+
252
+ for (const binding of imports) {
253
+ const component = binding.imported === 'default' && !binding.typeOnly ? known[binding.source] : undefined
254
+
255
+ if (component && !lazy.has(binding.local)) {
256
+ reach.add(component.key)
257
+
258
+ for (const key of component.reach)
259
+ reach.add(key)
260
+ }
261
+ }
262
+
263
+ return [...reach]
264
+ }
265
+
266
+ function positionsLazy(file: KayFile, imports: readonly ImportBinding[], known: ImportedComponents): Map<string, LazyImport> {
267
+ const candidates = new Map<string, { binding: ImportBinding, uses: boolean[] }>()
268
+ const specifiers = new Map<string, string>()
269
+
270
+ for (const binding of imports) {
271
+ if (binding.imported !== 'default' || binding.typeOnly)
272
+ continue
273
+
274
+ specifiers.set(binding.local, binding.source)
275
+
276
+ const declared = file.source.slice(binding.declaration.start, binding.declaration.end)
277
+
278
+ if ((known[binding.source]?.size ?? 0) >= LAZY_FLOOR && PLAIN_IMPORT.test(declared))
279
+ candidates.set(binding.local, { binding, uses: [] })
280
+ }
281
+
282
+ if (candidates.size === 0)
283
+ return new Map()
284
+
285
+ const refused = new Set<string>()
286
+
287
+ new Positions(file.source, (element) => {
288
+ const specifier = specifiers.get(element.name)
289
+
290
+ return specifier === undefined ? [] : known[specifier]?.deferred ?? []
291
+ }, {
292
+ use(element, deferred) {
293
+ const candidate = candidates.get(element.name)
294
+
295
+ if (!candidate)
296
+ return
297
+
298
+ candidate.uses.push(deferred)
299
+
300
+ if (serverIslandOf(element, file.source) !== undefined)
301
+ refused.add(element.name)
302
+ },
303
+ }).walk(file.template, false)
304
+
305
+ const code = codeOf(file)
306
+ const found = new Map<string, LazyImport>()
307
+
308
+ for (const [local, { binding, uses }] of candidates) {
309
+ const written = occurrences(code, local) - 1
310
+
311
+ if (uses.length === 0 || !uses.every(Boolean) || refused.has(local) || written !== uses.length)
312
+ continue
313
+
314
+ found.set(local, { key: known[binding.source]!.key, specifier: binding.source, declaration: binding.declaration })
315
+ }
316
+
317
+ return found
318
+ }
319
+
320
+ // Whether the file's output holds lazy imports: the browser's, and the server's render of an island or of a twin, which tells the island what it drew.
321
+ export function lazyImportsOf(file: KayFile, imports: readonly ImportBinding[], options: { components?: ImportedComponents | undefined, output?: string | undefined }, hydrated: boolean, routeFile: boolean, clientOnly: boolean): ReadonlyMap<string, LazyImport> | undefined {
322
+ return options.components !== undefined && !(routeFile || clientOnly) && (options.output === 'dom' || hydrated) ? lazyImports(file, imports, options.components) : undefined
323
+ }
324
+
325
+ // The import the server's output adds for a lazy import: what tells the island its code is wanted.
326
+ export function lazyNoteImport(lazy: ReadonlyMap<string, LazyImport> | undefined, mode: string): string {
327
+ return mode === 'html' && (lazy?.size ?? 0) > 0 ? `import { lazyNote as ${helperName('lazyNote')} } from 'kay/island'\n` : ''
328
+ }
329
+
330
+ // The import of the lazy runtime, only in a module that has lazy imports: a module naming it without using it still joins the chunks of every module that does, whatever the bundle drops after.
331
+ export function lazyDomImport(lazy: ReadonlyMap<string, LazyImport> | undefined): string {
332
+ return (lazy?.size ?? 0) > 0 ? `import { lazy as ${helperName('lazy')}, ready as ${helperName('ready')} } from 'kay/dom-lazy'\n` : ''
333
+ }
334
+
335
+ // The edits rewriting the lazy imports among a hoisted statement's `imports`: the browser's code asks for the module when the component first draws, the server's draws it as it always did and notes its key.
336
+ export function lazyEdits(imports: readonly ImportBinding[], span: Span, lazy: ReadonlyMap<string, LazyImport> | undefined, mode: string, moduleId: string | undefined): { start: number, end: number, text: string }[] {
337
+ if (mode !== 'dom' && mode !== 'html')
338
+ return []
339
+
340
+ const last = Math.max(-1, ...[...lazy?.values() ?? []].map(other => other.declaration.start))
341
+
342
+ return imports.flatMap((binding) => {
343
+ const found = lazy?.get(binding.local)
344
+
345
+ if (!found || binding.declaration.start < span.start || binding.declaration.end > span.end)
346
+ return []
347
+
348
+ const key = JSON.stringify(found.key)
349
+ const specifier = JSON.stringify(found.specifier)
350
+ const eager = `${helperName('lazy')}_${binding.local}`
351
+
352
+ // The module says which keys it imports lazily, once, after the last, so every key is registered by then.
353
+ const ready = found.declaration.start === last ? `\n${helperName('ready')}(${JSON.stringify(moduleId)}, ${JSON.stringify([...lazy?.values() ?? []].map(other => other.key))})` : ''
354
+
355
+ return [{ ...found.declaration, text: mode === 'dom' ? `const ${binding.local} = ${helperName('lazy')}(${key}, () => import(${specifier}))${ready}` : `import ${eager} from ${specifier}\nconst ${binding.local} = ${helperName('lazyNote')}(${key}, () => ${eager})` }]
356
+ })
357
+ }
@@ -2,7 +2,7 @@ import type { Attribute, AwaitBranch, AwaitNode, ConstNode, Details, Diagnostic,
2
2
  import { scanClassName } from './class-name'
3
3
  import { strayElse } from './else-problem'
4
4
  import { ifAlias } from './if-alias'
5
- import { rolesOf } from './island-declaration'
5
+ import { rolesOf, spelledOutFirstFence } from './island-declaration'
6
6
  import { Scanner } from './scanner'
7
7
 
8
8
  // `--- server`, `--- client`, `--- island`, `--- static`, `--- script`, `--- module`, or `--- <T extends Row>` making the component generic, its type parameters in the third or fourth group: `--- client <T>` takes both.
@@ -170,7 +170,7 @@ export function parseKay(source: string): KayFile {
170
170
  if (bareClosing)
171
171
  explainSecondFence(source, bareClosing, diagnostics)
172
172
 
173
- return { source, script: fences.script, server: fences.server, serverFence, module: fences.module, style: fences.style, typeParameters, client, pin, island, scriptLine, templateStart, template, diagnostics, unclosedServer }
173
+ return { source, script: fences.script, server: fences.server, serverFence, module: fences.module, style: fences.style, typeParameters, client, pin, island, scriptLine, templateStart, template, diagnostics: [...diagnostics, ...spelledOutFirstFence(source, scriptLine, fenceWord)], unclosedServer }
174
174
  }
175
175
 
176
176
  // A bare `---` in the template after a named fence closed by a bare `---`: the first bare line closed that fence, so the author meant the second fence as the script, which takes `--- script` once a named fence is there.
@@ -1,6 +1,7 @@
1
1
  import type { Lint } from './lints'
2
2
  import { LINK_LINTS } from './link-flags'
3
3
  import { LOOP_LINTS } from './loop-lints'
4
+ import { SCHEMA_LINTS } from './schema-lints'
4
5
  import { SERIAL_AWAITS_LINTS } from './serial-awaits'
5
6
 
6
7
  // The lints of the files under `src/routes`, of the content entries and of a page's server fence, with the other lints in `LINTS`.
@@ -8,6 +9,7 @@ export const ROUTE_LINTS = {
8
9
  ...SERIAL_AWAITS_LINTS,
9
10
  ...LOOP_LINTS,
10
11
  ...LINK_LINTS,
12
+ ...SCHEMA_LINTS,
11
13
  'text-size-colour': {
12
14
  level: 'warn',
13
15
  group: 'code',
@@ -0,0 +1,13 @@
1
+ import type { Lint } from './lints'
2
+
3
+ // The lints about the schemas an app hands the server, with the others in `LINTS`.
4
+ export const SCHEMA_LINTS = {
5
+ 'schema-without-json-schema': {
6
+ level: 'warn',
7
+ group: 'forms',
8
+ site: true,
9
+ summary: 'A Standard Schema validator with no `~standard.jsonSchema` where an action\'s `input` or an API route\'s `body`, `query`, `params`, `headers` or `cookie` takes a schema: it still validates, but a form shows no constraints, a form or a query is not read as numbers and booleans, and OpenAPI and MCP show an open schema. Read from the source, in `.ts` modules and `.kay` fences: a hand-written object with `~standard` and no `jsonSchema`, and what valibot builds (valibot carries none until `toStandardJsonSchema` from `@valibot/to-json-schema` wraps it), followed through a `const` and a relative import. Zod 4.2+ and ArkType 2.1.28+ carry it.',
10
+ wrong: '--- server\nimport { action } from \'kay\'\nimport * as v from \'valibot\'\n\nexport const add = action({ input: v.object({ title: v.string() }) }, ({ title }) => title)\n---\np x',
11
+ right: '--- server\nimport { action, s } from \'kay\'\n\nexport const add = action({ input: { title: s.string({ max: 200 }) } }, ({ title }) => title)\n---\np x',
12
+ },
13
+ } as const satisfies Record<string, Lint>
@@ -272,11 +272,14 @@ const BOT_PROTECTIONS = new Set(['honeypot', 'minTime', 'proofOfWork'])
272
272
 
273
273
  // What an `action(...)` call's options say, when one of its arguments is an object literal setting `tool`. Options it cannot read, a variable or an object spreading the `tool`, may make a tool: `output` is then false, so the type of its handler is probed, and `botProtected` false.
274
274
  function toolOptions(args: NodeOfType<'CallExpression'>['arguments']): { botProtected: boolean, output: boolean } | undefined {
275
- // The options follow the handler, which is the first argument of an action with no schema.
276
- const options = args[0]?.type === 'ArrowFunctionExpression' || args[0]?.type === 'FunctionExpression' ? args[1] : args[2]
275
+ const isHandler = (arg: Node | undefined): boolean => arg?.type === 'ArrowFunctionExpression' || arg?.type === 'FunctionExpression'
276
+ // The options come first when a handler follows them (`action({ input, ...options }, handler)`), else follow the handler, which is the first argument of an action with no schema.
277
+ const optionsFirst = args[0]?.type === 'ObjectExpression' && isHandler(args[1])
278
+ const afterHandler = isHandler(args[0]) ? args[1] : args[2]
279
+ const options = optionsFirst ? args[0] : afterHandler
277
280
  let unreadable = options !== undefined && options.type !== 'ObjectExpression'
278
281
 
279
- for (const arg of args.slice(1)) {
282
+ for (const arg of args.slice(options === args[0] ? 0 : 1)) {
280
283
  if (arg.type !== 'ObjectExpression')
281
284
  continue
282
285
 
@@ -1,7 +1,7 @@
1
1
  import type { TSchema } from 'elysia'
2
2
  import type { RawEntry } from './load'
3
3
  import type { ComponentSegment, Segment } from './markdown'
4
- import { errorText } from '../server/field-errors'
4
+ import { errorText } from '../runtime/field-errors'
5
5
  import { compiledCheck, dottedPath, fieldOf, refusals, unescaped } from '../server/schema-check'
6
6
  import { entryRefusal, kayWord } from '../server/words'
7
7
  import { humanize } from './humanize'
package/src/new.ts CHANGED
@@ -227,13 +227,17 @@ async function librariesDryRun(files: Record<string, string>, libraries: readonl
227
227
  }
228
228
  }
229
229
 
230
+ // The range a new project pins kay to: a 1.x minor may break, so it takes patches only (`~`), and from 2.0.0 a breaking change is a major, so it takes minors too (`^`).
231
+ export function kayRange(version: string): string {
232
+ return Number.parseInt(version, 10) >= 2 ? `^${version}` : `~${version}`
233
+ }
234
+
230
235
  // Every file of a project with these features in `dir`, named after it; run from a checkout of kay, packs it into `dir/vendor` for the project to depend on.
231
236
  export async function projectFiles(dir: string, features: ReadonlySet<Feature>, kayDependency?: string | undefined, settings: ConfigSettings = {}, host?: Host, runner?: string): Promise<Record<string, string>> {
232
237
  const has = holds(features)
233
238
  const name = packageName(basename(resolve(dir)))
234
239
  const kay = await Bun.file(join(KAY_ROOT, 'package.json')).json() as { name: string, version: string, dependencies: Record<string, string>, devDependencies: Record<string, string>, peerDependencies: Record<string, string> }
235
- // A kay minor may break, so the project takes its patches only.
236
- const dependency = kayDependency ?? (existsSync(join(KAY_ROOT, '.git')) ? await packKay(join(dir, 'vendor')) : `npm:${kay.name}@~${kay.version}`)
240
+ const dependency = kayDependency ?? (existsSync(join(KAY_ROOT, '.git')) ? await packKay(join(dir, 'vendor')) : `npm:${kay.name}@${kayRange(kay.version)}`)
237
241
  const scripts = {
238
242
  'dev': 'kay dev',
239
243
  ...has('server') ? { build: 'kay build', compile: 'kay build --compile server', start: 'kay start' } : { build: 'kay build --static' },
@@ -1,4 +1,4 @@
1
- // kay and its libraries were published under the `@kaynooo` scope up to 1.103 and are under `@kayjs` since 1.104: the new name of each, which `kay upgrade` and the `deprecated-package-names` lint move an app to.
1
+ // kay and its libraries were published under the `@kaynooo` scope up to 1.103 and are under `@kayjs` since 1.104: the new name of each, which `kay upgrade` moves an app to.
2
2
  export const RENAMED_PACKAGES: Readonly<Record<string, string>> = {
3
3
  '@kaynooo/kayjs': '@kayjs/kay',
4
4
  '@kaynooo/kayjs-ai': '@kayjs/ai',