@octanejs/codemod 0.2.1 → 0.2.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/README.md +107 -8
- package/dist/bin.js +2 -2
- package/dist/effect-receivers.d.ts +2 -0
- package/dist/effect-receivers.js +10 -0
- package/dist/expression-wrappers.d.ts +2 -0
- package/dist/expression-wrappers.js +19 -0
- package/dist/forwarded-props.d.ts +5 -1
- package/dist/forwarded-props.js +18 -3
- package/dist/local-react-hook.d.ts +8 -3
- package/dist/local-react-hook.js +106 -50
- package/dist/local-uses.d.ts +2 -0
- package/dist/local-uses.js +22 -0
- package/dist/mappings.d.ts +5 -1
- package/dist/mappings.js +16 -3
- package/dist/path-svg.d.ts +5 -0
- package/dist/path-svg.js +19 -2
- package/dist/project.js +997 -129
- package/dist/react-intl.d.ts +1 -1
- package/dist/react-intl.js +23 -3
- package/dist/react-island-sources.d.ts +32 -7
- package/dist/react-island-sources.js +812 -74
- package/dist/react-renderer.d.ts +2 -0
- package/dist/react-renderer.js +94 -0
- package/dist/scoped-store-contract.d.ts +6 -1
- package/dist/scoped-store-contract.js +199 -31
- package/dist/store-hook-uses.js +1 -20
- package/dist/store-inputs.js +3 -1
- package/dist/transform.d.ts +6 -0
- package/dist/transform.js +2183 -270
- package/dist/types.d.ts +27 -9
- package/package.json +4 -3
- package/src/path-svg.ts +20 -2
- package/templates/octane-path-svg.ts +4 -0
- package/templates/react-intl.tsrx +64 -11
package/dist/react-renderer.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
+
/** A direct, unconsumed React DOM server stream and its later full-body read. */
|
|
3
|
+
export declare function unreadServerStream(wait: ts.AwaitExpression, declarationOf: (name: ts.Identifier) => ts.Declaration | null, uses: (name: ts.Identifier) => ts.Identifier[], original: (module: string) => boolean): boolean;
|
|
2
4
|
/** The props object for a self-closing React element with closed literal attributes. */
|
|
3
5
|
export declare function staticReactProps(element: ts.JsxSelfClosingElement): string | null;
|
|
4
6
|
/** A React-owned value or renderer in an inline options object. */
|
package/dist/react-renderer.js
CHANGED
|
@@ -1,4 +1,98 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
+
import { REACT_MODULES } from './mappings.js';
|
|
3
|
+
import { inTypeNode } from './local-uses.js';
|
|
4
|
+
function constantStatement(decl) {
|
|
5
|
+
if (ts.isVariableDeclaration(decl) &&
|
|
6
|
+
ts.isVariableDeclarationList(decl.parent) &&
|
|
7
|
+
ts.isVariableStatement(decl.parent.parent) &&
|
|
8
|
+
(decl.parent.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
9
|
+
decl.parent.declarations.length === 1)
|
|
10
|
+
return decl.parent.parent;
|
|
11
|
+
}
|
|
12
|
+
/** A direct, unconsumed React DOM server stream and its later full-body read. */
|
|
13
|
+
export function unreadServerStream(wait, declarationOf, uses, original) {
|
|
14
|
+
const access = wait.expression;
|
|
15
|
+
if (!ts.isPropertyAccessExpression(access) ||
|
|
16
|
+
access.name.text !== 'allReady' ||
|
|
17
|
+
!ts.isExpressionStatement(wait.parent) ||
|
|
18
|
+
!ts.isBlock(wait.parent.parent) ||
|
|
19
|
+
ts.isOptionalChain(access) ||
|
|
20
|
+
!ts.isIdentifier(access.expression))
|
|
21
|
+
return false;
|
|
22
|
+
const stream = declarationOf(access.expression);
|
|
23
|
+
if (!stream || !ts.isVariableDeclaration(stream) || !ts.isIdentifier(stream.name))
|
|
24
|
+
return false;
|
|
25
|
+
const statement = constantStatement(stream);
|
|
26
|
+
const initializer = stream.initializer;
|
|
27
|
+
if (statement?.parent !== wait.parent.parent ||
|
|
28
|
+
statement.end > wait.pos ||
|
|
29
|
+
!initializer ||
|
|
30
|
+
!ts.isAwaitExpression(initializer) ||
|
|
31
|
+
!ts.isCallExpression(initializer.expression) ||
|
|
32
|
+
ts.isOptionalChain(initializer.expression) ||
|
|
33
|
+
!initializer.expression.arguments.length ||
|
|
34
|
+
initializer.expression.arguments.some(ts.isSpreadElement) ||
|
|
35
|
+
!ts.isIdentifier(initializer.expression.expression))
|
|
36
|
+
return false;
|
|
37
|
+
const refs = uses(stream.name).filter((ref) => !inTypeNode(ref));
|
|
38
|
+
if (refs.length !== 2 || refs[0] !== access.expression)
|
|
39
|
+
return false;
|
|
40
|
+
const response = refs[1].parent;
|
|
41
|
+
if (!ts.isNewExpression(response) ||
|
|
42
|
+
response.arguments?.length !== 1 ||
|
|
43
|
+
!ts.isIdentifier(response.expression) ||
|
|
44
|
+
response.expression.text !== 'Response' ||
|
|
45
|
+
declarationOf(response.expression))
|
|
46
|
+
return false;
|
|
47
|
+
const text = response.parent;
|
|
48
|
+
if (!ts.isPropertyAccessExpression(text) ||
|
|
49
|
+
text.name.text !== 'text' ||
|
|
50
|
+
!ts.isCallExpression(text.parent) ||
|
|
51
|
+
ts.isOptionalChain(text.parent) ||
|
|
52
|
+
text.parent.arguments.length ||
|
|
53
|
+
!ts.isAwaitExpression(text.parent.parent))
|
|
54
|
+
return false;
|
|
55
|
+
const read = ts.findAncestor(text.parent.parent, (node) => ts.isStatement(node) || ts.isFunctionLike(node));
|
|
56
|
+
if (read?.parent !== statement.parent || read.pos < wait.parent.end)
|
|
57
|
+
return false;
|
|
58
|
+
const factory = declarationOf(initializer.expression.expression);
|
|
59
|
+
if (!factory)
|
|
60
|
+
return false;
|
|
61
|
+
let module;
|
|
62
|
+
if (ts.isImportSpecifier(factory)) {
|
|
63
|
+
const clause = factory.parent.parent;
|
|
64
|
+
if (factory.isTypeOnly || clause.isTypeOnly || clause.parent.attributes)
|
|
65
|
+
return false;
|
|
66
|
+
if ((factory.propertyName ?? factory.name).text !== 'renderToReadableStream')
|
|
67
|
+
return false;
|
|
68
|
+
module = clause.parent.moduleSpecifier;
|
|
69
|
+
}
|
|
70
|
+
else if (ts.isBindingElement(factory) && ts.isObjectBindingPattern(factory.parent)) {
|
|
71
|
+
const key = factory.propertyName ?? factory.name;
|
|
72
|
+
const binding = factory.parent.parent;
|
|
73
|
+
const load = ts.isVariableDeclaration(binding) && constantStatement(binding);
|
|
74
|
+
const init = load && binding.initializer;
|
|
75
|
+
if (!load ||
|
|
76
|
+
load.parent !== statement.parent ||
|
|
77
|
+
load.end > statement.pos ||
|
|
78
|
+
factory.parent.elements.length !== 1 ||
|
|
79
|
+
factory.initializer ||
|
|
80
|
+
factory.dotDotDotToken ||
|
|
81
|
+
!ts.isIdentifier(key) ||
|
|
82
|
+
key.text !== 'renderToReadableStream' ||
|
|
83
|
+
!init ||
|
|
84
|
+
!ts.isAwaitExpression(init) ||
|
|
85
|
+
!ts.isCallExpression(init.expression) ||
|
|
86
|
+
init.expression.expression.kind !== ts.SyntaxKind.ImportKeyword ||
|
|
87
|
+
init.expression.arguments.length !== 1)
|
|
88
|
+
return false;
|
|
89
|
+
module = init.expression.arguments[0];
|
|
90
|
+
}
|
|
91
|
+
return (!!module &&
|
|
92
|
+
ts.isStringLiteral(module) &&
|
|
93
|
+
REACT_MODULES[module.text] === 'octane/server' &&
|
|
94
|
+
original(module.text));
|
|
95
|
+
}
|
|
2
96
|
/** The props object for a self-closing React element with closed literal attributes. */
|
|
3
97
|
export function staticReactProps(element) {
|
|
4
98
|
if (element.typeArguments)
|
|
@@ -7,9 +7,14 @@ export interface ScopedStoreHookPlan {
|
|
|
7
7
|
/** Checks a store factory against its original, renderer-independent declaration. */
|
|
8
8
|
isSharedExport(module: string, imported: string, from: string): boolean;
|
|
9
9
|
}
|
|
10
|
+
interface ScopedStoreHookContract {
|
|
11
|
+
plan?: ScopedStoreHookPlan;
|
|
12
|
+
/** Every normal return from the verified original export reaches a React hook. */
|
|
13
|
+
requiresReact(): boolean;
|
|
14
|
+
}
|
|
10
15
|
declare function runtime(s: ts.ImportDeclaration | ts.ExportDeclaration, verbatim?: boolean): boolean;
|
|
11
16
|
export { runtime as hasRuntimeDependency };
|
|
12
17
|
/** Audit every executable target of a bare dependency, including browser and server conditions. */
|
|
13
18
|
export declare function runtimeTargets(spec: string, resolved: string, from: string, unconditional?: boolean): string[];
|
|
14
19
|
/** Read linked, authored packages only. All output belongs to the consuming project. */
|
|
15
|
-
export declare function createScopedStorePlanner(outputDir: string, contextBridge: string, compilerOptions?: ts.CompilerOptions): (specifier: string, importedName: string, importer: string) =>
|
|
20
|
+
export declare function createScopedStorePlanner(outputDir: string, contextBridge: string, compilerOptions?: ts.CompilerOptions): (specifier: string, importedName: string, importer: string) => ScopedStoreHookContract | null;
|
|
@@ -3,6 +3,8 @@ import fs from 'node:fs';
|
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import ts from 'typescript';
|
|
6
|
+
import { skipWrappers } from './expression-wrappers.js';
|
|
7
|
+
import { namedRuntimeImport, safeSetupLiteral } from './local-react-hook.js';
|
|
6
8
|
// The application's React compiler must not synthesize dispatcher calls in Octane hooks.
|
|
7
9
|
const nativeDirective = "'use no memo';\n";
|
|
8
10
|
const hooks = new Set([
|
|
@@ -267,7 +269,15 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
267
269
|
if (!neutral.size)
|
|
268
270
|
return null;
|
|
269
271
|
const contextReaders = new Set();
|
|
270
|
-
function
|
|
272
|
+
function localExportBody(item, sf) {
|
|
273
|
+
const edge = item.parent.parent;
|
|
274
|
+
if (edge.isTypeOnly || edge.attributes || edge.moduleSpecifier || item.isTypeOnly)
|
|
275
|
+
return;
|
|
276
|
+
return checker
|
|
277
|
+
.getExportSpecifierLocalTargetSymbol(item)
|
|
278
|
+
?.declarations?.find((d) => ts.isFunctionDeclaration(d) && !!d.body && d.parent === sf);
|
|
279
|
+
}
|
|
280
|
+
function discover(sf, files, seen, shared, localExports) {
|
|
271
281
|
if (seen.has(sf.fileName))
|
|
272
282
|
return true;
|
|
273
283
|
if (!owned(sf.fileName) || !sourceFile.test(sf.fileName) || sf.isDeclarationFile)
|
|
@@ -339,6 +349,15 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
339
349
|
}
|
|
340
350
|
continue;
|
|
341
351
|
}
|
|
352
|
+
if (ts.isExportDeclaration(s) &&
|
|
353
|
+
s.exportClause &&
|
|
354
|
+
ts.isNamedExports(s.exportClause) &&
|
|
355
|
+
s.exportClause.elements.some((item) => !item.isTypeOnly) &&
|
|
356
|
+
s.exportClause.elements.every((item) => item.isTypeOnly || localExportBody(item, sf))) {
|
|
357
|
+
for (const item of s.exportClause.elements)
|
|
358
|
+
localExports.add(item);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
342
361
|
if (!ts.isImportDeclaration(s) ||
|
|
343
362
|
!s.importClause ||
|
|
344
363
|
s.attributes ||
|
|
@@ -424,7 +443,7 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
424
443
|
!named ||
|
|
425
444
|
!ts.isNamedImports(named) ||
|
|
426
445
|
!named.elements.every((x) => x.isTypeOnly || /^use[A-Z0-9]/.test((x.propertyName ?? x.name).text)) ||
|
|
427
|
-
!discover(child, files, seen, shared))
|
|
446
|
+
!discover(child, files, seen, shared, localExports))
|
|
428
447
|
return false;
|
|
429
448
|
for (const x of named.elements) {
|
|
430
449
|
const sym = checker.getSymbolAtLocation(x.name);
|
|
@@ -492,7 +511,9 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
492
511
|
valid = false;
|
|
493
512
|
return;
|
|
494
513
|
}
|
|
495
|
-
if (!ts.isIdentifier(node) ||
|
|
514
|
+
if (!ts.isIdentifier(node) ||
|
|
515
|
+
ts.isImportSpecifier(node.parent) ||
|
|
516
|
+
localExports.has(node.parent))
|
|
496
517
|
return;
|
|
497
518
|
const sym = ts.isShorthandPropertyAssignment(node.parent)
|
|
498
519
|
? checker.getShorthandAssignmentValueSymbol(node.parent)
|
|
@@ -552,7 +573,18 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
552
573
|
files.set(out, code.slice(0, start) + nativeDirective + code.slice(start));
|
|
553
574
|
return true;
|
|
554
575
|
}
|
|
555
|
-
function
|
|
576
|
+
function forwardedCall(fn, unwrap) {
|
|
577
|
+
if (fn.body?.statements.length !== 1)
|
|
578
|
+
return;
|
|
579
|
+
const [only] = fn.body.statements;
|
|
580
|
+
const expression = ts.isReturnStatement(only) ? only.expression : undefined;
|
|
581
|
+
if (!expression)
|
|
582
|
+
return;
|
|
583
|
+
const call = unwrap ? skipWrappers(expression) : expression;
|
|
584
|
+
if (ts.isCallExpression(call) && ts.isIdentifier(call.expression))
|
|
585
|
+
return call;
|
|
586
|
+
}
|
|
587
|
+
function readsContext(fn, seen, files, unwrap = false) {
|
|
556
588
|
const symbol = fn.name && checker.getSymbolAtLocation(fn.name);
|
|
557
589
|
if (!symbol || !fn.body || !files.has(output(fn.getSourceFile())) || seen.has(symbol))
|
|
558
590
|
return false;
|
|
@@ -560,16 +592,139 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
560
592
|
return true;
|
|
561
593
|
seen.add(symbol);
|
|
562
594
|
// A thin exported forwarding hook may share another hook's contextual read.
|
|
563
|
-
const
|
|
564
|
-
if (
|
|
565
|
-
!ts.isReturnStatement(only) ||
|
|
566
|
-
!only.expression ||
|
|
567
|
-
!ts.isCallExpression(only.expression) ||
|
|
568
|
-
!ts.isIdentifier(only.expression.expression))
|
|
595
|
+
const forward = forwardedCall(fn, unwrap);
|
|
596
|
+
if (!forward)
|
|
569
597
|
return false;
|
|
570
|
-
const called = referenced(checker,
|
|
598
|
+
const called = referenced(checker, forward.expression);
|
|
571
599
|
const body = called?.declarations?.find((d) => ts.isFunctionDeclaration(d) && !!d.body);
|
|
572
|
-
return !!body && readsContext(body, seen, files);
|
|
600
|
+
return !!body && readsContext(body, seen, files, unwrap);
|
|
601
|
+
}
|
|
602
|
+
function reachesReact(fn, seen = new Set()) {
|
|
603
|
+
if (!fn.body ||
|
|
604
|
+
seen.has(fn) ||
|
|
605
|
+
fn.parameters.some((param) => {
|
|
606
|
+
if (!ts.isIdentifier(param.name))
|
|
607
|
+
return true;
|
|
608
|
+
if (!param.initializer)
|
|
609
|
+
return false;
|
|
610
|
+
const value = skipWrappers(param.initializer);
|
|
611
|
+
return (!safeSetupLiteral(value, checker) &&
|
|
612
|
+
!(ts.isObjectLiteralExpression(value) && value.properties.length === 0));
|
|
613
|
+
}) ||
|
|
614
|
+
installed('react', fn.getSourceFile().fileName) !== installed('react', entry))
|
|
615
|
+
return false;
|
|
616
|
+
function dispatch(expression) {
|
|
617
|
+
const call = skipWrappers(expression);
|
|
618
|
+
if (!ts.isCallExpression(call) ||
|
|
619
|
+
ts.isOptionalChain(call) ||
|
|
620
|
+
!ts.isIdentifier(call.expression))
|
|
621
|
+
return false;
|
|
622
|
+
const binding = namedRuntimeImport(call.expression, checker);
|
|
623
|
+
// The arguments of an ordinary call run before the call can return.
|
|
624
|
+
return ((binding?.module === 'react' && hooks.has(binding.name)) || call.arguments.some(dispatch));
|
|
625
|
+
}
|
|
626
|
+
// Every entered branch must call React before it can return. Only simple setup
|
|
627
|
+
// statements and if arms without an else can continue to the next statement.
|
|
628
|
+
function setup(body) {
|
|
629
|
+
for (const statement of body.statements) {
|
|
630
|
+
if (ts.isReturnStatement(statement) || ts.isExpressionStatement(statement))
|
|
631
|
+
return !!statement.expression && dispatch(statement.expression);
|
|
632
|
+
if (ts.isIfStatement(statement) &&
|
|
633
|
+
!statement.elseStatement &&
|
|
634
|
+
ts.isBlock(statement.thenStatement) &&
|
|
635
|
+
setup(statement.thenStatement))
|
|
636
|
+
continue;
|
|
637
|
+
if (!ts.isVariableStatement(statement) ||
|
|
638
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
639
|
+
statement.declarationList.declarations.length !== 1)
|
|
640
|
+
return false;
|
|
641
|
+
const { initializer } = statement.declarationList.declarations[0];
|
|
642
|
+
if (initializer && dispatch(initializer))
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
if (setup(fn.body))
|
|
648
|
+
return true;
|
|
649
|
+
const call = forwardedCall(fn, true);
|
|
650
|
+
if (!call || ts.isOptionalChain(call))
|
|
651
|
+
return false;
|
|
652
|
+
// The module audit already rules out reassignment and async or generator
|
|
653
|
+
// helpers. An imported alias alone does not identify the runtime module.
|
|
654
|
+
const target = checker
|
|
655
|
+
.getSymbolAtLocation(call.expression)
|
|
656
|
+
?.declarations?.find((declaration) => ts.isFunctionDeclaration(declaration) &&
|
|
657
|
+
!!declaration.body &&
|
|
658
|
+
declaration.parent === fn.parent);
|
|
659
|
+
seen.add(fn);
|
|
660
|
+
return !!target && reachesReact(target, seen);
|
|
661
|
+
}
|
|
662
|
+
function localExportName(symbol, fn) {
|
|
663
|
+
if (fn.name?.text === symbol.name &&
|
|
664
|
+
fn.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.ExportKeyword))
|
|
665
|
+
return fn.name;
|
|
666
|
+
const alias = symbol.declarations?.find((item) => ts.isExportSpecifier(item) && localExportBody(item, fn.getSourceFile()) === fn);
|
|
667
|
+
return alias?.name;
|
|
668
|
+
}
|
|
669
|
+
function originalExportName(sf, name, fn, depth = 0) {
|
|
670
|
+
if (depth === 5)
|
|
671
|
+
return false;
|
|
672
|
+
const exported = exports(sf).find((item) => item.name === name);
|
|
673
|
+
if (!exported || !identity(exported).declarations?.includes(fn))
|
|
674
|
+
return false;
|
|
675
|
+
if (fn.parent === sf)
|
|
676
|
+
return localExportName(exported, fn)?.text ?? false;
|
|
677
|
+
const reexports = sf.statements
|
|
678
|
+
.filter(ts.isExportDeclaration)
|
|
679
|
+
.filter((edge) => !edge.isTypeOnly);
|
|
680
|
+
const named = reexports.filter(({ exportClause }) => exportClause &&
|
|
681
|
+
ts.isNamedExports(exportClause) &&
|
|
682
|
+
exportClause.elements.some((item) => !item.isTypeOnly && item.name.text === name));
|
|
683
|
+
// ESM resolves an explicit export before consulting any star exports.
|
|
684
|
+
const edges = named.length ? named : reexports.filter((edge) => !edge.exportClause);
|
|
685
|
+
// A barrel with multiple candidate exports does not prove which implementation runs.
|
|
686
|
+
if (edges.length !== 1)
|
|
687
|
+
return false;
|
|
688
|
+
const edge = edges[0];
|
|
689
|
+
if (edge.attributes || !edge.moduleSpecifier)
|
|
690
|
+
return false;
|
|
691
|
+
const specifier = request(edge.moduleSpecifier);
|
|
692
|
+
if (!specifier || (!specifier.startsWith('./') && !specifier.startsWith('../')))
|
|
693
|
+
return false;
|
|
694
|
+
const stem = path.resolve(path.dirname(sf.fileName), specifier);
|
|
695
|
+
if (fs.existsSync(path.join(stem, 'package.json')))
|
|
696
|
+
return false;
|
|
697
|
+
const next = target(specifier, sf.fileName);
|
|
698
|
+
if (!next || !owned(next) || fs.realpathSync(next) !== next || !sourceFile.test(next))
|
|
699
|
+
return false;
|
|
700
|
+
const executable = installed(specifier, sf.fileName);
|
|
701
|
+
if (executable && executable !== next)
|
|
702
|
+
return false;
|
|
703
|
+
const candidates = new Set([stem]);
|
|
704
|
+
for (const prefix of [stem, path.join(stem, 'index')])
|
|
705
|
+
for (const suffix of ['', ...(options.moduleSuffixes ?? [])])
|
|
706
|
+
for (const extension of [
|
|
707
|
+
'.ts',
|
|
708
|
+
'.tsx',
|
|
709
|
+
'.tsrx',
|
|
710
|
+
'.js',
|
|
711
|
+
'.jsx',
|
|
712
|
+
'.mjs',
|
|
713
|
+
'.mts',
|
|
714
|
+
'.cjs',
|
|
715
|
+
'.cts',
|
|
716
|
+
'.json',
|
|
717
|
+
])
|
|
718
|
+
candidates.add(prefix + suffix + extension);
|
|
719
|
+
if ([...candidates].some((file) => file !== next && fs.statSync(file, { throwIfNoEntry: false })?.isFile()))
|
|
720
|
+
return false;
|
|
721
|
+
const child = program.getSourceFile(next);
|
|
722
|
+
if (!child)
|
|
723
|
+
return false;
|
|
724
|
+
const binding = edge.exportClause && ts.isNamedExports(edge.exportClause)
|
|
725
|
+
? edge.exportClause.elements.find((item) => !item.isTypeOnly && item.name.text === name)
|
|
726
|
+
: undefined;
|
|
727
|
+
return originalExportName(child, binding?.propertyName?.text ?? name, fn, depth + 1);
|
|
573
728
|
}
|
|
574
729
|
const sharedExports = new Map();
|
|
575
730
|
function isSharedExport(module, imported, from) {
|
|
@@ -595,36 +750,49 @@ function modulePlan(pkg, project, entry, candidates, link, options, outputDir, b
|
|
|
595
750
|
const result = new Map();
|
|
596
751
|
return (requested) => {
|
|
597
752
|
if (!result.has(requested)) {
|
|
598
|
-
let
|
|
753
|
+
let contract = null;
|
|
599
754
|
const ex = exportsOfRoot.find((s) => s.name === requested), sym = ex && identity(ex);
|
|
600
755
|
const fn = sym?.declarations?.find((s) => ts.isFunctionDeclaration(s) && !!s.body);
|
|
601
756
|
if (fn?.name && /^use[A-Z0-9]/.test(fn.name.text)) {
|
|
602
757
|
const sf = fn.getSourceFile(), files = new Map();
|
|
603
758
|
const shared = new Set();
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
759
|
+
const localExports = new Set();
|
|
760
|
+
if (discover(sf, files, new Set(), shared, localExports) && shared.size) {
|
|
761
|
+
contract = {
|
|
762
|
+
requiresReact: () => readsContext(fn, new Set(), files, true) &&
|
|
763
|
+
originalExportName(root, requested, fn) !== false &&
|
|
764
|
+
reachesReact(fn),
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
const hasLocalExports = localExports.size > 0;
|
|
768
|
+
const name = hasLocalExports && contract ? originalExportName(root, requested, fn) : fn.name.text;
|
|
769
|
+
if (contract && name && readsContext(fn, new Set(), files)) {
|
|
607
770
|
const raw = output(sf), entry = raw.replace(/-impl\.ts$/, '.ts');
|
|
608
|
-
const bound = exports(sf).flatMap((
|
|
609
|
-
const impl = identity(
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
771
|
+
const bound = exports(sf).flatMap((symbol) => {
|
|
772
|
+
const impl = identity(symbol).declarations?.find((d) => ts.isFunctionDeclaration(d) && !!d.body);
|
|
773
|
+
if (!impl || impl.getSourceFile() !== sf)
|
|
774
|
+
return [];
|
|
775
|
+
const key = hasLocalExports ? localExportName(symbol, impl) : impl.name;
|
|
776
|
+
return key &&
|
|
777
|
+
ts.isIdentifier(key) &&
|
|
778
|
+
/^use[A-Z0-9]/.test(key.text) &&
|
|
613
779
|
readsContext(impl, new Set(), files)
|
|
614
|
-
? [
|
|
780
|
+
? [key.text]
|
|
615
781
|
: [];
|
|
616
782
|
});
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
783
|
+
if (bound.includes(name)) {
|
|
784
|
+
const code = [
|
|
785
|
+
`import { bindStoreHook } from ${JSON.stringify(relative(entry, shim))};`,
|
|
786
|
+
`import * as implementations from ${JSON.stringify(relative(entry, raw))};`,
|
|
787
|
+
...bound.map((name) => `export const ${name} = bindStoreHook(implementations.${name});`),
|
|
788
|
+
'',
|
|
789
|
+
].join('\n');
|
|
790
|
+
files.set(entry, nativeDirective + code);
|
|
791
|
+
contract.plan = { entry, name, files, isSharedExport };
|
|
792
|
+
}
|
|
625
793
|
}
|
|
626
794
|
}
|
|
627
|
-
result.set(requested,
|
|
795
|
+
result.set(requested, contract);
|
|
628
796
|
}
|
|
629
797
|
return result.get(requested) ?? null;
|
|
630
798
|
};
|
package/dist/store-hook-uses.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { hasEval, nativeOutputOwner, renderUses } from './forwarded-props.js';
|
|
2
2
|
import { componentTagOrExport, localSymbol, localUses, portableArguments, scalar, valueReads, } from './local-uses.js';
|
|
3
|
-
import { skipWrappers } from './expression-wrappers.js';
|
|
3
|
+
import { literalCondition, skipWrappers } from './expression-wrappers.js';
|
|
4
4
|
import ts from 'typescript';
|
|
5
5
|
function literal(value) {
|
|
6
6
|
return (ts.isStringLiteralLike(value) ||
|
|
@@ -20,25 +20,6 @@ function literalOptions(value) {
|
|
|
20
20
|
literalOptions(p.initializer));
|
|
21
21
|
return literal(value);
|
|
22
22
|
}
|
|
23
|
-
/** The short-circuit behavior of a literal operand, independent of project types. */
|
|
24
|
-
function literalCondition(expr) {
|
|
25
|
-
const value = skipWrappers(expr);
|
|
26
|
-
if (value.kind === ts.SyntaxKind.NullKeyword || ts.isVoidExpression(value))
|
|
27
|
-
return 'nullish';
|
|
28
|
-
if (ts.isStringLiteralLike(value))
|
|
29
|
-
return value.text.length ? 'truthy' : 'falsy';
|
|
30
|
-
if (ts.isNumericLiteral(value))
|
|
31
|
-
return Number(value.text) ? 'truthy' : 'falsy';
|
|
32
|
-
if (value.kind === ts.SyntaxKind.FalseKeyword)
|
|
33
|
-
return 'falsy';
|
|
34
|
-
if (value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
35
|
-
ts.isArrowFunction(value) ||
|
|
36
|
-
ts.isFunctionExpression(value) ||
|
|
37
|
-
ts.isClassExpression(value) ||
|
|
38
|
-
ts.isArrayLiteralExpression(value) ||
|
|
39
|
-
ts.isObjectLiteralExpression(value))
|
|
40
|
-
return 'truthy';
|
|
41
|
-
}
|
|
42
23
|
/** A later spread or unknown key may replace the selected own data property. */
|
|
43
24
|
function finalOwnField(source, key) {
|
|
44
25
|
if (key === '__proto__')
|
package/dist/store-inputs.js
CHANGED
|
@@ -247,8 +247,10 @@ export function storeSources(root, config, unchanged, changesRenderer = () => fa
|
|
|
247
247
|
return exported;
|
|
248
248
|
}
|
|
249
249
|
function typedFile(from, specifier) {
|
|
250
|
-
|
|
250
|
+
const file = ts.resolveModuleName(specifier, from, options, ts.sys, cache).resolvedModule
|
|
251
251
|
?.resolvedFileName;
|
|
252
|
+
// TypeScript uses slash-delimited paths even on Windows.
|
|
253
|
+
return file && path.normalize(file);
|
|
252
254
|
}
|
|
253
255
|
function inProject(file, directory) {
|
|
254
256
|
return (file.startsWith(directory + path.sep) && !file.includes(path.sep + 'node_modules' + path.sep));
|
package/dist/transform.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ export declare const MAP_PARAMETER_WRITE = "map-parameter-write";
|
|
|
5
5
|
export declare const MAP_CALLBACK_SCOPE = "map-callback-scope";
|
|
6
6
|
export declare const CONTEXT_ALIAS = "context-path-alias";
|
|
7
7
|
export declare const CONTEXT_PROVIDER_GETTER = "context-provider-getter";
|
|
8
|
+
export declare const ELEMENT_CHILD_ALIAS = "element-child-path-alias";
|
|
9
|
+
export declare const INTL_PROVIDER_DEFAULTS = "intl-provider-defaults";
|
|
10
|
+
export declare const REACT_TYPE_PROJECTION = "react-type-projection";
|
|
11
|
+
export declare const BUFFERED_SSR_OPTIONS = "buffered-ssr-options";
|
|
12
|
+
export declare const BUFFERED_SSR_REACT_COMPONENT = "buffered-ssr-react-component";
|
|
8
13
|
/**
|
|
9
14
|
* Rewrite one React module as Octane.
|
|
10
15
|
*/
|
|
@@ -25,6 +30,7 @@ export declare function bind(filename: string, source: string): {
|
|
|
25
30
|
checker: ts.TypeChecker;
|
|
26
31
|
};
|
|
27
32
|
export declare function nativeReactCompat(module: string, name: string): boolean;
|
|
33
|
+
export declare function skipParens(node: ts.Expression): ts.Expression;
|
|
28
34
|
/**
|
|
29
35
|
* What a type renders as: 1 when every member is a string, 2 when every
|
|
30
36
|
* member is a string, number, or bigint and one is not a string, else 0. The
|