@octanejs/codemod 0.1.3 → 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 +305 -5
- 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 +19 -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 -670
- package/src/strong.js +0 -149
- package/src/transform.js +0 -3425
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
type SharedInput = (module: string, name: string) => boolean | 'opaque';
|
|
3
|
+
type ResultCheck = {
|
|
4
|
+
item: ts.ImportSpecifier;
|
|
5
|
+
imported: SharedInput;
|
|
6
|
+
known: Map<ts.Expression, boolean>;
|
|
7
|
+
descriptors: (elements: ts.JsxSelfClosingElement[]) => boolean;
|
|
8
|
+
unchangedHelper?: (module: string, name: string) => boolean;
|
|
9
|
+
};
|
|
10
|
+
/** Bare SVG default imports can switch from a component to an asset URL for a .tsrx issuer. */
|
|
11
|
+
export declare function isBareSvgTag(tag: ts.JsxTagNameExpression, checker: ts.TypeChecker, imports: Set<ts.Node>): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* A store can contain React elements or objects that carry them. Follow the
|
|
14
|
+
* result of each direct hook call through local bindings to where it is used.
|
|
15
|
+
* A condition or discarded method result is safe even with no project types.
|
|
16
|
+
*/
|
|
17
|
+
export declare function storeHookUses(file: ts.SourceFile, checker: ts.TypeChecker, isComponent: (fn: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression) => boolean, typeOf: (node: ts.Node) => ts.Type): (item: ts.ImportSpecifier, imported: SharedInput, descriptors: ResultCheck["descriptors"], unchangedHelper?: ResultCheck["unchangedHelper"]) => boolean;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import { nativeOutputOwner, renderUses } from './forwarded-props.js';
|
|
2
|
+
import { componentTagOrExport, localUses, portableArguments, scalar } from './local-uses.js';
|
|
3
|
+
import { skipWrappers } from './expression-wrappers.js';
|
|
4
|
+
import ts from 'typescript';
|
|
5
|
+
function literal(value) {
|
|
6
|
+
return (ts.isStringLiteralLike(value) ||
|
|
7
|
+
ts.isNumericLiteral(value) ||
|
|
8
|
+
ts.isBigIntLiteral(value) ||
|
|
9
|
+
value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
10
|
+
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
11
|
+
value.kind === ts.SyntaxKind.NullKeyword);
|
|
12
|
+
}
|
|
13
|
+
function literalOptions(value) {
|
|
14
|
+
if (ts.isArrayLiteralExpression(value))
|
|
15
|
+
return value.elements.every(literalOptions);
|
|
16
|
+
if (ts.isObjectLiteralExpression(value))
|
|
17
|
+
return value.properties.every((p) => ts.isPropertyAssignment(p) &&
|
|
18
|
+
(ts.isIdentifier(p.name) || ts.isStringLiteral(p.name)) &&
|
|
19
|
+
p.name.text !== '__proto__' &&
|
|
20
|
+
literalOptions(p.initializer));
|
|
21
|
+
return literal(value);
|
|
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
|
+
/** A final, defined own property in an inline object prevents its binding default from running. */
|
|
43
|
+
function suppliesBinding(binding) {
|
|
44
|
+
const pattern = binding.parent;
|
|
45
|
+
const decl = pattern.parent;
|
|
46
|
+
// Reading an earlier binding can invoke a getter that mutates the object.
|
|
47
|
+
if (!ts.isObjectBindingPattern(pattern) ||
|
|
48
|
+
pattern.elements[0] !== binding ||
|
|
49
|
+
!ts.isVariableDeclaration(decl) ||
|
|
50
|
+
!decl.initializer)
|
|
51
|
+
return false;
|
|
52
|
+
const source = skipWrappers(decl.initializer);
|
|
53
|
+
const key = binding.propertyName ?? binding.name;
|
|
54
|
+
if (!ts.isObjectLiteralExpression(source) ||
|
|
55
|
+
!(ts.isIdentifier(key) || ts.isStringLiteralLike(key)) ||
|
|
56
|
+
key.text === '__proto__')
|
|
57
|
+
return false;
|
|
58
|
+
// A later spread or a key we cannot compare may overwrite the value.
|
|
59
|
+
for (let i = source.properties.length - 1; i >= 0; i--) {
|
|
60
|
+
const field = source.properties[i];
|
|
61
|
+
if (!field.name || !(ts.isIdentifier(field.name) || ts.isStringLiteralLike(field.name)))
|
|
62
|
+
return false;
|
|
63
|
+
if (field.name.text !== key.text)
|
|
64
|
+
continue;
|
|
65
|
+
if (!ts.isPropertyAssignment(field))
|
|
66
|
+
return false;
|
|
67
|
+
const value = skipWrappers(field.initializer);
|
|
68
|
+
return literal(value) || literalCondition(value) === 'truthy';
|
|
69
|
+
}
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
/** Bare SVG default imports can switch from a component to an asset URL for a .tsrx issuer. */
|
|
73
|
+
export function isBareSvgTag(tag, checker, imports) {
|
|
74
|
+
if (!ts.isIdentifier(tag) || /^[a-z]|-/.test(tag.text))
|
|
75
|
+
return false;
|
|
76
|
+
const seen = new Set();
|
|
77
|
+
function fromSvg(value) {
|
|
78
|
+
value = skipWrappers(value);
|
|
79
|
+
if (ts.isConditionalExpression(value))
|
|
80
|
+
return fromSvg(value.whenTrue) || fromSvg(value.whenFalse);
|
|
81
|
+
if (ts.isBinaryExpression(value)) {
|
|
82
|
+
const op = value.operatorToken.kind;
|
|
83
|
+
const left = literalCondition(value.left);
|
|
84
|
+
// A component or asset URL is truthy; the left side of && only tests it.
|
|
85
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
86
|
+
return (left === undefined || left === 'truthy') && fromSvg(value.right);
|
|
87
|
+
if (op === ts.SyntaxKind.BarBarToken)
|
|
88
|
+
return fromSvg(value.left) || (left !== 'truthy' && fromSvg(value.right));
|
|
89
|
+
if (op === ts.SyntaxKind.QuestionQuestionToken)
|
|
90
|
+
return (fromSvg(value.left) ||
|
|
91
|
+
((left === undefined || left === 'nullish') && fromSvg(value.right)));
|
|
92
|
+
}
|
|
93
|
+
if (!ts.isIdentifier(value))
|
|
94
|
+
return false;
|
|
95
|
+
const symbol = checker.getSymbolAtLocation(value);
|
|
96
|
+
if (!symbol || seen.has(symbol))
|
|
97
|
+
return false;
|
|
98
|
+
seen.add(symbol);
|
|
99
|
+
const d = symbol.declarations?.[0];
|
|
100
|
+
if (!d)
|
|
101
|
+
return false;
|
|
102
|
+
if ((ts.isParameter(d) || ts.isBindingElement(d)) && ts.isIdentifier(d.name) && d.initializer)
|
|
103
|
+
return !(ts.isBindingElement(d) && suppliesBinding(d)) && fromSvg(d.initializer);
|
|
104
|
+
if (ts.isVariableDeclaration(d) &&
|
|
105
|
+
d.initializer &&
|
|
106
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
107
|
+
d.parent.flags & ts.NodeFlags.Const)
|
|
108
|
+
return fromSvg(d.initializer);
|
|
109
|
+
return imports.has(d);
|
|
110
|
+
}
|
|
111
|
+
return fromSvg(tag);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A store can contain React elements or objects that carry them. Follow the
|
|
115
|
+
* result of each direct hook call through local bindings to where it is used.
|
|
116
|
+
* A condition or discarded method result is safe even with no project types.
|
|
117
|
+
*/
|
|
118
|
+
export function storeHookUses(file, checker, isComponent, typeOf) {
|
|
119
|
+
// Direct eval can reference the hook or a local result by spelling alone.
|
|
120
|
+
if (file.text.includes('eval') || file.text.includes('\\u')) {
|
|
121
|
+
const directEval = (n) => (ts.isIdentifier(n) && n.text === 'eval') || !!ts.forEachChild(n, directEval);
|
|
122
|
+
if (directEval(file))
|
|
123
|
+
return () => false;
|
|
124
|
+
}
|
|
125
|
+
const uses = localUses(file, checker);
|
|
126
|
+
function reactHook(expr, names) {
|
|
127
|
+
if (!ts.isIdentifier(expr))
|
|
128
|
+
return false;
|
|
129
|
+
const item = checker.getSymbolAtLocation(expr)?.declarations?.[0];
|
|
130
|
+
if (!item || !ts.isImportSpecifier(item) || item.isTypeOnly || item.parent.parent.isTypeOnly)
|
|
131
|
+
return false;
|
|
132
|
+
const statement = item.parent.parent.parent;
|
|
133
|
+
return (!statement.attributes &&
|
|
134
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
135
|
+
statement.moduleSpecifier.text === 'react' &&
|
|
136
|
+
names.includes((item.propertyName ?? item.name).text));
|
|
137
|
+
}
|
|
138
|
+
function effectCallback(fn) {
|
|
139
|
+
const call = fn.parent;
|
|
140
|
+
return (ts.isCallExpression(call) &&
|
|
141
|
+
!call.questionDotToken &&
|
|
142
|
+
call.arguments[0] === fn &&
|
|
143
|
+
reactHook(call.expression, ['useEffect', 'useLayoutEffect']));
|
|
144
|
+
}
|
|
145
|
+
function clickHandler(value, hook) {
|
|
146
|
+
let outer = value;
|
|
147
|
+
while (ts.isParenthesizedExpression(outer.parent))
|
|
148
|
+
outer = outer.parent;
|
|
149
|
+
const hole = outer.parent;
|
|
150
|
+
if (!ts.isJsxExpression(hole) || !ts.isJsxAttribute(hole.parent))
|
|
151
|
+
return false;
|
|
152
|
+
const attr = hole.parent;
|
|
153
|
+
const host = attr.parent.parent;
|
|
154
|
+
return (ts.isIdentifier(host.tagName) &&
|
|
155
|
+
/^[a-z][a-z0-9]*$/.test(host.tagName.text) &&
|
|
156
|
+
ts.isIdentifier(attr.name) &&
|
|
157
|
+
/^onClick(?:Capture)?$/.test(attr.name.text) &&
|
|
158
|
+
nativeOutputOwner(ts.isJsxOpeningElement(host) ? host.parent : host, () => false, {
|
|
159
|
+
typeOf,
|
|
160
|
+
text: (node) => scalar(typeOf(node)),
|
|
161
|
+
}) === ts.findAncestor(hook, ts.isFunctionLike));
|
|
162
|
+
}
|
|
163
|
+
function runsAfterRender(call, hook) {
|
|
164
|
+
const fn = ts.findAncestor(call, ts.isFunctionLike);
|
|
165
|
+
if (!fn)
|
|
166
|
+
return false;
|
|
167
|
+
if (effectCallback(fn) || clickHandler(fn, hook))
|
|
168
|
+
return true;
|
|
169
|
+
const parent = fn.parent;
|
|
170
|
+
if (!ts.isReturnStatement(parent) || parent.expression !== fn)
|
|
171
|
+
return false;
|
|
172
|
+
const setup = ts.findAncestor(parent, ts.isFunctionLike);
|
|
173
|
+
return !!setup && effectCallback(setup);
|
|
174
|
+
}
|
|
175
|
+
function componentUses(fn) {
|
|
176
|
+
if (!isComponent(fn))
|
|
177
|
+
return false;
|
|
178
|
+
const wrapper = fn.parent;
|
|
179
|
+
const outer = ts.isCallExpression(wrapper) &&
|
|
180
|
+
wrapper.arguments[0] === fn &&
|
|
181
|
+
reactHook(wrapper.expression, ['memo', 'forwardRef'])
|
|
182
|
+
? wrapper
|
|
183
|
+
: fn;
|
|
184
|
+
let name;
|
|
185
|
+
if (ts.isFunctionDeclaration(outer))
|
|
186
|
+
name = outer.name;
|
|
187
|
+
else if (ts.isVariableDeclaration(outer.parent) && ts.isIdentifier(outer.parent.name))
|
|
188
|
+
name = outer.parent.name;
|
|
189
|
+
else if (!ts.isExportAssignment(outer.parent))
|
|
190
|
+
return false;
|
|
191
|
+
return !name || renderUses(uses(name)).every(componentTagOrExport);
|
|
192
|
+
}
|
|
193
|
+
const portable = portableArguments(checker, typeOf);
|
|
194
|
+
function argument(expr, check, allowShared = false) {
|
|
195
|
+
return portable(expr, (module, name) => {
|
|
196
|
+
const input = check.imported(module, name);
|
|
197
|
+
// A scalar key can still dispatch to renderer-owned code through the store.
|
|
198
|
+
return input === 'opaque' ? 'reject' : allowShared && input;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function sharedHandle(expr, imported) {
|
|
202
|
+
if (!ts.isIdentifier(expr))
|
|
203
|
+
return false;
|
|
204
|
+
const d = checker.getSymbolAtLocation(expr)?.declarations?.[0];
|
|
205
|
+
if (!d || !ts.isImportSpecifier(d) || d.isTypeOnly || d.parent.parent.isTypeOnly)
|
|
206
|
+
return false;
|
|
207
|
+
const spec = d.parent.parent.parent.moduleSpecifier;
|
|
208
|
+
return (!d.parent.parent.parent.attributes &&
|
|
209
|
+
ts.isStringLiteral(spec) &&
|
|
210
|
+
imported(spec.text, (d.propertyName ?? d.name).text));
|
|
211
|
+
}
|
|
212
|
+
// A call using an original handle can publish its other arguments to another renderer.
|
|
213
|
+
// Only literals and constants initialized with literals qualify.
|
|
214
|
+
function plainData(value) {
|
|
215
|
+
if (ts.isParenthesizedExpression(value) || ts.isSatisfiesExpression(value))
|
|
216
|
+
return plainData(value.expression);
|
|
217
|
+
if (ts.isIdentifier(value)) {
|
|
218
|
+
const d = checker.getSymbolAtLocation(value)?.valueDeclaration;
|
|
219
|
+
return (!!d &&
|
|
220
|
+
ts.isVariableDeclaration(d) &&
|
|
221
|
+
!!d.initializer &&
|
|
222
|
+
d.end <= value.pos &&
|
|
223
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
224
|
+
!!(d.parent.flags & ts.NodeFlags.Const) &&
|
|
225
|
+
plainData(d.initializer));
|
|
226
|
+
}
|
|
227
|
+
return literal(value);
|
|
228
|
+
}
|
|
229
|
+
// A computed key, destructuring default, or intermediate call can evaluate renderer code.
|
|
230
|
+
// Only the original hook result, its named properties and simple const aliases gain handle inputs.
|
|
231
|
+
function originalReceiver(value, check, direct = false) {
|
|
232
|
+
if (ts.isParenthesizedExpression(value) ||
|
|
233
|
+
ts.isNonNullExpression(value) ||
|
|
234
|
+
ts.isSatisfiesExpression(value))
|
|
235
|
+
return originalReceiver(value.expression, check, direct);
|
|
236
|
+
if (!direct && ts.isPropertyAccessExpression(value))
|
|
237
|
+
return originalReceiver(value.expression, check);
|
|
238
|
+
if (ts.isCallExpression(value)) {
|
|
239
|
+
if ((!direct || !value.questionDotToken) &&
|
|
240
|
+
ts.isIdentifier(value.expression) &&
|
|
241
|
+
checker.getSymbolAtLocation(value.expression)?.declarations?.[0] === check.item &&
|
|
242
|
+
value.arguments.every((arg) => plainData(arg) || sharedHandle(arg, check.imported) === true))
|
|
243
|
+
return value;
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (!ts.isIdentifier(value))
|
|
247
|
+
return;
|
|
248
|
+
const d = checker.getSymbolAtLocation(value)?.valueDeclaration;
|
|
249
|
+
if (d &&
|
|
250
|
+
ts.isVariableDeclaration(d) &&
|
|
251
|
+
ts.isIdentifier(d.name) &&
|
|
252
|
+
d.initializer &&
|
|
253
|
+
d.end <= value.pos &&
|
|
254
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
255
|
+
d.parent.flags & ts.NodeFlags.Const)
|
|
256
|
+
return originalReceiver(d.initializer, check, direct);
|
|
257
|
+
}
|
|
258
|
+
// Only a service read directly from the original store with one verified handle
|
|
259
|
+
// receives descriptor arguments. Other calls retain the ordinary data checks.
|
|
260
|
+
function serviceMethod(method, check) {
|
|
261
|
+
const read = method.expression;
|
|
262
|
+
const named = (member) => !member.questionDotToken &&
|
|
263
|
+
ts.isIdentifier(member.name) &&
|
|
264
|
+
!(member.name.text in Function.prototype) &&
|
|
265
|
+
member.name.text !== 'prototype';
|
|
266
|
+
return (named(method) &&
|
|
267
|
+
ts.isCallExpression(read) &&
|
|
268
|
+
!read.questionDotToken &&
|
|
269
|
+
read.arguments.length === 1 &&
|
|
270
|
+
sharedHandle(read.arguments[0], check.imported) === true &&
|
|
271
|
+
ts.isPropertyAccessExpression(read.expression) &&
|
|
272
|
+
named(read.expression) &&
|
|
273
|
+
!!originalReceiver(read.expression.expression, check, true));
|
|
274
|
+
}
|
|
275
|
+
// The same helper can use React APIs if called during render. Preserve
|
|
276
|
+
// calls that are already deferred to an effect or a host click.
|
|
277
|
+
function passesOriginalStore(call, value, check) {
|
|
278
|
+
if (!check.unchangedHelper ||
|
|
279
|
+
call.questionDotToken ||
|
|
280
|
+
!(ts.isExpressionStatement(call.parent) || ts.isVoidExpression(call.parent)) ||
|
|
281
|
+
!ts.isIdentifier(call.expression) ||
|
|
282
|
+
!call.arguments.includes(value))
|
|
283
|
+
return false;
|
|
284
|
+
const item = checker.getSymbolAtLocation(call.expression)?.declarations?.[0];
|
|
285
|
+
if (!item || !ts.isImportSpecifier(item) || item.isTypeOnly || item.parent.parent.isTypeOnly)
|
|
286
|
+
return false;
|
|
287
|
+
const statement = item.parent.parent.parent;
|
|
288
|
+
const hook = originalReceiver(value, check, true);
|
|
289
|
+
return (!statement.attributes &&
|
|
290
|
+
ts.isStringLiteral(statement.moduleSpecifier) &&
|
|
291
|
+
!!hook &&
|
|
292
|
+
runsAfterRender(call, hook) &&
|
|
293
|
+
call.arguments.every((arg) => arg === value || plainData(arg) || literalOptions(arg)) &&
|
|
294
|
+
check.unchangedHelper(statement.moduleSpecifier.text, (item.propertyName ?? item.name).text));
|
|
295
|
+
}
|
|
296
|
+
function bound(name, check) {
|
|
297
|
+
if (ts.isIdentifier(name))
|
|
298
|
+
return uses(name).every((use) => safe(use, check));
|
|
299
|
+
return name.elements.every((el) => ts.isOmittedExpression(el) || bound(el.name, check));
|
|
300
|
+
}
|
|
301
|
+
function safe(value, check) {
|
|
302
|
+
const cached = check.known.get(value);
|
|
303
|
+
if (cached !== undefined)
|
|
304
|
+
return cached;
|
|
305
|
+
check.known.set(value, false);
|
|
306
|
+
const result = sink(value, check) || scalar(typeOf(value));
|
|
307
|
+
check.known.set(value, result);
|
|
308
|
+
return result;
|
|
309
|
+
}
|
|
310
|
+
function sink(value, check) {
|
|
311
|
+
const p = value.parent;
|
|
312
|
+
if (ts.isParenthesizedExpression(p) || ts.isNonNullExpression(p) || ts.isSatisfiesExpression(p))
|
|
313
|
+
return safe(p, check);
|
|
314
|
+
if (ts.isVariableDeclaration(p) &&
|
|
315
|
+
p.initializer === value &&
|
|
316
|
+
ts.isVariableDeclarationList(p.parent) &&
|
|
317
|
+
p.parent.flags & ts.NodeFlags.Const)
|
|
318
|
+
return !p.type && bound(p.name, check);
|
|
319
|
+
if (ts.isExpressionStatement(p) || ts.isVoidExpression(p) || ts.isTypeOfExpression(p))
|
|
320
|
+
return true;
|
|
321
|
+
if (ts.isIfStatement(p) || ts.isWhileStatement(p) || ts.isDoStatement(p))
|
|
322
|
+
return p.expression === value;
|
|
323
|
+
if (ts.isConditionalExpression(p))
|
|
324
|
+
return p.condition === value || safe(p, check);
|
|
325
|
+
if (ts.isPrefixUnaryExpression(p))
|
|
326
|
+
return [
|
|
327
|
+
ts.SyntaxKind.ExclamationToken,
|
|
328
|
+
ts.SyntaxKind.PlusToken,
|
|
329
|
+
ts.SyntaxKind.MinusToken,
|
|
330
|
+
ts.SyntaxKind.TildeToken,
|
|
331
|
+
].includes(p.operator);
|
|
332
|
+
if (ts.isTemplateSpan(p))
|
|
333
|
+
return !ts.isTaggedTemplateExpression(p.parent.parent);
|
|
334
|
+
if (ts.isBinaryExpression(p)) {
|
|
335
|
+
const op = p.operatorToken.kind;
|
|
336
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
337
|
+
return p.left === value || safe(p, check);
|
|
338
|
+
if (op === ts.SyntaxKind.BarBarToken || op === ts.SyntaxKind.QuestionQuestionToken)
|
|
339
|
+
return safe(p, check);
|
|
340
|
+
return ((op >= ts.SyntaxKind.LessThanToken && op <= ts.SyntaxKind.ExclamationEqualsEqualsToken) ||
|
|
341
|
+
(op >= ts.SyntaxKind.PlusToken && op <= ts.SyntaxKind.AsteriskAsteriskToken) ||
|
|
342
|
+
op === ts.SyntaxKind.InKeyword ||
|
|
343
|
+
op === ts.SyntaxKind.InstanceOfKeyword);
|
|
344
|
+
}
|
|
345
|
+
if (ts.isPropertyAccessExpression(p))
|
|
346
|
+
return p.expression === value && safe(p, check);
|
|
347
|
+
if (ts.isElementAccessExpression(p))
|
|
348
|
+
return p.expression === value && argument(p.argumentExpression, check) && safe(p, check);
|
|
349
|
+
if (ts.isArrayLiteralExpression(p) &&
|
|
350
|
+
ts.isCallExpression(p.parent) &&
|
|
351
|
+
p.parent.arguments[1] === p &&
|
|
352
|
+
reactHook(p.parent.expression, ['useEffect', 'useLayoutEffect', 'useMemo', 'useCallback']))
|
|
353
|
+
return true;
|
|
354
|
+
if (ts.isCallExpression(p)) {
|
|
355
|
+
if (p.expression === value) {
|
|
356
|
+
if (p.arguments.every((arg) => argument(arg, check)))
|
|
357
|
+
return safe(p, check);
|
|
358
|
+
if (!p.questionDotToken &&
|
|
359
|
+
ts.isPropertyAccessExpression(value) &&
|
|
360
|
+
p.arguments.some(ts.isJsxSelfClosingElement) &&
|
|
361
|
+
serviceMethod(value, check) &&
|
|
362
|
+
p.arguments.every((arg) => ts.isJsxSelfClosingElement(arg) || literalOptions(arg)) &&
|
|
363
|
+
safe(p, check))
|
|
364
|
+
return check.descriptors(p.arguments.filter(ts.isJsxSelfClosingElement));
|
|
365
|
+
const handle = p.arguments[0];
|
|
366
|
+
return (ts.isPropertyAccessExpression(value) &&
|
|
367
|
+
!!originalReceiver(value.expression, check) &&
|
|
368
|
+
!!handle &&
|
|
369
|
+
sharedHandle(handle, check.imported) === true &&
|
|
370
|
+
p.arguments.slice(1).every(plainData) &&
|
|
371
|
+
safe(p, check));
|
|
372
|
+
}
|
|
373
|
+
if (p.arguments.length === 1 &&
|
|
374
|
+
p.arguments[0] === value &&
|
|
375
|
+
ts.isIdentifier(p.expression) &&
|
|
376
|
+
['String', 'Number', 'Boolean'].includes(p.expression.text) &&
|
|
377
|
+
checker
|
|
378
|
+
.getSymbolAtLocation(p.expression)
|
|
379
|
+
?.declarations?.every((d) => d.getSourceFile().hasNoDefaultLib))
|
|
380
|
+
return true;
|
|
381
|
+
return passesOriginalStore(p, value, check);
|
|
382
|
+
}
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
return (item, imported, descriptors, unchangedHelper) => {
|
|
386
|
+
const check = { item, imported, known: new Map(), descriptors, unchangedHelper };
|
|
387
|
+
const references = uses(item.name);
|
|
388
|
+
return (references.length > 0 &&
|
|
389
|
+
references.every((ref) => {
|
|
390
|
+
const call = ref.parent;
|
|
391
|
+
if (!ts.isCallExpression(call) || call.expression !== ref || call.questionDotToken)
|
|
392
|
+
return false;
|
|
393
|
+
const owner = ts.findAncestor(call, ts.isFunctionLike);
|
|
394
|
+
return (!!owner &&
|
|
395
|
+
(ts.isFunctionDeclaration(owner) ||
|
|
396
|
+
ts.isArrowFunction(owner) ||
|
|
397
|
+
ts.isFunctionExpression(owner)) &&
|
|
398
|
+
componentUses(owner) &&
|
|
399
|
+
call.arguments.every((arg) => argument(arg, check, true)) &&
|
|
400
|
+
safe(call, check));
|
|
401
|
+
}));
|
|
402
|
+
};
|
|
403
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
type Factory = {
|
|
3
|
+
module: string;
|
|
4
|
+
name: string;
|
|
5
|
+
from: string;
|
|
6
|
+
};
|
|
7
|
+
type Opaque = {
|
|
8
|
+
opaque: true;
|
|
9
|
+
};
|
|
10
|
+
/** Resolve application source used by a workspace store consumer. */
|
|
11
|
+
export declare function storeSources(root: string, config: ts.CompilerOptions | null, unchanged: (file: string, source: string) => boolean): {
|
|
12
|
+
inputFactory: (from: string, specifier: string, name: string, shared?: (module: string, name: string, from: string) => boolean) => Factory | Opaque | null;
|
|
13
|
+
helperFunction: (from: string, specifier: string, name: string) => boolean;
|
|
14
|
+
localFile: (from: string, specifier: string) => string | null;
|
|
15
|
+
};
|
|
16
|
+
export {};
|