@octanejs/codemod 0.2.0 → 0.2.2
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 +140 -12
- package/dist/bin.js +2 -2
- package/dist/effect-receivers.d.ts +14 -2
- package/dist/effect-receivers.js +171 -22
- package/dist/expression-wrappers.d.ts +2 -0
- package/dist/expression-wrappers.js +19 -0
- package/dist/forwarded-props.d.ts +4 -1
- package/dist/forwarded-props.js +18 -3
- package/dist/linked-state.d.ts +1 -1
- package/dist/linked-state.js +7 -3
- package/dist/local-react-hook.d.ts +13 -6
- package/dist/local-react-hook.js +316 -53
- package/dist/local-uses.d.ts +10 -1
- package/dist/local-uses.js +87 -5
- package/dist/mappings.d.ts +7 -1
- package/dist/mappings.js +21 -3
- package/dist/path-svg.d.ts +68 -4
- package/dist/path-svg.js +162 -56
- package/dist/project.js +1589 -147
- package/dist/react-intl.d.ts +1 -1
- package/dist/react-intl.js +43 -6
- package/dist/react-island-sources.d.ts +53 -0
- package/dist/react-island-sources.js +979 -0
- package/dist/react-renderer.d.ts +2 -0
- package/dist/react-renderer.js +94 -0
- package/dist/scoped-store-contract.d.ts +9 -2
- package/dist/scoped-store-contract.js +209 -33
- package/dist/store-hook-uses.d.ts +7 -5
- package/dist/store-hook-uses.js +218 -62
- package/dist/store-inputs.d.ts +9 -2
- package/dist/store-inputs.js +213 -16
- package/dist/strong.d.ts +2 -2
- package/dist/strong.js +72 -18
- package/dist/transform.d.ts +14 -0
- package/dist/transform.js +2856 -292
- package/dist/types.d.ts +40 -10
- package/package.json +4 -3
- package/src/path-svg.ts +183 -62
- package/templates/octane-path-svg.ts +5 -1
- package/templates/react-intl.tsrx +64 -11
|
@@ -3,13 +3,16 @@ import { localUses } from './local-uses.js';
|
|
|
3
3
|
export declare function contains(node: ts.Node, matches: (node: ts.Node) => boolean): boolean;
|
|
4
4
|
export declare function hasEval(file: ts.SourceFile): boolean;
|
|
5
5
|
export declare function hasPrototypeAccess(file: ts.SourceFile): boolean;
|
|
6
|
+
export declare function rendersElementChildren(element: ts.JsxOpeningLikeElement): boolean;
|
|
6
7
|
export declare function renderUses(refs: ts.Identifier[]): ts.Identifier[];
|
|
8
|
+
/** A local root handle stays owned while callers only invoke its two root methods. */
|
|
9
|
+
export declare function rootHandleEscapes(refs: ts.Identifier[], inOwner?: (ref: ts.Identifier) => boolean): boolean;
|
|
7
10
|
type RenderTypes = {
|
|
8
11
|
typeOf: (node: ts.Node) => ts.Type;
|
|
9
12
|
text: (node: ts.Node) => boolean;
|
|
10
13
|
};
|
|
11
14
|
/** Follow the value being rendered all the way to its containing component's return. */
|
|
12
|
-
export declare function nativeOutputOwner(value: ts.Node, slot: (tag: ts.Identifier, prop: string) => boolean, { typeOf, text }: RenderTypes): ts.ArrowFunction | ts.
|
|
15
|
+
export declare function nativeOutputOwner(value: ts.Node, slot: (tag: ts.Identifier, prop: string) => boolean, { typeOf, text }: RenderTypes): ts.ArrowFunction | ts.FunctionExpression | ts.FunctionDeclaration | undefined;
|
|
13
16
|
export declare function literalJsxAttribute(attr: ts.JsxAttributeLike): boolean;
|
|
14
17
|
/** Follow one stored SVG value to its only runtime read, inside the same component body. */
|
|
15
18
|
export declare function svgLocalReads(checker: ts.TypeChecker, uses: ReturnType<typeof localUses>, typeOf: RenderTypes['typeOf'], reactNode: (annotation: ts.TypeNode) => boolean): (element: ts.JsxSelfClosingElement) => ts.Identifier | undefined;
|
package/dist/forwarded-props.js
CHANGED
|
@@ -73,6 +73,11 @@ function incoming(param, prop) {
|
|
|
73
73
|
// Raw-text and void HTML elements do not render ordinary child elements.
|
|
74
74
|
const nonElementHosts = /^(?:textarea|title|script|style|option|noscript|iframe|noembed|noframes|xmp|plaintext|area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
|
|
75
75
|
const hookName = /^use(?:[A-Z0-9]|$)/;
|
|
76
|
+
export function rendersElementChildren(element) {
|
|
77
|
+
return (ts.isIdentifier(element.tagName) &&
|
|
78
|
+
!nonElementHosts.test(element.tagName.text) &&
|
|
79
|
+
!element.attributes.properties.some((attr) => ts.isJsxAttribute(attr) && attr.name.getText() === 'dangerouslySetInnerHTML'));
|
|
80
|
+
}
|
|
76
81
|
// A scalar assertion can turn a child into a text binding. Require a member
|
|
77
82
|
// that the compiler cannot classify as primitive text, even through aliases.
|
|
78
83
|
function renderType(type) {
|
|
@@ -88,6 +93,18 @@ export function renderUses(refs) {
|
|
|
88
93
|
return !ts.isTypeQueryNode(name.parent) && !ts.isTypeOnlyExportDeclaration(ref.parent);
|
|
89
94
|
});
|
|
90
95
|
}
|
|
96
|
+
/** A local root handle stays owned while callers only invoke its two root methods. */
|
|
97
|
+
export function rootHandleEscapes(refs, inOwner) {
|
|
98
|
+
return renderUses(refs).some((ref) => {
|
|
99
|
+
const member = ref.parent;
|
|
100
|
+
return (!ts.isPropertyAccessExpression(member) ||
|
|
101
|
+
member.expression !== ref ||
|
|
102
|
+
(member.name.text !== 'render' && member.name.text !== 'unmount') ||
|
|
103
|
+
!ts.isCallExpression(member.parent) ||
|
|
104
|
+
member.parent.expression !== member ||
|
|
105
|
+
inOwner?.(ref) === false);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
91
108
|
/** Follow the value being rendered all the way to its containing component's return. */
|
|
92
109
|
export function nativeOutputOwner(value, slot, { typeOf, text }) {
|
|
93
110
|
for (;;) {
|
|
@@ -114,9 +131,7 @@ export function nativeOutputOwner(value, slot, { typeOf, text }) {
|
|
|
114
131
|
: ts.isJsxElement(parent) && !parent.children.some((child) => child === value))
|
|
115
132
|
return;
|
|
116
133
|
if (/^[a-z]/.test(tag.text)) {
|
|
117
|
-
if (attr ||
|
|
118
|
-
nonElementHosts.test(tag.text) ||
|
|
119
|
-
element.attributes.properties.some((a) => ts.isJsxAttribute(a) && a.name.getText() === 'dangerouslySetInnerHTML'))
|
|
134
|
+
if (attr || !rendersElementChildren(element))
|
|
120
135
|
return;
|
|
121
136
|
}
|
|
122
137
|
else {
|
package/dist/linked-state.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
/** A false-to-true update that returns null on the old pass before retrying this exact prelude. */
|
|
3
|
-
export declare function linkedRenderState(owner: ts.FunctionDeclaration, checker: ts.TypeChecker, uses: (name: ts.Identifier) => ts.Identifier[], isReactState: (expr: ts.Expression) => boolean): {
|
|
3
|
+
export declare function linkedRenderState(owner: ts.FunctionDeclaration, checker: ts.TypeChecker, uses: (name: ts.Identifier) => ts.Identifier[], isReactState: (expr: ts.Expression) => boolean, isBooleanValue: (expr: ts.Expression) => boolean): {
|
|
4
4
|
state: ts.Statement;
|
|
5
5
|
guard: ts.IfStatement;
|
|
6
6
|
name: string;
|
package/dist/linked-state.js
CHANGED
|
@@ -16,7 +16,7 @@ function ungroup(expr) {
|
|
|
16
16
|
return ts.isParenthesizedExpression(expr) ? ungroup(expr.expression) : expr;
|
|
17
17
|
}
|
|
18
18
|
/** A false-to-true update that returns null on the old pass before retrying this exact prelude. */
|
|
19
|
-
export function linkedRenderState(owner, checker, uses, isReactState) {
|
|
19
|
+
export function linkedRenderState(owner, checker, uses, isReactState, isBooleanValue) {
|
|
20
20
|
if (!owner.body ||
|
|
21
21
|
owner.asteriskToken ||
|
|
22
22
|
owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword) ||
|
|
@@ -96,8 +96,12 @@ export function linkedRenderState(owner, checker, uses, isReactState) {
|
|
|
96
96
|
const initial = ungroup(hook.arguments[0]);
|
|
97
97
|
if (body.length === 3) {
|
|
98
98
|
const once = negated(initial);
|
|
99
|
-
const read = once && negated(once);
|
|
100
|
-
if (
|
|
99
|
+
const read = (once && negated(once)) ?? initial;
|
|
100
|
+
if (prop(read) &&
|
|
101
|
+
prop(flag) &&
|
|
102
|
+
read.name.text === flag.name.text &&
|
|
103
|
+
// A raw callable prop would run as a React state initializer.
|
|
104
|
+
(read !== initial || isBooleanValue(initial))) {
|
|
101
105
|
return { state, guard, name: value.name.text, flag, eager: initial, replay: [read, flag] };
|
|
102
106
|
}
|
|
103
107
|
}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { localUses } from './local-uses.js';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
export declare function namedRuntimeImport(name: ts.Identifier, checker: ts.TypeChecker): {
|
|
4
|
+
module: string;
|
|
5
|
+
name: string;
|
|
6
|
+
} | undefined;
|
|
7
|
+
export declare function safeSetupLiteral(node: ts.Expression, checker: ts.TypeChecker): boolean;
|
|
8
|
+
/** A direct initialization in an ordinary component with native output. */
|
|
9
|
+
export declare function nativeSetupHook(ref: ts.Expression, uses: ReturnType<typeof localUses>, checker: ts.TypeChecker, diagnostic?: {
|
|
5
10
|
calleeName(callee: ts.Expression): string | null;
|
|
6
11
|
nativeOutput(element: ts.JsxElement | ts.JsxSelfClosingElement): boolean;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
originalReact(): boolean;
|
|
13
|
+
contextSetup?: boolean;
|
|
14
|
+
privateContext?: boolean;
|
|
15
|
+
}, allowAnonymousDefault?: boolean): false | "diagnostic" | "first" | "afterRefs";
|
|
16
|
+
/** Find renderer calls at the start of an unchanged export's setup. */
|
|
17
|
+
export declare function directRendererCalls(sf: ts.SourceFile, checker: ts.TypeChecker, module: 'react' | 'react-dom' | 'react-intl' | ((specifier: string, name: string) => boolean), accepts?: (name: string) => boolean, privateContextExports?: Set<string>): Map<string, string>;
|
|
11
18
|
/** Clone a stateless hook while leaving its DOM reader and subscription shared. */
|
|
12
19
|
export declare function nativeExternalStore(file: string, source: string, name: string, loadLeaf: (specifier: string) => {
|
|
13
20
|
sf: ts.SourceFile;
|
package/dist/local-react-hook.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { contains, renderUses } from './forwarded-props.js';
|
|
3
3
|
import { localUses } from './local-uses.js';
|
|
4
|
+
import { fixedBinding, initializedBefore } from './effect-receivers.js';
|
|
4
5
|
import { skipWrappers } from './expression-wrappers.js';
|
|
5
|
-
function
|
|
6
|
+
export function namedRuntimeImport(name, checker) {
|
|
6
7
|
const binding = checker.getSymbolAtLocation(name)?.declarations?.[0];
|
|
7
8
|
if (!binding || !ts.isImportSpecifier(binding) || binding.isTypeOnly)
|
|
8
9
|
return;
|
|
@@ -10,10 +11,36 @@ function reactImport(name, checker) {
|
|
|
10
11
|
const imp = clause.parent;
|
|
11
12
|
if (!clause.isTypeOnly &&
|
|
12
13
|
ts.isImportDeclaration(imp) &&
|
|
14
|
+
!imp.attributes &&
|
|
15
|
+
ts.isStringLiteral(imp.moduleSpecifier))
|
|
16
|
+
return { module: imp.moduleSpecifier.text, name: (binding.propertyName ?? binding.name).text };
|
|
17
|
+
}
|
|
18
|
+
function rendererImport(name, checker, module = 'react') {
|
|
19
|
+
const imported = namedRuntimeImport(name, checker);
|
|
20
|
+
if (imported?.module === module)
|
|
21
|
+
return imported.name;
|
|
22
|
+
}
|
|
23
|
+
function privateContextReader(callee, checker) {
|
|
24
|
+
if (ts.isIdentifier(callee))
|
|
25
|
+
return rendererImport(callee, checker) === 'useContext';
|
|
26
|
+
if (!ts.isPropertyAccessExpression(callee) && !ts.isElementAccessExpression(callee))
|
|
27
|
+
return false;
|
|
28
|
+
const member = ts.isPropertyAccessExpression(callee)
|
|
29
|
+
? callee.name.text
|
|
30
|
+
: ts.isStringLiteralLike(callee.argumentExpression)
|
|
31
|
+
? callee.argumentExpression.text
|
|
32
|
+
: undefined;
|
|
33
|
+
if (member !== 'useContext' || !ts.isIdentifier(callee.expression))
|
|
34
|
+
return false;
|
|
35
|
+
const binding = checker.getSymbolAtLocation(callee.expression)?.declarations?.[0];
|
|
36
|
+
if (!binding || !ts.isNamespaceImport(binding))
|
|
37
|
+
return false;
|
|
38
|
+
const clause = binding.parent;
|
|
39
|
+
const imp = clause.parent;
|
|
40
|
+
return (!clause.isTypeOnly &&
|
|
13
41
|
!imp.attributes &&
|
|
14
42
|
ts.isStringLiteral(imp.moduleSpecifier) &&
|
|
15
|
-
imp.moduleSpecifier.text === 'react')
|
|
16
|
-
return (binding.propertyName ?? binding.name).text;
|
|
43
|
+
imp.moduleSpecifier.text === 'react');
|
|
17
44
|
}
|
|
18
45
|
function nullRef(statement, checker) {
|
|
19
46
|
if (!ts.isVariableStatement(statement))
|
|
@@ -29,7 +56,7 @@ function nullRef(statement, checker) {
|
|
|
29
56
|
initializer.arguments.length === 1 &&
|
|
30
57
|
initializer.arguments[0].kind === ts.SyntaxKind.NullKeyword &&
|
|
31
58
|
ts.isIdentifier(initializer.expression) &&
|
|
32
|
-
|
|
59
|
+
rendererImport(initializer.expression, checker) === 'useRef');
|
|
33
60
|
}
|
|
34
61
|
function literalDeclaration(declaration) {
|
|
35
62
|
if (!ts.isIdentifier(declaration.name) || declaration.parent.flags & ts.NodeFlags.Using)
|
|
@@ -42,8 +69,58 @@ function literalDeclaration(declaration) {
|
|
|
42
69
|
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
43
70
|
value.kind === ts.SyntaxKind.NullKeyword);
|
|
44
71
|
}
|
|
45
|
-
|
|
46
|
-
|
|
72
|
+
export function safeSetupLiteral(node, checker) {
|
|
73
|
+
if (scalarLiteral(node))
|
|
74
|
+
return true;
|
|
75
|
+
if (ts.isIdentifier(node) && node.text === 'undefined') {
|
|
76
|
+
const symbol = checker.getSymbolAtLocation(node);
|
|
77
|
+
return !!symbol && checker.isUndefinedSymbol(symbol);
|
|
78
|
+
}
|
|
79
|
+
return (ts.isPrefixUnaryExpression(node) &&
|
|
80
|
+
(node.operator === ts.SyntaxKind.PlusToken || node.operator === ts.SyntaxKind.MinusToken) &&
|
|
81
|
+
ts.isNumericLiteral(skipWrappers(node.operand)));
|
|
82
|
+
}
|
|
83
|
+
/** A state declaration before the retained hook, with no explicit lazy initializer. */
|
|
84
|
+
function stateSetup(declaration, checker, owner) {
|
|
85
|
+
const { name, initializer } = declaration;
|
|
86
|
+
if ((declaration.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
87
|
+
declaredVariable(declaration.parent.parent) ||
|
|
88
|
+
!ts.isArrayBindingPattern(name) ||
|
|
89
|
+
!name.elements.every((element) => ts.isOmittedExpression(element) ||
|
|
90
|
+
(!element.dotDotDotToken && !element.initializer && ts.isIdentifier(element.name))) ||
|
|
91
|
+
!initializer ||
|
|
92
|
+
!ts.isCallExpression(initializer) ||
|
|
93
|
+
ts.isOptionalChain(initializer) ||
|
|
94
|
+
!ts.isIdentifier(initializer.expression) ||
|
|
95
|
+
rendererImport(initializer.expression, checker) !== 'useState' ||
|
|
96
|
+
initializer.arguments.length > 1)
|
|
97
|
+
return false;
|
|
98
|
+
const input = initializer.arguments[0];
|
|
99
|
+
if (!input)
|
|
100
|
+
return true;
|
|
101
|
+
const value = skipWrappers(input);
|
|
102
|
+
if (safeSetupLiteral(value, checker))
|
|
103
|
+
return true;
|
|
104
|
+
let binding = ts.isIdentifier(value)
|
|
105
|
+
? checker.getSymbolAtLocation(value)?.valueDeclaration
|
|
106
|
+
: undefined;
|
|
107
|
+
while (binding && ts.isBindingElement(binding))
|
|
108
|
+
binding = binding.parent.parent;
|
|
109
|
+
return !!binding && ts.isParameter(binding) && binding.parent === owner;
|
|
110
|
+
}
|
|
111
|
+
function conditionalOutput(value) {
|
|
112
|
+
if (!ts.isConditionalExpression(value))
|
|
113
|
+
return false;
|
|
114
|
+
const yes = skipWrappers(value.whenTrue);
|
|
115
|
+
const no = skipWrappers(value.whenFalse);
|
|
116
|
+
const jsx = (node) => ts.isJsxElement(node) || ts.isJsxSelfClosingElement(node) || ts.isJsxFragment(node);
|
|
117
|
+
const empty = (node) => node.kind === ts.SyntaxKind.NullKeyword || node.kind === ts.SyntaxKind.FalseKeyword;
|
|
118
|
+
return (jsx(yes) && (jsx(no) || empty(no))) || (empty(yes) && jsx(no));
|
|
119
|
+
}
|
|
120
|
+
/** A direct initialization in an ordinary component with native output. */
|
|
121
|
+
export function nativeSetupHook(ref, uses, checker, diagnostic,
|
|
122
|
+
// Store-root diagnostics can prove an anonymous declaration's default export.
|
|
123
|
+
allowAnonymousDefault = false) {
|
|
47
124
|
const call = ref.parent;
|
|
48
125
|
if (!ts.isCallExpression(call) || call.expression !== ref)
|
|
49
126
|
return false;
|
|
@@ -55,9 +132,10 @@ export function nativeSetupHook(ref, uses, checker, diagnostic) {
|
|
|
55
132
|
return false;
|
|
56
133
|
const statement = decl.parent.parent;
|
|
57
134
|
const block = statement.parent;
|
|
58
|
-
if (!ts.isBlock(block))
|
|
135
|
+
if (!ts.isBlock(block) || declaredVariable(statement))
|
|
59
136
|
return false;
|
|
60
137
|
const position = block.statements.indexOf(statement);
|
|
138
|
+
let afterState = false;
|
|
61
139
|
if (!diagnostic) {
|
|
62
140
|
if (position === 0) {
|
|
63
141
|
if (decl.parent.declarations[0] !== decl)
|
|
@@ -70,13 +148,37 @@ export function nativeSetupHook(ref, uses, checker, diagnostic) {
|
|
|
70
148
|
!block.statements.slice(0, position).every((prior) => nullRef(prior, checker)))
|
|
71
149
|
return false;
|
|
72
150
|
}
|
|
151
|
+
else if (diagnostic.contextSetup || diagnostic.privateContext) {
|
|
152
|
+
if (call.questionDotToken ||
|
|
153
|
+
(diagnostic.privateContext && call.arguments.length !== 0) ||
|
|
154
|
+
decl.parent.declarations.length !== 1 ||
|
|
155
|
+
(decl.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
156
|
+
!call.arguments.every((argument) => ts.isIdentifier(argument) &&
|
|
157
|
+
fixedBinding(checker, argument, call) &&
|
|
158
|
+
initializedBefore(checker, argument, call)) ||
|
|
159
|
+
// Earlier ordinary setup must have returned normally to reach this call.
|
|
160
|
+
!block.statements
|
|
161
|
+
.slice(0, position)
|
|
162
|
+
.every((prior) => ts.isVariableStatement(prior) &&
|
|
163
|
+
(prior.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
164
|
+
prior.declarationList.declarations.length === 1))
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
73
167
|
else {
|
|
168
|
+
const setupDeclaration = (declaration) => {
|
|
169
|
+
if (literalDeclaration(declaration))
|
|
170
|
+
return true;
|
|
171
|
+
if (!stateSetup(declaration, checker, block.parent) || !diagnostic.originalReact())
|
|
172
|
+
return false;
|
|
173
|
+
afterState = true;
|
|
174
|
+
return true;
|
|
175
|
+
};
|
|
74
176
|
const declarations = decl.parent.declarations;
|
|
75
177
|
if (!block.statements
|
|
76
178
|
.slice(0, position)
|
|
77
179
|
.every((prior) => ts.isVariableStatement(prior) &&
|
|
78
|
-
prior.declarationList.declarations.every(
|
|
79
|
-
!declarations.slice(0, declarations.indexOf(decl)).every(
|
|
180
|
+
prior.declarationList.declarations.every(setupDeclaration)) ||
|
|
181
|
+
!declarations.slice(0, declarations.indexOf(decl)).every(setupDeclaration))
|
|
80
182
|
return false;
|
|
81
183
|
}
|
|
82
184
|
const fn = block.parent;
|
|
@@ -109,19 +211,22 @@ export function nativeSetupHook(ref, uses, checker, diagnostic) {
|
|
|
109
211
|
owner = holder.parent.parent;
|
|
110
212
|
name = holder.name;
|
|
111
213
|
}
|
|
112
|
-
if (
|
|
113
|
-
!/^[A-Z]/.test(name.text)
|
|
214
|
+
if ((name
|
|
215
|
+
? !/^[A-Z]/.test(name.text)
|
|
216
|
+
: !allowAnonymousDefault ||
|
|
217
|
+
!owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword)) ||
|
|
114
218
|
!ts.isSourceFile(owner.parent) ||
|
|
115
219
|
!owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ||
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
(
|
|
120
|
-
ts.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
220
|
+
(name &&
|
|
221
|
+
renderUses(uses(name)).some((ref) => {
|
|
222
|
+
const tag = ref.parent;
|
|
223
|
+
return !(diagnostic &&
|
|
224
|
+
(ts.isJsxOpeningElement(tag) ||
|
|
225
|
+
ts.isJsxClosingElement(tag) ||
|
|
226
|
+
ts.isJsxSelfClosingElement(tag)) &&
|
|
227
|
+
tag.tagName === ref &&
|
|
228
|
+
diagnostic.nativeOutput(ts.isJsxSelfClosingElement(tag) ? tag : tag.parent));
|
|
229
|
+
})))
|
|
125
230
|
return false;
|
|
126
231
|
const last = block.statements.at(-1);
|
|
127
232
|
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
@@ -132,45 +237,203 @@ export function nativeSetupHook(ref, uses, checker, diagnostic) {
|
|
|
132
237
|
: ts.isJsxSelfClosingElement(value)
|
|
133
238
|
? value.tagName
|
|
134
239
|
: undefined;
|
|
135
|
-
|
|
240
|
+
const intrinsic = tag && ts.isIdentifier(tag) && /^[a-z]/.test(tag.text);
|
|
241
|
+
if (intrinsic && !afterState)
|
|
242
|
+
return diagnostic ? 'diagnostic' : position === 0 ? 'first' : 'afterRefs';
|
|
243
|
+
// The retained hook runs in setup, before the returned child has a renderer.
|
|
244
|
+
if (diagnostic &&
|
|
245
|
+
!diagnostic.privateContext &&
|
|
246
|
+
(tag || ts.isJsxFragment(value) || conditionalOutput(value)) &&
|
|
247
|
+
!ts.isOptionalChain(call) &&
|
|
248
|
+
!(decl.parent.flags & ts.NodeFlags.Using) &&
|
|
249
|
+
call.arguments.every((input) => {
|
|
250
|
+
const arg = skipWrappers(input);
|
|
251
|
+
return (safeSetupLiteral(arg, checker) ||
|
|
252
|
+
(ts.isIdentifier(arg) &&
|
|
253
|
+
fixedBinding(checker, arg, call) &&
|
|
254
|
+
initializedBefore(checker, arg, call)));
|
|
255
|
+
}))
|
|
256
|
+
return 'diagnostic';
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
function declaredVariable(statement) {
|
|
260
|
+
return (ts.isVariableStatement(statement) &&
|
|
261
|
+
!!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DeclareKeyword));
|
|
262
|
+
}
|
|
263
|
+
function firstRendererCall(body) {
|
|
264
|
+
if (!body)
|
|
265
|
+
return;
|
|
266
|
+
const first = ts.isBlock(body) ? body.statements[0] : undefined;
|
|
267
|
+
const value = ts.isBlock(body)
|
|
268
|
+
? first &&
|
|
269
|
+
(ts.isVariableStatement(first)
|
|
270
|
+
? first.declarationList.declarations[0]?.initializer
|
|
271
|
+
: ts.isReturnStatement(first) || ts.isExpressionStatement(first)
|
|
272
|
+
? first.expression
|
|
273
|
+
: undefined)
|
|
274
|
+
: skipWrappers(body);
|
|
275
|
+
if (value && ts.isCallExpression(value))
|
|
276
|
+
return { call: value, callee: value.expression, ambient: !!first && declaredVariable(first) };
|
|
277
|
+
}
|
|
278
|
+
function privateContextCall(sf, checker, uses, outer, callee) {
|
|
279
|
+
if (ts.isOptionalChain(outer) || outer.arguments.length)
|
|
280
|
+
return false;
|
|
281
|
+
const binding = checker.getSymbolAtLocation(callee)?.valueDeclaration;
|
|
282
|
+
if (!binding || (!ts.isFunctionDeclaration(binding) && !ts.isVariableDeclaration(binding)))
|
|
283
|
+
return false;
|
|
284
|
+
const name = binding.name;
|
|
285
|
+
if (!name || !ts.isIdentifier(name))
|
|
286
|
+
return false;
|
|
287
|
+
let helper;
|
|
288
|
+
if (ts.isFunctionDeclaration(binding)) {
|
|
289
|
+
if (binding.parent === sf)
|
|
290
|
+
helper = binding;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
const list = binding.parent;
|
|
294
|
+
const statement = list.parent;
|
|
295
|
+
if (ts.isVariableDeclarationList(list) &&
|
|
296
|
+
(list.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
297
|
+
ts.isVariableStatement(statement) &&
|
|
298
|
+
statement.parent === sf &&
|
|
299
|
+
!statement.modifiers?.length &&
|
|
300
|
+
binding.initializer) {
|
|
301
|
+
const initializer = skipWrappers(binding.initializer);
|
|
302
|
+
if (ts.isArrowFunction(initializer))
|
|
303
|
+
helper = initializer;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (!helper ||
|
|
307
|
+
helper.modifiers?.length ||
|
|
308
|
+
helper.asteriskToken ||
|
|
309
|
+
helper.parameters.length ||
|
|
310
|
+
renderUses(uses(name)).some((ref) => !ts.isCallExpression(ref.parent) || ref.parent.expression !== ref))
|
|
311
|
+
return false;
|
|
312
|
+
const read = firstRendererCall(helper.body);
|
|
313
|
+
if (!read ||
|
|
314
|
+
read.ambient ||
|
|
315
|
+
ts.isOptionalChain(read.call) ||
|
|
316
|
+
read.call.arguments.length !== 1 ||
|
|
317
|
+
!privateContextReader(read.callee, checker))
|
|
318
|
+
return false;
|
|
319
|
+
const context = read.call.arguments[0];
|
|
320
|
+
const decl = ts.isIdentifier(context) && checker.getSymbolAtLocation(context)?.valueDeclaration;
|
|
321
|
+
if (!decl || !ts.isVariableDeclaration(decl))
|
|
136
322
|
return false;
|
|
137
|
-
|
|
323
|
+
const list = decl.parent;
|
|
324
|
+
const init = decl.initializer;
|
|
325
|
+
return (ts.isVariableDeclarationList(list) &&
|
|
326
|
+
(list.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
327
|
+
ts.isVariableStatement(list.parent) &&
|
|
328
|
+
list.parent.parent === sf &&
|
|
329
|
+
!declaredVariable(list.parent) &&
|
|
330
|
+
!!init &&
|
|
331
|
+
ts.isCallExpression(init) &&
|
|
332
|
+
!ts.isOptionalChain(init) &&
|
|
333
|
+
ts.isIdentifier(init.expression) &&
|
|
334
|
+
rendererImport(init.expression, checker) === 'createContext' &&
|
|
335
|
+
init.arguments.length === 1 &&
|
|
336
|
+
scalarLiteral(init.arguments[0]));
|
|
138
337
|
}
|
|
139
|
-
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
338
|
+
/** A direct effect hook after straight-line const setup, if that setup completes. */
|
|
339
|
+
function setupEffect(body, checker) {
|
|
340
|
+
for (const statement of body.statements) {
|
|
341
|
+
const declaration = ts.isVariableStatement(statement) &&
|
|
342
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const &&
|
|
343
|
+
!declaredVariable(statement) &&
|
|
344
|
+
statement.declarationList.declarations.length === 1
|
|
345
|
+
? statement.declarationList.declarations[0]
|
|
346
|
+
: undefined;
|
|
347
|
+
const simple = declaration && ts.isIdentifier(declaration.name);
|
|
348
|
+
const expression = simple
|
|
349
|
+
? declaration.initializer
|
|
350
|
+
: ts.isExpressionStatement(statement) || ts.isReturnStatement(statement)
|
|
351
|
+
? statement.expression
|
|
352
|
+
: undefined;
|
|
353
|
+
if (!expression)
|
|
354
|
+
return;
|
|
355
|
+
const call = skipWrappers(expression);
|
|
356
|
+
if (ts.isCallExpression(call) &&
|
|
357
|
+
!ts.isOptionalChain(call) &&
|
|
358
|
+
ts.isIdentifier(call.expression)) {
|
|
359
|
+
const name = rendererImport(call.expression, checker);
|
|
360
|
+
if (name === 'useEffect' || name === 'useEffectEvent')
|
|
361
|
+
return name;
|
|
362
|
+
}
|
|
363
|
+
if (!simple)
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
/** Find renderer calls at the start of an unchanged export's setup. */
|
|
368
|
+
export function directRendererCalls(sf, checker, module, accepts, privateContextExports) {
|
|
369
|
+
const calls = new Map();
|
|
143
370
|
if (contains(sf, (n) => ts.isIdentifier(n) && n.text === 'eval'))
|
|
144
|
-
return
|
|
371
|
+
return calls;
|
|
145
372
|
const uses = localUses(sf, checker);
|
|
146
|
-
|
|
147
|
-
if (!
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const first = fn.body
|
|
156
|
-
if (!first)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
373
|
+
const record = (binding, fn) => {
|
|
374
|
+
if (!fn.body ||
|
|
375
|
+
fn.parameters.some((p) => !ts.isIdentifier(p.name) ||
|
|
376
|
+
(p.initializer &&
|
|
377
|
+
(module === 'react-dom' || !scalarLiteral(skipWrappers(p.initializer))))))
|
|
378
|
+
return;
|
|
379
|
+
const arrow = ts.isArrowFunction(fn);
|
|
380
|
+
const forward = typeof module === 'function';
|
|
381
|
+
// A retained wrapper forwards directly and has a stable exported binding.
|
|
382
|
+
const first = firstRendererCall(forward ? (arrow ? undefined : returned(fn)) : fn.body);
|
|
383
|
+
if (forward && (!first || ts.isOptionalChain(first.call)))
|
|
384
|
+
return;
|
|
385
|
+
if ((arrow && (!first || first.ambient || ts.isOptionalChain(first.call))) ||
|
|
386
|
+
(module === 'react-intl' &&
|
|
387
|
+
(!first || first.ambient || ts.isOptionalChain(first.call) || first.call.arguments.length)))
|
|
388
|
+
return;
|
|
389
|
+
// Check escapes before asking the workspace planner to inspect a foreign package.
|
|
390
|
+
if (renderUses(uses(binding)).some((ref) => !ts.isCallExpression(ref.parent) || ref.parent.expression !== ref))
|
|
391
|
+
return;
|
|
392
|
+
const imported = first && ts.isIdentifier(first.callee)
|
|
393
|
+
? namedRuntimeImport(first.callee, checker)
|
|
394
|
+
: undefined;
|
|
395
|
+
const direct = imported &&
|
|
396
|
+
(forward
|
|
397
|
+
? module(imported.module, imported.name)
|
|
398
|
+
: imported.module === module && accepts?.(imported.name))
|
|
399
|
+
? imported.name
|
|
400
|
+
: undefined;
|
|
401
|
+
if (!direct && (arrow || (!(first && privateContextExports) && module !== 'react')))
|
|
402
|
+
return;
|
|
403
|
+
if (direct)
|
|
404
|
+
calls.set(binding.text, direct);
|
|
405
|
+
else if (privateContextExports &&
|
|
406
|
+
first &&
|
|
407
|
+
ts.isIdentifier(first.callee) &&
|
|
408
|
+
!first.ambient &&
|
|
409
|
+
!fn.parameters.length &&
|
|
410
|
+
privateContextCall(sf, checker, uses, first.call, first.callee))
|
|
411
|
+
privateContextExports.add(binding.text);
|
|
412
|
+
else if (module === 'react' && ts.isBlock(fn.body)) {
|
|
413
|
+
const effect = setupEffect(fn.body, checker);
|
|
414
|
+
if (effect)
|
|
415
|
+
calls.set(binding.text, effect);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
for (const statement of sf.statements) {
|
|
419
|
+
if ((!ts.isFunctionDeclaration(statement) && !ts.isVariableStatement(statement)) ||
|
|
420
|
+
statement.modifiers?.length !== 1 ||
|
|
421
|
+
statement.modifiers[0].kind !== ts.SyntaxKind.ExportKeyword)
|
|
170
422
|
continue;
|
|
171
|
-
|
|
423
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
424
|
+
if (statement.name && !statement.asteriskToken)
|
|
425
|
+
record(statement.name, statement);
|
|
426
|
+
}
|
|
427
|
+
else if (module === 'react' &&
|
|
428
|
+
(statement.declarationList.flags & ts.NodeFlags.BlockScoped) === ts.NodeFlags.Const) {
|
|
429
|
+
for (const { name, initializer } of statement.declarationList.declarations) {
|
|
430
|
+
const arrow = initializer && skipWrappers(initializer);
|
|
431
|
+
if (ts.isIdentifier(name) && arrow && ts.isArrowFunction(arrow) && !arrow.modifiers?.length)
|
|
432
|
+
record(name, arrow);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
172
435
|
}
|
|
173
|
-
return
|
|
436
|
+
return calls;
|
|
174
437
|
}
|
|
175
438
|
function scalarLiteral(node) {
|
|
176
439
|
return (ts.isStringLiteralLike(node) ||
|
package/dist/local-uses.d.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
export declare function scalar(type: ts.Type): boolean;
|
|
3
|
+
export declare function localSymbol(checker: ts.TypeChecker, id: ts.Identifier): ts.Symbol | undefined;
|
|
4
|
+
/** A type query reads only the type. Class heritage reads the actual value. */
|
|
5
|
+
export declare function inTypeNode(node: ts.Identifier): boolean;
|
|
6
|
+
type Key = string | undefined;
|
|
3
7
|
export declare function hasLocalMemberWrite(name: ts.Identifier, checker: ts.TypeChecker): boolean;
|
|
4
8
|
/** Inspect syntax and the declarations and assignments supplying its values in this module.
|
|
5
9
|
* Returning false skips a subtree; undefined follows its children and local values. */
|
|
6
10
|
export declare function walkLocalValues(root: ts.Node, checker: ts.TypeChecker, inspect: (node: ts.Node, visit: (node: ts.Node) => boolean) => boolean | undefined): boolean;
|
|
7
11
|
/** Values that can cross a renderer boundary, optionally including a verified original store input. */
|
|
8
|
-
type ImportedArgument = (module: string, name: string) => boolean | 'reject';
|
|
12
|
+
type ImportedArgument = (module: string, name: string, selection?: Key[]) => boolean | 'reject';
|
|
13
|
+
/** Follow the bindings that can supply a value or a selected part of it. */
|
|
14
|
+
export declare function valueReads(value: ts.Node, checker: ts.TypeChecker, selection: Key[], binding: (symbol: ts.Symbol, selection: Key[]) => boolean, leaf: (node: ts.Node) => boolean): boolean;
|
|
15
|
+
export declare function transportedValue(value: ts.Node, checker: ts.TypeChecker, selection: Key[], imported: (module: string, name: string, selection: Key[]) => boolean, jsx?: () => boolean): boolean;
|
|
9
16
|
export declare function portableArguments(checker: ts.TypeChecker, typeOf: (expr: ts.Expression) => ts.Type): (expr: ts.Expression, imported?: ImportedArgument) => boolean;
|
|
10
17
|
/** Whether this value is only named as a JSX component or a module export. */
|
|
11
18
|
export declare function componentTagOrExport(ref: ts.Identifier): boolean;
|
|
19
|
+
/** Lexical context reads allowed by the retained Provider advisory. */
|
|
20
|
+
export declare function unchangedContextProviderUse(ref: ts.Identifier, isReactContextRead: (callee: ts.Expression) => boolean): boolean;
|
|
12
21
|
export declare function localUses(file: ts.SourceFile, checker: ts.TypeChecker): (id: ts.Identifier) => ts.Identifier[];
|
|
13
22
|
export {};
|