@kayjs/kay 1.110.0 → 1.112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/eslint.js +160 -27
- package/editors/zed/extension.toml +2 -2
- package/llms/build-and-start.md +1 -1
- package/llms/checks.md +1 -0
- package/llms/component-libraries.md +1 -1
- package/llms/deprecated-lints.md +1 -1
- package/llms/dev-tool.md +1 -1
- package/llms/effects.md +1 -0
- package/llms/head.md +1 -1
- package/llms/images.md +2 -0
- package/llms/islands-lazy-components.md +23 -0
- package/llms/islands.md +2 -14
- package/llms/lints.md +1 -1
- package/llms/navigation.md +2 -2
- package/llms/performance-lints.md +1 -0
- package/llms/prerendering.md +1 -1
- package/llms/project-new-and-add.md +2 -2
- package/llms/project.md +1 -1
- package/llms/templates-elements-slots-snippets.md +2 -2
- package/llms/upgrading-archive.md +107 -58
- package/llms/upgrading.md +20 -124
- package/llms/whats-new-archive.md +31 -31
- package/llms/whats-new.md +36 -32
- package/llms.txt +3 -2
- package/package.json +11 -11
- package/src/build/client.ts +10 -8
- package/src/build/island-images.ts +277 -0
- package/src/build/pipeline.ts +10 -4
- package/src/build/static.ts +99 -24
- package/src/check/schema-builders.ts +113 -8
- package/src/check/shadow-types.ts +3 -3
- package/src/compiler/codegen.ts +1 -1
- package/src/compiler/dom.ts +4 -3
- package/src/compiler/handed-data.ts +15 -0
- package/src/compiler/helper-imports.ts +2 -2
- package/src/compiler/html.ts +3 -7
- package/src/compiler/index.ts +3 -1
- package/src/compiler/items.ts +162 -0
- package/src/compiler/jsx.ts +6 -8
- package/src/compiler/kay-exports.ts +1 -0
- package/src/compiler/lints.ts +1 -1
- package/src/compiler/markup.ts +10 -3
- package/src/compiler/rewrite-types.ts +2 -0
- package/src/compiler/rewrite.ts +4 -2
- package/src/compiler/route-lints.ts +8 -0
- package/src/compiler/template-walk.ts +2 -1
- package/src/config-shape.ts +4 -1
- package/src/config.ts +2 -2
- package/src/features.ts +1 -1
- package/src/new.ts +16 -4
- package/src/project.ts +3 -3
- package/src/runtime/await.server.ts +4 -2
- package/src/runtime/dev-tab-env.ts +28 -3
- package/src/runtime/dom-loop.ts +2 -4
- package/src/runtime/dom.ts +1 -1
- package/src/runtime/html.ts +52 -6
- package/src/runtime/image.ts +22 -4
- package/src/runtime/index.ts +1 -1
- package/src/runtime/island.server.ts +3 -3
- package/src/runtime/navigate.ts +5 -0
- package/src/runtime/navigation.ts +4 -0
- package/src/runtime/page-swap.ts +4 -1
- package/src/runtime/reactive.ts +72 -0
- package/src/runtime/row-key.ts +4 -0
- package/src/runtime/swap-focus.ts +56 -9
- package/src/runtime/swap-kept.ts +28 -0
- package/src/server/client.ts +3 -0
- package/src/server/dev.ts +2 -0
- package/src/server/image.ts +1 -1
- package/src/server/kept-exports.ts +1 -0
- package/src/server/render-document.ts +3 -2
- package/src/server/render.ts +2 -2
- package/src/template-styles.ts +2 -2
- package/types/build/client.d.ts +6 -0
- package/types/build/client.d.ts.map +1 -1
- package/types/build/island-images.d.ts +45 -0
- package/types/build/island-images.d.ts.map +1 -0
- package/types/build/pipeline.d.ts.map +1 -1
- package/types/build/static.d.ts.map +1 -1
- package/types/check/schema-builders.d.ts.map +1 -1
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/handed-data.d.ts +5 -0
- package/types/compiler/handed-data.d.ts.map +1 -0
- package/types/compiler/html.d.ts.map +1 -1
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/items.d.ts +13 -0
- package/types/compiler/items.d.ts.map +1 -1
- package/types/compiler/jsx.d.ts.map +1 -1
- package/types/compiler/kay-exports.d.ts.map +1 -1
- package/types/compiler/lints.d.ts +9 -1
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/markup.d.ts +2 -0
- package/types/compiler/markup.d.ts.map +1 -1
- package/types/compiler/rewrite-types.d.ts +1 -0
- package/types/compiler/rewrite-types.d.ts.map +1 -1
- package/types/compiler/route-lints.d.ts +8 -0
- package/types/compiler/route-lints.d.ts.map +1 -1
- package/types/compiler/template-walk.d.ts.map +1 -1
- package/types/config-shape.d.ts.map +1 -1
- package/types/config.d.ts +3 -1
- package/types/config.d.ts.map +1 -1
- package/types/new.d.ts.map +1 -1
- package/types/project.d.ts +2 -2
- package/types/project.d.ts.map +1 -1
- package/types/runtime/await.server.d.ts.map +1 -1
- package/types/runtime/dev-tab-env.d.ts.map +1 -1
- package/types/runtime/dom-loop.d.ts +2 -1
- package/types/runtime/dom-loop.d.ts.map +1 -1
- package/types/runtime/dom.d.ts +1 -1
- package/types/runtime/dom.d.ts.map +1 -1
- package/types/runtime/html.d.ts +5 -2
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/image.d.ts.map +1 -1
- package/types/runtime/index.d.ts +1 -1
- package/types/runtime/index.d.ts.map +1 -1
- package/types/runtime/navigate.d.ts.map +1 -1
- package/types/runtime/navigation.d.ts +2 -0
- package/types/runtime/navigation.d.ts.map +1 -1
- package/types/runtime/reactive.d.ts +5 -0
- package/types/runtime/reactive.d.ts.map +1 -1
- package/types/runtime/row-key.d.ts +2 -0
- package/types/runtime/row-key.d.ts.map +1 -0
- package/types/runtime/swap-focus.d.ts +2 -0
- package/types/runtime/swap-focus.d.ts.map +1 -1
- package/types/runtime/swap-kept.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +3 -1
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/app.d.ts +3 -1
- package/types/server/app.d.ts.map +1 -1
- package/types/server/client.d.ts +2 -0
- package/types/server/client.d.ts.map +1 -1
- package/types/server/dev.d.ts +1 -0
- package/types/server/dev.d.ts.map +1 -1
- package/types/server/kept-exports.d.ts.map +1 -1
- package/types/server/render-document.d.ts.map +1 -1
- package/types/server/render.d.ts +2 -2
- package/types/server/render.d.ts.map +1 -1
package/src/compiler/items.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AwaitNode, ConstNode, ElementNode, ErrorNode, ForNode, IfNode, MatchNode, Node, PendingNode, SnippetNode, Span, TextExpression, TryNode } from './ast'
|
|
2
2
|
import type { CodeBuilder } from './mapping'
|
|
3
|
+
import type { Sites } from './rewrite-types'
|
|
3
4
|
import { isHead, isSlot, keyedByPosition } from './ast'
|
|
4
5
|
import { PROPS } from './names'
|
|
5
6
|
import { parsedBindingNames } from './pattern-names'
|
|
@@ -170,6 +171,140 @@ export function parameterNames(pattern: string): string[] {
|
|
|
170
171
|
return parsedBindingNames(pattern) ?? []
|
|
171
172
|
}
|
|
172
173
|
|
|
174
|
+
const PLAIN_PARAMETER = /^([\p{ID_Start}$_][\p{ID_Continue}$]*)(?:\s*\?)?\s*(?::|$)/u
|
|
175
|
+
const OPENING = '([{<'
|
|
176
|
+
const CLOSING = ')]}>'
|
|
177
|
+
const QUOTES = '"\'`'
|
|
178
|
+
|
|
179
|
+
function skipString(text: string, from: number): number {
|
|
180
|
+
const quote = text[from]!
|
|
181
|
+
let index = from + 1
|
|
182
|
+
|
|
183
|
+
while (index < text.length && text[index] !== quote)
|
|
184
|
+
index += text[index] === '\\' ? 2 : 1
|
|
185
|
+
|
|
186
|
+
return index
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The parameters of a block or a snippet that are plain names, typed or not, with their place in the list, which one of a component that renders again reads through an accessor, so a caller handing it what a `followed` draw follows gets the current value read each time. A pattern, a rest or a default stays as written. None when the list has no such name.
|
|
190
|
+
export function accessorParameters(params: string): { name: string, position: number }[] | undefined {
|
|
191
|
+
const names: { name: string, position: number }[] = []
|
|
192
|
+
let depth = 0
|
|
193
|
+
let from = 0
|
|
194
|
+
let defaulted = false
|
|
195
|
+
let position = 0
|
|
196
|
+
|
|
197
|
+
const close = (to: number) => {
|
|
198
|
+
const name = defaulted ? undefined : PLAIN_PARAMETER.exec(params.slice(from, to).trim())?.[1]
|
|
199
|
+
|
|
200
|
+
if (name !== undefined)
|
|
201
|
+
names.push({ name, position })
|
|
202
|
+
|
|
203
|
+
position++
|
|
204
|
+
|
|
205
|
+
from = to + 1
|
|
206
|
+
defaulted = false
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (let index = 0; index < params.length; index++) {
|
|
210
|
+
const char = params[index]!
|
|
211
|
+
|
|
212
|
+
if (char === '=' && params[index + 1] === '>') {
|
|
213
|
+
index++
|
|
214
|
+
}
|
|
215
|
+
else if (QUOTES.includes(char)) {
|
|
216
|
+
// A string in a type or a default, whose commas and brackets are no part of the list.
|
|
217
|
+
index = skipString(params, index)
|
|
218
|
+
}
|
|
219
|
+
else if (OPENING.includes(char)) {
|
|
220
|
+
depth++
|
|
221
|
+
}
|
|
222
|
+
else if (CLOSING.includes(char)) {
|
|
223
|
+
depth--
|
|
224
|
+
}
|
|
225
|
+
else if (char === ',' && depth === 0) {
|
|
226
|
+
close(index)
|
|
227
|
+
}
|
|
228
|
+
else if (char === '=' && depth === 0) {
|
|
229
|
+
defaulted = true
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
close(params.length)
|
|
234
|
+
|
|
235
|
+
return depth === 0 && names.length > 0 ? names : undefined
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const CHAIN = String.raw`[\p{ID_Start}$_][\p{ID_Continue}$]*(?:\.[\p{ID_Start}$_][\p{ID_Continue}$]*)*`
|
|
239
|
+
const CHAINS = new RegExp(String.raw`^\s*${CHAIN}(?:\s*,\s*${CHAIN})*\s*$`, 'u')
|
|
240
|
+
const COMMA = /,/g
|
|
241
|
+
|
|
242
|
+
// The spans of the arguments of a `slot` call when each is a name or a path of names, which the runtime reads one by one: the first as the item the block follows, the others where they change. None for any other argument.
|
|
243
|
+
function simpleArguments(src: string, args: Span): Span[] | undefined {
|
|
244
|
+
const text = src.slice(args.start, args.end)
|
|
245
|
+
|
|
246
|
+
if (!CHAINS.test(text))
|
|
247
|
+
return undefined
|
|
248
|
+
|
|
249
|
+
const spans: Span[] = []
|
|
250
|
+
let from = 0
|
|
251
|
+
|
|
252
|
+
for (const cut of [...[...text.matchAll(COMMA)].map(match => match.index), text.length]) {
|
|
253
|
+
const piece = text.slice(from, cut)
|
|
254
|
+
const lead = piece.length - piece.trimStart().length
|
|
255
|
+
|
|
256
|
+
spans.push({ start: args.start + from + lead, end: args.start + from + piece.trimEnd().length })
|
|
257
|
+
from = cut + 1
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return spans
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// A `slot` called with names or paths, which draws its block once for the item under its key, so a replacement leaves its nodes and their focus. False, and nothing written, for any other call.
|
|
264
|
+
export function addSlotCall(b: CodeBuilder, src: string, slot: ElementNode, helper: string): boolean {
|
|
265
|
+
const spans = slot.outlet?.args && simpleArguments(src, slot.outlet.args)
|
|
266
|
+
|
|
267
|
+
if (!slot.outlet || !spans)
|
|
268
|
+
return false
|
|
269
|
+
|
|
270
|
+
b.add(`${helper}(${PROPS}.`).addSource(slot.outlet.name)
|
|
271
|
+
|
|
272
|
+
for (const span of spans)
|
|
273
|
+
b.add(', () => ').addSource(span)
|
|
274
|
+
|
|
275
|
+
b.add(')')
|
|
276
|
+
|
|
277
|
+
return true
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// The parameters of a block or a snippet about to be written, which the rewrite reads through accessors where a component renders again.
|
|
281
|
+
export function markBlock(sites: Sites, b: CodeBuilder, src: string, params: Span, staticLoops?: boolean): void {
|
|
282
|
+
if (!staticLoops && accessorParameters(src.slice(params.start, params.end)))
|
|
283
|
+
sites.blocks.add(b.code.length)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const BARE_PARAMETER = /^[\p{ID_Start}$_][\p{ID_Continue}$\u200C\u200D]*$/u
|
|
287
|
+
|
|
288
|
+
// A block's parameters and its arrow: one plain name is written bare, so a rule about the parentheses around it, which kay would otherwise be the one to write, has none to report.
|
|
289
|
+
export function addBlockParameters(sites: Sites, b: CodeBuilder, src: string, params: Span, staticLoops?: boolean): void {
|
|
290
|
+
const bare = BARE_PARAMETER.test(src.slice(params.start, params.end))
|
|
291
|
+
|
|
292
|
+
b.add(bare ? '' : '(')
|
|
293
|
+
markBlock(sites, b, src, params, staticLoops)
|
|
294
|
+
b.addSource(params).add(bare ? ' => ' : ') => ')
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// What a block or a snippet renders, its plain parameters read where they are used (`param` is the helper), so one a `followed` draw follows gives the current item.
|
|
298
|
+
export function addParameterReads(b: CodeBuilder, names: { name: string, position: number }[] | undefined, param: string, render: () => void): void {
|
|
299
|
+
if (names)
|
|
300
|
+
b.add(`(${names.map(({ name, position }) => `${name} = ${param}(${name}${position === 0 ? '' : `, ${position}`}), `).join('')}`)
|
|
301
|
+
|
|
302
|
+
render()
|
|
303
|
+
|
|
304
|
+
if (names)
|
|
305
|
+
b.add(')')
|
|
306
|
+
}
|
|
307
|
+
|
|
173
308
|
// What a `slot` renders: the component's children, or the block prop it names, called with its arguments when it has some.
|
|
174
309
|
export function slotValue(b: CodeBuilder, slot: ElementNode): void {
|
|
175
310
|
if (!slot.outlet) {
|
|
@@ -224,6 +359,33 @@ export function addKeyParameters(b: CodeBuilder, node: ForNode, src: string): vo
|
|
|
224
359
|
b.add(')')
|
|
225
360
|
}
|
|
226
361
|
|
|
362
|
+
// What follows the fallback in the server's `loop` call: how an island's rows read their item, or, in a page or a static component, the key of a keyed loop (`rowKey` when it names none), which an island in a row with no `key` or `id` of its own takes its identity from.
|
|
363
|
+
export function addLoopTail(b: CodeBuilder, node: ForNode, src: string, accessorLoops: boolean, rowKey: string): void {
|
|
364
|
+
const keying = rowKeying(node, src)
|
|
365
|
+
const keyed = !accessorLoops && keying.kind !== 'positional'
|
|
366
|
+
|
|
367
|
+
if (!node.fallback && (accessorLoops || keyed))
|
|
368
|
+
b.add(', undefined')
|
|
369
|
+
|
|
370
|
+
if (accessorLoops)
|
|
371
|
+
b.add(keying.kind === 'positional' ? ', "positional"' : ', "keyed"')
|
|
372
|
+
else if (keyed)
|
|
373
|
+
b.add(', undefined')
|
|
374
|
+
|
|
375
|
+
if (!keyed)
|
|
376
|
+
return
|
|
377
|
+
|
|
378
|
+
b.add(', ')
|
|
379
|
+
|
|
380
|
+
if (keying.kind === 'expression') {
|
|
381
|
+
addKeyParameters(b, node, src)
|
|
382
|
+
b.add(' => (').addSource(keying.key).add(')')
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
b.add(rowKey)
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
227
389
|
// Whether an expression names `name` as a variable: not as a property after a dot, nor inside a longer name.
|
|
228
390
|
function readsName(expression: string, name: string): boolean {
|
|
229
391
|
return new RegExp(`(?<![\\w$.])${name.replaceAll('$', '\\$')}(?![\\w$])`).test(expression)
|
package/src/compiler/jsx.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { ElementShape } from './plan'
|
|
|
4
4
|
import type { Sites } from './rewrite-types'
|
|
5
5
|
import { bindsOwnProp, failedBlockOf, handedParts, isSlot, partAttribute, serverIslandOf } from './ast'
|
|
6
6
|
import { isFunctionAt, sentParts } from './expression'
|
|
7
|
-
import { addConstValue, addKeyParameters, addRowItem, contentOf, rowKeying, slotValue, usedProps } from './items'
|
|
7
|
+
import { addBlockParameters, addConstValue, addKeyParameters, addRowItem, contentOf, markBlock, rowKeying, slotValue, usedProps } from './items'
|
|
8
8
|
import { addMergedClass, addShorthandClass, quoteAttribute } from './jsx-class'
|
|
9
9
|
import { addFieldName, addInheritedName, addWrittenField } from './jsx-field'
|
|
10
10
|
import { DYNAMIC_TAG } from './mapping'
|
|
@@ -40,7 +40,6 @@ const JSX_TEXT_ESCAPES = /[{}<>]|[ \t]{2,}/g
|
|
|
40
40
|
// Attributes rendered as markup rather than text.
|
|
41
41
|
const MARKUP_ATTRIBUTES = new Set(['innerHTML', 'srcdoc'])
|
|
42
42
|
const EVENT_HANDLER = /^on[A-Z]/
|
|
43
|
-
const PLAIN_PARAMETER = /^[\p{ID_Start}$_][\p{ID_Continue}$\u200C\u200D]*$/u
|
|
44
43
|
|
|
45
44
|
// A template as JSX: what the analysis pass reads for scope, and `kay check` type-checks.
|
|
46
45
|
export class JsxTemplate {
|
|
@@ -179,7 +178,9 @@ export class JsxTemplate {
|
|
|
179
178
|
b.wrap(node, () => {
|
|
180
179
|
b.add('const ')
|
|
181
180
|
this.sites.snippets.add(b.code.length)
|
|
182
|
-
b.addSource(node.name).add(' = (')
|
|
181
|
+
b.addSource(node.name).add(' = (')
|
|
182
|
+
markBlock(this.sites, b, this.src, node.params, this.options.staticLoops)
|
|
183
|
+
b.addSource(node.params).add('): KayJSX.Element => (')
|
|
183
184
|
this.emitRoot(node.children)
|
|
184
185
|
b.add(');')
|
|
185
186
|
})
|
|
@@ -630,11 +631,8 @@ export class JsxTemplate {
|
|
|
630
631
|
|
|
631
632
|
b.add(' ').addSource(slot.name).add('={')
|
|
632
633
|
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
b.addSource(slot.params).add(' => ')
|
|
636
|
-
else if (slot.params)
|
|
637
|
-
b.add('(').addSource(slot.params).add(') => ')
|
|
634
|
+
if (slot.params)
|
|
635
|
+
addBlockParameters(this.sites, b, this.src, slot.params, this.options.staticLoops)
|
|
638
636
|
|
|
639
637
|
this.emitRoot(slot.children)
|
|
640
638
|
b.add('}')
|
package/src/compiler/lints.ts
CHANGED
|
@@ -476,7 +476,7 @@ export const LINTS = {
|
|
|
476
476
|
site: true,
|
|
477
477
|
summary: 'A root Dockerfile whose `FROM oven/bun:<version>` is older than the Bun kay needs (`engines.bun`), so the image fails to build once kay moves up, as `kay upgrade` does. `--fix` moves the tag to the oldest Bun kay runs on, dropping a digest pin that would keep the old image; a tag that is no full version, as `1` or `latest`, is left alone.',
|
|
478
478
|
wrong: 'FROM oven/bun:1.0.0 AS build',
|
|
479
|
-
right: 'FROM oven/bun:1.4.
|
|
479
|
+
right: 'FROM oven/bun:1.4.5 AS build',
|
|
480
480
|
},
|
|
481
481
|
'untyped-socket': {
|
|
482
482
|
level: 'warn',
|
package/src/compiler/markup.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { addHandler } from './event-modifiers'
|
|
|
15
15
|
import { decodeEntities, isFunctionAt } from './expression'
|
|
16
16
|
import { closeHandParts, handedItem, handsEntry, openClassList, writesTogether, writtenParts } from './hand-parts'
|
|
17
17
|
import { IMAGE_OPTIONS, imageDefaults as imageDefaultsOf, imageOptions, placeholderOff, writeImageArguments } from './image-attributes'
|
|
18
|
-
import { contentOf } from './items'
|
|
18
|
+
import { accessorParameters, addParameterReads, contentOf } from './items'
|
|
19
19
|
import { controlId, fieldAttribute, inheritedAttributes, inheritedField } from './markup-fields'
|
|
20
20
|
import { PROPS } from './names'
|
|
21
21
|
import { addGroupChecked, bindingOf, classMergeOf, formDefaultsOf, groupValue, shapeOf, shorthandClass, staticAttributeText, valueBoundBy, writesAttribute } from './plan'
|
|
@@ -160,6 +160,9 @@ export abstract class TemplateWriter {
|
|
|
160
160
|
// The server hands over the markup itself, which an island reads its static slots off, and so draws a slot's ids once per read where the browser draws them once per insert; the browser sets the ids it binds, so they agree.
|
|
161
161
|
protected readonly lazySlots: boolean = false
|
|
162
162
|
|
|
163
|
+
// Whether a block's plain parameters are accessors: where the analysis read them as a loop's row is read, which a component that renders again does and a static one does not.
|
|
164
|
+
protected accessorBlocks = true
|
|
165
|
+
|
|
163
166
|
// The helper scoping a form's content: the server's also lets the fields in it refill, the browser's only gives them the form's messages.
|
|
164
167
|
protected readonly scoping: 'formScope' | 'issueScope' = 'formScope'
|
|
165
168
|
|
|
@@ -353,11 +356,15 @@ export abstract class TemplateWriter {
|
|
|
353
356
|
|
|
354
357
|
b.wrap(node, () => {
|
|
355
358
|
b.add('const ').addSource(node.name).add(' = (').addSource(node.params).add(') => ')
|
|
356
|
-
this.childrenValue(node.children)
|
|
359
|
+
this.readingParameters(node.params, () => this.childrenValue(node.children))
|
|
357
360
|
b.add('; ')
|
|
358
361
|
})
|
|
359
362
|
}
|
|
360
363
|
|
|
364
|
+
private readingParameters(params: Span, render: () => void): void {
|
|
365
|
+
addParameterReads(this.b, this.accessorBlocks ? accessorParameters(this.source(params)) : undefined, helperName('param'), render)
|
|
366
|
+
}
|
|
367
|
+
|
|
361
368
|
// A component's props: its attributes, then its slots and children, each read through a getter or a function so it renders where the component puts it.
|
|
362
369
|
// `except` is a slot that is no prop, as a server island's `@failed`.
|
|
363
370
|
protected componentProps(element: ElementNode, getters: boolean, except?: SlotNode): void {
|
|
@@ -376,7 +383,7 @@ export abstract class TemplateWriter {
|
|
|
376
383
|
|
|
377
384
|
if (slot.params) {
|
|
378
385
|
b.add(`${JSON.stringify(this.source(slot.name))}: (`).addSource(slot.params).add(') => ')
|
|
379
|
-
this.childrenValue(slot.children)
|
|
386
|
+
this.readingParameters(slot.params, () => this.childrenValue(slot.children))
|
|
380
387
|
}
|
|
381
388
|
else {
|
|
382
389
|
b.add(`get ${JSON.stringify(this.source(slot.name))}() { return `)
|
|
@@ -10,6 +10,8 @@ export type Rewrites = Map<number, Rewrite & { end: number }>
|
|
|
10
10
|
export interface Sites {
|
|
11
11
|
// Arrow parameters, and the index a loop declares in its block, that code which runs reads through a call: a loop's row and index, an `if`'s alias.
|
|
12
12
|
rows: Set<number>
|
|
13
|
+
// The start of the parameters of a slot block whose plain names are read through accessors, as a loop's row is.
|
|
14
|
+
blocks: Set<number>
|
|
13
15
|
// A template `const`'s pattern, true when code that runs reads its names as memos.
|
|
14
16
|
consts: Map<number, boolean>
|
|
15
17
|
// The `consts` among them that destructure a loop's item, which behave as the item of a plain loop does.
|
package/src/compiler/rewrite.ts
CHANGED
|
@@ -539,9 +539,11 @@ class RewriteFinder {
|
|
|
539
539
|
|
|
540
540
|
// A loop's row and index and an `if`'s alias track the current value, so code that runs reads them through a call.
|
|
541
541
|
private rows(path: Path<ArrowFunctionExpression>) {
|
|
542
|
+
const block = this.input.sites.blocks.has(path.node.params[0]?.start ?? -1)
|
|
543
|
+
|
|
542
544
|
for (const [index, param] of path.node.params.entries()) {
|
|
543
|
-
if (param.type === 'Identifier' && this.input.sites.rows.has(param.start))
|
|
544
|
-
this.rowBinding(path.scope, param.name, index === 0 ? loopKey(path, param.name) : undefined)
|
|
545
|
+
if (param.type === 'Identifier' && (block || this.input.sites.rows.has(param.start)))
|
|
546
|
+
this.rowBinding(path.scope, param.name, index === 0 && !block ? loopKey(path, param.name) : undefined)
|
|
545
547
|
}
|
|
546
548
|
}
|
|
547
549
|
|
|
@@ -10,6 +10,14 @@ export const ROUTE_LINTS = {
|
|
|
10
10
|
...LOOP_LINTS,
|
|
11
11
|
...LINK_LINTS,
|
|
12
12
|
...SCHEMA_LINTS,
|
|
13
|
+
'static-remote-image': {
|
|
14
|
+
level: 'warn',
|
|
15
|
+
group: 'performance',
|
|
16
|
+
site: true,
|
|
17
|
+
summary: 'On a `kay build --static` export, an island image the build cannot write files for: an `image(source, { width, height })` whose source is neither an imported image nor a literal URL, a literal URL on a host `assets.images.remote` does not list, a width or height that is no number literal, or an `aspect`, `quality` or `sizes` that is no literal. The export has no image route, so the browser links the original image, or asks for a file that does not exist. Give `image()` the URL as a literal on a listed host, with its size and options as literals, and the build fetches and resizes it. A best-effort read of the island\'s source. Reported by `kay build --static`.',
|
|
18
|
+
wrong: 'img({...image(post.cover, { width: 1200, height: 800 })} alt="")',
|
|
19
|
+
right: 'img({...image(\'https://cms.example.com/cover.jpg\', { width: 1200, height: 800 })} alt="")',
|
|
20
|
+
},
|
|
13
21
|
'text-size-colour': {
|
|
14
22
|
level: 'warn',
|
|
15
23
|
group: 'code',
|
|
@@ -68,5 +68,6 @@ export function nameDestructuredRows(file: KayFile): KayFile {
|
|
|
68
68
|
node.children.unshift({ kind: 'const', ...node.item, pattern: node.item, value: at, row })
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
// The facts cached on the file name its old nodes, so the clone gets its own.
|
|
72
|
+
return { ...file, template, facts: undefined }
|
|
72
73
|
}
|
package/src/config-shape.ts
CHANGED
|
@@ -76,7 +76,10 @@ const CONFIG: Record<keyof ConfigInput, Rule> = {
|
|
|
76
76
|
seo: bag(SEO),
|
|
77
77
|
robots: either(bool, bag(ROBOTS)),
|
|
78
78
|
sitemap: either(bool, bag(SITEMAP)),
|
|
79
|
-
head: list(obj)
|
|
79
|
+
head: list(bag({ tag: str, props: obj }, (entry, path, out) => {
|
|
80
|
+
if ('children' in entry)
|
|
81
|
+
out.push(`\`${path}.children\` is ignored: a tag's text goes in \`props.children\``)
|
|
82
|
+
})),
|
|
80
83
|
budget: bag(BUDGET),
|
|
81
84
|
entry: str,
|
|
82
85
|
security: either(bool, bag(SECURITY_KEYS)),
|
package/src/config.ts
CHANGED
|
@@ -57,8 +57,8 @@ export interface KayConfig extends AppHooks {
|
|
|
57
57
|
robots?: RobotsConfig | boolean | undefined
|
|
58
58
|
// How `/sitemap.xml` is kept, or `false` to serve none (and name none in `/robots.txt`).
|
|
59
59
|
sitemap?: SitemapConfig | boolean | undefined
|
|
60
|
-
// Tags every page's head carries, written as a `head` block compiles them: `[{ tag: 'script', props: { src: '/stats.js', defer: true } }]`, a tag's text as `children`. A layout or the page setting a tag of the same identity wins, and the content security policy lets their inline scripts and styles through.
|
|
61
|
-
head?: HeadTag[] | undefined
|
|
60
|
+
// Tags every page's head carries, written as a `head` block compiles them: `[{ tag: 'script', props: { src: '/stats.js', defer: true } }]`, a tag's text as `props.children`. A layout or the page setting a tag of the same identity wins, and the content security policy lets their inline scripts and styles through.
|
|
61
|
+
head?: (HeadTag & { children?: never })[] | undefined
|
|
62
62
|
// The most a page may load, compressed: `{ js: '20kb', css: '10kb' }` for its scripts and stylesheets, `weight: '500kb'` for the whole page and `grade: 'C'` for the worst EcoIndex grade it may have. `kay build` reports each page over it with `over-budget`, naming the modules weighing most for `js` and `css`; a key left out sets no limit, and with none nothing is checked.
|
|
63
63
|
budget?: BudgetConfig | undefined
|
|
64
64
|
// The server entry, relative to the root, `src/server.ts` by default: what `kay dev`, `start`, `render`, `check` and `build` run and type against, unless `--entry` names another. With no file there, kay's own server of the pages.
|
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 orbit\'s near-black palette
|
|
7
|
+
tailwind: { description: 'Tailwind over orbit\'s near-black palette', 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: [] },
|
package/src/new.ts
CHANGED
|
@@ -130,15 +130,27 @@ export async function originHost(dir: string): Promise<Host | undefined> {
|
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
//
|
|
133
|
+
// What a fresh clone of a new forge repo holds besides `.git`: a folder with only these is scaffolded in place. A file the template writes too (`.gitignore`) is replaced, one it does not (`README.md`, the licence) is kept.
|
|
134
|
+
const FRESH_REPO_ENTRY = /^(?:\.git|README(?:\.\w+)?|LICENSE(?:[-.].+)?|\.gitignore|\.gitattributes)$/i
|
|
135
|
+
|
|
136
|
+
// Refuses a target `kay new` would write over: a file, or a directory that holds anything but what a fresh clone does.
|
|
134
137
|
export async function refuseTarget(target: string): Promise<void> {
|
|
135
138
|
const dir = resolve(target)
|
|
136
139
|
|
|
137
|
-
if (existsSync(dir)
|
|
140
|
+
if (!existsSync(dir))
|
|
141
|
+
return
|
|
142
|
+
|
|
143
|
+
if (!statSync(dir).isDirectory())
|
|
138
144
|
throw new Error(`kay: ${target} is not a directory`)
|
|
139
145
|
|
|
140
|
-
|
|
141
|
-
|
|
146
|
+
const inTheWay = (await readdir(dir)).filter(entry => !FRESH_REPO_ENTRY.test(entry)).sort()
|
|
147
|
+
|
|
148
|
+
if (inTheWay.length === 0)
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
const shown = inTheWay.slice(0, 5).join(', ')
|
|
152
|
+
|
|
153
|
+
throw new Error(`kay: ${target} is not empty: ${shown}${inTheWay.length > 5 ? `, and ${inTheWay.length - 5} more` : ''} in the way`)
|
|
142
154
|
}
|
|
143
155
|
|
|
144
156
|
// Whether `kay new` adds the feature through `kay add` after scaffolding: a library's, but for a templated one whose files the templates write.
|
package/src/project.ts
CHANGED
|
@@ -185,8 +185,8 @@ function customWidths(widths: number[] | undefined): number[] | undefined {
|
|
|
185
185
|
}
|
|
186
186
|
|
|
187
187
|
// How every page renders, served or prerendered alike.
|
|
188
|
-
export function renderOptionsOf(config:
|
|
189
|
-
return { ...pickConfig(config, RENDER_CONFIG), dev: mode.dev, devTools: mode.devRoutes, serverTiming: config.ops?.observe?.serverTiming ?? mode.dev, sameTabLinks: sameTabOn(config), linkSchemes: linkSchemes(config), fontCss: config.client?.saveData === false ? undefined : built.fontCss, islandPreloads: config.client?.saveData === false ? undefined : built.islandPreloads, head: built.head, base: basePath(config.base), maxDensity: built.maxDensity, imageWidths: customWidths(config.assets?.images?.widths), resolvedImages: config.assets?.images?.resolve !== undefined, offline: registersWorker(config, mode), installOffer: pushListed(config), editor: built.editor, contexts: built.contexts }
|
|
188
|
+
export function renderOptionsOf(config: LoadedConfig, mode: Pick<Mode, 'dev' | 'devRoutes'>, built: { head: SeoHead | undefined, maxDensity: number, editor?: string | undefined, contexts?: string[] | undefined, fontCss?: string | undefined, islandPreloads?: RenderOptions['islandPreloads'] }): RenderOptions {
|
|
189
|
+
return { ...pickConfig(config, RENDER_CONFIG), publicEnv: config.publicEnv === true, dev: mode.dev, devTools: mode.devRoutes, serverTiming: config.ops?.observe?.serverTiming ?? mode.dev, sameTabLinks: sameTabOn(config), linkSchemes: linkSchemes(config), fontCss: config.client?.saveData === false ? undefined : built.fontCss, islandPreloads: config.client?.saveData === false ? undefined : built.islandPreloads, head: built.head, base: basePath(config.base), maxDensity: built.maxDensity, imageWidths: customWidths(config.assets?.images?.widths), resolvedImages: config.assets?.images?.resolve !== undefined, offline: registersWorker(config, mode), installOffer: pushListed(config), editor: built.editor, contexts: built.contexts }
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
// A project as every entry point takes it: where it is, its config, where it keeps each part and how it runs.
|
|
@@ -207,7 +207,7 @@ export function writesFrench(config: Pick<KayConfig, 'lang' | 'i18n' | 'client'>
|
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
// The browser build a config asks for. `kay build`'s (`build` given) minifies unless the config says not to; one a server or a test makes on demand does only when the config says so.
|
|
210
|
-
export function clientOptionsOf(config: LoadedConfig, mode: Pick<Mode, 'dev' | 'underKayDev'>, build?: Pick<ClientOptions, 'exported' | 'report'>): ClientOptions {
|
|
210
|
+
export function clientOptionsOf(config: LoadedConfig, mode: Pick<Mode, 'dev' | 'underKayDev'>, build?: Pick<ClientOptions, 'exported' | 'staticImages' | 'report'>): ClientOptions {
|
|
211
211
|
return {
|
|
212
212
|
...pickConfig(config, CLIENT_BUILD_KEYS),
|
|
213
213
|
minify: config.minify ?? build !== undefined,
|
|
@@ -4,7 +4,7 @@ import type { JSX } from './jsx'
|
|
|
4
4
|
import type { Html } from './rendered'
|
|
5
5
|
import { AWAIT_FAILED, AWAIT_FAILED_KEY } from './await-failed'
|
|
6
6
|
import { escape } from './escape'
|
|
7
|
-
import { awaitNotes, child } from './html'
|
|
7
|
+
import { awaitNotes, child, keepLoopKeys } from './html'
|
|
8
8
|
import { insideIsland } from './island.server'
|
|
9
9
|
import { getOwner, runWithOwner } from './reactive'
|
|
10
10
|
import { html } from './rendered'
|
|
@@ -195,10 +195,12 @@ export function Await<T>(props: AwaitChannel<T>): JSX.Element {
|
|
|
195
195
|
// Rendered once the value settles, under the component that declared it, for what it provided.
|
|
196
196
|
const parent = getOwner()
|
|
197
197
|
const failed = props.failed
|
|
198
|
+
const inLoop = keepLoopKeys()
|
|
199
|
+
const resume = <R>(render: () => R) => inLoop(() => following(notes, render))
|
|
198
200
|
|
|
199
201
|
const fallback = props.fallback
|
|
200
202
|
|
|
201
|
-
collecting.blocks.push({ id, value, ready: (settled: never) => runWithOwner(parent, () =>
|
|
203
|
+
collecting.blocks.push({ id, value, ready: (settled: never) => runWithOwner(parent, () => resume(() => props.ready(settled))), failed: failed && (error => runWithOwner(parent, () => resume(() => failed(error)))), fallback: fallback && (error => runWithOwner(parent, () => resume(() => fallback(error)))), alert: props.alert ? error => runWithOwner(parent, () => resume(() => props.alert!(error))) : awaitFailed, site: failed ? undefined : new Error('await') })
|
|
202
204
|
// The streamed block replaces this one whole, so an `await` inside it renders its own pending block and streams nothing.
|
|
203
205
|
deferred = undefined
|
|
204
206
|
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import type { DevRequest } from '../server/dev'
|
|
1
2
|
import type { DevTab, DevTool } from './dev-tool'
|
|
2
3
|
|
|
3
4
|
// The Env tab's code, fetched at once as its badge counts the variables that look like secrets, with the tool's shared helpers from `tool.kit`.
|
|
4
5
|
const ENV_URL = '/_kay/env.js'
|
|
6
|
+
// Copied from dev-tool-constants.ts, as a tab module imports nothing.
|
|
7
|
+
const REQUEST = 'kay-dev-request'
|
|
5
8
|
const MODULE_START = 'export default '
|
|
6
9
|
const BUILT_EVENT = 'kay:dev-built'
|
|
7
10
|
|
|
@@ -54,6 +57,16 @@ export default function envTab(tool: DevTool, tab: DevTab): () => void {
|
|
|
54
57
|
const { copyButton, entry, filterable, ownFetch, signal, toolbar } = tool.kit
|
|
55
58
|
const win = tool.root.ownerDocument.defaultView!
|
|
56
59
|
let reread: () => void = () => undefined
|
|
60
|
+
// False only when the page's request block says so; a page with none, or an unreadable one, is asked.
|
|
61
|
+
const served = (): boolean => {
|
|
62
|
+
try {
|
|
63
|
+
return (JSON.parse(win.document.getElementById(REQUEST)?.textContent ?? 'null') as DevRequest | null)?.publicEnv !== false
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
let knew = served()
|
|
57
70
|
const content = tool.make('div', 'stack')
|
|
58
71
|
const bar = toolbar('Filter by name or value', () => bar.apply(content))
|
|
59
72
|
let asked = 0
|
|
@@ -63,10 +76,13 @@ export default function envTab(tool: DevTool, tab: DevTab): () => void {
|
|
|
63
76
|
let failure: string | undefined
|
|
64
77
|
|
|
65
78
|
try {
|
|
66
|
-
|
|
79
|
+
// The route is there only when a field is `s.Public`, which the page's request block says: asking an app without one logs a 404.
|
|
80
|
+
knew = served()
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
|
|
82
|
+
const response = knew ? await ownFetch(win, ENV_URL, { cache: 'no-store' }) : undefined
|
|
83
|
+
|
|
84
|
+
// Any answer but a 404 is a failure to read it.
|
|
85
|
+
if (response && response.status !== 404) {
|
|
70
86
|
if (!response.ok)
|
|
71
87
|
throw new Error(`the server answered ${response.status}`)
|
|
72
88
|
|
|
@@ -129,6 +145,15 @@ export default function envTab(tool: DevTool, tab: DevTab): () => void {
|
|
|
129
145
|
reread = () => void read()
|
|
130
146
|
tab.panel.append(bar.element, content)
|
|
131
147
|
win.document.addEventListener(BUILT_EVENT, reread, { signal })
|
|
148
|
+
|
|
149
|
+
// A restart that adds or drops the first or last public field brings the page a new block once it is patched in, after the built event.
|
|
150
|
+
const blocks = new MutationObserver(() => {
|
|
151
|
+
if (served() !== knew)
|
|
152
|
+
reread()
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
blocks.observe(win.document.head, { childList: true })
|
|
156
|
+
signal.addEventListener('abort', () => blocks.disconnect())
|
|
132
157
|
reread()
|
|
133
158
|
|
|
134
159
|
return () => reread()
|
package/src/runtime/dom-loop.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Unit } from './dom'
|
|
2
2
|
import { animated, blockIn, claiming, entered, firstOf, insertUnit, lastOf, Region, release, removeUnit, rowUnit, swap, units, within } from './dom'
|
|
3
3
|
import { createSignal, onCleanup, renderEffect, root, unrecorded, untrack } from './reactive'
|
|
4
|
+
import { rowKey } from './row-key'
|
|
4
5
|
|
|
5
6
|
declare const KAY_FRENCH: true | undefined
|
|
6
7
|
|
|
@@ -14,10 +15,7 @@ interface Row {
|
|
|
14
15
|
dispose: () => void
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
export function rowKey(item: unknown): unknown {
|
|
19
|
-
return typeof item === 'object' && item !== null && 'id' in item ? item.id : item
|
|
20
|
-
}
|
|
18
|
+
export { rowKey }
|
|
21
19
|
|
|
22
20
|
const entryKey = (entry: unknown) => (entry as [unknown, unknown])[0]
|
|
23
21
|
|
package/src/runtime/dom.ts
CHANGED
|
@@ -19,7 +19,7 @@ declare const KAY_BROWSER: true | undefined
|
|
|
19
19
|
declare const KAY_FRENCH: true | undefined
|
|
20
20
|
|
|
21
21
|
export { holdSubmits, listenSubmit, listenSubmitWith, listenWith } from './form-hold'
|
|
22
|
-
export { renderEffect as effect, memo, untrack } from './reactive'
|
|
22
|
+
export { renderEffect as effect, memo, param, slotCall, untrack } from './reactive'
|
|
23
23
|
export { transition } from './transition'
|
|
24
24
|
export { checkedMarkup, componentClass, handParts, merge, omit, partClass, partHanded, partProps, spreadAttributes, spreadWith } from './values'
|
|
25
25
|
|
package/src/runtime/html.ts
CHANGED
|
@@ -31,10 +31,11 @@ export { formKey } from './action'
|
|
|
31
31
|
export { escape, escapeAttribute } from './escape'
|
|
32
32
|
export { head } from './head'
|
|
33
33
|
export { loadScope } from './load-scope'
|
|
34
|
-
export { memo, untrack } from './reactive'
|
|
34
|
+
export { memo, param, untrack } from './reactive'
|
|
35
35
|
export { html, isRendered } from './rendered'
|
|
36
|
-
|
|
37
36
|
export type { Html } from './rendered'
|
|
37
|
+
|
|
38
|
+
export { rowKey } from './row-key'
|
|
38
39
|
export { checkedMarkup, componentClass, handParts, merge, omit, partClass, partHanded, partProps, spreadWith } from './values'
|
|
39
40
|
|
|
40
41
|
// Leads the markup of a `try` block the server rendered its `catch` block for.
|
|
@@ -572,7 +573,7 @@ export function show<T>(value: T, render: (value: () => Truthy<T>) => string, fa
|
|
|
572
573
|
|
|
573
574
|
// A `for`: each row's markup, or the fallback's when there is nothing to list.
|
|
574
575
|
// In an island a row reads its item, and a keyed row its index, through functions, as the browser build of the same template does.
|
|
575
|
-
export function loop<T>(each: Iterable<T> | null | undefined | false, render: (item: T | (() => T), index: number | (() => number)) => string, fallback?: () => string, accessors?: 'keyed' | 'positional'): string {
|
|
576
|
+
export function loop<T>(each: Iterable<T> | null | undefined | false, render: (item: T | (() => T), index: number | (() => number)) => string, fallback?: () => string, accessors?: 'keyed' | 'positional', key?: (item: T, index: number) => unknown): string {
|
|
576
577
|
if (each == null || each === false)
|
|
577
578
|
return fallback?.() ?? ''
|
|
578
579
|
|
|
@@ -584,16 +585,61 @@ export function loop<T>(each: Iterable<T> | null | undefined | false, render: (i
|
|
|
584
585
|
return fieldMarks.marking === undefined ? markup : markedList(each, markup)
|
|
585
586
|
}
|
|
586
587
|
|
|
587
|
-
return fieldMarks.marking === undefined ? rows(list, render, accessors) : markedList(each, rows(list, render, accessors))
|
|
588
|
+
return fieldMarks.marking === undefined ? rows(list, render, accessors, key) : markedList(each, rows(list, render, accessors, key))
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// The keys of the keyed loops being drawn, outermost first, where an island with no `key` or `id` of its own takes its identity from, so a swap pairs it with its own copy when a sibling row goes.
|
|
592
|
+
const loopKeys: (string | undefined)[] = []
|
|
593
|
+
|
|
594
|
+
export function loopIdentity(): string | undefined {
|
|
595
|
+
if (loopKeys.length === 0 || loopKeys.includes(undefined))
|
|
596
|
+
return undefined
|
|
597
|
+
|
|
598
|
+
return loopKeys.length === 1 ? loopKeys[0] : JSON.stringify(loopKeys)
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Renders again, later, with the keyed loops it is drawn in: an `await` block settles after its row was drawn, and an island in it still takes the row's key as its identity.
|
|
602
|
+
export function keepLoopKeys(): <T>(render: () => T) => T {
|
|
603
|
+
const kept = [...loopKeys]
|
|
604
|
+
|
|
605
|
+
return (render) => {
|
|
606
|
+
const outer = loopKeys.splice(0, loopKeys.length, ...kept)
|
|
607
|
+
|
|
608
|
+
try {
|
|
609
|
+
return render()
|
|
610
|
+
}
|
|
611
|
+
finally {
|
|
612
|
+
loopKeys.splice(0, loopKeys.length, ...outer)
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function drawRow<T>(render: (item: T | (() => T), index: number | (() => number)) => string, accessors: 'keyed' | 'positional' | undefined, item: T, index: number): string {
|
|
618
|
+
return accessors === undefined ? render(item, index) : render(() => item, accessors === 'keyed' ? () => index : index)
|
|
588
619
|
}
|
|
589
620
|
|
|
590
|
-
function rows<T>(each: readonly T[], render: (item: T | (() => T), index: number | (() => number)) => string, accessors?: 'keyed' | 'positional'): string {
|
|
621
|
+
function rows<T>(each: readonly T[], render: (item: T | (() => T), index: number | (() => number)) => string, accessors?: 'keyed' | 'positional', key?: (item: T, index: number) => unknown): string {
|
|
591
622
|
let markup = ''
|
|
592
623
|
|
|
593
624
|
for (let index = 0; index < each.length; index++) {
|
|
594
625
|
const item = each[index]!
|
|
595
626
|
|
|
596
|
-
|
|
627
|
+
if (key === undefined) {
|
|
628
|
+
markup += drawRow(render, accessors, item, index)
|
|
629
|
+
|
|
630
|
+
continue
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const value = key(item, index)
|
|
634
|
+
|
|
635
|
+
loopKeys.push(typeof value === 'string' || typeof value === 'number' ? String(value) : undefined)
|
|
636
|
+
|
|
637
|
+
try {
|
|
638
|
+
markup += drawRow(render, accessors, item, index)
|
|
639
|
+
}
|
|
640
|
+
finally {
|
|
641
|
+
loopKeys.pop()
|
|
642
|
+
}
|
|
597
643
|
}
|
|
598
644
|
|
|
599
645
|
return markup
|