octane 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/cjs/context-identity.cjs +29 -0
  2. package/dist/cjs/dom-stage.cjs +24 -18
  3. package/dist/cjs/error-codes.server.generated.cjs +5 -0
  4. package/dist/cjs/hydration-markers.cjs +12 -0
  5. package/dist/cjs/index.cjs +2 -0
  6. package/dist/cjs/runtime.cjs +253 -393
  7. package/dist/cjs/runtime.server.cjs +137 -35
  8. package/dist/cjs/server/index.cjs +2 -0
  9. package/dist/cjs/server/streamed-signals.cjs +41 -9
  10. package/dist/cjs/signals/client.cjs +1 -6
  11. package/dist/cjs/signals/engine.cjs +1 -12
  12. package/dist/cjs/version.cjs +1 -1
  13. package/dist/compiler/client-only-server.js +1 -28
  14. package/dist/compiler/compile-renderer-boundaries.js +1 -22
  15. package/dist/compiler/compile-universal.js +1 -22
  16. package/dist/compiler/compile.js +782 -359
  17. package/dist/compiler/dom-binding-fixed-props.js +11 -1
  18. package/dist/compiler/generated-origin.js +46 -0
  19. package/dist/compiler/hydrate-boundaries.js +88 -52
  20. package/dist/compiler/plain-hook-memo.js +5 -1
  21. package/dist/compiler/pure-factories.js +66 -0
  22. package/dist/compiler/register.js +4 -1
  23. package/dist/compiler/slot-hooks.js +113 -186
  24. package/dist/compiler/typescript-lowering.js +763 -0
  25. package/dist/compiler/void-roots.js +239 -0
  26. package/dist/compiler/volar.js +91 -38
  27. package/dist/context-identity.d.ts +14 -0
  28. package/dist/context-identity.js +28 -0
  29. package/dist/dom-stage.js +24 -18
  30. package/dist/error-codes.server.generated.d.ts +1 -0
  31. package/dist/error-codes.server.generated.js +5 -0
  32. package/dist/hydration/idle.js +1 -0
  33. package/dist/hydration/independent-island.js +53 -2
  34. package/dist/hydration/media.js +1 -0
  35. package/dist/hydration/types.d.ts +5 -0
  36. package/dist/hydration/visible.js +1 -0
  37. package/dist/hydration-markers.d.ts +8 -0
  38. package/dist/hydration-markers.js +8 -0
  39. package/dist/index.d.ts +1 -1
  40. package/dist/index.js +2 -0
  41. package/dist/runtime.d.ts +25 -40
  42. package/dist/runtime.js +254 -398
  43. package/dist/runtime.server.d.ts +23 -2
  44. package/dist/runtime.server.js +142 -36
  45. package/dist/server/index.d.ts +1 -1
  46. package/dist/server/index.js +2 -0
  47. package/dist/server/streamed-signals.js +41 -9
  48. package/dist/signals/client.js +3 -8
  49. package/dist/signals/engine.d.ts +0 -2
  50. package/dist/signals/engine.js +1 -13
  51. package/dist/universal-core.js +1 -1
  52. package/dist/version.js +1 -1
  53. package/package.json +1 -1
  54. package/dist/cjs/signals/diagnostic-local-hook.cjs +0 -37
  55. package/dist/compiler/local-void-roots.js +0 -154
  56. package/dist/signals/diagnostic-local-hook.d.ts +0 -8
  57. package/dist/signals/diagnostic-local-hook.js +0 -12
@@ -15,12 +15,8 @@
15
15
 
16
16
  import { parseModule, builders as b } from '@tsrx/core';
17
17
  import { parseModule as parseFallbackModule } from '#octane/compiler-parser';
18
- import {
19
- createLexicalAnalysis,
20
- forEachRuntimeAstChild,
21
- isIdentifierReference,
22
- } from './compile-universal.js';
23
- import { HOOK_NAMES, hookSlotHash } from './compile.js';
18
+ import { findRootFactoryImports, proveVoidRoots } from './void-roots.js';
19
+ import { HOOK_NAMES, collectNestedBindingNames, hookSlotHash } from './compile.js';
24
20
  import { NATIVE_SIGNAL_HOOK_NAMES } from './hook-names.js';
25
21
  import { METHOD_DEP_IMPORT, annotateHookCalls, analyzeStrongMemoCandidates } from './hook-deps.js';
26
22
  import { inlinePlainHookMemos } from './plain-hook-memo.js';
@@ -33,6 +29,12 @@ import { findLeadingJsxImportSourcePragma } from './pragma.js';
33
29
  import { collectProvenContextBindings, isProvenContextUse } from './context-use.js';
34
30
  import { assertNoLegacyContextProviders } from './context-provider.js';
35
31
  import { signalDeclarationSourceEdits } from './signal-declarations.js';
32
+ import {
33
+ collectPureFactoryLocals,
34
+ hasAuthoredPureAnnotation,
35
+ isPureFactoryCall,
36
+ octanePureFactoryNames,
37
+ } from './pure-factories.js';
36
38
  import {
37
39
  hookMethodName,
38
40
  hasHookMethods,
@@ -116,170 +118,42 @@ function octaneHookLocals(ast, nativeReads = false, explicitlyOwned = false) {
116
118
  // A disposable expression or nonescaping local const root can omit return-value
117
119
  // reconciliation only when every render calls an imported compiled void body.
118
120
  // The adapter resolves each export's actual ABI; lexical binding identity keeps
119
- // shadows, aliases, closure captures and unknown future renders on the generic path.
121
+ // shadows, aliases, closure captures and unknown future renders on the generic
122
+ // path. Each candidate is one root and lists every relative import it renders.
120
123
  function collectVoidRootCandidates(ast) {
121
- const createRootLocals = new Set();
124
+ const factories = findRootFactoryImports(ast);
125
+ if (factories.size === 0) return [];
122
126
  const componentImports = new Map();
123
127
  for (const node of ast.body || []) {
124
- if (node.type !== 'ImportDeclaration' || typeof node.source?.value !== 'string') continue;
125
- if (node.importKind === 'type') continue;
128
+ if (
129
+ node.type !== 'ImportDeclaration' ||
130
+ typeof node.source?.value !== 'string' ||
131
+ node.importKind === 'type'
132
+ )
133
+ continue;
126
134
  const request = node.source.value;
135
+ if (!request.startsWith('./') && !request.startsWith('../')) continue;
127
136
  for (const sp of node.specifiers || []) {
128
- if (sp.importKind === 'type') continue;
129
- if (request === 'octane' && sp.type === 'ImportSpecifier') {
130
- const imported = sp.imported?.name ?? sp.imported?.value;
131
- if (imported === 'createRoot' && sp.local?.name) createRootLocals.add(sp.local.name);
132
- continue;
133
- }
134
- if (!request.startsWith('./') && !request.startsWith('../')) continue;
135
- if (sp.type === 'ImportDefaultSpecifier' && sp.local?.name) {
137
+ if (sp.importKind === 'type' || !sp.local?.name) continue;
138
+ if (sp.type === 'ImportDefaultSpecifier') {
136
139
  componentImports.set(sp.local.name, { request, imported: 'default' });
137
- } else if (sp.type === 'ImportSpecifier' && sp.local?.name) {
140
+ } else if (sp.type === 'ImportSpecifier') {
138
141
  const imported = sp.imported?.name ?? sp.imported?.value;
139
- if (typeof imported === 'string') {
142
+ if (typeof imported === 'string')
140
143
  componentImports.set(sp.local.name, { request, imported });
141
- }
142
144
  }
143
145
  }
144
146
  }
145
- if (createRootLocals.size === 0 || componentImports.size === 0) return [];
146
-
147
- const analysis = createLexicalAnalysis(ast);
148
- const importedBinding = (node, imports) =>
149
- node?.type === 'Identifier' &&
150
- imports.has(node.name) &&
151
- analysis.resolveBinding(analysis.nodeScopes.get(node), node.name)?.scope === analysis.rootScope;
152
- const candidates = [];
153
- const localRoots = new Map();
154
- const actualFunctionScopes = new Set();
155
- let opaqueLexicalAccess = false;
156
- const walk = (node, parent, key) => {
157
- if (node === null || typeof node !== 'object') return;
158
- if (
159
- node.type === 'FunctionDeclaration' ||
160
- node.type === 'FunctionExpression' ||
161
- node.type === 'ArrowFunctionExpression'
162
- ) {
163
- const scope = analysis.nodeScopes.get(node.body)?.functionScope;
164
- if (scope !== undefined) actualFunctionScopes.add(scope);
165
- }
166
- if (
167
- node.type === 'WithStatement' ||
168
- (node.type === 'Identifier' &&
169
- node.name === 'eval' &&
170
- isIdentifierReference(node, parent, key, analysis) &&
171
- analysis.resolveBinding(analysis.nodeScopes.get(node), 'eval') === null)
172
- )
173
- opaqueLexicalAccess = true;
174
- if (
175
- node.type === 'VariableDeclarator' &&
176
- parent?.type === 'VariableDeclaration' &&
177
- parent.kind === 'const' &&
178
- node.id?.type === 'Identifier' &&
179
- node.init?.type === 'CallExpression' &&
180
- node.init.optional !== true &&
181
- importedBinding(node.init.callee, createRootLocals)
182
- ) {
183
- const scope = analysis.resolveBinding(analysis.nodeScopes.get(node.id), node.id.name)?.scope;
184
- // Namespaces and static blocks also own lexical function scopes, but
185
- // neither establishes the nonescaping function-local root lifetime.
186
- if (scope && actualFunctionScopes.has(scope.functionScope)) {
187
- let names = localRoots.get(scope);
188
- if (names === undefined) localRoots.set(scope, (names = new Map()));
189
- names.set(node.id.name, {
190
- valid: true,
191
- components: [],
192
- scope,
193
- start: node.init.callee.start,
194
- end: node.init.callee.end,
195
- });
196
- }
197
- }
198
- forEachRuntimeAstChild(node, (child, childKey) => walk(child, node, childKey));
199
- };
200
- walk(ast);
201
- if (!opaqueLexicalAccess && localRoots.size > 0) {
202
- const inspect = (node, parent, key, grandparent) => {
203
- if (node === null || typeof node !== 'object') return;
204
- if (node.type === 'Identifier' && isIdentifierReference(node, parent, key, analysis)) {
205
- const scope = analysis.resolveBinding(analysis.nodeScopes.get(node), node.name)?.scope;
206
- const root = localRoots.get(scope)?.get(node.name);
207
- if (root !== undefined) {
208
- const member = parent;
209
- const call = grandparent;
210
- const directMethod =
211
- analysis.nodeScopes.get(node) === root.scope &&
212
- member?.type === 'MemberExpression' &&
213
- key === 'object' &&
214
- member.computed !== true &&
215
- member.optional !== true &&
216
- member.property?.type === 'Identifier' &&
217
- call?.type === 'CallExpression' &&
218
- call.callee === member &&
219
- call.optional !== true;
220
- if (directMethod && member.property.name === 'unmount' && call.arguments.length === 0) {
221
- // unmount never changes the root's component-return contract.
222
- } else if (
223
- directMethod &&
224
- member.property.name === 'render' &&
225
- call.arguments.length >= 1 &&
226
- call.arguments.length <= 2 &&
227
- !call.arguments.some((argument) => argument.type === 'SpreadElement') &&
228
- importedBinding(call.arguments[0], componentImports)
229
- ) {
230
- root.components.push(componentImports.get(call.arguments[0].name));
231
- } else root.valid = false;
232
- }
233
- }
234
- forEachRuntimeAstChild(node, (child, childKey) => inspect(child, node, childKey, parent));
235
- };
236
- inspect(ast);
237
- for (const names of localRoots.values())
238
- for (const root of names.values()) {
239
- if (!root.valid || root.components.length === 0) continue;
240
- for (const component of root.components)
241
- candidates.push({ ...component, start: root.start, end: root.end });
242
- }
243
- }
244
-
245
- for (const statement of ast.body || []) {
246
- if (statement.type !== 'ExpressionStatement') continue;
247
- const renderCall = statement.expression;
248
- if (
249
- renderCall?.type !== 'CallExpression' ||
250
- renderCall.optional === true ||
251
- renderCall.arguments.length < 1 ||
252
- renderCall.arguments.length > 2
253
- )
254
- continue;
255
- const member = renderCall.callee;
256
- if (
257
- member?.type !== 'MemberExpression' ||
258
- member.computed === true ||
259
- member.optional === true ||
260
- member.property?.type !== 'Identifier' ||
261
- member.property.name !== 'render'
262
- )
263
- continue;
264
- const rootCall = member.object;
265
- if (
266
- rootCall?.type !== 'CallExpression' ||
267
- rootCall.optional === true ||
268
- rootCall.callee?.type !== 'Identifier' ||
269
- !importedBinding(rootCall.callee, createRootLocals)
270
- )
271
- continue;
272
- const component = renderCall.arguments[0];
273
- if (component?.type !== 'Identifier') continue;
274
- if (!importedBinding(component, componentImports)) continue;
275
- const imported = componentImports.get(component.name);
276
- candidates.push({
277
- ...imported,
278
- start: rootCall.callee.start,
279
- end: rootCall.callee.end,
280
- });
281
- }
282
- return candidates;
147
+ if (componentImports.size === 0) return [];
148
+ return proveVoidRoots(ast, {
149
+ factories,
150
+ component: (name) => componentImports.get(name),
151
+ }).map(({ callee, helper, components }) => ({
152
+ helper,
153
+ components,
154
+ start: callee.start,
155
+ end: callee.end,
156
+ }));
283
157
  }
284
158
 
285
159
  /**
@@ -295,9 +169,9 @@ export function findVoidRootImports(source, id) {
295
169
  return [];
296
170
  }
297
171
  const unique = new Map();
298
- for (const { request, imported } of collectVoidRootCandidates(ast)) {
299
- unique.set(`${request}\0${imported}`, { request, imported });
300
- }
172
+ for (const { components } of collectVoidRootCandidates(ast))
173
+ for (const { request, imported } of components)
174
+ unique.set(`${request}\0${imported}`, { request, imported });
301
175
  return [...unique.values()];
302
176
  }
303
177
 
@@ -375,7 +249,7 @@ export function findVoidComponentImports(source, id) {
375
249
  }
376
250
  const unique = new Map();
377
251
  for (const candidate of [
378
- ...collectVoidRootCandidates(ast),
252
+ ...collectVoidRootCandidates(ast).flatMap((root) => root.components),
379
253
  ...collectVoidJsxImportCandidates(ast),
380
254
  ]) {
381
255
  const { request, imported } = candidate;
@@ -384,24 +258,22 @@ export function findVoidComponentImports(source, id) {
384
258
  return [...unique.values()];
385
259
  }
386
260
 
387
- function collectVoidRootEdits(ast, st, isVoidComponentImport) {
261
+ function collectVoidRootEdits(candidates, st, isVoidComponentImport) {
388
262
  if (typeof isVoidComponentImport !== 'function') return;
389
- const groups = new Map();
390
- for (const candidate of collectVoidRootCandidates(ast)) {
391
- let group = groups.get(candidate.start);
392
- if (group === undefined) groups.set(candidate.start, (group = []));
393
- group.push(candidate);
394
- }
395
- for (const group of groups.values()) {
396
- if (!group.every((candidate) => isVoidComponentImport(candidate.request, candidate.imported)))
263
+ for (const candidate of candidates) {
264
+ if (
265
+ !candidate.components.every(({ request, imported }) =>
266
+ isVoidComponentImport(request, imported),
267
+ )
268
+ )
397
269
  continue;
398
- const candidate = group[0];
399
- if (st.voidRootName === null) st.voidRootName = allocSlotName(st, '_$createVoidRoot');
400
- st.edits.push({
401
- pos: candidate.start,
402
- end: candidate.end,
403
- text: st.voidRootName,
404
- });
270
+ let local = st.voidRootNames.get(candidate.helper);
271
+ if (local === undefined)
272
+ st.voidRootNames.set(
273
+ candidate.helper,
274
+ (local = allocSlotName(st, '_$' + candidate.helper.slice(2))),
275
+ );
276
+ st.edits.push({ pos: candidate.start, end: candidate.end, text: local });
405
277
  }
406
278
  }
407
279
 
@@ -1419,6 +1291,38 @@ function collectManualHookEdits(ast, providers, st) {
1419
1291
  visit(ast);
1420
1292
  }
1421
1293
 
1294
+ /**
1295
+ * Direct calls to Octane's side-effect-free factories that still need a
1296
+ * call-site `@__PURE__` (see pure-factories.js), as start → end offsets.
1297
+ * Locals shadowed anywhere below module scope are skipped wholesale, matching
1298
+ * the compiled-module pass.
1299
+ */
1300
+ function collectPureFactoryCalls(ast, source, names) {
1301
+ const calls = new Map();
1302
+ const locals = collectPureFactoryLocals(ast.body, names);
1303
+ if (locals.size === 0) return calls;
1304
+ for (const name of collectNestedBindingNames(ast.body)) locals.delete(name);
1305
+ if (locals.size === 0) return calls;
1306
+ function visit(node) {
1307
+ if (!node || typeof node !== 'object') return;
1308
+ if (Array.isArray(node)) {
1309
+ for (const item of node) visit(item);
1310
+ return;
1311
+ }
1312
+ if (isPureFactoryCall(node, locals) && !hasAuthoredPureAnnotation(source, node.start)) {
1313
+ calls.set(node.start, node.end);
1314
+ }
1315
+ for (const key in node) {
1316
+ if (key === 'loc' || key === 'metadata' || key === 'parent' || key.startsWith('_octane'))
1317
+ continue;
1318
+ const value = node[key];
1319
+ if (value && typeof value === 'object') visit(value);
1320
+ }
1321
+ }
1322
+ visit(ast.body);
1323
+ return calls;
1324
+ }
1325
+
1422
1326
  function parseHookSource(source, id) {
1423
1327
  try {
1424
1328
  return { ast: parseModule(source, id), canPrint: true };
@@ -1496,6 +1400,16 @@ export function slotHooks(source, id, options) {
1496
1400
  const manualProviders = options?.manualSlots ? findManualHookProviders(ast) : new Map();
1497
1401
  const nativeReadActivation = options?.nativeReads === true && importsNativeRenderer(ast);
1498
1402
  const signalLowering = signalDeclarationSourceEdits(ast, id, source);
1403
+ const pureCalls = collectPureFactoryCalls(
1404
+ ast,
1405
+ source,
1406
+ octanePureFactoryNames({
1407
+ clientDom:
1408
+ environment === 'client' &&
1409
+ (options?.renderer?.target ?? 'dom') === 'dom' &&
1410
+ options?.universalRuntime == null,
1411
+ }),
1412
+ );
1499
1413
  const canSpecializeRoot =
1500
1414
  !options?.manualSlots &&
1501
1415
  !options?.hmr &&
@@ -1507,7 +1421,8 @@ export function slotHooks(source, id, options) {
1507
1421
  !canSpecializeRoot &&
1508
1422
  !nativeReadActivation &&
1509
1423
  !signalLowering.usesSignals &&
1510
- !manualProviders.size
1424
+ !manualProviders.size &&
1425
+ pureCalls.size === 0
1511
1426
  ) {
1512
1427
  return strongAnalysis?.diagnostics.length ? { code: source, map: null, ...strongHints } : null;
1513
1428
  }
@@ -1515,6 +1430,8 @@ export function slotHooks(source, id, options) {
1515
1430
  // module whose hook calls carry their `_octane*` props (start/end offsets are
1516
1431
  // preserved, so the text edits below stay valid), with the dependency
1517
1432
  // inference keyed by the rebuilt calls.
1433
+ // Annotation preserves offsets, so the authored tree's proof stays valid.
1434
+ const voidRoots = canSpecializeRoot ? collectVoidRootCandidates(ast) : [];
1518
1435
  let inferred = new Map();
1519
1436
  if (importInfo.importsHook) {
1520
1437
  const annotated = annotateHookCalls(ast, {
@@ -1543,7 +1460,7 @@ export function slotHooks(source, id, options) {
1543
1460
  !options?.profile &&
1544
1461
  options?.universalRuntime == null &&
1545
1462
  options?.renderer?.target !== 'universal' &&
1546
- !(canSpecializeRoot && collectVoidRootCandidates(ast).length > 0)
1463
+ voidRoots.length === 0
1547
1464
  ) {
1548
1465
  const inlined = inlinePlainHookMemos(ast, source, id, {
1549
1466
  hookLocals: importInfo.locals,
@@ -1557,6 +1474,7 @@ export function slotHooks(source, id, options) {
1557
1474
  inferred,
1558
1475
  getterCalls,
1559
1476
  stateGetterHelpers: STATE_GETTER_HELPERS,
1477
+ pureCalls,
1560
1478
  });
1561
1479
  if (inlined !== null) return { ...inlined, ...strongHints };
1562
1480
  }
@@ -1590,7 +1508,7 @@ export function slotHooks(source, id, options) {
1590
1508
  usedNames: collectIdentifierNames(ast),
1591
1509
  slotBaseName: null,
1592
1510
  hookSlotsName: null,
1593
- voidRootName: null,
1511
+ voidRootNames: new Map(),
1594
1512
  };
1595
1513
  if (!st.hmr && !st.profile) st.slotBaseName = allocSlotName(st, '_hs$');
1596
1514
  if (importInfo.importsHook) {
@@ -1601,7 +1519,18 @@ export function slotHooks(source, id, options) {
1601
1519
  collectManualHookEdits(ast, findManualHookProviders(ast), st);
1602
1520
  }
1603
1521
  if (canSpecializeRoot) {
1604
- collectVoidRootEdits(ast, st, options.isVoidComponentImport);
1522
+ collectVoidRootEdits(voidRoots, st, options.isVoidComponentImport);
1523
+ }
1524
+ if (pureCalls.size) {
1525
+ // A replacement covering the call's start owns that text, so its own
1526
+ // output decides the annotation. Otherwise the mark goes first in the
1527
+ // list: among same-offset insertions it lands last, next to the call.
1528
+ const pureEdits = [];
1529
+ for (const start of pureCalls.keys()) {
1530
+ if (!st.edits.some((edit) => edit.end !== undefined && edit.pos <= start && start < edit.end))
1531
+ pureEdits.push({ pos: start, text: '/* @__PURE__ */ ' });
1532
+ }
1533
+ st.edits.unshift(...pureEdits);
1605
1534
  }
1606
1535
  if (st.edits.length === 0 && !nativeReadActivation && !signalLowering.usesSignals)
1607
1536
  return strongAnalysis?.diagnostics.length ? { code: source, map: null, ...strongHints } : null;
@@ -1618,9 +1547,7 @@ export function slotHooks(source, id, options) {
1618
1547
  const helperSpecifiers = [...st.getterHelpers].map(
1619
1548
  ([hook, local]) => `${STATE_GETTER_HELPERS[hook]} as ${local}`,
1620
1549
  );
1621
- if (st.voidRootName !== null) {
1622
- helperSpecifiers.push(`__createVoidRoot as ${st.voidRootName}`);
1623
- }
1550
+ for (const [helper, local] of st.voidRootNames) helperSpecifiers.push(`${helper} as ${local}`);
1624
1551
  for (const helper of st.parallelHelpers.values()) {
1625
1552
  if (helper.request === 'octane') {
1626
1553
  helperSpecifiers.push(`${helper.imported} as ${helper.local}`);