@ecopages/react 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/README.md +50 -87
  2. package/package.json +32 -14
  3. package/src/bundling/bootstrap-package-paths.d.ts +13 -1
  4. package/src/bundling/bootstrap-package-paths.js +25 -3
  5. package/src/bundling/bundle.d.ts +8 -36
  6. package/src/bundling/bundle.js +42 -73
  7. package/src/client-graph/ast-transform-eco-page.d.ts +10 -6
  8. package/src/client-graph/ast-transform-eco-page.js +56 -35
  9. package/src/client-graph/ast-transform-import-edits.d.ts +10 -2
  10. package/src/client-graph/ast-transform-import-edits.js +36 -6
  11. package/src/client-graph/ast-transform-stripped-imports.d.ts +37 -0
  12. package/src/client-graph/ast-transform-stripped-imports.js +191 -0
  13. package/src/client-graph/ast-transform-walk-imports.d.ts +4 -0
  14. package/src/client-graph/ast-transform-walk-imports.js +15 -1
  15. package/src/client-graph/ast-transform.d.ts +12 -3
  16. package/src/client-graph/ast-transform.js +41 -56
  17. package/src/client-graph/boundary-plugin-read-file-inline.d.ts +3 -0
  18. package/src/client-graph/boundary-plugin-read-file-inline.js +14 -10
  19. package/src/client-graph/boundary-plugin-transform.d.ts +5 -4
  20. package/src/client-graph/boundary-plugin-transform.js +6 -13
  21. package/src/client-graph/boundary-plugin.d.ts +47 -1
  22. package/src/client-graph/boundary-plugin.js +267 -53
  23. package/src/client-graph/foreign-boundary-rewrite.d.ts +31 -0
  24. package/src/client-graph/foreign-boundary-rewrite.js +360 -0
  25. package/src/client-graph/foreign-jsx-reach.d.ts +25 -0
  26. package/src/client-graph/foreign-jsx-reach.js +68 -0
  27. package/src/client-graph/module-classification.js +3 -10
  28. package/src/client-graph/reachability-traverse.js +35 -105
  29. package/src/client-graph/requested-exports-plan.d.ts +62 -0
  30. package/src/client-graph/requested-exports-plan.js +125 -0
  31. package/src/client-graph/scope-walk.d.ts +37 -0
  32. package/src/client-graph/scope-walk.js +407 -0
  33. package/src/client-graph/specifier-classification.d.ts +2 -1
  34. package/src/client-graph/specifier-classification.js +9 -7
  35. package/src/contracts/router-adapter.d.ts +9 -24
  36. package/src/dev-transform/react-dev-transform-contributor.d.ts +4 -4
  37. package/src/dev-transform/react-dev-transform-contributor.js +6 -7
  38. package/src/hmr/hmr-strategy-process.d.ts +7 -5
  39. package/src/hmr/hmr-strategy-process.js +21 -16
  40. package/src/hmr/hmr-strategy.d.ts +11 -10
  41. package/src/hmr/hmr-strategy.js +32 -53
  42. package/src/hmr/page-metadata-cache.js +1 -1
  43. package/src/hydration/browser/foreign-boundary.d.ts +29 -0
  44. package/src/hydration/browser/foreign-boundary.js +128 -0
  45. package/src/hydration/browser/island-hydration.d.ts +3 -4
  46. package/src/hydration/browser/island-hydration.js +3 -4
  47. package/src/hydration/browser/page-hydration-hmr.d.ts +8 -7
  48. package/src/hydration/browser/page-hydration-hmr.js +53 -10
  49. package/src/hydration/browser/page-hydration.d.ts +6 -5
  50. package/src/hydration/browser/page-hydration.js +8 -6
  51. package/src/hydration/foreign-subtree.d.ts +27 -0
  52. package/src/hydration/foreign-subtree.js +69 -0
  53. package/src/hydration/hydration-asset.d.ts +37 -29
  54. package/src/hydration/hydration-asset.js +74 -138
  55. package/src/hydration/hydration-island-entry-source.d.ts +17 -0
  56. package/src/hydration/hydration-island-entry-source.js +32 -0
  57. package/src/hydration/hydration-page-entry-source.d.ts +20 -2
  58. package/src/hydration/hydration-page-entry-source.js +17 -12
  59. package/src/hydration/page-data-reader.d.ts +4 -7
  60. package/src/hydration/page-data-reader.js +5 -10
  61. package/src/mdx/mdx-config-dependency.d.ts +10 -15
  62. package/src/mdx/mdx-config-dependency.js +9 -22
  63. package/src/mdx/mdx-loader-plugin.d.ts +2 -0
  64. package/src/mdx/mdx-loader-plugin.js +1 -0
  65. package/src/page/page-module.js +1 -1
  66. package/src/plugin/react.plugin.d.ts +0 -7
  67. package/src/plugin/react.plugin.js +4 -33
  68. package/src/plugin/react.types.d.ts +0 -29
  69. package/src/render/component-ssr.d.ts +18 -0
  70. package/src/render/component-ssr.js +37 -0
  71. package/src/render/layout-compose.d.ts +4 -2
  72. package/src/render/layout-compose.js +13 -6
  73. package/src/render/layout-render-tracker.d.ts +20 -0
  74. package/src/render/layout-render-tracker.js +33 -0
  75. package/src/render/react-renderer.d.ts +28 -10
  76. package/src/render/react-renderer.js +103 -38
  77. package/src/render/react-runtime.js +2 -2
  78. package/src/bundling/runtime-alias-map.d.ts +0 -12
  79. package/src/bundling/runtime-alias-map.js +0 -110
  80. package/src/bundling/runtime-bundle.d.ts +0 -70
  81. package/src/bundling/runtime-bundle.js +0 -233
  82. package/src/bundling/runtime-interop-plugin.d.ts +0 -5
  83. package/src/bundling/runtime-interop-plugin.js +0 -32
  84. package/src/bundling/runtime-mode.d.ts +0 -1
  85. package/src/bundling/runtime-mode.js +0 -10
  86. package/src/bundling/runtime-modules.d.ts +0 -21
  87. package/src/bundling/runtime-modules.js +0 -28
  88. package/src/hydration/bootstrap-imports.d.ts +0 -25
  89. package/src/hydration/bootstrap-imports.js +0 -33
  90. package/src/hydration/compiled-page-hydration-options.d.ts +0 -19
  91. package/src/hydration/compiled-page-hydration-options.js +0 -1
  92. package/src/hydration/hydration-script-compiler.d.ts +0 -47
  93. package/src/hydration/hydration-script-compiler.js +0 -213
  94. package/src/runtime/use-sync-external-store-with-selector.d.ts +0 -3
  95. package/src/runtime/use-sync-external-store-with-selector.js +0 -54
@@ -0,0 +1,62 @@
1
+ import type { ParseResult } from 'oxc-parser';
2
+ import { type RequestedExportRules } from './specifier-classification.js';
3
+ /** One module as the boundary transforms it: its source after read-file inlining, parsed once. */
4
+ export type PlannedModule = {
5
+ source: string;
6
+ program: ParseResult['program'] | undefined;
7
+ /** Whether read-file inlining rewrote `source`. */
8
+ inlined: boolean;
9
+ /** Files inlined into `source`, which the module's build declares as watch files. */
10
+ inlinedFiles: string[];
11
+ };
12
+ /**
13
+ * Where an import leads: a module the scan follows, one it leaves alone (a package, an external or a
14
+ * virtual module), or nowhere the build could find.
15
+ */
16
+ export type ResolvedImport = {
17
+ kind: 'module';
18
+ filePath: string;
19
+ } | {
20
+ kind: 'skip';
21
+ } | {
22
+ kind: 'unresolved';
23
+ };
24
+ export type RequestedExportsPlanOptions = {
25
+ /** Entry files of the build; the scan starts from them. */
26
+ seeds: Iterable<string>;
27
+ /** Resolves an import the way the build does. */
28
+ resolveImport(specifier: string, importer: string): Promise<ResolvedImport>;
29
+ /** Reads and parses a module the way the boundary transforms it, or `undefined` when it cannot be read. */
30
+ readModule(filePath: string): PlannedModule | undefined;
31
+ /** Whether the boundary transforms `filePath`, so its imports pass requested exports on. */
32
+ transforms(filePath: string): boolean;
33
+ };
34
+ export type RequestedExportsPlan = {
35
+ /** The exports the module's importers use, from the whole graph the scan reached; `undefined` for an entry. */
36
+ requestedExports(filePath: string): RequestedExportRules | undefined;
37
+ /** Hands the scanned module to its load once; later calls, and modules the scan missed, get `undefined`. */
38
+ takeModule(filePath: string): PlannedModule | undefined;
39
+ };
40
+ /**
41
+ * Computes, before any module transforms, which exports each module's importers use.
42
+ *
43
+ * @remarks
44
+ * A module's pruning depends on the exports its importers request, so computing them while modules load
45
+ * made the result depend on which importer Rolldown loaded first. The plan starts from the build's entries
46
+ * and follows only the imports a module's browser code reaches, after server-only Page options are set
47
+ * aside, as the boundary's own analysis finds them: a module reached only through a stripped `staticProps`
48
+ * is never loaded, so it requests nothing. A module the boundary does not transform, such as a
49
+ * bootstrap entry, passes every import on and requests nothing. Requested exports flow from importers to
50
+ * imports through a worklist that analyses a module again whenever its requests grow, so a cycle settles on
51
+ * the union of what reaches it. Each module is read and parsed once, and its load reuses that parse. An
52
+ * entry keeps all of its exports, so a build that loads only its entries, such as a development transform,
53
+ * never needs the plan.
54
+ *
55
+ * Imports resolve the way the build resolves them, so a workspace package imported by name is followed like
56
+ * a relative import; installed packages, externals and virtual modules are not. A module reached through
57
+ * an import the registry has no key for (a package name or a tsconfig path alias) keeps all of its exports,
58
+ * as it does when no importer requests anything, and still passes its own imports on. When an import the
59
+ * scan follows resolves nowhere, the plan cannot know what reaches its target, so it requests nothing from
60
+ * any module and every module keeps all of its exports.
61
+ */
62
+ export declare function createRequestedExportsPlan(options: RequestedExportsPlanOptions): Promise<RequestedExportsPlan>;
@@ -0,0 +1,125 @@
1
+ import { analyzeReachability } from './reachability-analyzer.js';
2
+ import { walkAstNodes } from './ast-walk.js';
3
+ import { mergeRequestedExportRules, normalizeRequestedExportsKey, isServerOnlySpecifier, resolveRequestedExportsKey, } from './specifier-classification.js';
4
+ import { toRealPath } from './foreign-jsx-reach.js';
5
+ function importSpecifiers(program) {
6
+ const specifiers = [];
7
+ walkAstNodes(program, (node) => {
8
+ const source = node.type === 'ImportDeclaration' ||
9
+ node.type === 'ExportNamedDeclaration' ||
10
+ node.type === 'ExportAllDeclaration' ||
11
+ node.type === 'ImportExpression'
12
+ ? node.source
13
+ : node.type === 'CallExpression' && node.callee?.type === 'Identifier' && node.callee.name === 'require'
14
+ ? node.arguments?.[0]
15
+ : undefined;
16
+ if (typeof source?.value === 'string')
17
+ specifiers.push(source.value);
18
+ });
19
+ return specifiers;
20
+ }
21
+ /** Specifiers of the top-level import and re-export statements, the edges that pass requested exports on. */
22
+ function statementSpecifiers(program) {
23
+ const specifiers = new Set();
24
+ for (const statement of program.body) {
25
+ if (statement.type === 'ImportDeclaration' ||
26
+ ((statement.type === 'ExportNamedDeclaration' || statement.type === 'ExportAllDeclaration') &&
27
+ statement.source)) {
28
+ specifiers.add(statement.source.value);
29
+ }
30
+ }
31
+ return specifiers;
32
+ }
33
+ /**
34
+ * Computes, before any module transforms, which exports each module's importers use.
35
+ *
36
+ * @remarks
37
+ * A module's pruning depends on the exports its importers request, so computing them while modules load
38
+ * made the result depend on which importer Rolldown loaded first. The plan starts from the build's entries
39
+ * and follows only the imports a module's browser code reaches, after server-only Page options are set
40
+ * aside, as the boundary's own analysis finds them: a module reached only through a stripped `staticProps`
41
+ * is never loaded, so it requests nothing. A module the boundary does not transform, such as a
42
+ * bootstrap entry, passes every import on and requests nothing. Requested exports flow from importers to
43
+ * imports through a worklist that analyses a module again whenever its requests grow, so a cycle settles on
44
+ * the union of what reaches it. Each module is read and parsed once, and its load reuses that parse. An
45
+ * entry keeps all of its exports, so a build that loads only its entries, such as a development transform,
46
+ * never needs the plan.
47
+ *
48
+ * Imports resolve the way the build resolves them, so a workspace package imported by name is followed like
49
+ * a relative import; installed packages, externals and virtual modules are not. A module reached through
50
+ * an import the registry has no key for (a package name or a tsconfig path alias) keeps all of its exports,
51
+ * as it does when no importer requests anything, and still passes its own imports on. When an import the
52
+ * scan follows resolves nowhere, the plan cannot know what reaches its target, so it requests nothing from
53
+ * any module and every module keeps all of its exports.
54
+ */
55
+ export async function createRequestedExportsPlan(options) {
56
+ const modules = new Map();
57
+ const readModule = (filePath) => {
58
+ if (!modules.has(filePath))
59
+ modules.set(filePath, options.readModule(filePath));
60
+ return modules.get(filePath);
61
+ };
62
+ const seeds = new Set([...options.seeds].map(toRealPath));
63
+ const registry = new Map();
64
+ let complete = true;
65
+ const requestedExports = (filePath) => !complete || seeds.has(filePath) ? undefined : registry.get(normalizeRequestedExportsKey(filePath));
66
+ const resolveAll = (specifiers, importer) => Promise.all([...new Set(specifiers)]
67
+ .filter((specifier) => !isServerOnlySpecifier(specifier))
68
+ .map(async (specifier) => {
69
+ const resolved = await options.resolveImport(specifier, importer);
70
+ if (resolved.kind === 'unresolved')
71
+ complete = false;
72
+ return [specifier, resolved.kind === 'module' ? resolved.filePath : undefined];
73
+ })).then((entries) => new Map(entries));
74
+ const reached = new Set();
75
+ const queued = new Set();
76
+ const queue = [];
77
+ const enqueue = (filePath) => {
78
+ if (queued.has(filePath))
79
+ return;
80
+ queued.add(filePath);
81
+ queue.push(filePath);
82
+ };
83
+ const reach = (filePath) => {
84
+ if (reached.has(filePath))
85
+ return;
86
+ reached.add(filePath);
87
+ enqueue(filePath);
88
+ };
89
+ for (const seed of [...seeds].sort())
90
+ reach(seed);
91
+ while (complete && queue.length > 0) {
92
+ const filePath = queue.shift();
93
+ queued.delete(filePath);
94
+ const module = readModule(filePath);
95
+ if (!module?.program)
96
+ continue;
97
+ if (!options.transforms(filePath)) {
98
+ for (const target of (await resolveAll(importSpecifiers(module.program), filePath)).values()) {
99
+ if (target)
100
+ reach(target);
101
+ }
102
+ continue;
103
+ }
104
+ const reachability = analyzeReachability(module.source, filePath, module.program, requestedExports(filePath));
105
+ const keyed = statementSpecifiers(module.program);
106
+ const targets = await resolveAll(reachability.reachableImports.keys(), filePath);
107
+ for (const [specifier, rules] of reachability.reachableImports) {
108
+ const target = targets.get(specifier);
109
+ const key = keyed.has(specifier) ? resolveRequestedExportsKey(filePath, specifier) : undefined;
110
+ if (key && mergeRequestedExportRules(registry, key, rules) && target)
111
+ enqueue(target);
112
+ if (target)
113
+ reach(target);
114
+ }
115
+ }
116
+ return {
117
+ requestedExports: (filePath) => requestedExports(toRealPath(filePath)),
118
+ takeModule(filePath) {
119
+ const realPath = toRealPath(filePath);
120
+ const module = modules.get(realPath);
121
+ modules.delete(realPath);
122
+ return module;
123
+ },
124
+ };
125
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Scope-aware walk over an Oxc ESTree program that reports runtime references to names
3
+ * the walked code does not declare itself.
4
+ */
5
+ /**
6
+ * Hooks for {@link walkScopedReferences}.
7
+ */
8
+ export type ScopedReferenceVisitor = {
9
+ /** Called for each runtime reference to a name no enclosing scope inside the walk declares. */
10
+ onReference(name: string, node: any): void;
11
+ /**
12
+ * Called before a node is walked. Return `true` to skip the node and its children.
13
+ *
14
+ * @remarks
15
+ * The hook can walk children itself with `walk(child)`; they keep the scope of `node`.
16
+ */
17
+ onNode?(node: any, walk: (child: unknown) => void): boolean;
18
+ };
19
+ /**
20
+ * Adds every name a binding pattern declares to `names`.
21
+ */
22
+ export declare function collectPatternNames(pattern: any, names: Set<string>): void;
23
+ /**
24
+ * Collects the names a statement declares in the block that directly contains it.
25
+ */
26
+ export declare function collectLexicalDeclarations(statement: any, names: Set<string>): void;
27
+ /**
28
+ * Walks `root` and reports each runtime reference to a name that no scope inside the walk declares.
29
+ *
30
+ * @remarks
31
+ * Type-only syntax (annotations, interfaces, type aliases, `import type`, `declare`) is skipped;
32
+ * the operand of `as`, `satisfies` and `!`, enums and namespaces are walked. Property keys,
33
+ * member names, labels and intrinsic JSX tags are not references, and neither is the bare
34
+ * identifier operand of `typeof`, which cannot throw. Walking a `Program` puts its top-level declarations
35
+ * in scope; walk its statements one by one to see references to them.
36
+ */
37
+ export declare function walkScopedReferences(root: unknown, visitor: ScopedReferenceVisitor, scope?: ReadonlySet<string>): void;
@@ -0,0 +1,407 @@
1
+ /**
2
+ * Scope-aware walk over an Oxc ESTree program that reports runtime references to names
3
+ * the walked code does not declare itself.
4
+ */
5
+ /** Keys that hold type-only syntax and never contain a runtime reference. */
6
+ const TYPE_ONLY_KEYS = new Set([
7
+ 'typeAnnotation',
8
+ 'typeParameters',
9
+ 'typeArguments',
10
+ 'returnType',
11
+ 'superTypeArguments',
12
+ 'implements',
13
+ ]);
14
+ /** TypeScript expression wrappers whose `expression` operand is evaluated at runtime. */
15
+ const RUNTIME_TS_EXPRESSIONS = new Set([
16
+ 'TSAsExpression',
17
+ 'TSSatisfiesExpression',
18
+ 'TSNonNullExpression',
19
+ 'TSTypeAssertion',
20
+ 'TSInstantiationExpression',
21
+ ]);
22
+ const FUNCTION_TYPES = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression']);
23
+ /**
24
+ * Adds every name a binding pattern declares to `names`.
25
+ */
26
+ export function collectPatternNames(pattern, names) {
27
+ if (!pattern || typeof pattern !== 'object')
28
+ return;
29
+ switch (pattern.type) {
30
+ case 'Identifier':
31
+ names.add(pattern.name);
32
+ return;
33
+ case 'ObjectPattern':
34
+ for (const property of pattern.properties ?? []) {
35
+ collectPatternNames(property.type === 'RestElement' ? property.argument : property.value, names);
36
+ }
37
+ return;
38
+ case 'ArrayPattern':
39
+ for (const element of pattern.elements ?? [])
40
+ collectPatternNames(element, names);
41
+ return;
42
+ case 'RestElement':
43
+ collectPatternNames(pattern.argument, names);
44
+ return;
45
+ case 'AssignmentPattern':
46
+ collectPatternNames(pattern.left, names);
47
+ return;
48
+ case 'TSParameterProperty':
49
+ collectPatternNames(pattern.parameter, names);
50
+ return;
51
+ }
52
+ }
53
+ function isDeclaredOnly(node) {
54
+ return node.declare === true || node.type === 'TSDeclareFunction';
55
+ }
56
+ const NAMED_DECLARATION_TYPES = new Set([
57
+ 'FunctionDeclaration',
58
+ 'ClassDeclaration',
59
+ 'TSEnumDeclaration',
60
+ 'TSDeclareFunction',
61
+ 'TSModuleDeclaration',
62
+ 'TSImportEqualsDeclaration',
63
+ ]);
64
+ function unwrapExport(statement) {
65
+ return statement?.type === 'ExportNamedDeclaration' || statement?.type === 'ExportDefaultDeclaration'
66
+ ? statement.declaration
67
+ : statement;
68
+ }
69
+ /**
70
+ * Collects the names a statement declares in the block that directly contains it.
71
+ */
72
+ export function collectLexicalDeclarations(statement, names) {
73
+ const declaration = unwrapExport(statement);
74
+ if (declaration?.type === 'VariableDeclaration') {
75
+ for (const declarator of declaration.declarations)
76
+ collectPatternNames(declarator.id, names);
77
+ }
78
+ else if (NAMED_DECLARATION_TYPES.has(declaration?.type) && declaration.id?.type === 'Identifier') {
79
+ names.add(declaration.id.name);
80
+ }
81
+ else if (declaration?.type === 'ImportDeclaration') {
82
+ for (const specifier of declaration.specifiers ?? [])
83
+ names.add(specifier.local.name);
84
+ }
85
+ }
86
+ /**
87
+ * Collects `var` names hoisted to the function or program scope from nested blocks.
88
+ */
89
+ function collectHoistedVars(node, names) {
90
+ if (!node || typeof node !== 'object')
91
+ return;
92
+ if (Array.isArray(node)) {
93
+ for (const child of node)
94
+ collectHoistedVars(child, names);
95
+ return;
96
+ }
97
+ const astNode = node;
98
+ if (FUNCTION_TYPES.has(astNode.type) || astNode.type === 'ClassBody')
99
+ return;
100
+ if (astNode.type === 'VariableDeclaration' && astNode.kind === 'var') {
101
+ for (const declarator of astNode.declarations)
102
+ collectPatternNames(declarator.id, names);
103
+ }
104
+ for (const key in astNode) {
105
+ if (key !== 'type' && key !== 'start' && key !== 'end' && !TYPE_ONLY_KEYS.has(key)) {
106
+ collectHoistedVars(astNode[key], names);
107
+ }
108
+ }
109
+ }
110
+ function extendScope(scope, names) {
111
+ const extended = new Set(scope);
112
+ for (const name of names)
113
+ extended.add(name);
114
+ return extended;
115
+ }
116
+ function blockScope(scope, statements) {
117
+ const names = new Set();
118
+ for (const statement of statements)
119
+ collectLexicalDeclarations(statement, names);
120
+ return names.size > 0 ? extendScope(scope, names) : scope;
121
+ }
122
+ class ScopedReferenceWalker {
123
+ visitor;
124
+ constructor(visitor) {
125
+ this.visitor = visitor;
126
+ }
127
+ walk(node, scope) {
128
+ if (!node || typeof node !== 'object')
129
+ return;
130
+ if (Array.isArray(node)) {
131
+ for (const child of node)
132
+ this.walk(child, scope);
133
+ return;
134
+ }
135
+ const astNode = node;
136
+ if (typeof astNode.type !== 'string')
137
+ return;
138
+ if (this.visitor.onNode?.(astNode, (child) => this.walk(child, scope)))
139
+ return;
140
+ if (!this.walkKnownNode(astNode, scope))
141
+ this.walkChildren(astNode, scope);
142
+ }
143
+ reference(name, node, scope) {
144
+ if (!scope.has(name))
145
+ this.visitor.onReference(name, node);
146
+ }
147
+ walkChildren(node, scope) {
148
+ for (const key in node) {
149
+ if (key === 'type' || key === 'start' || key === 'end' || TYPE_ONLY_KEYS.has(key)) {
150
+ continue;
151
+ }
152
+ this.walk(node[key], scope);
153
+ }
154
+ }
155
+ /**
156
+ * Walks the runtime parts of a binding pattern: defaults, computed keys and decorators.
157
+ */
158
+ walkPattern(pattern, scope) {
159
+ if (!pattern || typeof pattern !== 'object')
160
+ return;
161
+ switch (pattern.type) {
162
+ case 'Identifier':
163
+ this.walk(pattern.decorators, scope);
164
+ return;
165
+ case 'ObjectPattern':
166
+ for (const property of pattern.properties ?? []) {
167
+ if (property.type === 'RestElement') {
168
+ this.walkPattern(property.argument, scope);
169
+ continue;
170
+ }
171
+ if (property.computed)
172
+ this.walk(property.key, scope);
173
+ this.walkPattern(property.value, scope);
174
+ }
175
+ return;
176
+ case 'ArrayPattern':
177
+ for (const element of pattern.elements ?? [])
178
+ this.walkPattern(element, scope);
179
+ return;
180
+ case 'RestElement':
181
+ this.walkPattern(pattern.argument, scope);
182
+ return;
183
+ case 'AssignmentPattern':
184
+ this.walkPattern(pattern.left, scope);
185
+ this.walk(pattern.right, scope);
186
+ return;
187
+ case 'TSParameterProperty':
188
+ this.walk(pattern.decorators, scope);
189
+ this.walkPattern(pattern.parameter, scope);
190
+ return;
191
+ default:
192
+ this.walk(pattern, scope);
193
+ }
194
+ }
195
+ /**
196
+ * Walks a function: parameter defaults see the parameters but not the body's declarations.
197
+ */
198
+ walkFunction(node, scope) {
199
+ if (isDeclaredOnly(node) || !node.body)
200
+ return;
201
+ const paramNames = new Set();
202
+ if (node.type !== 'FunctionDeclaration' && node.id?.type === 'Identifier')
203
+ paramNames.add(node.id.name);
204
+ for (const param of node.params ?? [])
205
+ collectPatternNames(param, paramNames);
206
+ if (node.type !== 'ArrowFunctionExpression')
207
+ paramNames.add('arguments');
208
+ const paramScope = extendScope(scope, paramNames);
209
+ for (const param of node.params ?? [])
210
+ this.walkPattern(param, paramScope);
211
+ if (node.body.type !== 'BlockStatement') {
212
+ this.walk(node.body, paramScope);
213
+ return;
214
+ }
215
+ const bodyNames = new Set();
216
+ collectHoistedVars(node.body.body, bodyNames);
217
+ for (const statement of node.body.body)
218
+ collectLexicalDeclarations(statement, bodyNames);
219
+ this.walk(node.body.body, extendScope(paramScope, bodyNames));
220
+ }
221
+ walkClass(node, scope) {
222
+ if (isDeclaredOnly(node))
223
+ return;
224
+ this.walk(node.decorators, scope);
225
+ this.walk(node.superClass, scope);
226
+ const classScope = node.type === 'ClassExpression' && node.id?.type === 'Identifier'
227
+ ? extendScope(scope, [node.id.name])
228
+ : scope;
229
+ this.walk(node.body, classScope);
230
+ }
231
+ walkClassMember(node, scope) {
232
+ this.walk(node.decorators, scope);
233
+ if (node.computed)
234
+ this.walk(node.key, scope);
235
+ this.walk(node.value, scope);
236
+ }
237
+ walkJsxName(node, scope, isObject = false) {
238
+ if (node?.type === 'JSXIdentifier') {
239
+ if (node.name !== 'this' && (isObject || !/^[a-z]/.test(node.name)))
240
+ this.reference(node.name, node, scope);
241
+ return;
242
+ }
243
+ if (node?.type === 'JSXMemberExpression')
244
+ this.walkJsxName(node.object, scope, true);
245
+ }
246
+ walkForStatement(node, scope) {
247
+ const head = node.type === 'ForStatement' ? node.init : node.left;
248
+ const loopScope = head?.type === 'VariableDeclaration' ? blockScope(scope, [head]) : scope;
249
+ this.walkChildren(node, loopScope);
250
+ }
251
+ /**
252
+ * Walks node types whose children are not all runtime references in the current scope.
253
+ *
254
+ * @returns `true` when the node was fully walked.
255
+ */
256
+ walkKnownNode(node, scope) {
257
+ const handler = NODE_HANDLERS[node.type];
258
+ if (handler) {
259
+ handler(this, node, scope);
260
+ return true;
261
+ }
262
+ if (RUNTIME_TS_EXPRESSIONS.has(node.type)) {
263
+ this.walk(node.expression, scope);
264
+ return true;
265
+ }
266
+ return node.type.startsWith('TS');
267
+ }
268
+ walkSwitch(node, scope) {
269
+ this.walk(node.discriminant, scope);
270
+ const caseScope = blockScope(scope, node.cases.flatMap((switchCase) => switchCase.consequent));
271
+ for (const switchCase of node.cases) {
272
+ this.walk(switchCase.test, caseScope);
273
+ this.walk(switchCase.consequent, caseScope);
274
+ }
275
+ }
276
+ walkCatch(node, scope) {
277
+ const names = new Set();
278
+ collectPatternNames(node.param, names);
279
+ const catchScope = extendScope(scope, names);
280
+ this.walkPattern(node.param, catchScope);
281
+ this.walk(node.body, catchScope);
282
+ }
283
+ walkVariableDeclaration(node, scope) {
284
+ if (node.declare)
285
+ return;
286
+ for (const declarator of node.declarations) {
287
+ this.walkPattern(declarator.id, scope);
288
+ this.walk(declarator.init, scope);
289
+ }
290
+ }
291
+ walkExportNamed(node, scope) {
292
+ if (node.exportKind === 'type')
293
+ return;
294
+ if (node.declaration) {
295
+ this.walk(node.declaration, scope);
296
+ return;
297
+ }
298
+ if (node.source)
299
+ return;
300
+ for (const specifier of node.specifiers ?? []) {
301
+ if (specifier.exportKind !== 'type' && specifier.local?.type === 'Identifier') {
302
+ this.reference(specifier.local.name, specifier.local, scope);
303
+ }
304
+ }
305
+ }
306
+ }
307
+ const skip = () => { };
308
+ const walkFunction = (walker, node, scope) => walker.walkFunction(node, scope);
309
+ const walkBlock = (walker, node, scope) => walker.walk(node.body, blockScope(scope, node.body));
310
+ const walkFor = (walker, node, scope) => walker.walkForStatement(node, scope);
311
+ const walkClass = (walker, node, scope) => walker.walkClass(node, scope);
312
+ const walkClassMember = (walker, node, scope) => walker.walkClassMember(node, scope);
313
+ /** Node types whose children are not all runtime references in the enclosing scope. */
314
+ const NODE_HANDLERS = {
315
+ Identifier: (walker, node, scope) => walker.reference(node.name, node, scope),
316
+ Program: (walker, node, scope) => walker.walk(node.body, blockScope(extendScope(scope, collectProgramVars(node)), node.body)),
317
+ FunctionDeclaration: walkFunction,
318
+ FunctionExpression: walkFunction,
319
+ ArrowFunctionExpression: walkFunction,
320
+ TSDeclareFunction: skip,
321
+ BlockStatement: walkBlock,
322
+ StaticBlock: walkBlock,
323
+ SwitchStatement: (walker, node, scope) => walker.walkSwitch(node, scope),
324
+ ForStatement: walkFor,
325
+ ForInStatement: walkFor,
326
+ ForOfStatement: walkFor,
327
+ CatchClause: (walker, node, scope) => walker.walkCatch(node, scope),
328
+ VariableDeclaration: (walker, node, scope) => walker.walkVariableDeclaration(node, scope),
329
+ ClassDeclaration: walkClass,
330
+ ClassExpression: walkClass,
331
+ MethodDefinition: walkClassMember,
332
+ PropertyDefinition: walkClassMember,
333
+ AccessorProperty: walkClassMember,
334
+ TSEnumDeclaration: (walker, node, scope) => {
335
+ if (node.declare)
336
+ return;
337
+ const members = node.body?.members ?? node.members ?? [];
338
+ const memberNames = members.flatMap((member) => member.id?.type === 'Identifier' ? [member.id.name] : []);
339
+ const enumScope = extendScope(scope, memberNames);
340
+ for (const member of members)
341
+ walker.walk(member.initializer, enumScope);
342
+ },
343
+ TSModuleDeclaration: (walker, node, scope) => {
344
+ if (node.declare || node.body?.type !== 'TSModuleBlock') {
345
+ if (!node.declare && node.body)
346
+ walker.walk(node.body, scope);
347
+ return;
348
+ }
349
+ walker.walk(node.body.body, blockScope(scope, node.body.body));
350
+ },
351
+ TSImportEqualsDeclaration: (walker, node, scope) => {
352
+ if (node.importKind === 'type')
353
+ return;
354
+ let reference = node.moduleReference;
355
+ while (reference?.type === 'TSQualifiedName')
356
+ reference = reference.left;
357
+ if (reference?.type === 'Identifier')
358
+ walker.reference(reference.name, reference, scope);
359
+ },
360
+ UnaryExpression: (walker, node, scope) => {
361
+ if (node.operator === 'typeof' && node.argument?.type === 'Identifier')
362
+ return;
363
+ walker.walk(node.argument, scope);
364
+ },
365
+ TSExportAssignment: (walker, node, scope) => walker.walk(node.expression, scope),
366
+ MemberExpression: (walker, node, scope) => {
367
+ walker.walk(node.object, scope);
368
+ if (node.computed)
369
+ walker.walk(node.property, scope);
370
+ },
371
+ Property: (walker, node, scope) => {
372
+ if (node.computed)
373
+ walker.walk(node.key, scope);
374
+ walker.walk(node.value, scope);
375
+ },
376
+ LabeledStatement: (walker, node, scope) => walker.walk(node.body, scope),
377
+ BreakStatement: skip,
378
+ ContinueStatement: skip,
379
+ MetaProperty: skip,
380
+ ImportDeclaration: skip,
381
+ ExportAllDeclaration: skip,
382
+ ExportNamedDeclaration: (walker, node, scope) => walker.walkExportNamed(node, scope),
383
+ JSXOpeningElement: (walker, node, scope) => {
384
+ walker.walkJsxName(node.name, scope);
385
+ walker.walk(node.attributes, scope);
386
+ },
387
+ JSXClosingElement: skip,
388
+ JSXAttribute: (walker, node, scope) => walker.walk(node.value, scope),
389
+ };
390
+ function collectProgramVars(program) {
391
+ const names = new Set();
392
+ collectHoistedVars(program.body, names);
393
+ return names;
394
+ }
395
+ /**
396
+ * Walks `root` and reports each runtime reference to a name that no scope inside the walk declares.
397
+ *
398
+ * @remarks
399
+ * Type-only syntax (annotations, interfaces, type aliases, `import type`, `declare`) is skipped;
400
+ * the operand of `as`, `satisfies` and `!`, enums and namespaces are walked. Property keys,
401
+ * member names, labels and intrinsic JSX tags are not references, and neither is the bare
402
+ * identifier operand of `typeof`, which cannot throw. Walking a `Program` puts its top-level declarations
403
+ * in scope; walk its statements one by one to see references to them.
404
+ */
405
+ export function walkScopedReferences(root, visitor, scope = new Set()) {
406
+ new ScopedReferenceWalker(visitor).walk(root, scope);
407
+ }
@@ -70,5 +70,6 @@ export declare function resolveRequestedExportsKey(importer: string, specifier:
70
70
  * @param registry - Cross-module requested-export registry.
71
71
  * @param moduleKey - Normalized local module key.
72
72
  * @param rules - Newly observed reachable export rules for the module.
73
+ * @returns Whether the registry changed.
73
74
  */
74
- export declare function mergeRequestedExportRules(registry: Map<string, RequestedExportRules>, moduleKey: string, rules: Set<string> | '*'): void;
75
+ export declare function mergeRequestedExportRules(registry: Map<string, RequestedExportRules>, moduleKey: string, rules: Set<string> | '*'): boolean;