@barefootjs/jsx 0.35.7 → 0.36.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/analyzer.d.ts.map +1 -1
- package/dist/directives.d.ts +70 -0
- package/dist/directives.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +174 -120
- package/dist/ir-to-client-js/compute-inlinability.d.ts +10 -0
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +25 -5
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/preprocess-inline-jsx-callbacks.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/csr-substitute-bare-getter.test.ts +117 -0
- package/src/__tests__/csr-template-scope-soundness.test.ts +125 -0
- package/src/__tests__/module-function-helper-chain-3000.test.ts +200 -0
- package/src/__tests__/module-helper-scope-2986.test.ts +296 -0
- package/src/__tests__/module-scope-arrow-const-2988.test.ts +180 -0
- package/src/__tests__/reactive-factory-cross-file.test.ts +37 -0
- package/src/__tests__/ssr-defaults.test.ts +24 -2
- package/src/analyzer.ts +48 -19
- package/src/directives.ts +75 -0
- package/src/index.ts +3 -0
- package/src/ir-to-client-js/compute-inlinability.ts +92 -16
- package/src/ir-to-client-js/compute-scope.ts +72 -18
- package/src/ir-to-client-js/csr-substitute.ts +61 -11
- package/src/ir-to-client-js/emit-module-level.ts +10 -1
- package/src/ir-to-client-js/imports.ts +5 -0
- package/src/jsx-to-ir.ts +1 -1
- package/src/preprocess-inline-jsx-callbacks.ts +2 -1
- package/src/ssr-defaults.ts +32 -0
package/src/analyzer.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import ts from 'typescript'
|
|
10
|
+
import { CLIENT_DIRECTIVE_INTERIOR_RE, USE_CLIENT_DIRECTIVE, isUseClientDirectiveText } from './directives.ts'
|
|
10
11
|
import type { ImportSpecifier, TypeInfo, ParamInfo, ReactiveFactoryInfo, DeclinedReactiveFactory, RequiredFactoryImport, FactoryRenameSite, SourceLocation } from './types.ts'
|
|
11
12
|
import { parseExpression, parseBlockBodyTolerant, foldBlockToExpr, tsNodeToParsedExpr } from './expression-parser.ts'
|
|
12
13
|
import type { CallbackBodyAcceptor } from './adapters/interface.ts'
|
|
@@ -386,6 +387,28 @@ function collectNamedExports(sourceFile: ts.SourceFile): Set<string> {
|
|
|
386
387
|
// Single Pass Visitor
|
|
387
388
|
// =============================================================================
|
|
388
389
|
|
|
390
|
+
/**
|
|
391
|
+
* Does this node open a new function scope?
|
|
392
|
+
*
|
|
393
|
+
* The single answer to the one question every declaration collector in
|
|
394
|
+
* this file asks: bindings declared inside a function belong to that
|
|
395
|
+
* function, never to the scope being collected. `visitComponentBody` has
|
|
396
|
+
* always stopped here; `visit` did not, so the inner `const`/`function`
|
|
397
|
+
* declarations of a module-level `const f = (…) => { … }` were collected
|
|
398
|
+
* as module-level declarations of the FILE — hoisted into the init body
|
|
399
|
+
* and into the marked template, where their references to the helper's
|
|
400
|
+
* own parameters are unbound (#2986).
|
|
401
|
+
*/
|
|
402
|
+
function isFunctionScope(node: ts.Node): boolean {
|
|
403
|
+
return ts.isArrowFunction(node)
|
|
404
|
+
|| ts.isFunctionExpression(node)
|
|
405
|
+
|| ts.isFunctionDeclaration(node)
|
|
406
|
+
|| ts.isMethodDeclaration(node)
|
|
407
|
+
|| ts.isGetAccessorDeclaration(node)
|
|
408
|
+
|| ts.isSetAccessorDeclaration(node)
|
|
409
|
+
|| ts.isConstructorDeclaration(node)
|
|
410
|
+
}
|
|
411
|
+
|
|
389
412
|
function visit(
|
|
390
413
|
node: ts.Node,
|
|
391
414
|
ctx: AnalyzerContext,
|
|
@@ -394,10 +417,7 @@ function visit(
|
|
|
394
417
|
): void {
|
|
395
418
|
// Check for 'use client' directive at module level
|
|
396
419
|
if (ts.isExpressionStatement(node) && ts.isStringLiteral(node.expression)) {
|
|
397
|
-
if (
|
|
398
|
-
node.expression.text === 'use client' ||
|
|
399
|
-
node.expression.text === "'use client'"
|
|
400
|
-
) {
|
|
420
|
+
if (isUseClientDirectiveText(node.expression.text)) {
|
|
401
421
|
ctx.hasUseClientDirective = true
|
|
402
422
|
}
|
|
403
423
|
}
|
|
@@ -607,7 +627,23 @@ function visit(
|
|
|
607
627
|
}
|
|
608
628
|
}
|
|
609
629
|
|
|
610
|
-
ts.forEachChild(node, (child) =>
|
|
630
|
+
ts.forEachChild(node, (child) => {
|
|
631
|
+
// Stop at a nested function scope (#2986). Two carve-outs at module level:
|
|
632
|
+
//
|
|
633
|
+
// • `function` declarations must still be VISITED, because the
|
|
634
|
+
// module-level branch above is what collects them — and it `return`s
|
|
635
|
+
// immediately afterwards, so the descent-stop is already encoded
|
|
636
|
+
// there. This walk is also the ONLY one that reaches a `function`
|
|
637
|
+
// declaration inside a component body (`visitComponentBody` skips
|
|
638
|
+
// them); `compute-scope`'s fixpoint then demotes it to init scope.
|
|
639
|
+
// Skipping them here silently produces zero-output components.
|
|
640
|
+
//
|
|
641
|
+
// • The target component's own arrow (`const App = () => { … }`):
|
|
642
|
+
// `analyzeComponentBody` handles its body, but `visit` must still
|
|
643
|
+
// descend to reach the `function` declarations inside it.
|
|
644
|
+
if (isFunctionScope(child) && !ts.isFunctionDeclaration(child) && child !== ctx.componentNode) return
|
|
645
|
+
visit(child, ctx, targetComponentName, namedExports)
|
|
646
|
+
})
|
|
611
647
|
}
|
|
612
648
|
|
|
613
649
|
// =============================================================================
|
|
@@ -846,17 +882,11 @@ function visitComponentBody(node: ts.Node, ctx: AnalyzerContext): void {
|
|
|
846
882
|
ctx.jsxReturn = unwrapJsxTransparent(node.expression)
|
|
847
883
|
}
|
|
848
884
|
|
|
849
|
-
// Skip recursion into
|
|
850
|
-
//
|
|
885
|
+
// Skip recursion into a nested function scope — its bindings are
|
|
886
|
+
// function-scoped, not component init-scope declarations. Same predicate
|
|
887
|
+
// the module-level walk (`visit`) uses.
|
|
851
888
|
ts.forEachChild(node, (child) => {
|
|
852
|
-
|
|
853
|
-
if (
|
|
854
|
-
ts.isArrowFunction(child) ||
|
|
855
|
-
ts.isFunctionExpression(child) ||
|
|
856
|
-
ts.isFunctionDeclaration(child)
|
|
857
|
-
) {
|
|
858
|
-
return
|
|
859
|
-
}
|
|
889
|
+
if (isFunctionScope(child)) return
|
|
860
890
|
visitComponentBody(child, ctx)
|
|
861
891
|
})
|
|
862
892
|
}
|
|
@@ -3059,7 +3089,6 @@ function declarationIsReactiveFactoryCall(
|
|
|
3059
3089
|
// Module-level `/* @client */` directive detection + collection
|
|
3060
3090
|
// =============================================================================
|
|
3061
3091
|
|
|
3062
|
-
const CLIENT_DIRECTIVE_INTERIOR_RE = /^\s*@client\s*$/
|
|
3063
3092
|
const BLOCK_COMMENT_RE = /\/\*([\s\S]*?)\*\//g
|
|
3064
3093
|
|
|
3065
3094
|
/**
|
|
@@ -4256,7 +4285,7 @@ function fileHasUseClientDirective(filePath: string): boolean {
|
|
|
4256
4285
|
function visit(node: ts.Node): void {
|
|
4257
4286
|
if (found) return
|
|
4258
4287
|
if (ts.isExpressionStatement(node) && ts.isStringLiteral(node.expression)) {
|
|
4259
|
-
if (node.expression.text ===
|
|
4288
|
+
if (node.expression.text === USE_CLIENT_DIRECTIVE) {
|
|
4260
4289
|
found = true
|
|
4261
4290
|
return
|
|
4262
4291
|
}
|
|
@@ -4440,7 +4469,7 @@ function listComponentFunctionsFromSourceFile(sourceFile: ts.SourceFile): string
|
|
|
4440
4469
|
const hasUseClient = sourceFile.statements.some(stmt =>
|
|
4441
4470
|
ts.isExpressionStatement(stmt) &&
|
|
4442
4471
|
ts.isStringLiteral(stmt.expression) &&
|
|
4443
|
-
(stmt.expression.text
|
|
4472
|
+
isUseClientDirectiveText(stmt.expression.text)
|
|
4444
4473
|
)
|
|
4445
4474
|
const namedExports = collectNamedExports(sourceFile)
|
|
4446
4475
|
|
|
@@ -5873,7 +5902,7 @@ function factoryImportInsertionOffset(sf: ts.SourceFile): number {
|
|
|
5873
5902
|
for (const stmt of sf.statements) {
|
|
5874
5903
|
if (ts.isImportDeclaration(stmt)) { lastImportEnd = stmt.getEnd(); continue }
|
|
5875
5904
|
if (directiveEnd === -1 && ts.isExpressionStatement(stmt) &&
|
|
5876
|
-
ts.isStringLiteral(stmt.expression) && stmt.expression.text ===
|
|
5905
|
+
ts.isStringLiteral(stmt.expression) && stmt.expression.text === USE_CLIENT_DIRECTIVE) {
|
|
5877
5906
|
directiveEnd = stmt.getEnd()
|
|
5878
5907
|
}
|
|
5879
5908
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The source-level directives the compiler recognizes. This module is the
|
|
3
|
+
* single definition of their spelling: every detection site in the
|
|
4
|
+
* analyzer, the IR builder, and the preprocessors reads the constants
|
|
5
|
+
* below rather than repeating the literal, and the API reference
|
|
6
|
+
* (`docs/core/advanced/api-reference.md`, generated by
|
|
7
|
+
* `scripts/generate-api-reference.ts`) reads their JSDoc tags.
|
|
8
|
+
*
|
|
9
|
+
* The docstrings below write the client directive with a zero-width space
|
|
10
|
+
* (U+200B) before the `/` that would close this comment, and before the `c`
|
|
11
|
+
* of a space-preceded at-sign that TypeScript's JSDoc scanner would
|
|
12
|
+
* otherwise read as a new tag. The generator strips every U+200B, so the
|
|
13
|
+
* published reference shows — and a reader can copy — the real syntax.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Module-level `"use client"` directive. A component file that opens with
|
|
18
|
+
* it ships client JS and hydrates; without it the file's components render
|
|
19
|
+
* to static HTML only, and importing a browser-only API from it is a
|
|
20
|
+
* compile error.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* "use client"
|
|
25
|
+
* import { createSignal } from '@barefootjs/client'
|
|
26
|
+
*
|
|
27
|
+
* export function Counter() {
|
|
28
|
+
* const [count, setCount] = createSignal(0)
|
|
29
|
+
* return <button onClick={() => setCount(count() + 1)}>{count()}</button>
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @since 0.1.0
|
|
34
|
+
* @stability beta
|
|
35
|
+
*/
|
|
36
|
+
export const USE_CLIENT_DIRECTIVE = 'use client'
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Expression-level `/* @client */` directive, placed as a block comment
|
|
40
|
+
* directly before a JSX expression, an attribute initializer or a component
|
|
41
|
+
* prop initializer. It tells the compiler to skip server evaluation of that
|
|
42
|
+
* expression and evaluate it on the client only — the escape for expressions
|
|
43
|
+
* no template adapter can lower.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* // No template adapter can lower `toLocaleString()`, so evaluate it on the
|
|
48
|
+
* // client only. The server renders nothing there; hydration fills it in.
|
|
49
|
+
* <span>{/* @client */ total().toLocaleString()}</span>
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @since 0.1.0
|
|
53
|
+
* @stability beta
|
|
54
|
+
*/
|
|
55
|
+
export const CLIENT_EXPRESSION_DIRECTIVE = '@client'
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* True when a string-literal expression statement's text is the
|
|
59
|
+
* `"use client"` directive. Accepts the doubly-quoted spelling
|
|
60
|
+
* (`"'use client'"`) some detection sites have always tolerated.
|
|
61
|
+
*
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
export function isUseClientDirectiveText(text: string): boolean {
|
|
65
|
+
return text === USE_CLIENT_DIRECTIVE || text === `'${USE_CLIENT_DIRECTIVE}'`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Matches the interior of a block comment that is exactly the client
|
|
70
|
+
* expression directive (whitespace-padded), so the directive word as a
|
|
71
|
+
* substring of some other comment never matches.
|
|
72
|
+
*
|
|
73
|
+
* @internal
|
|
74
|
+
*/
|
|
75
|
+
export const CLIENT_DIRECTIVE_INTERIOR_RE = new RegExp(`^\\s*${CLIENT_EXPRESSION_DIRECTIVE}\\s*$`)
|
package/src/index.ts
CHANGED
|
@@ -345,6 +345,9 @@ export type {
|
|
|
345
345
|
// HTML constants
|
|
346
346
|
export { BOOLEAN_ATTRS, isBooleanAttr } from './html-constants.ts'
|
|
347
347
|
|
|
348
|
+
// The compiler's source-level directives (single definition of their spelling)
|
|
349
|
+
export { USE_CLIENT_DIRECTIVE, CLIENT_EXPRESSION_DIRECTIVE } from './directives.ts'
|
|
350
|
+
|
|
348
351
|
// Shared props-object-pattern helpers for the Go / Mojo template adapters
|
|
349
352
|
export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, collectContextConsumers } from './augment-inherited-props.ts'
|
|
350
353
|
export type { RecordIndexAccess, RecordIndexEntry, ContextConsumer } from './augment-inherited-props.ts'
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
* Stage E.4 of issue #1021.
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
-
import type { ConstantInfo, IRNode, ReferencesGraph } from '../types.ts'
|
|
26
|
+
import type { ConstantInfo, DeclarationScope, IRNode, ReferencesGraph } from '../types.ts'
|
|
27
27
|
import type { ClientJsContext } from './types.ts'
|
|
28
|
-
import {
|
|
28
|
+
import { computeDeclarationScopes } from './compute-scope.ts'
|
|
29
29
|
import { extractIdentifiers, extractTemplateIdentifiers } from './identifiers.ts'
|
|
30
30
|
import { isInlinableInTemplate, buildRelocateEnvFromIR } from '../relocate.ts'
|
|
31
31
|
import type { RelocateEnv, RelocateDecision } from '../relocate.ts'
|
|
@@ -103,6 +103,14 @@ export type ConstantInlinability =
|
|
|
103
103
|
* The function identity is per-instance; inlining a function
|
|
104
104
|
* literal into a template would close over the wrong scope. */
|
|
105
105
|
| { kind: 'arrow-literal' }
|
|
106
|
+
/** Module-scope arrow-valued constant that does NOT touch component
|
|
107
|
+
* internals (`compute-scope.ts`'s forward-reachability fixpoint
|
|
108
|
+
* placed it at `'module'` scope, #2988) — the mirror image of
|
|
109
|
+
* `FunctionInlinability`'s `'module-scope-safe'`. The template can
|
|
110
|
+
* reference it by bare name because the emitted client JS declares
|
|
111
|
+
* it at module scope too; unlike `'arrow-literal'`, this is NOT
|
|
112
|
+
* unsafe for template inlining. */
|
|
113
|
+
| { kind: 'module-scope-safe' }
|
|
106
114
|
/** Initializer reads a signal/memo. Template inlining would freeze
|
|
107
115
|
* the reactive value at SSR time. */
|
|
108
116
|
| { kind: 'reactive-read' }
|
|
@@ -232,9 +240,26 @@ export function computeInlinability(
|
|
|
232
240
|
const constants = new Map<string, ConstantInlinability>()
|
|
233
241
|
const functions = new Map<string, FunctionInlinability>()
|
|
234
242
|
|
|
243
|
+
// #3000: single producer for "is this module-level function safe to
|
|
244
|
+
// reference by bare name from the CSR template lambda" — reuses
|
|
245
|
+
// `compute-scope.ts`'s forward-reachability fixpoint (already the sole
|
|
246
|
+
// producer for constants as of #2998) instead of the independent, more
|
|
247
|
+
// conservative `functionReferencesDeclaredName`, which flagged a
|
|
248
|
+
// module-scope function unsafe on a reference to ANY name in
|
|
249
|
+
// `graph.declaredNames` — including another module-scope-SAFE helper —
|
|
250
|
+
// rather than only names that are themselves unsafe. A module-scope
|
|
251
|
+
// function calling another module-scope pure helper (`fmt(s) { return
|
|
252
|
+
// inner(s) }`) is exactly the shape `compute-scope.ts`'s own fixpoint
|
|
253
|
+
// already gets right for both functions transitively (it demotes a
|
|
254
|
+
// candidate only when it transitively references something actually
|
|
255
|
+
// `init`-scoped) — computing the shared scopes once here, before both
|
|
256
|
+
// the function loop and the constant classification below, makes
|
|
257
|
+
// functions and constants resolve through the same producer.
|
|
258
|
+
const { constantScope, functionScope } = computeDeclarationScopes(ctx, graph)
|
|
259
|
+
|
|
235
260
|
// --- Functions ---
|
|
236
261
|
for (const fn of ctx.localFunctions) {
|
|
237
|
-
functions.set(fn.name,
|
|
262
|
+
functions.set(fn.name, functionScope.get(fn.name) === 'module'
|
|
238
263
|
? { kind: 'module-scope-safe' }
|
|
239
264
|
: { kind: 'references-component-scope' })
|
|
240
265
|
}
|
|
@@ -257,6 +282,18 @@ export function computeInlinability(
|
|
|
257
282
|
// rejections during classification.
|
|
258
283
|
const env = buildEnvFromCtx(ctx)
|
|
259
284
|
|
|
285
|
+
// #2988: single producer for "is this module-level constant safe to
|
|
286
|
+
// reference by bare name from the template lambda" — reuses
|
|
287
|
+
// `compute-scope.ts`'s forward-reachability fixpoint, the SAME
|
|
288
|
+
// decision `classifyLocalDeclarations` (init-declarations.ts) uses to
|
|
289
|
+
// place a constant's own `var` declaration at module vs init scope in
|
|
290
|
+
// the compiled bundle, instead of `classifyConstantInitial` re-deriving
|
|
291
|
+
// an independent (and, pre-#2988, unconditional) `arrow-literal`
|
|
292
|
+
// verdict for every arrow-valued constant. `constantScope` was already
|
|
293
|
+
// computed above (#3000) alongside `functionScope`, from the same
|
|
294
|
+
// `(ctx, graph)` pair with nothing mutating either in between — reused
|
|
295
|
+
// here rather than calling `computeDeclarationScopes` a second time.
|
|
296
|
+
|
|
260
297
|
const decisionsByName = new Map<string, RelocateDecision[]>()
|
|
261
298
|
for (const c of ctx.localConstants) {
|
|
262
299
|
const { status, decisions } = classifyConstantInitial(
|
|
@@ -267,6 +304,7 @@ export function computeInlinability(
|
|
|
267
304
|
memoNames,
|
|
268
305
|
env,
|
|
269
306
|
getterAliases,
|
|
307
|
+
constantScope,
|
|
270
308
|
)
|
|
271
309
|
constants.set(c.name, status)
|
|
272
310
|
decisionsByName.set(c.name, decisions)
|
|
@@ -281,7 +319,7 @@ export function computeInlinability(
|
|
|
281
319
|
// transformation. The map lives on `ClientJsContext` — not on the
|
|
282
320
|
// cross-adapter `ConstantInfo` IR — so SSR adapters don't see CSR
|
|
283
321
|
// substitution semantics they have no use for.
|
|
284
|
-
populateCsrInlinable(ctx, env, getterAliases)
|
|
322
|
+
populateCsrInlinable(ctx, env, getterAliases, constantScope)
|
|
285
323
|
|
|
286
324
|
return { constants, functions, decisionsByName, templateRiskyNames }
|
|
287
325
|
}
|
|
@@ -300,14 +338,26 @@ export function computeInlinability(
|
|
|
300
338
|
*
|
|
301
339
|
* A const ends with `ctx.csrInlinable.get(name) === null` when:
|
|
302
340
|
* - it has no `value` (placeholder-let)
|
|
303
|
-
* - the value contains an arrow / function expression (identity per
|
|
341
|
+
* - the value contains an arrow / function expression (identity per
|
|
342
|
+
* render) — UNLESS `constantScope` places it at module scope
|
|
343
|
+
* (#2988: the arrow closes over nothing but its own params, same
|
|
344
|
+
* shape a module-scope `function` declaration already gets right),
|
|
345
|
+
* in which case it gets an identity entry (`rewrittenValue: name`)
|
|
346
|
+
* instead of `null` — the compiled bundle declares the const at
|
|
347
|
+
* module scope too, so referencing it by its own bare name from the
|
|
348
|
+
* CSR template is exactly as safe as a `module-scope-safe` function
|
|
304
349
|
* - it's a system construct (`createContext`, `new WeakMap`)
|
|
305
350
|
* - it's a JSX literal (handled by jsx-inline routing)
|
|
306
351
|
* - it's a bare alias-hop chain naming a signal/memo getter (`getterAliases`)
|
|
307
352
|
* - the substituted form fails `isInlinableInTemplate` (would re-execute
|
|
308
353
|
* a non-pure call at template-eval time — #1138)
|
|
309
354
|
*/
|
|
310
|
-
function populateCsrInlinable(
|
|
355
|
+
function populateCsrInlinable(
|
|
356
|
+
ctx: ClientJsContext,
|
|
357
|
+
relocateEnv: RelocateEnv,
|
|
358
|
+
getterAliases: ReadonlyMap<string, string>,
|
|
359
|
+
constantScope: ReadonlyMap<string, DeclarationScope>,
|
|
360
|
+
): void {
|
|
311
361
|
if (ctx.localConstants.length === 0) return
|
|
312
362
|
|
|
313
363
|
// Base env: signal getters + memo calls in raw (props.X) form, PLUS
|
|
@@ -345,6 +395,24 @@ function populateCsrInlinable(ctx: ClientJsContext, relocateEnv: RelocateEnv, ge
|
|
|
345
395
|
// call-kind entry (`buildEnvWithConsts`'s Map spread puts `constSubs`
|
|
346
396
|
// last) — the literal `(undefined)()` this issue is about (#2778).
|
|
347
397
|
for (const c of ctx.localConstants) {
|
|
398
|
+
// `mutatedAfterDeclaration` takes precedence, matching
|
|
399
|
+
// `classifyConstantInitial`'s order exactly (checked before
|
|
400
|
+
// `containsArrow` there too) — a same-scope mutation after
|
|
401
|
+
// declaration means `c.name`'s bare reference no longer reflects
|
|
402
|
+
// the value this identity substitution would imply, even if
|
|
403
|
+
// `compute-scope.ts` still placed the declaration at module scope.
|
|
404
|
+
if (c.containsArrow && !c.mutatedAfterDeclaration && constantScope.get(c.name) === 'module') {
|
|
405
|
+
// #2988: module-scope-safe arrow helper — CSR substitution is
|
|
406
|
+
// identity. The compiled bundle declares `c.name` at module
|
|
407
|
+
// scope (same `computeDeclarationScopes` answer that placed it
|
|
408
|
+
// there), so the template lambda — itself module scope — can
|
|
409
|
+
// reference it by its own bare name with no rewrite needed, the
|
|
410
|
+
// same principle a `module-scope-safe` function already gets via
|
|
411
|
+
// `functionScope` (#3000) above.
|
|
412
|
+
ctx.csrInlinable.set(c.name, { rewrittenValue: c.name, freeIdentifiers: new Set() })
|
|
413
|
+
finalised.add(c.name)
|
|
414
|
+
continue
|
|
415
|
+
}
|
|
348
416
|
if (c.isJsx || !c.value || c.containsArrow || c.systemConstructKind || c.mutatedAfterDeclaration || getterAliases.has(c.name)) {
|
|
349
417
|
ctx.csrInlinable.set(c.name, null)
|
|
350
418
|
finalised.add(c.name)
|
|
@@ -629,11 +697,23 @@ function classifyConstantInitial(
|
|
|
629
697
|
memoNames: Set<string>,
|
|
630
698
|
env: RelocateEnv,
|
|
631
699
|
getterAliases: ReadonlyMap<string, string>,
|
|
700
|
+
constantScope: ReadonlyMap<string, DeclarationScope>,
|
|
632
701
|
): { status: ConstantInlinability; decisions: RelocateDecision[] } {
|
|
633
702
|
if (c.isJsx) return { status: { kind: 'jsx-inline' }, decisions: [] }
|
|
634
703
|
if (!c.value) return { status: { kind: 'placeholder-let' }, decisions: [] }
|
|
635
704
|
if (c.mutatedAfterDeclaration) return { status: { kind: 'mutated-after-declaration' }, decisions: [] }
|
|
636
|
-
if (c.containsArrow)
|
|
705
|
+
if (c.containsArrow) {
|
|
706
|
+
// #2988: `compute-scope.ts`'s fixpoint already answered "is this
|
|
707
|
+
// safe to reference by bare name at module scope" for arrow-valued
|
|
708
|
+
// module constants — read it instead of unconditionally treating
|
|
709
|
+
// every arrow value as per-instance/unsafe. A non-module arrow (or
|
|
710
|
+
// one that transitively touches component internals) still lands
|
|
711
|
+
// in `'init'`/`'skip'` there, so this stays `arrow-literal` for
|
|
712
|
+
// every case the pre-#2988 cascade already got right.
|
|
713
|
+
return constantScope.get(c.name) === 'module'
|
|
714
|
+
? { status: { kind: 'module-scope-safe' }, decisions: [] }
|
|
715
|
+
: { status: { kind: 'arrow-literal' }, decisions: [] }
|
|
716
|
+
}
|
|
637
717
|
if (c.systemConstructKind) return { status: { kind: 'system-construct' }, decisions: [] }
|
|
638
718
|
// A multi-hop alias chain to a getter (`const a = items; const b = a`)
|
|
639
719
|
// has `b`'s OWN free id as `a` — an `init-local`, not a getter — so the
|
|
@@ -694,14 +774,6 @@ function classifyConstantInitial(
|
|
|
694
774
|
}
|
|
695
775
|
}
|
|
696
776
|
|
|
697
|
-
function functionReferencesDeclaredName(graph: ReferencesGraph, fnName: string): boolean {
|
|
698
|
-
const refs = graphFunctionReferences(graph, fnName)
|
|
699
|
-
for (const r of refs) {
|
|
700
|
-
if (graph.declaredNames.has(r)) return true
|
|
701
|
-
}
|
|
702
|
-
return false
|
|
703
|
-
}
|
|
704
|
-
|
|
705
777
|
/**
|
|
706
778
|
* Convert the tagged-union analysis back into the legacy shape the
|
|
707
779
|
* rest of the template pipeline expects: a map of inlinable constants
|
|
@@ -729,8 +801,12 @@ export function toLegacyInlinability(
|
|
|
729
801
|
for (const [name, status] of analysis.constants) {
|
|
730
802
|
if (status.kind === 'inlinable') {
|
|
731
803
|
inlinableConstants.set(name, status.value)
|
|
732
|
-
} else if (status.kind === 'jsx-inline' || status.kind === 'system-construct') {
|
|
804
|
+
} else if (status.kind === 'jsx-inline' || status.kind === 'system-construct' || status.kind === 'module-scope-safe') {
|
|
733
805
|
// Not inlinable AND not unsafe — they have their own routing.
|
|
806
|
+
// `module-scope-safe` (#2988) is referenced by bare name via
|
|
807
|
+
// `ctx.csrInlinable`'s identity entry (`populateCsrInlinable`),
|
|
808
|
+
// the same "own routing" shape `jsx-inline`/`system-construct`
|
|
809
|
+
// already use here.
|
|
734
810
|
} else {
|
|
735
811
|
unsafeLocalNames.add(name)
|
|
736
812
|
}
|
|
@@ -38,7 +38,7 @@ export function computeDeclarationScopes(
|
|
|
38
38
|
const initStmtAssigned = graphAssignedIdentifiers(graph)
|
|
39
39
|
|
|
40
40
|
// ============================================================================
|
|
41
|
-
// Constants
|
|
41
|
+
// Constants — immediate classification
|
|
42
42
|
//
|
|
43
43
|
// Cascade order mirrors generate-init.ts L114-151 (pre-Stage C):
|
|
44
44
|
//
|
|
@@ -50,26 +50,55 @@ export function computeDeclarationScopes(
|
|
|
50
50
|
// 3. no value (bare `let x`) → init (emitted as `let X` placeholder)
|
|
51
51
|
// 4. systemConstructKind → module (unique identity)
|
|
52
52
|
// 5. isModule + assignment target → module (#933 ReferenceError avoidance)
|
|
53
|
-
// 6.
|
|
53
|
+
// 6. isModule + arrow-valued → deferred to the fixpoint below (#2988)
|
|
54
|
+
// 7. otherwise → init
|
|
55
|
+
//
|
|
56
|
+
// Step 6 is new (#2988): an arrow-valued module-level const closing
|
|
57
|
+
// over nothing but its own params (`const fmt = (s) => s.toUpperCase()`)
|
|
58
|
+
// is exactly the shape a `function fmt(s) { ... }` declaration already
|
|
59
|
+
// gets right via the forward-reachability fixpoint just below — the
|
|
60
|
+
// pre-#2988 cascade had no fixpoint-reachability equivalent for
|
|
61
|
+
// constants and fell straight to `init`, which silently disagreed with
|
|
62
|
+
// `compute-inlinability.ts`'s independent (and, for constants,
|
|
63
|
+
// unconditional) `arrow-literal` verdict and produced an empty CSR
|
|
64
|
+
// template seed. Scoped to arrow-valued constants specifically, not
|
|
65
|
+
// every value-bearing module constant — widening the fixpoint to cover
|
|
66
|
+
// every module constant shape is a larger, separately-scoped change.
|
|
54
67
|
// ============================================================================
|
|
55
68
|
|
|
56
69
|
const providerContextNames = new Set<string>()
|
|
57
70
|
for (const p of ctx.providerSetups) providerContextNames.add(p.contextName)
|
|
58
71
|
|
|
72
|
+
const pendingConstants: ConstantInfo[] = []
|
|
59
73
|
for (const c of ctx.localConstants) {
|
|
60
|
-
|
|
74
|
+
const immediate = classifyConstantImmediate(c, usedIdentifiers, initStmtAssigned, providerContextNames)
|
|
75
|
+
if (immediate === 'pending-fixpoint') {
|
|
76
|
+
pendingConstants.push(c)
|
|
77
|
+
} else {
|
|
78
|
+
constantScope.set(c.name, immediate)
|
|
79
|
+
}
|
|
61
80
|
}
|
|
62
81
|
|
|
63
82
|
// ============================================================================
|
|
64
|
-
// Functions — forward-reachability fixpoint
|
|
83
|
+
// Functions + deferred constants — forward-reachability fixpoint
|
|
65
84
|
//
|
|
66
85
|
// Seed `initRequired` with anything that must live inside the init
|
|
67
86
|
// function (reactive roots, props, and every constant classified as
|
|
68
|
-
// `init` above). Then walk every module-level candidate
|
|
69
|
-
//
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
87
|
+
// `init` above). Then walk every module-level candidate — functions
|
|
88
|
+
// AND, as of #2988, the arrow-valued constants deferred above: if its
|
|
89
|
+
// body/value references any name in the set, demote it to init scope
|
|
90
|
+
// and add its own name so transitive callers demote in the next
|
|
91
|
+
// iteration. Loop until stable. Survivors are `module`. Mirrors
|
|
92
|
+
// generate-init.ts L218-280 (pre-Stage C) for functions; constants
|
|
93
|
+
// join the same fixpoint rather than getting a second, independent
|
|
94
|
+
// implementation of the same reachability question.
|
|
95
|
+
//
|
|
96
|
+
// Functions read their reference set from the graph's (regex-derived,
|
|
97
|
+
// not scope-aware) `graphFunctionReferences`, so `fn.params` must be
|
|
98
|
+
// stripped explicitly. Constants use `c.freeIdentifiers` instead — the
|
|
99
|
+
// analyzer's TS-AST walk (`extractFreeIdentifiersFromNode`) already
|
|
100
|
+
// excludes an arrow's own bound parameter names, so no equivalent
|
|
101
|
+
// stripping is needed here.
|
|
73
102
|
// ============================================================================
|
|
74
103
|
|
|
75
104
|
const initRequired = new Set<string>()
|
|
@@ -84,20 +113,22 @@ export function computeDeclarationScopes(
|
|
|
84
113
|
if (constantScope.get(c.name) === 'init') initRequired.add(c.name)
|
|
85
114
|
}
|
|
86
115
|
|
|
87
|
-
let
|
|
116
|
+
let pendingFns: FunctionInfo[] = []
|
|
88
117
|
for (const fn of ctx.localFunctions) {
|
|
89
118
|
if (fn.isJsxFunction) { functionScope.set(fn.name, 'skip'); continue }
|
|
90
119
|
if (fn.isMultiReturnJsxHelper) { functionScope.set(fn.name, 'skip'); continue }
|
|
91
120
|
if (!usedIdentifiers.has(fn.name)) { functionScope.set(fn.name, 'skip'); continue }
|
|
92
121
|
if (!fn.isModule) { functionScope.set(fn.name, 'init'); continue }
|
|
93
|
-
|
|
122
|
+
pendingFns.push(fn)
|
|
94
123
|
}
|
|
124
|
+
let pendingConsts = pendingConstants
|
|
95
125
|
|
|
96
126
|
let changed = true
|
|
97
127
|
while (changed) {
|
|
98
128
|
changed = false
|
|
99
|
-
|
|
100
|
-
|
|
129
|
+
|
|
130
|
+
const stillPendingFns: FunctionInfo[] = []
|
|
131
|
+
for (const fn of pendingFns) {
|
|
101
132
|
const refs = graphFunctionReferences(graph, fn.name)
|
|
102
133
|
// Parameters shadow outer names inside the function body.
|
|
103
134
|
for (const p of fn.params) refs.delete(p.name)
|
|
@@ -107,22 +138,44 @@ export function computeDeclarationScopes(
|
|
|
107
138
|
initRequired.add(fn.name)
|
|
108
139
|
changed = true
|
|
109
140
|
} else {
|
|
110
|
-
|
|
141
|
+
stillPendingFns.push(fn)
|
|
111
142
|
}
|
|
112
143
|
}
|
|
113
|
-
|
|
144
|
+
pendingFns = stillPendingFns
|
|
145
|
+
|
|
146
|
+
const stillPendingConsts: ConstantInfo[] = []
|
|
147
|
+
for (const c of pendingConsts) {
|
|
148
|
+
const refs = c.freeIdentifiers ?? new Set<string>()
|
|
149
|
+
const referencesInit = [...refs].some(r => initRequired.has(r))
|
|
150
|
+
if (referencesInit) {
|
|
151
|
+
constantScope.set(c.name, 'init')
|
|
152
|
+
initRequired.add(c.name)
|
|
153
|
+
changed = true
|
|
154
|
+
} else {
|
|
155
|
+
stillPendingConsts.push(c)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
pendingConsts = stillPendingConsts
|
|
114
159
|
}
|
|
115
|
-
for (const fn of
|
|
160
|
+
for (const fn of pendingFns) functionScope.set(fn.name, 'module')
|
|
161
|
+
for (const c of pendingConsts) constantScope.set(c.name, 'module')
|
|
116
162
|
|
|
117
163
|
return { constantScope, functionScope }
|
|
118
164
|
}
|
|
119
165
|
|
|
120
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Immediate (non-fixpoint) constant classification. Returns
|
|
168
|
+
* `'pending-fixpoint'` for the one case (#2988) that can't be decided
|
|
169
|
+
* without the reachability walk `computeDeclarationScopes` runs after
|
|
170
|
+
* this — the caller collects those into `pendingConstants` instead of
|
|
171
|
+
* setting a scope here.
|
|
172
|
+
*/
|
|
173
|
+
function classifyConstantImmediate(
|
|
121
174
|
c: ConstantInfo,
|
|
122
175
|
usedIdentifiers: Set<string>,
|
|
123
176
|
initStmtAssigned: Set<string>,
|
|
124
177
|
providerContextNames: Set<string>,
|
|
125
|
-
): DeclarationScope {
|
|
178
|
+
): DeclarationScope | 'pending-fixpoint' {
|
|
126
179
|
if (c.isJsx) return 'skip' // Inlined at IR level (#547)
|
|
127
180
|
if (c.isJsxFunction) return 'skip' // Inlined at call sites (#569)
|
|
128
181
|
|
|
@@ -140,5 +193,6 @@ function classifyConstant(
|
|
|
140
193
|
if (!c.value) return 'init' // `let x` placeholder
|
|
141
194
|
if (c.systemConstructKind) return 'module'
|
|
142
195
|
if (c.isModule && initStmtAssigned.has(c.name)) return 'module'
|
|
196
|
+
if (c.isModule && c.containsArrow) return 'pending-fixpoint' // #2988
|
|
143
197
|
return 'init'
|
|
144
198
|
}
|