@octanejs/codemod 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +295 -4
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +162 -0
- package/dist/compiler.d.ts +11 -0
- package/dist/compiler.js +48 -0
- package/dist/effect-receivers.d.ts +11 -0
- package/dist/effect-receivers.js +315 -0
- package/dist/expression-wrappers.d.ts +6 -0
- package/dist/expression-wrappers.js +16 -0
- package/dist/forwarded-props.d.ts +27 -0
- package/dist/forwarded-props.js +489 -0
- package/dist/index.d.ts +6 -0
- package/{src → dist}/index.js +1 -0
- package/dist/linked-state.d.ts +17 -0
- package/dist/linked-state.js +126 -0
- package/dist/local-calls.d.ts +20 -0
- package/dist/local-calls.js +143 -0
- package/dist/local-react-hook.d.ts +15 -0
- package/dist/local-react-hook.js +353 -0
- package/dist/local-uses.d.ts +13 -0
- package/dist/local-uses.js +987 -0
- package/dist/mappings.d.ts +57 -0
- package/dist/mappings.js +343 -0
- package/dist/path-svg.d.ts +60 -0
- package/dist/path-svg.js +207 -0
- package/dist/project.d.ts +9 -0
- package/dist/project.js +1568 -0
- package/dist/react-intl.d.ts +4 -0
- package/dist/react-intl.js +389 -0
- package/dist/react-renderer.d.ts +9 -0
- package/dist/react-renderer.js +106 -0
- package/dist/scoped-signal.d.ts +4 -0
- package/dist/scoped-signal.js +480 -0
- package/dist/scoped-store-contract.d.ts +13 -0
- package/dist/scoped-store-contract.js +741 -0
- package/dist/store-hook-uses.d.ts +18 -0
- package/dist/store-hook-uses.js +403 -0
- package/dist/store-inputs.d.ts +16 -0
- package/dist/store-inputs.js +391 -0
- package/dist/strong.d.ts +24 -0
- package/dist/strong.js +142 -0
- package/dist/transform.d.ts +27 -0
- package/dist/transform.js +4890 -0
- package/dist/types.d.ts +203 -0
- package/dist/types.js +1 -0
- package/package.json +18 -6
- package/src/path-svg.ts +218 -0
- package/templates/octane-path-svg.ts +56 -0
- package/templates/octane-store-context.tsrx +49 -0
- package/templates/octane-store-hooks.ts +61 -0
- package/templates/react-intl.tsrx +116 -0
- package/src/bin.js +0 -151
- package/src/compiler.js +0 -61
- package/src/index.d.ts +0 -95
- package/src/mappings.js +0 -429
- package/src/project.js +0 -738
- package/src/strong.js +0 -149
- package/src/transform.js +0 -3455
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { isWrapper, skipWrappers } from './expression-wrappers.js';
|
|
3
|
+
function isHelper(node) {
|
|
4
|
+
return (ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isArrowFunction(node));
|
|
5
|
+
}
|
|
6
|
+
export function localCalls(file, checker, reassigned) {
|
|
7
|
+
const targets = new Map();
|
|
8
|
+
const callers = new Map();
|
|
9
|
+
const results = new Map();
|
|
10
|
+
const frames = new Map();
|
|
11
|
+
const owners = new Map();
|
|
12
|
+
function owner(node) {
|
|
13
|
+
if (!owners.has(node)) {
|
|
14
|
+
const fn = ts.findAncestor(node.parent, ts.isFunctionLike);
|
|
15
|
+
owners.set(node, fn && isHelper(fn) ? fn : undefined);
|
|
16
|
+
}
|
|
17
|
+
return owners.get(node);
|
|
18
|
+
}
|
|
19
|
+
function scan(call) {
|
|
20
|
+
let expr = call.expression;
|
|
21
|
+
const aliases = new Set();
|
|
22
|
+
while (ts.isIdentifier(expr) || isWrapper(expr)) {
|
|
23
|
+
if (isWrapper(expr)) {
|
|
24
|
+
expr = expr.expression;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const symbol = checker.getSymbolAtLocation(expr);
|
|
28
|
+
const d = symbol?.valueDeclaration;
|
|
29
|
+
if (!symbol || !d || aliases.has(symbol) || reassigned(symbol) || d.getSourceFile() !== file)
|
|
30
|
+
return;
|
|
31
|
+
aliases.add(symbol);
|
|
32
|
+
if (ts.isVariableDeclaration(d) &&
|
|
33
|
+
d.initializer &&
|
|
34
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
35
|
+
d.parent.flags & ts.NodeFlags.Const)
|
|
36
|
+
expr = d.initializer;
|
|
37
|
+
else {
|
|
38
|
+
expr = d;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (!isHelper(expr) || !expr.body)
|
|
43
|
+
return;
|
|
44
|
+
targets.set(call, expr);
|
|
45
|
+
const uses = callers.get(expr);
|
|
46
|
+
if (uses)
|
|
47
|
+
uses.push(call);
|
|
48
|
+
else
|
|
49
|
+
callers.set(expr, [call]);
|
|
50
|
+
}
|
|
51
|
+
function returns(fn) {
|
|
52
|
+
let found = results.get(fn);
|
|
53
|
+
if (!found) {
|
|
54
|
+
found = [];
|
|
55
|
+
results.set(fn, found);
|
|
56
|
+
if (fn.body &&
|
|
57
|
+
!fn.asteriskToken &&
|
|
58
|
+
!fn.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword)) {
|
|
59
|
+
if (!ts.isBlock(fn.body))
|
|
60
|
+
found.push(fn.body);
|
|
61
|
+
else {
|
|
62
|
+
const collect = (node) => {
|
|
63
|
+
if (ts.isFunctionLike(node) || ts.isClassLike(node))
|
|
64
|
+
return;
|
|
65
|
+
if (ts.isReturnStatement(node)) {
|
|
66
|
+
if (node.expression)
|
|
67
|
+
found.push(node.expression);
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
ts.forEachChild(node, collect);
|
|
71
|
+
};
|
|
72
|
+
ts.forEachChild(fn.body, collect);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
function frame(call, outer) {
|
|
79
|
+
let byOuter = frames.get(call);
|
|
80
|
+
if (!byOuter) {
|
|
81
|
+
byOuter = new Map();
|
|
82
|
+
frames.set(call, byOuter);
|
|
83
|
+
}
|
|
84
|
+
let invocation = byOuter.get(outer);
|
|
85
|
+
if (!invocation) {
|
|
86
|
+
invocation = { call, helper: targets.get(call), outer };
|
|
87
|
+
byOuter.set(outer, invocation);
|
|
88
|
+
}
|
|
89
|
+
return invocation;
|
|
90
|
+
}
|
|
91
|
+
function enclosing(context, fn) {
|
|
92
|
+
for (let f = context; f; f = f.outer)
|
|
93
|
+
if (f.helper === fn)
|
|
94
|
+
return f;
|
|
95
|
+
}
|
|
96
|
+
const identityParameters = new Map();
|
|
97
|
+
function forwardedArgument(call) {
|
|
98
|
+
const fn = targets.get(call);
|
|
99
|
+
if (!fn?.body)
|
|
100
|
+
return;
|
|
101
|
+
let index = identityParameters.get(fn);
|
|
102
|
+
if (index === undefined) {
|
|
103
|
+
const [returned] = returns(fn);
|
|
104
|
+
const result = returned && skipWrappers(returned);
|
|
105
|
+
const single = !ts.isBlock(fn.body) ||
|
|
106
|
+
(fn.body.statements.length === 1 && ts.isReturnStatement(fn.body.statements[0]));
|
|
107
|
+
const plain = fn.parameters.every((p) => ts.isIdentifier(p.name) && !p.initializer && !p.dotDotDotToken && p.name.text !== 'this');
|
|
108
|
+
const param = plain && single && result && ts.isIdentifier(result)
|
|
109
|
+
? checker.getSymbolAtLocation(result)?.valueDeclaration
|
|
110
|
+
: undefined;
|
|
111
|
+
index = param && ts.isParameter(param) ? fn.parameters.indexOf(param) : -1;
|
|
112
|
+
identityParameters.set(fn, index);
|
|
113
|
+
}
|
|
114
|
+
return !call.arguments.some(ts.isSpreadElement) ? call.arguments[index] : undefined;
|
|
115
|
+
}
|
|
116
|
+
// A write inside a wrapper is also a template for invocations whose outer caller
|
|
117
|
+
// was still unknown when its local receiver was indexed.
|
|
118
|
+
function atRead(source, written, read) {
|
|
119
|
+
if (!written || !read)
|
|
120
|
+
return source === undefined ? read : source;
|
|
121
|
+
let left = written, right = read;
|
|
122
|
+
while (left && right) {
|
|
123
|
+
if (left.call !== right.call)
|
|
124
|
+
return false;
|
|
125
|
+
left = left.outer;
|
|
126
|
+
right = right.outer;
|
|
127
|
+
}
|
|
128
|
+
if (!right || !source)
|
|
129
|
+
return source;
|
|
130
|
+
function complete(context) {
|
|
131
|
+
return context === written
|
|
132
|
+
? read
|
|
133
|
+
: context.outer
|
|
134
|
+
? frame(context.call, complete(context.outer))
|
|
135
|
+
: context;
|
|
136
|
+
}
|
|
137
|
+
return complete(source);
|
|
138
|
+
}
|
|
139
|
+
function sites(fn) {
|
|
140
|
+
return isHelper(fn) ? (callers.get(fn) ?? []) : [];
|
|
141
|
+
}
|
|
142
|
+
return { scan, targets, sites, returns, forwardedArgument, owner, frame, enclosing, atRead };
|
|
143
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { localUses } from './local-uses.js';
|
|
3
|
+
/** A direct initialization in an ordinary component returning a native host. */
|
|
4
|
+
export declare function nativeSetupHook(ref: ts.Identifier, uses: ReturnType<typeof localUses>, checker: ts.TypeChecker, diagnostic?: {
|
|
5
|
+
calleeName(callee: ts.Expression): string | null;
|
|
6
|
+
nativeOutput(element: ts.JsxElement | ts.JsxSelfClosingElement): boolean;
|
|
7
|
+
}): false | "diagnostic" | "first" | "afterRefs";
|
|
8
|
+
/** Only a call in the first evaluated statement witnesses a hook in this export.
|
|
9
|
+
* An import used by a sibling or a deferred callback says nothing about it. */
|
|
10
|
+
export declare function directReactHooks(sf: ts.SourceFile, checker: ts.TypeChecker): Map<string, string>;
|
|
11
|
+
/** Clone a stateless hook while leaving its DOM reader and subscription shared. */
|
|
12
|
+
export declare function nativeExternalStore(file: string, source: string, name: string, loadLeaf: (specifier: string) => {
|
|
13
|
+
sf: ts.SourceFile;
|
|
14
|
+
checker: ts.TypeChecker;
|
|
15
|
+
} | undefined): string | undefined;
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { contains, renderUses } from './forwarded-props.js';
|
|
3
|
+
import { localUses } from './local-uses.js';
|
|
4
|
+
import { skipWrappers } from './expression-wrappers.js';
|
|
5
|
+
function reactImport(name, checker) {
|
|
6
|
+
const binding = checker.getSymbolAtLocation(name)?.declarations?.[0];
|
|
7
|
+
if (!binding || !ts.isImportSpecifier(binding) || binding.isTypeOnly)
|
|
8
|
+
return;
|
|
9
|
+
const clause = binding.parent.parent;
|
|
10
|
+
const imp = clause.parent;
|
|
11
|
+
if (!clause.isTypeOnly &&
|
|
12
|
+
ts.isImportDeclaration(imp) &&
|
|
13
|
+
!imp.attributes &&
|
|
14
|
+
ts.isStringLiteral(imp.moduleSpecifier) &&
|
|
15
|
+
imp.moduleSpecifier.text === 'react')
|
|
16
|
+
return (binding.propertyName ?? binding.name).text;
|
|
17
|
+
}
|
|
18
|
+
function nullRef(statement, checker) {
|
|
19
|
+
if (!ts.isVariableStatement(statement))
|
|
20
|
+
return false;
|
|
21
|
+
const { flags, declarations } = statement.declarationList;
|
|
22
|
+
if (!(flags & ts.NodeFlags.Const) || flags & ts.NodeFlags.Using || declarations.length !== 1)
|
|
23
|
+
return false;
|
|
24
|
+
const { name, initializer } = declarations[0];
|
|
25
|
+
return (ts.isIdentifier(name) &&
|
|
26
|
+
!!initializer &&
|
|
27
|
+
ts.isCallExpression(initializer) &&
|
|
28
|
+
!initializer.questionDotToken &&
|
|
29
|
+
initializer.arguments.length === 1 &&
|
|
30
|
+
initializer.arguments[0].kind === ts.SyntaxKind.NullKeyword &&
|
|
31
|
+
ts.isIdentifier(initializer.expression) &&
|
|
32
|
+
reactImport(initializer.expression, checker) === 'useRef');
|
|
33
|
+
}
|
|
34
|
+
function literalDeclaration(declaration) {
|
|
35
|
+
if (!ts.isIdentifier(declaration.name) || declaration.parent.flags & ts.NodeFlags.Using)
|
|
36
|
+
return false;
|
|
37
|
+
if (!declaration.initializer)
|
|
38
|
+
return true;
|
|
39
|
+
const value = skipWrappers(declaration.initializer);
|
|
40
|
+
return (ts.isLiteralExpression(value) ||
|
|
41
|
+
value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
42
|
+
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
43
|
+
value.kind === ts.SyntaxKind.NullKeyword);
|
|
44
|
+
}
|
|
45
|
+
/** A direct initialization in an ordinary component returning a native host. */
|
|
46
|
+
export function nativeSetupHook(ref, uses, checker, diagnostic) {
|
|
47
|
+
const call = ref.parent;
|
|
48
|
+
if (!ts.isCallExpression(call) || call.expression !== ref)
|
|
49
|
+
return false;
|
|
50
|
+
const decl = call.parent;
|
|
51
|
+
if (!ts.isVariableDeclaration(decl) ||
|
|
52
|
+
decl.initializer !== call ||
|
|
53
|
+
!ts.isVariableDeclarationList(decl.parent) ||
|
|
54
|
+
!ts.isVariableStatement(decl.parent.parent))
|
|
55
|
+
return false;
|
|
56
|
+
const statement = decl.parent.parent;
|
|
57
|
+
const block = statement.parent;
|
|
58
|
+
if (!ts.isBlock(block))
|
|
59
|
+
return false;
|
|
60
|
+
const position = block.statements.indexOf(statement);
|
|
61
|
+
if (!diagnostic) {
|
|
62
|
+
if (position === 0) {
|
|
63
|
+
if (decl.parent.declarations[0] !== decl)
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
else if (decl.parent.flags & ts.NodeFlags.Using ||
|
|
67
|
+
call.questionDotToken ||
|
|
68
|
+
call.arguments.length ||
|
|
69
|
+
decl.parent.declarations.length !== 1 ||
|
|
70
|
+
!block.statements.slice(0, position).every((prior) => nullRef(prior, checker)))
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const declarations = decl.parent.declarations;
|
|
75
|
+
if (!block.statements
|
|
76
|
+
.slice(0, position)
|
|
77
|
+
.every((prior) => ts.isVariableStatement(prior) &&
|
|
78
|
+
prior.declarationList.declarations.every(literalDeclaration)) ||
|
|
79
|
+
!declarations.slice(0, declarations.indexOf(decl)).every(literalDeclaration))
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const fn = block.parent;
|
|
83
|
+
if (!(ts.isFunctionDeclaration(fn) || ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) ||
|
|
84
|
+
fn.asteriskToken ||
|
|
85
|
+
fn.modifiers?.some((m) => m.kind === ts.SyntaxKind.AsyncKeyword))
|
|
86
|
+
return false;
|
|
87
|
+
let owner;
|
|
88
|
+
let name;
|
|
89
|
+
if (ts.isFunctionDeclaration(fn)) {
|
|
90
|
+
owner = fn;
|
|
91
|
+
name = fn.name;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
if (!diagnostic || (ts.isFunctionExpression(fn) && fn.name && renderUses(uses(fn.name)).length))
|
|
95
|
+
return false;
|
|
96
|
+
let value = fn;
|
|
97
|
+
while (ts.isCallExpression(value.parent) &&
|
|
98
|
+
value.parent.arguments[0] === value &&
|
|
99
|
+
/^(?:memo|forwardRef)$/.test(diagnostic.calleeName(value.parent.expression) ?? ''))
|
|
100
|
+
value = value.parent;
|
|
101
|
+
const holder = value.parent;
|
|
102
|
+
if (!ts.isVariableDeclaration(holder) ||
|
|
103
|
+
holder.initializer !== value ||
|
|
104
|
+
!ts.isIdentifier(holder.name) ||
|
|
105
|
+
!ts.isVariableDeclarationList(holder.parent) ||
|
|
106
|
+
(holder.parent.flags & ts.NodeFlags.BlockScoped) !== ts.NodeFlags.Const ||
|
|
107
|
+
!ts.isVariableStatement(holder.parent.parent))
|
|
108
|
+
return false;
|
|
109
|
+
owner = holder.parent.parent;
|
|
110
|
+
name = holder.name;
|
|
111
|
+
}
|
|
112
|
+
if (!name ||
|
|
113
|
+
!/^[A-Z]/.test(name.text) ||
|
|
114
|
+
!ts.isSourceFile(owner.parent) ||
|
|
115
|
+
!owner.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) ||
|
|
116
|
+
renderUses(uses(name)).some((ref) => {
|
|
117
|
+
const tag = ref.parent;
|
|
118
|
+
return !(diagnostic &&
|
|
119
|
+
(ts.isJsxOpeningElement(tag) ||
|
|
120
|
+
ts.isJsxClosingElement(tag) ||
|
|
121
|
+
ts.isJsxSelfClosingElement(tag)) &&
|
|
122
|
+
tag.tagName === ref &&
|
|
123
|
+
diagnostic.nativeOutput(ts.isJsxSelfClosingElement(tag) ? tag : tag.parent));
|
|
124
|
+
}))
|
|
125
|
+
return false;
|
|
126
|
+
const last = block.statements.at(-1);
|
|
127
|
+
if (!last || !ts.isReturnStatement(last) || !last.expression)
|
|
128
|
+
return false;
|
|
129
|
+
const value = skipWrappers(last.expression);
|
|
130
|
+
const tag = ts.isJsxElement(value)
|
|
131
|
+
? value.openingElement.tagName
|
|
132
|
+
: ts.isJsxSelfClosingElement(value)
|
|
133
|
+
? value.tagName
|
|
134
|
+
: undefined;
|
|
135
|
+
if (!tag || !ts.isIdentifier(tag) || !/^[a-z]/.test(tag.text))
|
|
136
|
+
return false;
|
|
137
|
+
return diagnostic ? 'diagnostic' : position === 0 ? 'first' : 'afterRefs';
|
|
138
|
+
}
|
|
139
|
+
/** Only a call in the first evaluated statement witnesses a hook in this export.
|
|
140
|
+
* An import used by a sibling or a deferred callback says nothing about it. */
|
|
141
|
+
export function directReactHooks(sf, checker) {
|
|
142
|
+
const hooks = new Map();
|
|
143
|
+
if (contains(sf, (n) => ts.isIdentifier(n) && n.text === 'eval'))
|
|
144
|
+
return hooks;
|
|
145
|
+
const uses = localUses(sf, checker);
|
|
146
|
+
for (const fn of sf.statements) {
|
|
147
|
+
if (!ts.isFunctionDeclaration(fn) ||
|
|
148
|
+
!fn.name ||
|
|
149
|
+
!fn.body ||
|
|
150
|
+
fn.asteriskToken ||
|
|
151
|
+
fn.modifiers?.length !== 1 ||
|
|
152
|
+
fn.modifiers[0].kind !== ts.SyntaxKind.ExportKeyword ||
|
|
153
|
+
fn.parameters.some((p) => !ts.isIdentifier(p.name) || p.initializer))
|
|
154
|
+
continue;
|
|
155
|
+
const first = fn.body.statements[0];
|
|
156
|
+
if (!first)
|
|
157
|
+
continue;
|
|
158
|
+
const value = ts.isVariableStatement(first)
|
|
159
|
+
? first.declarationList.declarations[0]?.initializer
|
|
160
|
+
: ts.isReturnStatement(first) || ts.isExpressionStatement(first)
|
|
161
|
+
? first.expression
|
|
162
|
+
: undefined;
|
|
163
|
+
if (!value || !ts.isCallExpression(value) || !ts.isIdentifier(value.expression))
|
|
164
|
+
continue;
|
|
165
|
+
const hook = reactImport(value.expression, checker);
|
|
166
|
+
if (hook !== 'useState' && hook !== 'useSyncExternalStore')
|
|
167
|
+
continue;
|
|
168
|
+
// A local write can replace the exported function before the importer calls it.
|
|
169
|
+
if (renderUses(uses(fn.name)).some((ref) => !ts.isCallExpression(ref.parent) || ref.parent.expression !== ref))
|
|
170
|
+
continue;
|
|
171
|
+
hooks.set(fn.name.text, hook);
|
|
172
|
+
}
|
|
173
|
+
return hooks;
|
|
174
|
+
}
|
|
175
|
+
function scalarLiteral(node) {
|
|
176
|
+
return (ts.isStringLiteralLike(node) ||
|
|
177
|
+
ts.isNumericLiteral(node) ||
|
|
178
|
+
node.kind === ts.SyntaxKind.TrueKeyword ||
|
|
179
|
+
node.kind === ts.SyntaxKind.FalseKeyword ||
|
|
180
|
+
node.kind === ts.SyntaxKind.NullKeyword);
|
|
181
|
+
}
|
|
182
|
+
function returned(fn) {
|
|
183
|
+
const body = fn.body?.statements;
|
|
184
|
+
const stmt = body?.length === 1 ? body[0] : undefined;
|
|
185
|
+
return stmt && ts.isReturnStatement(stmt) ? stmt.expression : undefined;
|
|
186
|
+
}
|
|
187
|
+
function primitiveSnapshot(node) {
|
|
188
|
+
node = skipWrappers(node);
|
|
189
|
+
if (scalarLiteral(node))
|
|
190
|
+
return true;
|
|
191
|
+
if (ts.isConditionalExpression(node))
|
|
192
|
+
return primitiveSnapshot(node.whenTrue) && primitiveSnapshot(node.whenFalse);
|
|
193
|
+
return ((ts.isPrefixUnaryExpression(node) && node.operator === ts.SyntaxKind.ExclamationToken) ||
|
|
194
|
+
(ts.isBinaryExpression(node) &&
|
|
195
|
+
[
|
|
196
|
+
ts.SyntaxKind.EqualsEqualsToken,
|
|
197
|
+
ts.SyntaxKind.ExclamationEqualsToken,
|
|
198
|
+
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
199
|
+
ts.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
200
|
+
].includes(node.operatorToken.kind)));
|
|
201
|
+
}
|
|
202
|
+
/** An unchanged leaf can read DOM attributes and use an observer, without accessing a renderer. */
|
|
203
|
+
function neutralAttributeStore(sf, checker, read) {
|
|
204
|
+
const ambient = (n, name) => ts.isIdentifier(n) && n.text === name && !checker.getSymbolAtLocation(n);
|
|
205
|
+
const element = (n) => ts.isPropertyAccessExpression(n) &&
|
|
206
|
+
ambient(n.expression, 'document') &&
|
|
207
|
+
(n.name.text === 'documentElement' || n.name.text === 'body');
|
|
208
|
+
const dataset = (n) => ts.isPropertyAccessExpression(n) && n.name.text === 'dataset' && element(n.expression);
|
|
209
|
+
const observer = (n) => {
|
|
210
|
+
if (!ts.isIdentifier(n))
|
|
211
|
+
return false;
|
|
212
|
+
const decl = checker.getSymbolAtLocation(n)?.valueDeclaration;
|
|
213
|
+
return (decl &&
|
|
214
|
+
ts.isVariableDeclaration(decl) &&
|
|
215
|
+
ts.isVariableDeclarationList(decl.parent) &&
|
|
216
|
+
(decl.parent.flags & ts.NodeFlags.Const) !== 0 &&
|
|
217
|
+
decl.initializer &&
|
|
218
|
+
ts.isNewExpression(decl.initializer) &&
|
|
219
|
+
ambient(decl.initializer.expression, 'MutationObserver'));
|
|
220
|
+
};
|
|
221
|
+
const observerMethod = (n) => ts.isPropertyAccessExpression(n) &&
|
|
222
|
+
(n.name.text === 'observe' || n.name.text === 'disconnect') &&
|
|
223
|
+
observer(n.expression);
|
|
224
|
+
const unsafe = (n) => {
|
|
225
|
+
if (ts.isTypeNode(n))
|
|
226
|
+
return false;
|
|
227
|
+
if (ts.isPropertyAccessExpression(n) &&
|
|
228
|
+
!element(n) &&
|
|
229
|
+
!dataset(n) &&
|
|
230
|
+
!observerMethod(n) &&
|
|
231
|
+
!(dataset(n.expression) && !Object.hasOwn(Object.prototype, n.name.text)))
|
|
232
|
+
return true;
|
|
233
|
+
if (ts.isCallExpression(n) && !observerMethod(n.expression))
|
|
234
|
+
return true;
|
|
235
|
+
if (ts.isNewExpression(n) && !ambient(n.expression, 'MutationObserver'))
|
|
236
|
+
return true;
|
|
237
|
+
if (ts.isElementAccessExpression(n) ||
|
|
238
|
+
ts.isTaggedTemplateExpression(n) ||
|
|
239
|
+
ts.isDeleteExpression(n) ||
|
|
240
|
+
ts.isPostfixUnaryExpression(n) ||
|
|
241
|
+
(ts.isPrefixUnaryExpression(n) &&
|
|
242
|
+
(n.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
243
|
+
n.operator === ts.SyntaxKind.MinusMinusToken)) ||
|
|
244
|
+
n.kind === ts.SyntaxKind.ThisKeyword ||
|
|
245
|
+
n.kind === ts.SyntaxKind.SuperKeyword ||
|
|
246
|
+
(ts.isBinaryExpression(n) &&
|
|
247
|
+
n.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
248
|
+
n.operatorToken.kind <= ts.SyntaxKind.LastAssignment))
|
|
249
|
+
return true;
|
|
250
|
+
if (ts.isIdentifier(n)) {
|
|
251
|
+
const parent = n.parent;
|
|
252
|
+
const property = (ts.isPropertyAccessExpression(parent) || ts.isPropertyAssignment(parent)) &&
|
|
253
|
+
parent.name === n;
|
|
254
|
+
if (!property &&
|
|
255
|
+
!checker.getSymbolAtLocation(n) &&
|
|
256
|
+
!['document', 'MutationObserver', 'undefined'].includes(n.text))
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
return !!ts.forEachChild(n, unsafe);
|
|
260
|
+
};
|
|
261
|
+
return (!ts.forEachChild(sf, unsafe) &&
|
|
262
|
+
!contains(read, (node) => ts.isCallExpression(node) ||
|
|
263
|
+
ts.isNewExpression(node) ||
|
|
264
|
+
(ts.isReturnStatement(node) && !!node.expression && !primitiveSnapshot(node.expression))));
|
|
265
|
+
}
|
|
266
|
+
/** Clone a stateless hook while leaving its DOM reader and subscription shared. */
|
|
267
|
+
export function nativeExternalStore(file, source, name, loadLeaf) {
|
|
268
|
+
const sf = ts.createSourceFile(file, source, ts.ScriptTarget.Latest);
|
|
269
|
+
if (sf.statements.length !== 4)
|
|
270
|
+
return;
|
|
271
|
+
const imports = sf.statements.filter(ts.isImportDeclaration);
|
|
272
|
+
const functions = sf.statements.filter(ts.isFunctionDeclaration);
|
|
273
|
+
if (imports.length !== 2 || functions.length !== 2)
|
|
274
|
+
return;
|
|
275
|
+
const named = (imp) => {
|
|
276
|
+
const clause = imp.importClause;
|
|
277
|
+
const bindings = clause?.namedBindings;
|
|
278
|
+
return !imp.attributes &&
|
|
279
|
+
clause &&
|
|
280
|
+
!clause.name &&
|
|
281
|
+
!clause.isTypeOnly &&
|
|
282
|
+
bindings &&
|
|
283
|
+
ts.isNamedImports(bindings) &&
|
|
284
|
+
bindings.elements.every((el) => !el.isTypeOnly)
|
|
285
|
+
? bindings.elements
|
|
286
|
+
: undefined;
|
|
287
|
+
};
|
|
288
|
+
const react = imports.find((imp) => ts.isStringLiteral(imp.moduleSpecifier) && imp.moduleSpecifier.text === 'react');
|
|
289
|
+
const store = imports.find((imp) => imp !== react);
|
|
290
|
+
if (!react ||
|
|
291
|
+
!store ||
|
|
292
|
+
!ts.isStringLiteral(store.moduleSpecifier) ||
|
|
293
|
+
!store.moduleSpecifier.text.startsWith('./'))
|
|
294
|
+
return;
|
|
295
|
+
const reactNames = named(react), storeNames = named(store);
|
|
296
|
+
if (reactNames?.length !== 1 || storeNames?.length !== 2)
|
|
297
|
+
return;
|
|
298
|
+
const hook = reactNames[0];
|
|
299
|
+
if ((hook.propertyName ?? hook.name).text !== 'useSyncExternalStore')
|
|
300
|
+
return;
|
|
301
|
+
const wrapper = functions.find((fn) => fn.name?.text === name);
|
|
302
|
+
const server = functions.find((fn) => fn !== wrapper);
|
|
303
|
+
if (!wrapper ||
|
|
304
|
+
wrapper.modifiers?.length !== 1 ||
|
|
305
|
+
wrapper.modifiers[0].kind !== ts.SyntaxKind.ExportKeyword ||
|
|
306
|
+
wrapper.parameters.length ||
|
|
307
|
+
wrapper.asteriskToken ||
|
|
308
|
+
!server?.name ||
|
|
309
|
+
server.modifiers ||
|
|
310
|
+
server.parameters.length ||
|
|
311
|
+
server.asteriskToken)
|
|
312
|
+
return;
|
|
313
|
+
const snapshot = returned(server);
|
|
314
|
+
const call = returned(wrapper);
|
|
315
|
+
if (!snapshot ||
|
|
316
|
+
!scalarLiteral(snapshot) ||
|
|
317
|
+
!call ||
|
|
318
|
+
!ts.isCallExpression(call) ||
|
|
319
|
+
call.questionDotToken ||
|
|
320
|
+
!ts.isIdentifier(call.expression) ||
|
|
321
|
+
call.expression.text !== hook.name.text ||
|
|
322
|
+
call.arguments.length !== 3 ||
|
|
323
|
+
!call.arguments.every(ts.isIdentifier) ||
|
|
324
|
+
call.arguments[2].getText(sf) !== server.name.text)
|
|
325
|
+
return;
|
|
326
|
+
const imported = (node) => storeNames.find((el) => el.name.text === node.text);
|
|
327
|
+
const subscribe = imported(call.arguments[0]);
|
|
328
|
+
const read = imported(call.arguments[1]);
|
|
329
|
+
if (!subscribe || !read || subscribe === read)
|
|
330
|
+
return;
|
|
331
|
+
const leaf = loadLeaf(store.moduleSpecifier.text);
|
|
332
|
+
if (!leaf)
|
|
333
|
+
return;
|
|
334
|
+
const { sf: storeFile, checker } = leaf;
|
|
335
|
+
if (!storeFile.statements.every((stmt) => ts.isFunctionDeclaration(stmt) &&
|
|
336
|
+
stmt.name &&
|
|
337
|
+
!stmt.asteriskToken &&
|
|
338
|
+
!['document', 'MutationObserver', 'undefined'].includes(stmt.name.text) &&
|
|
339
|
+
stmt.modifiers?.length === 1 &&
|
|
340
|
+
stmt.modifiers[0].kind === ts.SyntaxKind.ExportKeyword))
|
|
341
|
+
return;
|
|
342
|
+
const exported = (item) => storeFile.statements
|
|
343
|
+
.filter(ts.isFunctionDeclaration)
|
|
344
|
+
.find((fn) => fn.name?.text === (item.propertyName ?? item.name).text);
|
|
345
|
+
const subscribeFn = exported(subscribe), readFn = exported(read);
|
|
346
|
+
if (!subscribeFn || !readFn || !neutralAttributeStore(storeFile, checker, readFn))
|
|
347
|
+
return;
|
|
348
|
+
const spec = react.moduleSpecifier;
|
|
349
|
+
return ("// Native external-store wrapper generated by Octane codemod.\n'use no memo';\n" +
|
|
350
|
+
source.slice(0, spec.getStart(sf)) +
|
|
351
|
+
"'octane'" +
|
|
352
|
+
source.slice(spec.end));
|
|
353
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
export declare function scalar(type: ts.Type): boolean;
|
|
3
|
+
export declare function hasLocalMemberWrite(name: ts.Identifier, checker: ts.TypeChecker): boolean;
|
|
4
|
+
/** Inspect syntax and the declarations and assignments supplying its values in this module.
|
|
5
|
+
* Returning false skips a subtree; undefined follows its children and local values. */
|
|
6
|
+
export declare function walkLocalValues(root: ts.Node, checker: ts.TypeChecker, inspect: (node: ts.Node, visit: (node: ts.Node) => boolean) => boolean | undefined): boolean;
|
|
7
|
+
/** Values that can cross a renderer boundary, optionally including a verified original store input. */
|
|
8
|
+
type ImportedArgument = (module: string, name: string) => boolean | 'reject';
|
|
9
|
+
export declare function portableArguments(checker: ts.TypeChecker, typeOf: (expr: ts.Expression) => ts.Type): (expr: ts.Expression, imported?: ImportedArgument) => boolean;
|
|
10
|
+
/** Whether this value is only named as a JSX component or a module export. */
|
|
11
|
+
export declare function componentTagOrExport(ref: ts.Identifier): boolean;
|
|
12
|
+
export declare function localUses(file: ts.SourceFile, checker: ts.TypeChecker): (id: ts.Identifier) => ts.Identifier[];
|
|
13
|
+
export {};
|