@kayjs/kay 1.104.1 → 1.104.3

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 (116) hide show
  1. package/README.md +2 -7
  2. package/dist/eslint.js +208 -34
  3. package/editors/zed/extension.toml +2 -2
  4. package/llms/check-and-tools.md +5 -3
  5. package/llms/code-lints.md +1 -0
  6. package/llms/components.md +1 -1
  7. package/llms/islands.md +1 -1
  8. package/llms/project-new-and-add.md +5 -5
  9. package/llms/project.md +1 -2
  10. package/llms/styles-and-assets.md +1 -1
  11. package/llms/testing-browser.md +1 -0
  12. package/llms/upgrading.md +4 -0
  13. package/llms/whats-new-archive.md +17 -1
  14. package/llms/whats-new.md +20 -17
  15. package/llms.txt +1 -1
  16. package/package.json +9 -7
  17. package/src/add.ts +2 -0
  18. package/src/build/class-rules.ts +8 -0
  19. package/src/build/client.ts +9 -6
  20. package/src/build/recipe-classes.ts +11 -1
  21. package/src/build/recipe-utilities.ts +183 -0
  22. package/src/build/signals.ts +113 -14
  23. package/src/build/style-uses.ts +7 -6
  24. package/src/build/styles.ts +32 -21
  25. package/src/build/supports.ts +4 -4
  26. package/src/check/removed-ui-tokens.ts +2 -2
  27. package/src/check/shadow-types.ts +2 -0
  28. package/src/check/text-size-colour.ts +57 -0
  29. package/src/commands/new.ts +17 -7
  30. package/src/commands/upgrade-majors.ts +26 -0
  31. package/src/commands/upgrade-manifest.ts +23 -0
  32. package/src/commands/upgrade-messages.ts +16 -0
  33. package/src/commands/upgrade-peers.ts +89 -0
  34. package/src/commands/upgrade.ts +68 -36
  35. package/src/compiler/codegen.ts +1 -1
  36. package/src/compiler/dom.ts +12 -1
  37. package/src/compiler/index.ts +32 -17
  38. package/src/compiler/island-cause.ts +5 -2
  39. package/src/compiler/module-signals.ts +111 -8
  40. package/src/compiler/rewrite-reads.ts +1 -1
  41. package/src/compiler/route-lints.ts +8 -0
  42. package/src/compiler/script-props.ts +18 -1
  43. package/src/compiler/script-shared.ts +2 -0
  44. package/src/eslint/index.ts +19 -2
  45. package/src/features.ts +2 -2
  46. package/src/lsp/semantic-tokens.ts +2 -2
  47. package/src/migrate/project.ts +2 -2
  48. package/src/new.ts +6 -4
  49. package/src/runtime/dom.ts +11 -12
  50. package/src/template-config.ts +11 -4
  51. package/src/template-docker.ts +1 -1
  52. package/src/template-shared.ts +4 -1
  53. package/src/template-starter.ts +25 -4
  54. package/src/template-styles.ts +55 -43
  55. package/src/template-ws.ts +1 -1
  56. package/src/templates.ts +2 -4
  57. package/src/test/browser.ts +2 -5
  58. package/src/test/text-match.ts +44 -0
  59. package/types/add.d.ts.map +1 -1
  60. package/types/build/class-rules.d.ts +1 -0
  61. package/types/build/class-rules.d.ts.map +1 -1
  62. package/types/build/client.d.ts +1 -1
  63. package/types/build/client.d.ts.map +1 -1
  64. package/types/build/recipe-classes.d.ts +2 -0
  65. package/types/build/recipe-classes.d.ts.map +1 -1
  66. package/types/build/recipe-utilities.d.ts +4 -0
  67. package/types/build/recipe-utilities.d.ts.map +1 -0
  68. package/types/build/signals.d.ts.map +1 -1
  69. package/types/build/style-uses.d.ts +1 -0
  70. package/types/build/style-uses.d.ts.map +1 -1
  71. package/types/build/styles.d.ts +1 -1
  72. package/types/build/styles.d.ts.map +1 -1
  73. package/types/build/supports.d.ts +1 -1
  74. package/types/build/supports.d.ts.map +1 -1
  75. package/types/check/removed-ui-tokens.d.ts +2 -0
  76. package/types/check/removed-ui-tokens.d.ts.map +1 -1
  77. package/types/check/shadow-types.d.ts.map +1 -1
  78. package/types/check/text-size-colour.d.ts +3 -0
  79. package/types/check/text-size-colour.d.ts.map +1 -0
  80. package/types/commands/new.d.ts.map +1 -1
  81. package/types/commands/upgrade-majors.d.ts +7 -0
  82. package/types/commands/upgrade-majors.d.ts.map +1 -0
  83. package/types/commands/upgrade-manifest.d.ts +6 -0
  84. package/types/commands/upgrade-manifest.d.ts.map +1 -0
  85. package/types/commands/upgrade-messages.d.ts +4 -0
  86. package/types/commands/upgrade-messages.d.ts.map +1 -0
  87. package/types/commands/upgrade-peers.d.ts +23 -0
  88. package/types/commands/upgrade-peers.d.ts.map +1 -0
  89. package/types/commands/upgrade.d.ts +7 -0
  90. package/types/commands/upgrade.d.ts.map +1 -1
  91. package/types/compiler/dom.d.ts +1 -0
  92. package/types/compiler/dom.d.ts.map +1 -1
  93. package/types/compiler/index.d.ts.map +1 -1
  94. package/types/compiler/island-cause.d.ts +2 -2
  95. package/types/compiler/island-cause.d.ts.map +1 -1
  96. package/types/compiler/lints.d.ts +8 -0
  97. package/types/compiler/lints.d.ts.map +1 -1
  98. package/types/compiler/module-signals.d.ts +14 -1
  99. package/types/compiler/module-signals.d.ts.map +1 -1
  100. package/types/compiler/route-lints.d.ts +8 -0
  101. package/types/compiler/route-lints.d.ts.map +1 -1
  102. package/types/compiler/script-props.d.ts.map +1 -1
  103. package/types/compiler/script-shared.d.ts +1 -0
  104. package/types/compiler/script-shared.d.ts.map +1 -1
  105. package/types/new.d.ts.map +1 -1
  106. package/types/runtime/dom.d.ts +1 -0
  107. package/types/runtime/dom.d.ts.map +1 -1
  108. package/types/template-config.d.ts.map +1 -1
  109. package/types/template-shared.d.ts +1 -1
  110. package/types/template-shared.d.ts.map +1 -1
  111. package/types/template-starter.d.ts.map +1 -1
  112. package/types/template-styles.d.ts.map +1 -1
  113. package/types/templates.d.ts.map +1 -1
  114. package/types/test/browser.d.ts.map +1 -1
  115. package/types/test/text-match.d.ts +13 -0
  116. package/types/test/text-match.d.ts.map +1 -0
@@ -874,7 +874,7 @@ class ModuleShell {
874
874
 
875
875
  // A default runs the first time an `undefined` prop is read, then keeps its value like a destructuring default; it is typed as the prop, so a function or an object literal in it gets its type from the props type.
876
876
  for (const [index, binding] of props.bindings.entries()) {
877
- if (!binding.fallback)
877
+ if (!binding.fallback || (binding.literal !== undefined && this.options.check !== true))
878
878
  continue
879
879
 
880
880
  const name = defaultName(index)
@@ -14,7 +14,7 @@ import { addGroupChecked, checksMarkup, groupValue, staticAttributeText } from '
14
14
  import { bindEvent } from './validate'
15
15
 
16
16
  // The `kay/dom` helpers browser output calls, each imported under a `Kay` name.
17
- const DOM_HELPERS = ['append', 'attribute', 'branches', 'checkedMarkup', 'choose', 'close', 'component', 'componentClass', 'effect', 'errorParagraph', 'fragment', 'holdSubmits', 'insert', 'listen', 'listenSubmit', 'listenSubmitWith', 'listenWith', 'markers', 'memo', 'merge', 'omit', 'outside', 'partClass', 'partHanded', 'partsClass', 'property', 'ref', 'setClass', 'setStyle', 'spreadAttributes', 'spreadWith', 'template', 'text', 'transition', 'untrack']
17
+ const DOM_HELPERS = ['append', 'attribute', 'branches', 'checkedMarkup', 'choose', 'close', 'component', 'componentClass', 'effect', 'errorParagraph', 'fragment', 'holdSubmits', 'insert', 'listen', 'listenSubmit', 'listenSubmitWith', 'listenWith', 'markers', 'memo', 'merge', 'omit', 'outside', 'part', 'partClass', 'partHanded', 'partsClass', 'property', 'ref', 'setClass', 'setStyle', 'spreadAttributes', 'spreadWith', 'template', 'text', 'transition', 'untrack']
18
18
  // The helpers in modules of their own, as their runtimes weigh on a page whose islands use none of them: the module of each is only loaded by a page with an island that compiles to it.
19
19
  const APART = [['kay/dom-loop', ['loop', 'rowKey']], ['kay/dom-await', ['awaited']], ['kay/dom-try', ['attempt']], ['kay/dom-spread', ['dynamic', 'spread']], ['kay/dom-image', ['image']], ['kay/pending', ['pendingCall']]] as const
20
20
 
@@ -68,6 +68,8 @@ export class DomTemplate extends TemplateWriter {
68
68
  this.b.add('null')
69
69
  else if (items.length === 1 && only.kind === 'element')
70
70
  this.element(only.node)
71
+ else if (items.length === 1 && only.kind === 'component')
72
+ this.part(only.node)
71
73
  else
72
74
  this.fragment(nodes)
73
75
  }
@@ -78,6 +80,15 @@ export class DomTemplate extends TemplateWriter {
78
80
  return `${this.templates.map((html, index) => `const KayT${index} = KayTemplate(${JSON.stringify(html)})\n`).join('')}${this.writesLinks || navigates ? 'import \'kay/i18n-links\'\n' : ''}`
79
81
  }
80
82
 
83
+ // One component between its markers: the claim and the render of a fragment holding it, in one call.
84
+ private part(node: ElementNode): void {
85
+ this.b.wrap(node, () => {
86
+ this.b.add(`${helperName('part')}(() => `)
87
+ this.component(node)
88
+ this.b.add(')')
89
+ })
90
+ }
91
+
81
92
  private fragment(nodes: Node[]): void {
82
93
  const { b } = this
83
94
  const parent = `KayP${this.counter++}`
@@ -135,23 +135,42 @@ const BACKSLASH = /\\/g
135
135
  // An icon of an installed set, `~icons/tabler/home`, which the template may draw as a tag.
136
136
  const ICON_IMPORT = /^~icons\//
137
137
 
138
+ // What a module exports a name as, none for `toString` and the other names an object inherits.
139
+ function kindOf(imported: ImportedSignals | undefined, source: string, name: string): SignalKind | undefined {
140
+ const module = imported?.[source]
141
+
142
+ return module && Object.hasOwn(module, name) ? module[name] : undefined
143
+ }
144
+
138
145
  // The live values a script imports, which make its component an island: the server changes them and the page follows.
139
146
  function liveBindings(script: ScriptAnalysis, imported: ImportedSignals | undefined): ScriptAnalysis['imports'] {
140
- return script.imports.filter(binding => !binding.typeOnly && imported?.[binding.source]?.[binding.imported] === 'live')
147
+ return script.imports.filter(binding => !binding.typeOnly && kindOf(imported, binding.source, binding.imported) === 'live')
148
+ }
149
+
150
+ // The first live or persisted value a script imports, which makes its component an island: the browser holds the one, the server changes the other.
151
+ function browserBinding(script: ScriptAnalysis, imported: ImportedSignals | undefined): { local: string, kind: 'live' | 'persisted' } | undefined {
152
+ for (const binding of script.imports) {
153
+ const kind = binding.typeOnly ? undefined : kindOf(imported, binding.source, binding.imported)
154
+
155
+ if (kind === 'live' || kind === 'persisted')
156
+ return { local: binding.local, kind }
157
+ }
158
+
159
+ return undefined
141
160
  }
142
161
 
143
- // The live value an island imports, at its import.
144
- function liveCause(file: KayFile, script: ScriptAnalysis, live: string | undefined): IslandCause | undefined {
145
- const binding = script.imports.find(binding => binding.local === live)
162
+ // The live or persisted value an island imports, at its import.
163
+ function browserCause(file: KayFile, script: ScriptAnalysis, value: ReturnType<typeof browserBinding>): IslandCause | undefined {
164
+ const binding = script.imports.find(binding => binding.local === value?.local)
146
165
 
147
- return live === undefined || binding === undefined ? undefined : { kind: 'live', name: live, span: binding.span, line: lineOf(file.source, binding.span.start) }
166
+ return value === undefined || binding === undefined ? undefined : { kind: value.kind, name: value.local, span: binding.span, line: lineOf(file.source, binding.span.start) }
148
167
  }
149
168
 
150
- // The cause with the props the component's `island` export lists as making a use interactive, when it lists some: a client-only component and one following a live value are islands in every use.
169
+ // The cause with the props the component's `island` export lists as making a use interactive, when it lists some: a client-only component and one following a live or persisted value are islands in every use.
151
170
  function withInteractive(cause: IslandCause | undefined, script: ScriptAnalysis): IslandCause | undefined {
152
171
  const interactive = interactiveProps(script)
153
172
 
154
- return cause !== undefined && interactive !== undefined && cause.kind !== 'client' && cause.kind !== 'live' ? { ...cause, interactive } : cause
173
+ return cause !== undefined && interactive !== undefined && cause.kind !== 'client' && cause.kind !== 'live' && cause.kind !== 'persisted' ? { ...cause, interactive } : cause
155
174
  }
156
175
 
157
176
  // A cause found in the expanded source, in the file: its span, and its line counted in what the author wrote.
@@ -169,10 +188,6 @@ function staticPin(span: Span, cause: IslandCause): Diagnostic {
169
188
  return { ...span, code: 'static-pin', message: `This component is pinned \`--- static\`, and is an island: ${describeCause(cause)}`, note: 'it needs the browser, so it hydrates there', help: 'take that out of the component, or pin it `--- island`' }
170
189
  }
171
190
 
172
- function liveImport(script: ScriptAnalysis, imported: ImportedSignals | undefined): string | undefined {
173
- return liveBindings(script, imported)[0]?.local
174
- }
175
-
176
191
  // What the server fence declares for the template: its data and its exports, which the script reads as declared rather than as kay values to import.
177
192
  function serverNames(server: ServerAnalysis | undefined): string[] | undefined {
178
193
  return server && [...server.data.map(data => data.name), ...server.exportNames]
@@ -184,7 +199,7 @@ interface Role {
184
199
  always: boolean
185
200
  // A `--- island` fence: an island unless the file is a page, a layout or a special page, which is never one by its pin.
186
201
  pinned: boolean
187
- // It imports a live value, which makes a file that is not a layout, a special page or one with a server fence an island.
202
+ // It imports a live or persisted value, which makes a file that is not a layout, a special page or one with a server fence an island.
188
203
  live: boolean
189
204
  server: boolean
190
205
  }
@@ -211,7 +226,7 @@ function roleOf(original: string, imported: ImportedSignals | undefined, page: b
211
226
  const script = analyzeScript(source, file.script, serverNames(server), file.module)
212
227
  const always = file.client !== undefined || isInteractive(file, script)
213
228
  const pinned = file.pin?.kind === 'island' && !page
214
- const role = { always, pinned, live: liveImport(script, imported) !== undefined, server: file.server !== undefined }
229
+ const role = { always, pinned, live: browserBinding(script, imported) !== undefined, server: file.server !== undefined }
215
230
 
216
231
  if (kept) {
217
232
  const inner = kept.get(original) ?? new Map<string, Role>()
@@ -281,21 +296,21 @@ export function compileKay(original: string, filename: string, options: CompileO
281
296
  const failed = !client && !routeFile && options.page !== true ? takeBlock(file, filename, options, script, scope, 'failed') : undefined
282
297
  const failedAt = failed?.code.search(FAILED_FUNCTION) ?? -1
283
298
  const failedCode = failed && options.output === 'html' && failedAt >= 0 ? failed.code.slice(failedAt) : undefined
284
- const live = routeFile ? undefined : liveImport(script, options.importedSignals)
299
+ const live = routeFile ? undefined : browserBinding(script, options.importedSignals)
285
300
  const pinned = file.pin?.kind === 'island' && !pageFile
286
301
  const island = client || pinned || isInteractive(file, script) || live !== undefined ? islandId(filename, options.root) : undefined
287
302
  const hydratableServer = options.output === 'html' && options.hydratable === true
288
303
  const twin = hydratableServer && !options.twinBuild && island === undefined && !routeFile ? `./${basename(filename)}${ISLAND_TWIN}` : undefined
289
304
  const pin = file.pin
290
- const rawCause = island === undefined ? undefined : islandCause(file, script) ?? liveCause(file, script, live)
305
+ const rawCause = island === undefined ? undefined : islandCause(file, script) ?? browserCause(file, script, live)
291
306
  const cause = withInteractive(realCause(rawCause, expansion, original), script)
292
307
  const generateOptions = { cause: options.inspect === true && cause !== undefined ? describeCause(cause) : undefined, classTable: options.classTable, classMerge: options.classMerge, island, client, lives: island === undefined ? undefined : liveBindings(script, options.importedSignals).map(binding => binding.local), twin, output: options.output, failedCode, hydratable: hydratableServer && (island !== undefined || options.twinBuild === true), moduleId: islandId(filename, options.root), includeServer: options.includeServer, params: options.params, paramTypes: options.paramTypes, outerQueries: options.outerQueries, outerGuards: options.outerGuards, outerSlots: options.outerSlots, layoutFile: basename(filename) === LAYOUT, component: options.page === false && !routeFile, check: options.check, route: options.check === true && pageFile, pageFile, namedRefresh: options.namedRefresh, insideIsland: options.insideIsland, inspect: options.inspect, inspectSource: expansion ? { text: original, real: (offset: number) => expansion.real(offset) } : undefined, relocateImport: options.relocateImport, contextModule: options.check === true ? options.contextModule : undefined, ui: options.output === undefined ? undefined : options.ui, uiAdded: options.output === undefined ? options.ui : undefined, errorPage: isErrorPage(basename(filename)), cardPage: basename(filename) === CARD_PAGE, special: basename(filename).startsWith('_') && basename(filename) !== LAYOUT }
293
308
  // The analysis pass names the component so no read resolves to it, then the name is chosen free of what the code reads.
294
309
  const analysis = generate(file, script, server, { ...generateOptions, name: SELF })
295
310
  const imported = new Map<string, SignalKind>([...script.imports, ...server?.imports ?? []].flatMap((binding) => {
296
- const kind = binding.typeOnly ? undefined : options.importedSignals?.[binding.source]?.[binding.imported]
311
+ const kind = binding.typeOnly ? undefined : kindOf(options.importedSignals, binding.source, binding.imported)
297
312
 
298
- return kind ? [[binding.local, kind] as const] : []
313
+ return kind ? [[binding.local, kind === 'persisted' ? 'signal' : kind] as const] : []
299
314
  }))
300
315
  // Only code that runs calls the accessors of rows and aliases; the type checker reads them as values, which it narrows.
301
316
  const values = options.output === undefined
@@ -1,7 +1,7 @@
1
1
  import type { Span } from './ast'
2
2
 
3
3
  // What makes a component an island: where it sits in the file and what it is, for the editor, `kay build`, `kay explain island` and the dev tool to say the same thing.
4
- export type IslandCauseKind = 'client' | 'island' | 'attribute' | 'value' | 'live'
4
+ export type IslandCauseKind = 'client' | 'island' | 'attribute' | 'value' | 'live' | 'persisted'
5
5
 
6
6
  export interface IslandCause {
7
7
  kind: IslandCauseKind
@@ -41,6 +41,8 @@ function causeText(cause: IslandCause): string {
41
41
  return `\`${cause.name}\` from ${cause.source}`
42
42
  case 'live':
43
43
  return `\`${cause.name}\`, a live value`
44
+ case 'persisted':
45
+ return `\`${cause.name}\`, a persisted value`
44
46
  }
45
47
  }
46
48
 
@@ -51,7 +53,8 @@ export const CAUSE_KINDS: Record<IslandCauseKind, string> = {
51
53
  attribute: 'an `on*`, `bind:` or `ref` attribute: the browser handles it',
52
54
  value: 'a `kay` value of the browser, such as `signal` or `effect`, that its script uses',
53
55
  live: 'a live value its script imports: the browser follows it',
56
+ persisted: 'a persisted value its script imports: only the browser holds it',
54
57
  }
55
58
 
56
59
  // The rule, as `kay explain island` says it.
57
- export const ISLAND_RULE = 'A component is an island when it needs the browser: an `on*`, `bind:` or `ref` in its template, a browser value of `kay` in its script, a live value it imports, or a `--- client` or `--- island` fence; what an island renders is part of it. A static component that passes an island a function or a block taking parameters is an `island-prop` error, and is made an island by `--- island`. A component whose `export const island` lists `interactive` props is an island only in a use that turns one on: any other use renders as markup with no script.'
60
+ export const ISLAND_RULE = 'A component is an island when it needs the browser: an `on*`, `bind:` or `ref` in its template, a browser value of `kay` in its script, a live or persisted value it imports, or a `--- client` or `--- island` fence; what an island renders is part of it. A static component that passes an island a function or a block taking parameters is an `island-prop` error, and is made an island by `--- island`. A component whose `export const island` lists `interactive` props is an island only in a use that turns one on: any other use renders as markup with no script.'
@@ -1,14 +1,15 @@
1
- import type { Node, Program } from './estree'
1
+ import type { Node, NodeOfType, Program } from './estree'
2
2
  import { parseProgram } from './estree'
3
3
 
4
- // A signal is written by assigning its name; a memo is only read; a live value is only read in the browser, the server changing it.
5
- export type SignalKind = 'signal' | 'memo' | 'live'
4
+ // A signal is written by assigning its name; a memo is only read; a live value is only read in the browser, the server changing it; a persisted one is a signal the browser's storage holds, which makes a module's reader an island as a live value does.
5
+ export type SignalKind = 'signal' | 'memo' | 'live' | 'persisted'
6
6
 
7
7
  // The signals and memos of each module a component imports, by the specifier it imports them with.
8
8
  export type ImportedSignals = Record<string, Record<string, SignalKind>>
9
9
 
10
10
  export const MADE: Record<string, SignalKind> = { signal: 'signal', persisted: 'signal', linked: 'signal', queryParam: 'signal', memo: 'memo', debounced: 'memo', throttled: 'memo', live: 'live' }
11
11
  const NAMES_KAY = /["']kay["']/
12
+ const EXPORT = /\bexport\b/
12
13
 
13
14
  // The entries `persisted` carries, one per storage.
14
15
  export const PERSISTED_ENTRIES = new Set(['cookie', 'local', 'session', 'query'])
@@ -20,6 +21,13 @@ export function moduleSignals(source: string, jsx = false): Record<string, Signa
20
21
  return ast ? signalsIn(ast) : {}
21
22
  }
22
23
 
24
+ // A module's signals and what it hands on from other modules, from one parse that a module naming neither `kay` nor an export skips.
25
+ export function scanModule(source: string, jsx = false): { signals: Record<string, SignalKind>, links: ModuleLinks } {
26
+ const ast = NAMES_KAY.test(source) || EXPORT.test(source) ? parseModule(source, jsx) : undefined
27
+
28
+ return { signals: ast && NAMES_KAY.test(source) ? signalsIn(ast) : {}, links: ast ? linksIn(ast) : { stars: [], own: new Set(), names: [] } }
29
+ }
30
+
23
31
  function parseModule(source: string, jsx: boolean): Program | undefined {
24
32
  try {
25
33
  return parseProgram(source, { lang: jsx ? 'tsx' : 'ts', semantic: true }).program
@@ -71,30 +79,125 @@ function signalsIn(ast: Program): Record<string, SignalKind> {
71
79
 
72
80
  for (const specifier of statement.specifiers) {
73
81
  const imported = specifier.type === 'ImportSpecifier' && specifier.importKind !== 'type' ? specifier.imported : undefined
74
- const kind = imported && MADE[imported.type === 'Identifier' ? imported.name : imported.value]
82
+ const importedName = imported ? nameOf(imported) : undefined
83
+ const kind = importedName === 'persisted' ? 'persisted' : MADE[importedName ?? '']
75
84
 
76
85
  if (kind)
77
86
  made.set(specifier.local.name, kind)
78
87
 
79
- if (imported && (imported.type === 'Identifier' ? imported.name : imported.value) === 'persisted')
88
+ if (importedName === 'persisted')
80
89
  persisted.add(specifier.local.name)
81
90
  }
82
91
  }
83
92
 
93
+ // Every top-level `const` made by a signal call, exported or not, so an `export { name }` or an alias finds it.
94
+ const locals = new Map<string, SignalKind>()
95
+
84
96
  for (const statement of ast.body) {
85
- const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : undefined
97
+ const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement
86
98
 
87
99
  if (declaration?.type !== 'VariableDeclaration' || declaration.kind !== 'const')
88
100
  continue
89
101
 
90
102
  for (const { id, init } of declaration.declarations) {
91
103
  const name = init?.type === 'CallExpression' ? madeBy(init.callee, persisted) : undefined
92
- const kind = id.type === 'Identifier' && name !== undefined ? made.get(name) : undefined
104
+ const kind = init?.type === 'Identifier' ? locals.get(init.name) : made.get(name ?? '')
105
+
106
+ if (id.type !== 'Identifier' || !kind)
107
+ continue
108
+
109
+ locals.set(id.name, kind)
93
110
 
94
- if (id.type === 'Identifier' && kind)
111
+ if (statement.type === 'ExportNamedDeclaration')
95
112
  found[id.name] = kind
96
113
  }
97
114
  }
98
115
 
116
+ for (const statement of ast.body) {
117
+ if (statement.type !== 'ExportNamedDeclaration' || statement.source || statement.exportKind === 'type')
118
+ continue
119
+
120
+ for (const specifier of statement.specifiers) {
121
+ const local = specifier.type === 'ExportSpecifier' && specifier.exportKind !== 'type' && specifier.local.type === 'Identifier' ? locals.get(specifier.local.name) : undefined
122
+
123
+ if (local)
124
+ found[nameOf(specifier.exported)] = local
125
+ }
126
+ }
127
+
99
128
  return found
100
129
  }
130
+
131
+ function nameOf(node: Node): string {
132
+ if (node.type === 'Identifier')
133
+ return node.name
134
+
135
+ return node.type === 'Literal' ? String(node.value) : ''
136
+ }
137
+
138
+ // What a module hands on from another: the names `export { a as b } from`, `export * from`, or an import it exports (`import { a } from`, then `export { a }` or `export const b = a`) take from the module they come from, for the build to look their kind up there.
139
+ export interface ModuleLinks {
140
+ stars: string[]
141
+ // Every name the module exports itself, which shadows the same name of a star re-export.
142
+ own: Set<string>
143
+ names: { exported: string, source: string, imported: string }[]
144
+ }
145
+
146
+ function linksIn(ast: Program): ModuleLinks {
147
+ const links: ModuleLinks = { stars: [], own: new Set(), names: [] }
148
+ const imports = new Map<string, { source: string, imported: string }>()
149
+
150
+ for (const statement of ast.body) {
151
+ if (statement.type !== 'ImportDeclaration' || statement.importKind === 'type')
152
+ continue
153
+
154
+ for (const specifier of statement.specifiers) {
155
+ if (specifier.type === 'ImportSpecifier' && specifier.importKind !== 'type')
156
+ imports.set(specifier.local.name, { source: statement.source.value, imported: nameOf(specifier.imported) })
157
+ }
158
+ }
159
+
160
+ for (const statement of ast.body) {
161
+ if (statement.type === 'ExportAllDeclaration' && statement.exportKind !== 'type' && !statement.exported)
162
+ links.stars.push(statement.source.value)
163
+
164
+ if (statement.type === 'ExportNamedDeclaration' && statement.exportKind !== 'type')
165
+ linkNamed(statement, imports, links)
166
+ }
167
+
168
+ return links
169
+ }
170
+
171
+ function linkNamed(statement: NodeOfType<'ExportNamedDeclaration'>, imports: Map<string, { source: string, imported: string }>, links: ModuleLinks): void {
172
+ const declared = statement.declaration
173
+
174
+ if (declared?.type === 'VariableDeclaration') {
175
+ for (const { id } of declared.declarations) {
176
+ if (id.type === 'Identifier')
177
+ links.own.add(id.name)
178
+ }
179
+ }
180
+ else if (declared && 'id' in declared && declared.id?.type === 'Identifier') {
181
+ links.own.add(declared.id.name)
182
+ }
183
+
184
+ for (const specifier of statement.specifiers) {
185
+ if (specifier.type !== 'ExportSpecifier' || specifier.exportKind === 'type')
186
+ continue
187
+
188
+ links.own.add(nameOf(specifier.exported))
189
+
190
+ const local = specifier.local.type === 'Identifier' ? imports.get(specifier.local.name) : undefined
191
+ const from = statement.source ? { source: statement.source.value, imported: nameOf(specifier.local) } : local
192
+
193
+ if (from)
194
+ links.names.push({ exported: nameOf(specifier.exported), ...from })
195
+ }
196
+
197
+ for (const { id, init } of statement.declaration?.type === 'VariableDeclaration' && statement.declaration.kind === 'const' ? statement.declaration.declarations : []) {
198
+ const from = id.type === 'Identifier' && init?.type === 'Identifier' ? imports.get(init.name) : undefined
199
+
200
+ if (from && id.type === 'Identifier')
201
+ links.names.push({ exported: id.name, ...from })
202
+ }
203
+ }
@@ -320,7 +320,7 @@ export function propRead(binding: PropsBinding, index: number, local: string, ch
320
320
 
321
321
  const text = quoted ? `${PROPS}[${JSON.stringify(binding.key)}]` : `${PROPS}.${binding.key}`
322
322
 
323
- return { before: `(${access.before}`, replace: access.replace, after: `${access.after ?? ''} !== undefined ? ${text} : ${defaultName(index)}())` }
323
+ return { before: `(${access.before}`, replace: access.replace, after: `${access.after ?? ''} !== undefined ? ${text} : ${binding.literal ?? `${defaultName(index)}()`})` }
324
324
  }
325
325
 
326
326
  // A prop with a default reads without `undefined`, which `typeof __props.size` would keep.
@@ -6,6 +6,14 @@ import { SERIAL_AWAITS_LINTS } from './serial-awaits'
6
6
  export const ROUTE_LINTS = {
7
7
  ...SERIAL_AWAITS_LINTS,
8
8
  ...LOOP_LINTS,
9
+ 'text-size-colour': {
10
+ level: 'warn',
11
+ group: 'code',
12
+ site: true,
13
+ summary: 'A theme colour named like one of Tailwind\'s text sizes (`--color-base`, `--color-lg`, `--color-2xl`) in the project\'s CSS, while a file uses that `text-` utility: `text-base` is the font size and the text colour at once, so the text takes the colour, the page background where `base` is the ground, and disappears. Every use of the utility is reported. Rename the colour (`--color-background`, kayjs-ui\'s name for the ground) or write the size as `text-[1rem]`.',
14
+ wrong: '@theme { --color-base: #1e1e2e; }\nbutton.text-base Pick',
15
+ right: '@theme { --color-background: #1e1e2e; }\nbutton.text-base Pick',
16
+ },
9
17
  'unguarded-route': {
10
18
  level: 'deny',
11
19
  group: 'security',
@@ -76,12 +76,29 @@ export function propsDeclaration(statement: Statement, body: Statement[], shift:
76
76
  const previous = properties[index - 1]
77
77
  const removal = next ? { start: shift(property).start, end: shift(next).start } : previous && { start: shift(previous).end, end: shift(property).end }
78
78
 
79
- declaration.bindings.push({ local: shift(local), key, fallback: value.type === 'AssignmentPattern' ? shift(value.right) : undefined, removal, bindable: bindable.has(key) })
79
+ declaration.bindings.push({ local: shift(local), key, fallback: value.type === 'AssignmentPattern' ? shift(value.right) : undefined, literal: value.type === 'AssignmentPattern' ? literalSource(value.right) : undefined, removal, bindable: bindable.has(key) })
80
80
  }
81
81
 
82
82
  return declaration
83
83
  }
84
84
 
85
+ // A default with no identity and nothing to run: its text is as short as a read of its holder.
86
+ function literalSource(node: Node): string | undefined {
87
+ if (node.type === 'Literal')
88
+ return 'regex' in node || 'bigint' in node ? undefined : node.raw ?? undefined
89
+
90
+ if (node.type === 'UnaryExpression' && node.operator === '-' && node.argument.type === 'Literal' && typeof node.argument.value === 'number')
91
+ return `-${node.argument.raw}`
92
+
93
+ if (node.type === 'TemplateLiteral' && node.expressions.length === 0) {
94
+ const cooked = node.quasis[0]?.value.cooked
95
+
96
+ return typeof cooked === 'string' ? JSON.stringify(cooked) : undefined
97
+ }
98
+
99
+ return undefined
100
+ }
101
+
85
102
  function kayImports(body: Statement[], wanted: Set<string>): Map<string, string> {
86
103
  const local = new Map<string, string>()
87
104
 
@@ -15,6 +15,8 @@ export interface PropsBinding {
15
15
  local: Span
16
16
  key: string
17
17
  fallback?: Span | undefined
18
+ // The default's source when it is a string, number, boolean or null, which a read writes in place of a holder.
19
+ literal?: string | undefined
18
20
  // What leaves the destructuring without it, separator included, when another name stays.
19
21
  removal?: Span | undefined
20
22
  // Typed `Bindable<T>`: read as a plain value, and written by assigning it, which a parent's `bind:` takes.
@@ -413,9 +413,10 @@ function mapMessage(linted: Linted, message: LintMessage): LintMessage | undefin
413
413
  const endPosition = end === undefined ? undefined : positionAt(entry.source, span.end)
414
414
  // Props compile to `__props.name`, in a fence as in the template, so a message or fix quoting that code is put back to what the .kay file wrote, unless the file writes `__props` itself.
415
415
  const named = entry.source.includes('__props') ? (text: string) => text : (text: string) => text.replaceAll(COMPILED_PROPS, '')
416
- const fix = message.fix && mapFix(entry, unindentedFix(linted, message.fix), tailwind)
416
+ const mapped = (written: LintFix) => mapFix(entry, unindentedFix(linted, written), tailwind) ?? mapFix(entry, unindentedFix(linted, narrowed(linted.text, written)), tailwind)
417
+ const fix = message.fix && mapped(message.fix)
417
418
  const suggestions = message.suggestions?.flatMap((suggestion) => {
418
- const mappedFix = mapFix(entry, unindentedFix(linted, suggestion.fix), tailwind)
419
+ const mappedFix = mapped(suggestion.fix)
419
420
 
420
421
  return mappedFix ? [{ ...suggestion, desc: named(suggestion.desc), fix: { ...mappedFix, text: named(mappedFix.text) } }] : []
421
422
  })
@@ -423,6 +424,22 @@ function mapMessage(linted: Linted, message: LintMessage): LintMessage | undefin
423
424
  return { ...message, message: named(levelled(linted, message, start)), ...positionAt(entry.source, span.start), endLine: endPosition?.line, endColumn: endPosition?.column, fix: fix && { ...fix, text: named(fix.text) }, suggestions }
424
425
  }
425
426
 
427
+ // A fix cut down to what it changes: a rule that rewrites up to the next token (`style/semi` takes the `;` with the code after it, to keep the line from joining the next) reaches into the code the shadow generated, which a fix cannot cross, though what it changes is the author's own.
428
+ function narrowed(text: string, fix: LintFix): LintFix {
429
+ const old = text.slice(fix.range[0], fix.range[1])
430
+ let head = 0
431
+
432
+ while (head < old.length && head < fix.text.length && old[head] === fix.text[head])
433
+ head++
434
+
435
+ let tail = 0
436
+
437
+ while (tail < old.length - head && tail < fix.text.length - head && old[old.length - 1 - tail] === fix.text[fix.text.length - 1 - tail])
438
+ tail++
439
+
440
+ return { range: [fix.range[0] + head, fix.range[1] - tail], text: fix.text.slice(head, fix.text.length - tail) }
441
+ }
442
+
426
443
  // A fix stands only when the code it replaces was copied verbatim from the .kay file, or is a class the template writes as the string joins it, and a line break it adds only in a fence: a template expression is one line of the file.
427
444
  function mapFix(entry: ShadowFile, fix: LintFix, classes: boolean): LintFix | undefined {
428
445
  const span = codeEdit(entry, fix.range[0], fix.range[1])
package/src/features.ts CHANGED
@@ -4,7 +4,7 @@ import type { Feature } from './template-shared'
4
4
  export const FEATURES: Record<Feature, { description: string, needs: readonly Feature[], packages?: readonly string[], brings?: readonly Feature[], templated?: true }> = {
5
5
  server: { description: 'an Elysia server, run in production as one executable; off, a static site built to dist', needs: [] },
6
6
  starter: { description: 'a header, a 404 page, and a home page greeting from the server with a counter island', needs: [] },
7
- tailwind: { description: 'Tailwind over the Catppuccin Mocha palette', needs: [] },
7
+ tailwind: { description: 'Tailwind over orbit\'s near-black palette, and with `ui` a starfield behind the page', needs: [] },
8
8
  accents: { description: 'an accent picker in the header, kept in a cookie', needs: ['starter', 'tailwind'] },
9
9
  eslint: { description: 'ESLint through @kaynooo/eslint, .kay fences and template expressions included', needs: [] },
10
10
  tests: { description: 'kay test through kay/test, with a first test', needs: [] },
@@ -16,7 +16,7 @@ export const FEATURES: Record<Feature, { description: string, needs: readonly Fe
16
16
  ai: { description: 'kayjs-ai: ask(), an LLM called with no SDK, streaming into await blocks and actions, answered by a scripted fake in development and tests', needs: [], packages: ['@kayjs/ai'] },
17
17
  mail: { description: 'kayjs-mail: a mailer reading SMTP from the environment, with the outbox page in development', needs: [], packages: ['@kayjs/mail'] },
18
18
  cms: { description: 'kayjs-cms: collection screens under /admin, and content edited on the page it renders', needs: [], packages: ['@kayjs/cms', '@kayjs/ui', '@iconify-json/tabler'] },
19
- ui: { description: 'kayjs-ui: the theme, one Toaster and one TooltipHost for every page, with no line in the layout or app.css', needs: ['tailwind'], packages: ['@kayjs/ui', '@iconify-json/tabler'] },
19
+ ui: { description: 'kayjs-ui: its space skin, one Toaster and one TooltipHost for every page, and the header drawn with AppHeader', needs: ['tailwind'], packages: ['@kayjs/ui', '@iconify-json/tabler'], templated: true },
20
20
  }
21
21
 
22
22
  export const FEATURE_NAMES = Object.keys(FEATURES) as Feature[]
@@ -127,8 +127,8 @@ function madeBy(callee: string, made: ReadonlyMap<string, SignalKind | undefined
127
127
  return Object.hasOwn(MADE, callee) ? MADE[callee] : undefined
128
128
  }
129
129
 
130
- function signalCategory(kind: 'signal' | 'memo' | 'live'): Category {
131
- return { type: VARIABLE, modifiers: SIGNAL | (kind === 'signal' ? 0 : READONLY) }
130
+ function signalCategory(kind: SignalKind): Category {
131
+ return { type: VARIABLE, modifiers: SIGNAL | (kind === 'signal' || kind === 'persisted' ? 0 : READONLY) }
132
132
  }
133
133
 
134
134
  // Marks the names a fence declares at its top level, and every read or write of them; `known` holds those of the module that the code reads without declaring, which the server fence hands the script. The categories of what the fence declares are put in `declared`.
@@ -330,9 +330,9 @@ export async function migrateAstroProject(root: string, out = root, options: Mig
330
330
 
331
331
  project['package.json'] = carried
332
332
 
333
- // A `@kaynooo/*` range the registry the .npmrc names may hold no version of, which fails `bun install`.
333
+ // A `@kaynooo/*` range npm may hold no version of, which fails `bun install`.
334
334
  for (const [name, range] of Object.entries({ ...written.dependencies, ...written.devDependencies }).filter(([name]) => KAYNOOO_PACKAGE.test(name) && !(name in { ...own.dependencies, ...own.devDependencies })))
335
- note('package.json', undefined, { line: lineOfPackage(astroText, name), message: `\`${name}@${range}\` is carried over, and the Forgejo registry the .npmrc names may hold no version in that range: if \`bun install\` fails with No version matching, change the range to a published one` })
335
+ note('package.json', undefined, { line: lineOfPackage(astroText, name), message: `\`${name}@${range}\` is carried over, and npm may hold no version in that range: if \`bun install\` fails with No version matching, change the range to a published one` })
336
336
 
337
337
  if (Object.keys(paths).length > 0)
338
338
  project['tsconfig.json'] = `${JSON.stringify({ extends: 'kay/tsconfig.json', compilerOptions: { paths } }, null, 2)}\n`
package/src/new.ts CHANGED
@@ -8,6 +8,7 @@ import { checklist, select } from '@kaynooo/cli-kit/prompt'
8
8
  import { addFeatures, LibraryNotInstalledError } from './add'
9
9
  import { nativeExe } from './compiler/native-typescript'
10
10
  import { FEATURE_NAMES, FEATURES } from './features'
11
+ import { holds } from './template-shared'
11
12
  import { AGENT_FILES, agentsFile, CLAUDE_FILE, MCP_FILE, templateFiles } from './templates'
12
13
 
13
14
  // The kay this command runs from: a git checkout, which a new project depends on a packed copy of, or the published package.
@@ -28,7 +29,7 @@ export type { Feature, Host }
28
29
 
29
30
  export type Preset = 'blank' | 'server' | 'site' | 'app'
30
31
 
31
- const SITE: readonly Feature[] = ['starter', 'tailwind', 'eslint', 'tests', 'ci', 'docker']
32
+ const SITE: readonly Feature[] = ['starter', 'tailwind', 'ui', 'eslint', 'tests', 'ci', 'docker']
32
33
 
33
34
  export const PRESETS: Record<Preset, { description: string, features: readonly Feature[] }> = {
34
35
  blank: { description: 'one bare page and nothing else', features: [] },
@@ -228,7 +229,7 @@ async function librariesDryRun(files: Record<string, string>, libraries: readonl
228
229
 
229
230
  // 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.
230
231
  export async function projectFiles(dir: string, features: ReadonlySet<Feature>, kayDependency?: string | undefined, settings: ConfigSettings = {}, host?: Host, runner?: string): Promise<Record<string, string>> {
231
- const has = (feature: Feature) => features.has(feature)
232
+ const has = holds(features)
232
233
  const name = packageName(basename(resolve(dir)))
233
234
  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> }
234
235
  // A kay minor may break, so the project takes its patches only.
@@ -244,7 +245,8 @@ export async function projectFiles(dir: string, features: ReadonlySet<Feature>,
244
245
  ...has('postgres') && { 'db:generate': 'pg-orm generate', 'db:migrate': 'pg-orm apply' },
245
246
  }
246
247
  const devDependencies = [
247
- (has('starter') || has('accents') || has('auth')) && '@iconify-json/tabler',
248
+ ...has('ui') ? ['@fontsource-variable/jetbrains-mono', '@fontsource-variable/space-grotesk'] : [],
249
+ (has('starter') || has('accents') || has('ui')) && '@iconify-json/tabler',
248
250
  has('eslint') && '@kaynooo/eslint',
249
251
  '@types/bun',
250
252
  // The type checks and the accessibility checks load these from the app, at the ranges kay declares as its optional peers.
@@ -262,7 +264,7 @@ export async function projectFiles(dir: string, features: ReadonlySet<Feature>,
262
264
  type: 'module',
263
265
  private: true,
264
266
  scripts,
265
- dependencies: { ...has('auth') && { '@kayjs/auth': `~${kay.version}`, '@kayjs/ui': `~${kay.version}`, '@kaynooo/pg-orm': kay.devDependencies['@kaynooo/pg-orm'] }, elysia: kay.dependencies.elysia, kay: dependency },
267
+ dependencies: { ...has('auth') && { '@kayjs/auth': `~${kay.version}` }, ...has('ui') && { '@kayjs/ui': `~${kay.version}` }, ...has('auth') && { '@kaynooo/pg-orm': kay.devDependencies['@kaynooo/pg-orm'] }, elysia: kay.dependencies.elysia, kay: dependency },
266
268
  devDependencies: Object.fromEntries(devDependencies.map(dev => [dev, kay.peerDependencies[dev] ?? kay.devDependencies[dev]!])),
267
269
  }, null, 2)}\n`,
268
270
  'tsconfig.json': `${JSON.stringify({ extends: 'kay/tsconfig.json' }, null, 2)}\n`,
@@ -269,22 +269,18 @@ export function fragment(build: (parent: Node) => void): Unit | null {
269
269
 
270
270
  const last = claim.next ? claim.next.previousSibling : parent.lastChild
271
271
 
272
- if (!first || !last || first === claim.next)
273
- return null
274
-
275
- return first === last ? first : new Range(first, last)
272
+ return unitBetween(first === claim.next ? null : first, last)
276
273
  }
277
274
 
278
275
  const holder = document.createDocumentFragment()
279
276
 
280
277
  build(holder)
281
278
 
282
- const { firstChild, lastChild } = holder
283
-
284
- if (!firstChild || !lastChild)
285
- return null
279
+ return unitBetween(holder.firstChild, holder.lastChild)
280
+ }
286
281
 
287
- return firstChild === lastChild ? firstChild : new Range(firstChild, lastChild)
282
+ function unitBetween(first: Node | null, last: Node | null): Unit | null {
283
+ return first === last ? first : first && last && new Range(first, last)
288
284
  }
289
285
 
290
286
  // An element of a block, already claimed when hydrating.
@@ -320,6 +316,11 @@ export function errorParagraph(message: string | undefined, id: string, classes:
320
316
  return element
321
317
  }
322
318
 
319
+ // A block holding one component: a fragment of its markers and an `insert` that renders once.
320
+ export function part(render: () => unknown): Unit | null {
321
+ return fragment(parent => insert(null, ...markers(parent), render, false))
322
+ }
323
+
323
324
  // The markers around a dynamic part of a block.
324
325
  export function markers(parent: Node): [start: Node, end: Node] {
325
326
  if (claim) {
@@ -382,10 +383,8 @@ export class Region {
382
383
  return
383
384
  }
384
385
 
385
- const parent = this.parent
386
-
387
386
  while (this.start.nextSibling && this.start.nextSibling !== this.end)
388
- parent.removeChild(this.start.nextSibling)
387
+ this.parent.removeChild(this.start.nextSibling)
389
388
  }
390
389
  }
391
390