@barefootjs/jsx 0.41.1 → 0.41.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.
- package/dist/adapters/attr-plan.d.ts +8 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +19 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/inherited-const-server-read.d.ts +37 -0
- package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
- package/dist/adapters/loop-row-type.d.ts +18 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +8 -2
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
- package/dist/adapters/unapplied-prop-default.d.ts +34 -0
- package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
- package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
- package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +4 -22
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/caller-value-reach.d.ts +57 -0
- package/dist/caller-value-reach.d.ts.map +1 -0
- package/dist/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2685 -1381
- package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/component-scope.d.ts +13 -2
- package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/local-call-body.d.ts +112 -0
- package/dist/local-call-body.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +10 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-default-resolution.d.ts +52 -0
- package/dist/prop-default-resolution.d.ts.map +1 -0
- package/dist/props-binding.d.ts +15 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/server-read-walk.d.ts +66 -0
- package/dist/server-read-walk.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +24 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/static-const-member.d.ts +92 -0
- package/dist/static-const-member.d.ts.map +1 -0
- package/dist/static-literal.d.ts +21 -1
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/computed-const.test.ts +7 -1
- package/src/__tests__/const-identity-child-prop.test.ts +127 -0
- package/src/__tests__/inherited-const-server-read.test.ts +116 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
- package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
- package/src/__tests__/local-call-body.test.ts +101 -0
- package/src/__tests__/loop-branch-bare-expression-reactive-text.test.ts +212 -56
- package/src/__tests__/nested-loop-conditional.test.ts +16 -16
- package/src/__tests__/prop-default-resolution.test.ts +185 -0
- package/src/__tests__/props-spread-shadowed.test.ts +113 -0
- package/src/__tests__/server-read-walk.test.ts +125 -0
- package/src/__tests__/ssr-defaults.test.ts +67 -7
- package/src/__tests__/static-literal.test.ts +47 -2
- package/src/__tests__/static-record-literal-array.test.ts +82 -11
- package/src/adapters/attr-plan.ts +12 -1
- package/src/adapters/component-props.ts +2 -1
- package/src/adapters/computed-const.ts +108 -12
- package/src/adapters/dsl-template-adapter.ts +17 -4
- package/src/adapters/inherited-const-server-read.ts +174 -0
- package/src/adapters/loop-row-type.ts +69 -2
- package/src/adapters/non-primitive-equality.ts +17 -2
- package/src/adapters/unapplied-prop-default.ts +83 -0
- package/src/adapters/uninlinable-seed-call.ts +62 -0
- package/src/analyzer.ts +43 -3
- package/src/augment-inherited-props.ts +4 -49
- package/src/caller-value-reach.ts +436 -0
- package/src/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +10 -5
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +53 -68
- package/src/ir-to-client-js/component-scope.ts +16 -2
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/html-template.ts +24 -4
- package/src/ir-to-client-js/index.ts +2 -2
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/jsx-to-ir.ts +40 -2
- package/src/local-call-body.ts +191 -0
- package/src/nullable-signal-attr.ts +39 -17
- package/src/prop-default-resolution.ts +195 -0
- package/src/props-binding.ts +16 -0
- package/src/server-read-walk.ts +174 -0
- package/src/ssr-defaults.ts +93 -10
- package/src/ssr-seed-plan.ts +104 -18
- package/src/static-const-member.ts +243 -0
- package/src/static-literal.ts +55 -1
- package/src/types.ts +63 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BF101 for a computed destructure default no template can apply (#3725).
|
|
3
|
+
*
|
|
4
|
+
* A backend applies a computed default when it renders, from the expression
|
|
5
|
+
* the default stands for once the consts and functions it reads are
|
|
6
|
+
* resolved (`appliedPropDefaults`). A default reading a component
|
|
7
|
+
* declaration with no such value (a signal, a memo, a `let`, a reassigned
|
|
8
|
+
* const, a function with parameters or statements, a const declared after
|
|
9
|
+
* the destructure) leaves nothing to apply, so an omitted prop would render
|
|
10
|
+
* empty where JS renders the default.
|
|
11
|
+
*
|
|
12
|
+
* Only a prop the server render reads is refused (`walkServerReads`): in a
|
|
13
|
+
* position, through a signal seed, a memo body or a const, or through
|
|
14
|
+
* another prop's default. A prop only an event handler reads (`{ onDone =
|
|
15
|
+
* handleDone }`) never renders its default on the server.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { freeIdentifiers } from '../expression-parser.ts'
|
|
19
|
+
import { walkServerReads, type ServerReadWalkOptions } from '../server-read-walk.ts'
|
|
20
|
+
import { appliedPropDefaults, type PropDefaultApplication } from '../ssr-seed-plan.ts'
|
|
21
|
+
import type { CompilerError, ComponentIR } from '../types.ts'
|
|
22
|
+
|
|
23
|
+
/** Where {@link refuseUnappliedPropDefaults} records its errors. */
|
|
24
|
+
export interface UnappliedPropDefaultSink {
|
|
25
|
+
readonly errors: CompilerError[]
|
|
26
|
+
readonly componentName: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Record BF101 for each prop the server render reads whose computed default no template can apply. */
|
|
30
|
+
export function refuseUnappliedPropDefaults(ir: ComponentIR, sink: UnappliedPropDefaultSink): void {
|
|
31
|
+
const { defaults } = appliedPropDefaults(ir.metadata)
|
|
32
|
+
if (![...defaults.values()].some(d => d.blocked.length > 0)) return
|
|
33
|
+
const read = propsReadFrom(ir, defaults)
|
|
34
|
+
// A component renders on the server whatever directive its prop carries.
|
|
35
|
+
const readByChild = propsReadFrom(ir, defaults, position => position.role === 'prop')
|
|
36
|
+
for (const param of ir.metadata.propsParams) {
|
|
37
|
+
const blocked = read.has(param.name) ? defaults.get(param.name)!.blocked : []
|
|
38
|
+
if (blocked.length === 0) continue
|
|
39
|
+
const names = blocked.map(name => `'${name}'`).join(', ')
|
|
40
|
+
const deferrable = !readByChild.has(param.name)
|
|
41
|
+
sink.errors.push({
|
|
42
|
+
code: 'BF101',
|
|
43
|
+
severity: 'error',
|
|
44
|
+
message:
|
|
45
|
+
`The default of prop '${param.name}' reads ${names}, which no template can compute when it applies the default: ` +
|
|
46
|
+
`only a prop, or a 'const' or a function returning one expression that reads no signal, memo, 'let' or reassigned ` +
|
|
47
|
+
`value, is read there. The server render would show '${param.name}' empty where JS shows the default.`,
|
|
48
|
+
// A body destructure's own binding, or the component for a parameter default.
|
|
49
|
+
loc: defaults.get(param.name)!.loc ?? ir.root.loc,
|
|
50
|
+
suggestion: deferrable
|
|
51
|
+
? {
|
|
52
|
+
message: `Compute the default from props and consts only, or defer the reads of '${param.name}' to the client with /* @client */.`,
|
|
53
|
+
escape: [{ kind: 'client-directive' }],
|
|
54
|
+
}
|
|
55
|
+
: { message: `Compute the default from props and consts only.` },
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The props with a computed default that the server render reads, starting
|
|
62
|
+
* from the positions `startsAt` keeps (every position when unset): in a
|
|
63
|
+
* position, through a declaration it reads, or through another such prop's
|
|
64
|
+
* default.
|
|
65
|
+
*/
|
|
66
|
+
export function propsReadFrom(
|
|
67
|
+
ir: ComponentIR,
|
|
68
|
+
defaults: PropDefaultApplication['defaults'],
|
|
69
|
+
startsAt?: ServerReadWalkOptions['startsAt'],
|
|
70
|
+
): ReadonlySet<string> {
|
|
71
|
+
const read = new Set<string>()
|
|
72
|
+
const pending: string[] = []
|
|
73
|
+
const visitProp = (name: string): void => {
|
|
74
|
+
if (read.has(name) || !defaults.has(name)) return
|
|
75
|
+
read.add(name)
|
|
76
|
+
pending.push(name)
|
|
77
|
+
}
|
|
78
|
+
walkServerReads(ir, () => {}, { visitProp, startsAt })
|
|
79
|
+
for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
|
|
80
|
+
for (const free of freeIdentifiers(defaults.get(name)!.expr) ?? []) visitProp(free)
|
|
81
|
+
}
|
|
82
|
+
return read
|
|
83
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BF101 for a value the server render reads that calls a function a
|
|
3
|
+
* template cannot inline (#3721).
|
|
4
|
+
*
|
|
5
|
+
* A template computes a signal's or memo's SSR value from its seed or body
|
|
6
|
+
* (the SSR seed plan, the static SSR defaults, Go's constructor), and a
|
|
7
|
+
* const's from its initializer. A zero-argument call of a local function is
|
|
8
|
+
* read there as the expression the function returns (`localCall`'s
|
|
9
|
+
* `inline`); a call of any other local function (`refuse`: parameters, a
|
|
10
|
+
* body other than a single `return`) has no value a template can compute,
|
|
11
|
+
* so the value would render as its empty static default where JS renders
|
|
12
|
+
* the call's result. The same call written in markup is refused by the
|
|
13
|
+
* shared inlining (`inlineComputedConsts`); this is the refusal for the
|
|
14
|
+
* value positions that inlining never sees.
|
|
15
|
+
*
|
|
16
|
+
* Only a value the server render reads is refused, however it is reached
|
|
17
|
+
* (`walkServerReads`): through a signal, a memo, a const initializer or an
|
|
18
|
+
* inlined function, each name resolved lexically. A `/* @client *\/` read
|
|
19
|
+
* and a loop row named like a component value read none.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { walkServerReads, type ServerRead } from '../server-read-walk.ts'
|
|
23
|
+
import type { CompilerError, ComponentIR, SourceLocation } from '../types.ts'
|
|
24
|
+
|
|
25
|
+
/** Where {@link refuseUninlinableSeedCalls} records its errors. */
|
|
26
|
+
export interface UninlinableSeedCallSink {
|
|
27
|
+
readonly errors: CompilerError[]
|
|
28
|
+
readonly componentName: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Record BF101 for each declaration the server render reads that calls an uninlinable local function. */
|
|
32
|
+
export function refuseUninlinableSeedCalls(ir: ComponentIR, sink: UninlinableSeedCallSink): void {
|
|
33
|
+
walkServerReads(ir, (read) => {
|
|
34
|
+
for (const fn of read.refusedCalls) record(read.kind, read.name, fn, read.loc, sink)
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const SUBJECT: Record<ServerRead['kind'], (name: string) => string> = {
|
|
39
|
+
signal: name => `Signal '${name}' is seeded from`,
|
|
40
|
+
memo: name => `Memo '${name}' computes`,
|
|
41
|
+
const: name => `'${name}' is initialized from`,
|
|
42
|
+
function: name => `Function '${name}' returns`,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function record(kind: ServerRead['kind'], name: string, fn: string, loc: SourceLocation, sink: UninlinableSeedCallSink): void {
|
|
46
|
+
const what = SUBJECT[kind](name)
|
|
47
|
+
const message =
|
|
48
|
+
`${what} '${fn}()', a call of a function a template cannot compute: only a function that takes no parameters and ` +
|
|
49
|
+
`whose body is a single 'return <expr>' is read on the server, as the expression it returns. The server render ` +
|
|
50
|
+
`would show '${name}' empty where JS shows the call's result.`
|
|
51
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
52
|
+
sink.errors.push({
|
|
53
|
+
code: 'BF101',
|
|
54
|
+
severity: 'error',
|
|
55
|
+
message,
|
|
56
|
+
loc,
|
|
57
|
+
suggestion: {
|
|
58
|
+
message: `Make '${fn}' a single 'return <expr>' with no parameters, or defer the reads of '${name}' to the client with /* @client */.`,
|
|
59
|
+
escape: [{ kind: 'client-directive' }],
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
}
|
package/src/analyzer.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
import ts from 'typescript'
|
|
10
10
|
import { foldRenderReassignmentsInSource } from './fold-render-reassignments.ts'
|
|
11
11
|
import { CLIENT_DIRECTIVE_INTERIOR_RE, USE_CLIENT_DIRECTIVE, isUseClientDirectiveText } from './directives.ts'
|
|
12
|
-
import type { ImportSpecifier, TypeInfo, ParamInfo, PropDefaultPart, PropDefaultRead, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall, MemoInfo, SignalInfo } from './types.ts'
|
|
12
|
+
import type { ImportSpecifier, TypeInfo, ParamInfo, PropDefaultPart, PropDefaultRead, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation, SignalFactoryCall, MemoInfo, SignalInfo, ConstantInfo } from './types.ts'
|
|
13
13
|
import { CALLBACK_METHODS, parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr, stringifyParsedExpr, type ParsedExpr } from './expression-parser.ts'
|
|
14
14
|
import type { CallbackBodyAcceptor } from './adapters/interface.ts'
|
|
15
15
|
import { collectAssignmentTargets, rewriteBarePropRefs, splitScopedValueRefs } from './prop-rewrite.ts'
|
|
@@ -39,6 +39,7 @@ import { separateSignalPropBindings, programWithSeparatedSource } from './signal
|
|
|
39
39
|
import { isAssignmentTargetIdentifier } from './assignment-target.ts'
|
|
40
40
|
import { addSourceReplacement } from './strip-types.ts'
|
|
41
41
|
import { walkWithScope } from './scope/lexical-scope-walk.ts'
|
|
42
|
+
import { markIdentityBoundConstants, recordIdentityCandidate } from './const-identity.ts'
|
|
42
43
|
|
|
43
44
|
// =============================================================================
|
|
44
45
|
// TypeScript Program Creation
|
|
@@ -3429,6 +3430,8 @@ function collectFunction(
|
|
|
3429
3430
|
// rewrite (the failure mode behind #1130) preserves the modifier from IR
|
|
3430
3431
|
// rather than reconstructing it from text.
|
|
3431
3432
|
const isGenerator = !!node.asteriskToken
|
|
3433
|
+
// A body rewritten for branch-local names above no longer matches its tree.
|
|
3434
|
+
const returnParsed = plainReturnParsed(node, body, isJsxFunction, ctx)
|
|
3432
3435
|
|
|
3433
3436
|
ctx.localFunctions.push({
|
|
3434
3437
|
name,
|
|
@@ -3444,12 +3447,44 @@ function collectFunction(
|
|
|
3444
3447
|
isGenerator: isGenerator || undefined,
|
|
3445
3448
|
declarationKind: 'function',
|
|
3446
3449
|
isModule: _isModule || undefined,
|
|
3450
|
+
inComponentBody: inComponentBody(node, ctx),
|
|
3451
|
+
returnParsed,
|
|
3447
3452
|
isJsxFunction: isJsxFunction || undefined,
|
|
3448
3453
|
freeIdentifiers: extractFreeIdentifiersFromNode(node),
|
|
3449
3454
|
loc: getSourceLocation(node, ctx.sourceFile, ctx.filePath),
|
|
3450
3455
|
})
|
|
3451
3456
|
}
|
|
3452
3457
|
|
|
3458
|
+
/**
|
|
3459
|
+
* The tree of the expression a parameterless, non-async, non-generator
|
|
3460
|
+
* function whose body is one `return <expr>` statement returns; `undefined`
|
|
3461
|
+
* for any other function, a body rewritten from its source (`body`), or an
|
|
3462
|
+
* expression the parser does not support.
|
|
3463
|
+
*/
|
|
3464
|
+
function plainReturnParsed(
|
|
3465
|
+
node: ts.FunctionDeclaration,
|
|
3466
|
+
body: string,
|
|
3467
|
+
isJsxFunction: boolean,
|
|
3468
|
+
ctx: AnalyzerContext,
|
|
3469
|
+
): ParsedExpr | undefined {
|
|
3470
|
+
if (node.parameters.length > 0 || !node.body || isJsxFunction || node.asteriskToken) return undefined
|
|
3471
|
+
if (node.modifiers?.some(m => m.kind === ts.SyntaxKind.AsyncKeyword) || body !== ctx.getJS(node.body)) return undefined
|
|
3472
|
+
const [statement, ...rest] = node.body.statements
|
|
3473
|
+
if (!statement || rest.length > 0 || !ts.isReturnStatement(statement) || !statement.expression) return undefined
|
|
3474
|
+
const parsed = parseExpression(ctx.getJS(statement.expression))
|
|
3475
|
+
return parsed && parsed.kind !== 'unsupported' ? parsed : undefined
|
|
3476
|
+
}
|
|
3477
|
+
|
|
3478
|
+
/** `FunctionInfo.inComponentBody`: `true` when `node` is nested inside the component function. */
|
|
3479
|
+
function inComponentBody(node: ts.Node, ctx: AnalyzerContext): true | undefined {
|
|
3480
|
+
const component = ctx.componentNode
|
|
3481
|
+
if (!component) return undefined
|
|
3482
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
3483
|
+
if (current === component) return true
|
|
3484
|
+
}
|
|
3485
|
+
return undefined
|
|
3486
|
+
}
|
|
3487
|
+
|
|
3453
3488
|
// =============================================================================
|
|
3454
3489
|
// Constant Collection
|
|
3455
3490
|
// =============================================================================
|
|
@@ -4609,7 +4644,7 @@ function collectConstant(
|
|
|
4609
4644
|
? parseExpression(`(${value.trim()})`)
|
|
4610
4645
|
: undefined
|
|
4611
4646
|
|
|
4612
|
-
|
|
4647
|
+
const constant: ConstantInfo = {
|
|
4613
4648
|
name,
|
|
4614
4649
|
value,
|
|
4615
4650
|
parsed,
|
|
@@ -4633,7 +4668,9 @@ function collectConstant(
|
|
|
4633
4668
|
scope: isModule ? 'module' : 'init',
|
|
4634
4669
|
effect: 'pure',
|
|
4635
4670
|
},
|
|
4636
|
-
}
|
|
4671
|
+
}
|
|
4672
|
+
recordIdentityCandidate(constant, node.initializer, ctx.checker)
|
|
4673
|
+
ctx.localConstants.push(constant)
|
|
4637
4674
|
}
|
|
4638
4675
|
|
|
4639
4676
|
// =============================================================================
|
|
@@ -5333,6 +5370,9 @@ function validateContext(ctx: AnalyzerContext): void {
|
|
|
5333
5370
|
// downstream inlining never re-embeds their stale initializer text (#2910).
|
|
5334
5371
|
markMutatedConstants(ctx)
|
|
5335
5372
|
markWrittenBindings(ctx)
|
|
5373
|
+
// A const whose initializer allocates and reads nothing reactive is read
|
|
5374
|
+
// as the binding, never re-evaluated at a prop read (#3868).
|
|
5375
|
+
markIdentityBoundConstants(ctx)
|
|
5336
5376
|
writeTemplatePropReads(ctx)
|
|
5337
5377
|
|
|
5338
5378
|
// BF052: init statements that write to an identifier with no visible
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
import ts from 'typescript'
|
|
14
14
|
|
|
15
15
|
import type { ComponentIR, IRMetadata, IRNode, IRElement, IRTemplatePart, TypeInfo } from './types.ts'
|
|
16
|
-
import type { ParsedExpr } from './expression-parser.ts'
|
|
17
16
|
import { isBooleanAttr } from './html-constants.ts'
|
|
18
|
-
import { literalMemberIndex } from './literal-member-index.ts'
|
|
19
17
|
|
|
20
18
|
/**
|
|
21
19
|
* A `const x = useContext(SomeContext)` consumer in a component body. SSR
|
|
@@ -474,7 +472,7 @@ type StaticConstants = IRMetadata['localConstants'] | undefined
|
|
|
474
472
|
* The const declaration a read of `name` reaches, when its declared value
|
|
475
473
|
* is what the read sees — the ONE decision behind every compile-time lookup
|
|
476
474
|
* of a literal const's member (`lookupStaticRecordLiteral`,
|
|
477
|
-
* `
|
|
475
|
+
* `lookupStaticConstMember`, the Go adapter's record lookup). Returns
|
|
478
476
|
* `null` when the read must fall back to the generic lowering:
|
|
479
477
|
*
|
|
480
478
|
* - `isShadowed(name)`: an enclosing loop callback's param, index,
|
|
@@ -515,8 +513,9 @@ export function resolveStaticConstBinding(
|
|
|
515
513
|
* compile-time lookup (the icon registry's `strokePaths['chevron-down']`,
|
|
516
514
|
* pagination's `variantClasses.ghost`; #1896 / #1897). Returns the
|
|
517
515
|
* looked-up scalar, or `null` for any other shape so callers fall back
|
|
518
|
-
* to their generic lowering. A
|
|
519
|
-
* literal
|
|
516
|
+
* to their generic lowering. A read the literal const answers structurally
|
|
517
|
+
* (an array element, a key the literal lacks) goes through
|
|
518
|
+
* `lookupStaticConstMember` (`static-const-member.ts`) first.
|
|
520
519
|
* Shared by all seven template-string adapters;
|
|
521
520
|
* the prop-KEYED variant of the pattern lives in `parseRecordIndexAccess`.
|
|
522
521
|
*
|
|
@@ -566,50 +565,6 @@ export function lookupStaticRecordLiteral(
|
|
|
566
565
|
return null
|
|
567
566
|
}
|
|
568
567
|
|
|
569
|
-
/**
|
|
570
|
-
* The element at `key` of a module or component-local array-literal const
|
|
571
|
-
* (`QS[0]`, `QS['1']`)
|
|
572
|
-
* or its `length`, as the element's own typed `ParsedExpr`: a string or
|
|
573
|
-
* number `literal`, or a unary `-` over a number literal. The caller emits
|
|
574
|
-
* it through its own expression emitter, exactly as if the source had
|
|
575
|
-
* written the element in place, so a value such as `-0` keeps its sign in
|
|
576
|
-
* arithmetic and a comparison (`1 / QS[0] < 0`) and is spelled `0` only
|
|
577
|
-
* where JS stringifies it. An index key must be canonical (`'01'` names no
|
|
578
|
-
* element). A hole or spread at or before the element (either also makes
|
|
579
|
-
* `length` unknown), an out-of-range index or any other element shape is
|
|
580
|
-
* `null`, so the caller falls back to its generic lowering.
|
|
581
|
-
*
|
|
582
|
-
* `isShadowed` is REQUIRED for the same reason as in
|
|
583
|
-
* `lookupStaticRecordLiteral`; which declaration the name reaches is
|
|
584
|
-
* `resolveStaticConstBinding`'s decision.
|
|
585
|
-
*/
|
|
586
|
-
export function lookupStaticArrayElement(
|
|
587
|
-
objectName: string,
|
|
588
|
-
key: string,
|
|
589
|
-
constants: StaticConstants,
|
|
590
|
-
isShadowed: (name: string) => boolean,
|
|
591
|
-
): ParsedExpr | null {
|
|
592
|
-
const array = resolveStaticConstBinding(objectName, constants, isShadowed)?.parsed
|
|
593
|
-
if (array?.kind !== 'array-literal') return null
|
|
594
|
-
if (key === 'length') {
|
|
595
|
-
if (array.elements.some(e => e.kind === 'unsupported')) return null
|
|
596
|
-
return { kind: 'literal', value: array.elements.length, literalType: 'number', raw: String(array.elements.length) }
|
|
597
|
-
}
|
|
598
|
-
const index = literalMemberIndex(key)
|
|
599
|
-
if (index.kind !== 'literal' || typeof index.value !== 'number') return null
|
|
600
|
-
const upTo = array.elements.slice(0, index.value + 1)
|
|
601
|
-
if (upTo.length <= index.value || upTo.some(e => e.kind === 'unsupported')) return null
|
|
602
|
-
const element = upTo[index.value]
|
|
603
|
-
if (element.kind === 'literal' && (element.literalType === 'string' || element.literalType === 'number')) return element
|
|
604
|
-
if (
|
|
605
|
-
element.kind === 'unary' && element.op === '-' &&
|
|
606
|
-
element.argument.kind === 'literal' && element.argument.literalType === 'number'
|
|
607
|
-
) {
|
|
608
|
-
return element
|
|
609
|
-
}
|
|
610
|
-
return null
|
|
611
|
-
}
|
|
612
|
-
|
|
613
568
|
/**
|
|
614
569
|
* A literal-initialized const's value. A number carries its `value` (a
|
|
615
570
|
* `const NZ = -0` is negative zero, which `text` cannot say), which each
|