@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,987 @@
|
|
|
1
|
+
import ts from 'typescript';
|
|
2
|
+
import { isWrapper, skipWrappers } from './expression-wrappers.js';
|
|
3
|
+
import { localCalls } from './local-calls.js';
|
|
4
|
+
export function scalar(type) {
|
|
5
|
+
if (type.isUnion())
|
|
6
|
+
return type.types.every(scalar);
|
|
7
|
+
// A primitive intersection keeps its runtime representation, e.g. a branded string ID.
|
|
8
|
+
if (type.isIntersection())
|
|
9
|
+
return type.types.some(scalar);
|
|
10
|
+
return !!(type.flags &
|
|
11
|
+
(ts.TypeFlags.StringLike |
|
|
12
|
+
ts.TypeFlags.NumberLike |
|
|
13
|
+
ts.TypeFlags.BooleanLike |
|
|
14
|
+
ts.TypeFlags.BigIntLike |
|
|
15
|
+
ts.TypeFlags.Null |
|
|
16
|
+
ts.TypeFlags.Undefined));
|
|
17
|
+
}
|
|
18
|
+
function localSymbol(checker, id) {
|
|
19
|
+
return ts.isShorthandPropertyAssignment(id.parent) && id.parent.name === id
|
|
20
|
+
? checker.getShorthandAssignmentValueSymbol(id.parent)
|
|
21
|
+
: ts.isExportSpecifier(id.parent)
|
|
22
|
+
? checker.getExportSpecifierLocalTargetSymbol(id.parent)
|
|
23
|
+
: checker.getSymbolAtLocation(id);
|
|
24
|
+
}
|
|
25
|
+
function isMember(node) {
|
|
26
|
+
return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node);
|
|
27
|
+
}
|
|
28
|
+
function isLogical(node) {
|
|
29
|
+
return (ts.isBinaryExpression(node) &&
|
|
30
|
+
(node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
31
|
+
node.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
32
|
+
node.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken));
|
|
33
|
+
}
|
|
34
|
+
function sourcePath(selection, path, writing) {
|
|
35
|
+
for (let i = selection.length - 1; i >= 0; i--) {
|
|
36
|
+
const step = selection[i];
|
|
37
|
+
if (typeof step !== 'object') {
|
|
38
|
+
path = [step, ...path];
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
if (writing && !path.length)
|
|
42
|
+
return;
|
|
43
|
+
if (step.offset !== undefined && path.length) {
|
|
44
|
+
const index = path[0];
|
|
45
|
+
path = [
|
|
46
|
+
index !== undefined && /^(0|[1-9]\d*)$/.test(index)
|
|
47
|
+
? String(step.offset + Number(index))
|
|
48
|
+
: undefined,
|
|
49
|
+
...path.slice(1),
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return path;
|
|
55
|
+
}
|
|
56
|
+
// Bound the number of distinct nested call paths explored for a write or returned value.
|
|
57
|
+
const MAX_CALL_CONTEXTS = 64;
|
|
58
|
+
// A file can be queried once per island or store argument. Index its writes once
|
|
59
|
+
// for the checker that owns its symbols, and release them with the source file.
|
|
60
|
+
const writesByFile = new WeakMap();
|
|
61
|
+
function localWrites(file, checker) {
|
|
62
|
+
const cached = writesByFile.get(file);
|
|
63
|
+
if (cached?.checker === checker)
|
|
64
|
+
return cached.writes;
|
|
65
|
+
const bindings = new Map();
|
|
66
|
+
const bindingDefaults = new Set();
|
|
67
|
+
const calls = localCalls(file, checker, (symbol) => bindings.has(symbol));
|
|
68
|
+
const possibleCalls = [];
|
|
69
|
+
const members = new Map();
|
|
70
|
+
const mutationOnly = new Set();
|
|
71
|
+
const pending = [];
|
|
72
|
+
const assigns = new Set();
|
|
73
|
+
function keyOf(node) {
|
|
74
|
+
if (isWrapper(node))
|
|
75
|
+
return keyOf(node.expression);
|
|
76
|
+
if (ts.isStringLiteralLike(node) || ts.isNumericLiteral(node))
|
|
77
|
+
return node.text;
|
|
78
|
+
if (ts.isIdentifier(node)) {
|
|
79
|
+
const d = localSymbol(checker, node)?.valueDeclaration;
|
|
80
|
+
if (d &&
|
|
81
|
+
ts.isVariableDeclaration(d) &&
|
|
82
|
+
d.initializer &&
|
|
83
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
84
|
+
d.parent.flags & ts.NodeFlags.Const &&
|
|
85
|
+
d.end <= node.pos)
|
|
86
|
+
return keyOf(d.initializer);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function memberKey(node) {
|
|
90
|
+
return ts.isPropertyAccessExpression(node) ? node.name.text : keyOf(node.argumentExpression);
|
|
91
|
+
}
|
|
92
|
+
function bindingStep(d) {
|
|
93
|
+
const index = ts.isArrayBindingPattern(d.parent) ? d.parent.elements.indexOf(d) : undefined;
|
|
94
|
+
if (d.dotDotDotToken)
|
|
95
|
+
return { offset: index };
|
|
96
|
+
return index === undefined ? propertyName(d.propertyName ?? d.name) : String(index);
|
|
97
|
+
}
|
|
98
|
+
function definitelyPresent(value) {
|
|
99
|
+
if (isWrapper(value))
|
|
100
|
+
return definitelyPresent(value.expression);
|
|
101
|
+
if (ts.isIdentifier(value)) {
|
|
102
|
+
const d = localSymbol(checker, value)?.valueDeclaration;
|
|
103
|
+
return (!!d &&
|
|
104
|
+
ts.isVariableDeclaration(d) &&
|
|
105
|
+
!!d.initializer &&
|
|
106
|
+
d.end <= value.pos &&
|
|
107
|
+
ts.isVariableDeclarationList(d.parent) &&
|
|
108
|
+
!!(d.parent.flags & ts.NodeFlags.Const) &&
|
|
109
|
+
definitelyPresent(d.initializer));
|
|
110
|
+
}
|
|
111
|
+
return (ts.isLiteralExpression(value) ||
|
|
112
|
+
ts.isObjectLiteralExpression(value) ||
|
|
113
|
+
ts.isArrayLiteralExpression(value) ||
|
|
114
|
+
ts.isNewExpression(value) ||
|
|
115
|
+
ts.isFunctionExpression(value) ||
|
|
116
|
+
ts.isArrowFunction(value) ||
|
|
117
|
+
value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
118
|
+
value.kind === ts.SyntaxKind.FalseKeyword ||
|
|
119
|
+
value.kind === ts.SyntaxKind.NullKeyword);
|
|
120
|
+
}
|
|
121
|
+
function inputs(d, call, path, writing, visit, fallback) {
|
|
122
|
+
const fn = calls.targets.get(call);
|
|
123
|
+
const offset = fn.parameters[0]?.name.getText(file) === 'this' ? 1 : 0;
|
|
124
|
+
let index = fn.parameters.indexOf(d) - offset;
|
|
125
|
+
if (index < 0)
|
|
126
|
+
return false;
|
|
127
|
+
let many = false;
|
|
128
|
+
if (d.dotDotDotToken) {
|
|
129
|
+
if (!path.length && writing)
|
|
130
|
+
return false;
|
|
131
|
+
const key = path[0];
|
|
132
|
+
if (key === undefined)
|
|
133
|
+
many = true;
|
|
134
|
+
else if (/^(0|[1-9]\d*)$/.test(key))
|
|
135
|
+
index += Number(key);
|
|
136
|
+
else
|
|
137
|
+
return false;
|
|
138
|
+
path = path.slice(1);
|
|
139
|
+
}
|
|
140
|
+
let position = 0;
|
|
141
|
+
let uncertain = false;
|
|
142
|
+
let present = false;
|
|
143
|
+
function actual(arg) {
|
|
144
|
+
if (ts.isSpreadElement(arg)) {
|
|
145
|
+
if (ts.isArrayLiteralExpression(arg.expression))
|
|
146
|
+
return arg.expression.elements.some(actual);
|
|
147
|
+
// An unknown iterable can supply any remaining slot, including none.
|
|
148
|
+
uncertain = true;
|
|
149
|
+
return (many || position <= index) && visit(arg.expression, [undefined, ...path]);
|
|
150
|
+
}
|
|
151
|
+
const slot = position++;
|
|
152
|
+
if (d.initializer && !uncertain && slot === index)
|
|
153
|
+
present = definitelyPresent(arg);
|
|
154
|
+
const matches = many
|
|
155
|
+
? uncertain || slot >= index
|
|
156
|
+
: slot === index || (uncertain && slot < index);
|
|
157
|
+
return matches && visit(arg, path);
|
|
158
|
+
}
|
|
159
|
+
return call.arguments.some(actual) || (!!d.initializer && !present && fallback());
|
|
160
|
+
}
|
|
161
|
+
function argumentSources(node, declaration, frame, visit) {
|
|
162
|
+
let d = declaration;
|
|
163
|
+
let path = [];
|
|
164
|
+
let ref = node;
|
|
165
|
+
while ((isMember(ref.parent) && ref.parent.expression === ref) ||
|
|
166
|
+
(isWrapper(ref.parent) && ref.parent.expression === ref)) {
|
|
167
|
+
ref = ref.parent;
|
|
168
|
+
if (isMember(ref))
|
|
169
|
+
path.push(memberKey(ref));
|
|
170
|
+
}
|
|
171
|
+
const defaults = [];
|
|
172
|
+
while (path && ts.isBindingElement(d)) {
|
|
173
|
+
if (d.initializer)
|
|
174
|
+
defaults.push(d.initializer);
|
|
175
|
+
path = sourcePath([bindingStep(d)], path, false);
|
|
176
|
+
d = d.parent.parent;
|
|
177
|
+
}
|
|
178
|
+
if (!path || !ts.isParameter(d))
|
|
179
|
+
return;
|
|
180
|
+
if (defaults.some((value) => visit(value, frame)))
|
|
181
|
+
return true;
|
|
182
|
+
const invocation = calls.enclosing(frame, d.parent);
|
|
183
|
+
const read = (call) => inputs(d, call, path, false, (arg, keys) => visit(arg, invocation?.outer ?? null, keys), () => visit(d.initializer, frame));
|
|
184
|
+
return invocation ? read(invocation.call) : calls.sites(d.parent).some(read);
|
|
185
|
+
}
|
|
186
|
+
// Visit the storage locations a local reference may name, including a binding's
|
|
187
|
+
// initializer and subsequent assignments. Unknown computed keys match any field.
|
|
188
|
+
function locations(node, path, writing, match, initial, unbound) {
|
|
189
|
+
const states = new Map();
|
|
190
|
+
const activeReturns = new Set();
|
|
191
|
+
const joinedReturns = new Map();
|
|
192
|
+
let returnVisits = 0;
|
|
193
|
+
function trace(node, path, frame) {
|
|
194
|
+
if (isWrapper(node))
|
|
195
|
+
return trace(node.expression, path, frame);
|
|
196
|
+
if (isMember(node)) {
|
|
197
|
+
return trace(node.expression, [memberKey(node), ...path], frame);
|
|
198
|
+
}
|
|
199
|
+
if (ts.isCallExpression(node) && assigns.has(node)) {
|
|
200
|
+
// Object.assign returns the target. Its source objects only share nested values.
|
|
201
|
+
return node.arguments.some((arg, i) => (i === 0 || path.length > 0) && trace(arg, path, frame));
|
|
202
|
+
}
|
|
203
|
+
if (ts.isCallExpression(node)) {
|
|
204
|
+
const fn = calls.targets.get(node);
|
|
205
|
+
if (fn && !activeReturns.has(node)) {
|
|
206
|
+
const joined = frame === null || ++returnVisits > MAX_CALL_CONTEXTS;
|
|
207
|
+
if (joined) {
|
|
208
|
+
const key = JSON.stringify(path);
|
|
209
|
+
let paths = joinedReturns.get(node);
|
|
210
|
+
if (paths?.has(key))
|
|
211
|
+
return false;
|
|
212
|
+
if (!paths)
|
|
213
|
+
joinedReturns.set(node, (paths = new Set()));
|
|
214
|
+
paths.add(key);
|
|
215
|
+
}
|
|
216
|
+
activeReturns.add(node);
|
|
217
|
+
const next = joined ? null : calls.frame(node, frame);
|
|
218
|
+
const found = calls.returns(fn).some((result) => trace(result, path, next));
|
|
219
|
+
activeReturns.delete(node);
|
|
220
|
+
return found;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (ts.isConditionalExpression(node))
|
|
224
|
+
return trace(node.whenTrue, path, frame) || trace(node.whenFalse, path, frame);
|
|
225
|
+
if (isLogical(node))
|
|
226
|
+
return trace(node.left, path, frame) || trace(node.right, path, frame);
|
|
227
|
+
if ((path.length || !writing) && ts.isArrayLiteralExpression(node)) {
|
|
228
|
+
const [index, ...rest] = path;
|
|
229
|
+
if (index === undefined || node.elements.some(ts.isSpreadElement)) {
|
|
230
|
+
return node.elements.some((el) => ts.isSpreadElement(el)
|
|
231
|
+
? trace(el.expression, [undefined, ...rest], frame)
|
|
232
|
+
: trace(el, rest, frame));
|
|
233
|
+
}
|
|
234
|
+
const item = /^(0|[1-9]\d*)$/.test(index) && node.elements[Number(index)];
|
|
235
|
+
return !!item && trace(item, rest, frame);
|
|
236
|
+
}
|
|
237
|
+
if ((path.length || !writing) && ts.isObjectLiteralExpression(node)) {
|
|
238
|
+
const [key, ...rest] = path;
|
|
239
|
+
return node.properties.some((field) => {
|
|
240
|
+
if (ts.isSpreadAssignment(field))
|
|
241
|
+
return trace(field.expression, path, frame);
|
|
242
|
+
if (!ts.isPropertyAssignment(field) && !ts.isShorthandPropertyAssignment(field))
|
|
243
|
+
return false;
|
|
244
|
+
const name = propertyName(field.name);
|
|
245
|
+
return ((key === undefined || name === undefined || key === name) &&
|
|
246
|
+
trace(ts.isPropertyAssignment(field) ? field.initializer : field.name, rest, frame));
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
if (!ts.isIdentifier(node))
|
|
250
|
+
return false;
|
|
251
|
+
const symbol = localSymbol(checker, node);
|
|
252
|
+
let state = states.get(frame);
|
|
253
|
+
if (!state) {
|
|
254
|
+
state = { active: new Set(), visited: new Map() };
|
|
255
|
+
states.set(frame, state);
|
|
256
|
+
}
|
|
257
|
+
const { active, visited } = state;
|
|
258
|
+
if (!symbol || active.has(symbol))
|
|
259
|
+
return false;
|
|
260
|
+
// Conditional aliases can name exponentially many paths. After 64 distinct
|
|
261
|
+
// paths on one binding, keep the first field and depth and allow any nested keys.
|
|
262
|
+
let paths = visited.get(symbol);
|
|
263
|
+
if (!paths) {
|
|
264
|
+
paths = new Set();
|
|
265
|
+
visited.set(symbol, paths);
|
|
266
|
+
}
|
|
267
|
+
const exact = JSON.stringify(path);
|
|
268
|
+
const group = path.length + ':' + JSON.stringify(path[0]);
|
|
269
|
+
if (paths.has(exact) || paths.has(group))
|
|
270
|
+
return false;
|
|
271
|
+
if (paths.size >= 64 && path.length > 1) {
|
|
272
|
+
paths.add(group);
|
|
273
|
+
path = [path[0], ...Array(path.length - 1).fill(undefined)];
|
|
274
|
+
}
|
|
275
|
+
else
|
|
276
|
+
paths.add(exact);
|
|
277
|
+
let d = symbol.valueDeclaration;
|
|
278
|
+
const invocation = frame && d && calls.enclosing(frame, calls.owner(d));
|
|
279
|
+
// A helper's parameters and locals are fresh at each invocation.
|
|
280
|
+
if (match(symbol, path, invocation))
|
|
281
|
+
return true;
|
|
282
|
+
if (d?.getSourceFile() !== file)
|
|
283
|
+
return false;
|
|
284
|
+
active.add(symbol);
|
|
285
|
+
let from = path;
|
|
286
|
+
while (from && ts.isBindingElement(d)) {
|
|
287
|
+
if (d.initializer && trace(d.initializer, from, frame))
|
|
288
|
+
return true;
|
|
289
|
+
from = sourcePath([bindingStep(d)], from, writing);
|
|
290
|
+
d = d.parent.parent;
|
|
291
|
+
}
|
|
292
|
+
let found = false;
|
|
293
|
+
if (from && (ts.isVariableDeclaration(d) || ts.isParameter(d))) {
|
|
294
|
+
if (ts.isParameter(d)) {
|
|
295
|
+
const parameter = d;
|
|
296
|
+
if (invocation)
|
|
297
|
+
found = inputs(parameter, invocation.call, from, writing, (arg, keys) => trace(arg, keys, invocation.outer), () => trace(parameter.initializer, from, frame));
|
|
298
|
+
else {
|
|
299
|
+
if (frame === null) {
|
|
300
|
+
found = calls.sites(d.parent).some((call) => inputs(parameter, call, from, writing, (arg, keys) => trace(arg, keys, null), () => trace(parameter.initializer, from, null)));
|
|
301
|
+
}
|
|
302
|
+
else
|
|
303
|
+
unbound?.(d.parent);
|
|
304
|
+
if (d.initializer)
|
|
305
|
+
found ||= trace(d.initializer, from, frame);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
else if (d.initializer)
|
|
309
|
+
found = trace(d.initializer, from, frame);
|
|
310
|
+
const list = d.parent;
|
|
311
|
+
if (ts.isVariableDeclarationList(list) && ts.isForOfStatement(list.parent))
|
|
312
|
+
found ||= trace(list.parent.expression, [undefined, ...from], frame);
|
|
313
|
+
}
|
|
314
|
+
found ||= !!bindings.get(symbol)?.some(({ source, selection }) => {
|
|
315
|
+
const origin = sourcePath(selection, path, writing);
|
|
316
|
+
return origin !== undefined && trace(source, origin, frame);
|
|
317
|
+
});
|
|
318
|
+
active.delete(symbol);
|
|
319
|
+
return found;
|
|
320
|
+
}
|
|
321
|
+
return trace(node, path, initial);
|
|
322
|
+
}
|
|
323
|
+
function propertyName(name) {
|
|
324
|
+
if (ts.isComputedPropertyName(name))
|
|
325
|
+
return keyOf(name.expression);
|
|
326
|
+
if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name))
|
|
327
|
+
return name.text;
|
|
328
|
+
}
|
|
329
|
+
// Only direct statement copies consume the RHS without exposing it as the
|
|
330
|
+
// result of an assignment expression. Its statement also pins execution order.
|
|
331
|
+
function copyStatement(read, objectOnly = false) {
|
|
332
|
+
while (isWrapper(read.parent))
|
|
333
|
+
read = read.parent;
|
|
334
|
+
const copy = read.parent;
|
|
335
|
+
if (ts.isVariableDeclaration(copy) &&
|
|
336
|
+
copy.initializer === read &&
|
|
337
|
+
(ts.isObjectBindingPattern(copy.name) || (!objectOnly && ts.isArrayBindingPattern(copy.name)))) {
|
|
338
|
+
return ts.isVariableStatement(copy.parent.parent) ? copy.parent.parent : undefined;
|
|
339
|
+
}
|
|
340
|
+
if (!ts.isBinaryExpression(copy) ||
|
|
341
|
+
copy.right !== read ||
|
|
342
|
+
copy.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
|
|
343
|
+
!(ts.isObjectLiteralExpression(copy.left) ||
|
|
344
|
+
(!objectOnly && ts.isArrayLiteralExpression(copy.left))))
|
|
345
|
+
return;
|
|
346
|
+
let result = copy;
|
|
347
|
+
while (isWrapper(result.parent))
|
|
348
|
+
result = result.parent;
|
|
349
|
+
return ts.isExpressionStatement(result.parent) ? result.parent : undefined;
|
|
350
|
+
}
|
|
351
|
+
// A local array can have a custom iterator, and a missing key can be inherited.
|
|
352
|
+
// Narrow those reads only while the literal stays in tracked data operations.
|
|
353
|
+
let references;
|
|
354
|
+
// Prototype access and eval can change what these locals expose without a
|
|
355
|
+
// statically visible use of the receiver.
|
|
356
|
+
let dynamicAccess = false;
|
|
357
|
+
function plainField(field) {
|
|
358
|
+
if (!ts.isPropertyAssignment(field) && !ts.isShorthandPropertyAssignment(field))
|
|
359
|
+
return false;
|
|
360
|
+
const name = propertyName(field.name);
|
|
361
|
+
return name !== undefined && name !== '__proto__';
|
|
362
|
+
}
|
|
363
|
+
function confined(literal, read, seen = new Set()) {
|
|
364
|
+
if (dynamicAccess || !copyStatement(read))
|
|
365
|
+
return false;
|
|
366
|
+
function local(value) {
|
|
367
|
+
if (value === read || seen.has(value))
|
|
368
|
+
return true;
|
|
369
|
+
seen.add(value);
|
|
370
|
+
const parent = value.parent;
|
|
371
|
+
if (isWrapper(parent))
|
|
372
|
+
return local(parent);
|
|
373
|
+
if (isMember(parent) && parent.expression === value) {
|
|
374
|
+
const key = memberKey(parent);
|
|
375
|
+
return key !== undefined && !(key in Object.prototype) && key !== 'length' && local(parent);
|
|
376
|
+
}
|
|
377
|
+
if (ts.isBinaryExpression(parent) &&
|
|
378
|
+
parent.left === value &&
|
|
379
|
+
isMember(value) &&
|
|
380
|
+
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken)
|
|
381
|
+
return true;
|
|
382
|
+
if ((ts.isPropertyAssignment(parent) && parent.initializer === value) ||
|
|
383
|
+
(ts.isShorthandPropertyAssignment(parent) && parent.name === value))
|
|
384
|
+
return (ts.isObjectLiteralExpression(parent.parent) &&
|
|
385
|
+
parent.parent.properties.every(plainField) &&
|
|
386
|
+
local(parent.parent));
|
|
387
|
+
if (ts.isVariableDeclaration(parent) &&
|
|
388
|
+
parent.initializer === value &&
|
|
389
|
+
ts.isIdentifier(parent.name) &&
|
|
390
|
+
ts.isVariableDeclarationList(parent.parent) &&
|
|
391
|
+
parent.parent.flags & ts.NodeFlags.Const &&
|
|
392
|
+
!(ts.getCombinedModifierFlags(parent) & ts.ModifierFlags.Export)) {
|
|
393
|
+
references ??= localUses(file, checker);
|
|
394
|
+
return references(parent.name).every(local);
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
if (local(literal))
|
|
399
|
+
return true;
|
|
400
|
+
// Discard tentative paths when any use of this literal escaped.
|
|
401
|
+
seen.clear();
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
// A parent replacement can supply a child. Return its value, null for a
|
|
405
|
+
// missing property, or undefined to inspect the whole source when the read is opaque.
|
|
406
|
+
function selected(source, path, read, confinedNodes) {
|
|
407
|
+
const origin = read ?? source;
|
|
408
|
+
function selectMember(access, rest) {
|
|
409
|
+
return selected(access.expression, [memberKey(access), ...rest], origin, (confinedNodes ??= new Set()));
|
|
410
|
+
}
|
|
411
|
+
function scalarField(annotation, start) {
|
|
412
|
+
let type = checker.getTypeAtLocation(annotation);
|
|
413
|
+
for (let i = start; i < path.length; i++) {
|
|
414
|
+
const key = path[i];
|
|
415
|
+
const field = key === undefined ? undefined : type.getProperty(key);
|
|
416
|
+
if (!field)
|
|
417
|
+
return false;
|
|
418
|
+
type = checker.getTypeOfSymbolAtLocation(field, annotation);
|
|
419
|
+
}
|
|
420
|
+
return scalar(type);
|
|
421
|
+
}
|
|
422
|
+
for (const [i, key] of path.entries()) {
|
|
423
|
+
if (key === undefined)
|
|
424
|
+
return;
|
|
425
|
+
while (isWrapper(source) || ts.isIdentifier(source)) {
|
|
426
|
+
if (isWrapper(source)) {
|
|
427
|
+
if ('type' in source &&
|
|
428
|
+
!ts.isConstTypeReference(source.type) &&
|
|
429
|
+
!scalarField(source.type, i))
|
|
430
|
+
return;
|
|
431
|
+
source = source.expression;
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
const symbol = localSymbol(checker, source);
|
|
435
|
+
const d = symbol?.valueDeclaration;
|
|
436
|
+
if (!symbol ||
|
|
437
|
+
!d ||
|
|
438
|
+
!ts.isVariableDeclaration(d) ||
|
|
439
|
+
!d.initializer ||
|
|
440
|
+
d.getSourceFile() !== file ||
|
|
441
|
+
d.end > source.pos ||
|
|
442
|
+
!ts.isVariableDeclarationList(d.parent) ||
|
|
443
|
+
!(d.parent.flags & ts.NodeFlags.Const) ||
|
|
444
|
+
bindings.has(symbol) ||
|
|
445
|
+
(d.type && !scalarField(d.type, i)))
|
|
446
|
+
return;
|
|
447
|
+
source = d.initializer;
|
|
448
|
+
}
|
|
449
|
+
if (isMember(source))
|
|
450
|
+
return selectMember(source, path.slice(i));
|
|
451
|
+
if (ts.isArrayLiteralExpression(source)) {
|
|
452
|
+
if (!/^(0|[1-9]\d*)$/.test(key) ||
|
|
453
|
+
source.elements.some(ts.isSpreadElement) ||
|
|
454
|
+
!confined(source, origin, confinedNodes))
|
|
455
|
+
return;
|
|
456
|
+
const item = source.elements[Number(key)];
|
|
457
|
+
if (!item || ts.isOmittedExpression(item))
|
|
458
|
+
return;
|
|
459
|
+
source = item;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
if (!ts.isObjectLiteralExpression(source) ||
|
|
463
|
+
(read && !confined(source, origin, confinedNodes)))
|
|
464
|
+
return;
|
|
465
|
+
let value;
|
|
466
|
+
for (const field of source.properties) {
|
|
467
|
+
if (!ts.isPropertyAssignment(field) && !ts.isShorthandPropertyAssignment(field))
|
|
468
|
+
return;
|
|
469
|
+
const name = propertyName(field.name);
|
|
470
|
+
if (name === undefined)
|
|
471
|
+
return;
|
|
472
|
+
if (name === key)
|
|
473
|
+
value = ts.isPropertyAssignment(field) ? field.initializer : field.name;
|
|
474
|
+
}
|
|
475
|
+
if (!value) {
|
|
476
|
+
if (i === path.length - 1 &&
|
|
477
|
+
!(key in Object.prototype) &&
|
|
478
|
+
confined(source, origin, confinedNodes))
|
|
479
|
+
return null;
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
source = value;
|
|
483
|
+
}
|
|
484
|
+
if (isMember(source)) {
|
|
485
|
+
const projected = selectMember(source, []);
|
|
486
|
+
if (projected !== undefined)
|
|
487
|
+
return projected;
|
|
488
|
+
}
|
|
489
|
+
return source;
|
|
490
|
+
}
|
|
491
|
+
function consumingUse(node) {
|
|
492
|
+
// Check a reference at its consuming use. Its receiver or alias initializer
|
|
493
|
+
// alone would select the whole object and incorrectly include sibling writes.
|
|
494
|
+
let outer = node;
|
|
495
|
+
// Follow only value-bearing edges. Conditions and computed property names are
|
|
496
|
+
// separate reads, and an opaque call can consume its entire argument.
|
|
497
|
+
for (;;) {
|
|
498
|
+
const parent = outer.parent;
|
|
499
|
+
if ((ts.isPropertyAssignment(parent) && parent.initializer === outer) ||
|
|
500
|
+
(ts.isShorthandPropertyAssignment(parent) &&
|
|
501
|
+
parent.name === outer &&
|
|
502
|
+
!parent.objectAssignmentInitializer))
|
|
503
|
+
outer = parent.parent;
|
|
504
|
+
else if ((isWrapper(parent) && parent.expression === outer) ||
|
|
505
|
+
(ts.isCallExpression(parent) &&
|
|
506
|
+
((assigns.has(parent) && parent.arguments[0] === outer) ||
|
|
507
|
+
calls.forwardedArgument(parent) === outer)) ||
|
|
508
|
+
ts.isArrayLiteralExpression(parent) ||
|
|
509
|
+
isLogical(parent) ||
|
|
510
|
+
(ts.isConditionalExpression(parent) &&
|
|
511
|
+
(parent.whenTrue === outer || parent.whenFalse === outer)))
|
|
512
|
+
outer = parent;
|
|
513
|
+
else
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
const p = outer.parent;
|
|
517
|
+
return !((isMember(p) && p.expression === outer) ||
|
|
518
|
+
ts.isVariableDeclaration(p) ||
|
|
519
|
+
ts.isParameter(p) ||
|
|
520
|
+
ts.isBindingElement(p) ||
|
|
521
|
+
bindingDefaults.has(outer) ||
|
|
522
|
+
(ts.isBinaryExpression(p) &&
|
|
523
|
+
p.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
524
|
+
ts.isIdentifier(p.left) &&
|
|
525
|
+
ts.isExpressionStatement(p.parent)));
|
|
526
|
+
}
|
|
527
|
+
// A field replacement cannot change a scalar already copied from a fresh
|
|
528
|
+
// receiver in this execution. Shared receivers can survive another iteration or call.
|
|
529
|
+
function afterSnapshot(root, read, key, source, projected) {
|
|
530
|
+
const write = source.parent;
|
|
531
|
+
if (!ts.isBinaryExpression(write) ||
|
|
532
|
+
write.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
|
|
533
|
+
write.right !== source ||
|
|
534
|
+
!isMember(write.left) ||
|
|
535
|
+
!ts.isExpressionStatement(write.parent))
|
|
536
|
+
return false;
|
|
537
|
+
if (!projected) {
|
|
538
|
+
const d = ts.isIdentifier(read) && localSymbol(checker, read)?.valueDeclaration;
|
|
539
|
+
if (!d || !ts.isBindingElement(d) || d.initializer || bindingStep(d) !== key)
|
|
540
|
+
return false;
|
|
541
|
+
const owner = d.parent.parent;
|
|
542
|
+
if (!ts.isVariableDeclaration(owner) ||
|
|
543
|
+
!owner.initializer ||
|
|
544
|
+
!ts.isVariableDeclarationList(owner.parent) ||
|
|
545
|
+
!(owner.parent.flags & ts.NodeFlags.Const) ||
|
|
546
|
+
!ts.isIdentifier(owner.initializer) ||
|
|
547
|
+
localSymbol(checker, owner.initializer) !== root)
|
|
548
|
+
return false;
|
|
549
|
+
read = owner.initializer;
|
|
550
|
+
}
|
|
551
|
+
if (write.pos <= read.end)
|
|
552
|
+
return false;
|
|
553
|
+
const scope = write.parent.parent;
|
|
554
|
+
const d = root.valueDeclaration;
|
|
555
|
+
if (!d ||
|
|
556
|
+
!ts.isVariableDeclaration(d) ||
|
|
557
|
+
!d.initializer ||
|
|
558
|
+
!ts.isObjectLiteralExpression(d.initializer) ||
|
|
559
|
+
!ts.isVariableDeclarationList(d.parent) ||
|
|
560
|
+
!(d.parent.flags & ts.NodeFlags.Const) ||
|
|
561
|
+
bindings.has(root) ||
|
|
562
|
+
!ts.isVariableStatement(d.parent.parent) ||
|
|
563
|
+
d.parent.parent.parent !== scope)
|
|
564
|
+
return false;
|
|
565
|
+
if (copyStatement(read, true)?.parent !== scope || !confined(d.initializer, read))
|
|
566
|
+
return false;
|
|
567
|
+
const value = selected(d.initializer, [key]);
|
|
568
|
+
return !!value && scalar(checker.getTypeAtLocation(skipWrappers(value)));
|
|
569
|
+
}
|
|
570
|
+
function memberSources(node, visit, frame, selectionPath) {
|
|
571
|
+
if (!members.size || (!selectionPath && !consumingUse(node)))
|
|
572
|
+
return false;
|
|
573
|
+
return locations(node, selectionPath ?? [], false, (root, read, instance) => !!members
|
|
574
|
+
.get(root)
|
|
575
|
+
?.some(({ path, source, selection, frame: originFrame, instance: written }) => {
|
|
576
|
+
const sourceFrame = calls.atRead(originFrame, written, instance);
|
|
577
|
+
if (sourceFrame === false)
|
|
578
|
+
return false;
|
|
579
|
+
if (!path.every((key, i) => key === undefined || read[i] === undefined || key === read[i]))
|
|
580
|
+
return false;
|
|
581
|
+
if ((!selectionPath || selectionPath.length === 1) &&
|
|
582
|
+
read.length === 1 &&
|
|
583
|
+
path.length === 1 &&
|
|
584
|
+
path[0] !== undefined &&
|
|
585
|
+
path[0] === read[0] &&
|
|
586
|
+
afterSnapshot(root, node, path[0], source, !!selectionPath))
|
|
587
|
+
return false;
|
|
588
|
+
const origin = sourcePath(selection, read.slice(path.length), false);
|
|
589
|
+
return visit(source, sourceFrame === undefined ? frame : sourceFrame, origin);
|
|
590
|
+
}), frame);
|
|
591
|
+
}
|
|
592
|
+
function record(target, source, selection) {
|
|
593
|
+
if (ts.isIdentifier(target)) {
|
|
594
|
+
if (!source)
|
|
595
|
+
return;
|
|
596
|
+
const symbol = localSymbol(checker, target);
|
|
597
|
+
if (symbol) {
|
|
598
|
+
const values = bindings.get(symbol);
|
|
599
|
+
if (values)
|
|
600
|
+
values.push({ source, selection });
|
|
601
|
+
else
|
|
602
|
+
bindings.set(symbol, [{ source, selection }]);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
else if (isMember(target)) {
|
|
606
|
+
pending.push({ receiver: target.expression, key: memberKey(target), source, selection });
|
|
607
|
+
}
|
|
608
|
+
else if (ts.isArrayLiteralExpression(target)) {
|
|
609
|
+
target.elements.forEach((entry, index) => {
|
|
610
|
+
if (ts.isSpreadElement(entry))
|
|
611
|
+
record(entry.expression, source, [...selection, { offset: index }]);
|
|
612
|
+
else
|
|
613
|
+
record(entry, source, [...selection, String(index)]);
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
else if (ts.isObjectLiteralExpression(target)) {
|
|
617
|
+
for (const field of target.properties) {
|
|
618
|
+
if (ts.isSpreadAssignment(field))
|
|
619
|
+
record(field.expression, source, [...selection, {}]);
|
|
620
|
+
else if (ts.isPropertyAssignment(field))
|
|
621
|
+
record(field.initializer, source, [...selection, propertyName(field.name)]);
|
|
622
|
+
else if (ts.isShorthandPropertyAssignment(field)) {
|
|
623
|
+
record(field.name, source, [...selection, field.name.text]);
|
|
624
|
+
if (field.objectAssignmentInitializer) {
|
|
625
|
+
bindingDefaults.add(field.objectAssignmentInitializer);
|
|
626
|
+
record(field.name, field.objectAssignmentInitializer, []);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
else if (ts.isBinaryExpression(target) &&
|
|
632
|
+
target.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
633
|
+
// scan also visits the default and records its right side as a binding source.
|
|
634
|
+
if (ts.isIdentifier(target.left))
|
|
635
|
+
bindingDefaults.add(target.right);
|
|
636
|
+
record(target.left, source, selection);
|
|
637
|
+
}
|
|
638
|
+
else if (isWrapper(target)) {
|
|
639
|
+
record(target.expression, source, selection);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function recordObjectWrite(node) {
|
|
643
|
+
const method = skipWrappers(node.expression);
|
|
644
|
+
const receiver = node.arguments[0];
|
|
645
|
+
if (!receiver || !isMember(method))
|
|
646
|
+
return;
|
|
647
|
+
const object = skipWrappers(method.expression);
|
|
648
|
+
if (!ts.isIdentifier(object) || (object.text !== 'Object' && object.text !== 'Reflect'))
|
|
649
|
+
return;
|
|
650
|
+
const methodName = memberKey(method);
|
|
651
|
+
const symbol = localSymbol(checker, object);
|
|
652
|
+
const builtin = symbol?.valueDeclaration?.getSourceFile().hasNoDefaultLib;
|
|
653
|
+
if ((object.text === 'Object' &&
|
|
654
|
+
builtin &&
|
|
655
|
+
(methodName === 'defineProperty' ||
|
|
656
|
+
methodName === 'defineProperties' ||
|
|
657
|
+
methodName === 'setPrototypeOf')) ||
|
|
658
|
+
(object.text === 'Reflect' &&
|
|
659
|
+
(!symbol || builtin) &&
|
|
660
|
+
(methodName === 'set' ||
|
|
661
|
+
methodName === 'defineProperty' ||
|
|
662
|
+
methodName === 'deleteProperty' ||
|
|
663
|
+
methodName === 'setPrototypeOf'))) {
|
|
664
|
+
pending.push({ receiver, key: null, source: undefined, selection: [] });
|
|
665
|
+
if (object.text === 'Reflect' && methodName === 'set' && node.arguments[3])
|
|
666
|
+
pending.push({ receiver: node.arguments[3], key: null, source: undefined, selection: [] });
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
if (object.text !== 'Object' || methodName !== 'assign' || !builtin)
|
|
670
|
+
return;
|
|
671
|
+
assigns.add(node);
|
|
672
|
+
function copy(source) {
|
|
673
|
+
source = skipWrappers(source);
|
|
674
|
+
if (!ts.isObjectLiteralExpression(source)) {
|
|
675
|
+
pending.push({ receiver, key: null, source, selection: [] });
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
for (const field of source.properties) {
|
|
679
|
+
if (ts.isSpreadAssignment(field))
|
|
680
|
+
copy(field.expression);
|
|
681
|
+
else {
|
|
682
|
+
const key = propertyName(field.name);
|
|
683
|
+
const value = ts.isPropertyAssignment(field)
|
|
684
|
+
? field.initializer
|
|
685
|
+
: ts.isShorthandPropertyAssignment(field)
|
|
686
|
+
? field.name
|
|
687
|
+
: source;
|
|
688
|
+
pending.push({ receiver, key, source: value, selection: [] });
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
for (let i = 1; i < node.arguments.length; i++)
|
|
693
|
+
copy(node.arguments[i]);
|
|
694
|
+
}
|
|
695
|
+
function scan(node) {
|
|
696
|
+
if (!dynamicAccess && (ts.isIdentifier(node) || ts.isStringLiteralLike(node))) {
|
|
697
|
+
dynamicAccess =
|
|
698
|
+
node.text === 'eval' ||
|
|
699
|
+
node.text === 'prototype' ||
|
|
700
|
+
node.text === '__proto__' ||
|
|
701
|
+
node.text === 'getPrototypeOf';
|
|
702
|
+
}
|
|
703
|
+
if (ts.isCallExpression(node)) {
|
|
704
|
+
possibleCalls.push(node);
|
|
705
|
+
recordObjectWrite(node);
|
|
706
|
+
}
|
|
707
|
+
if (ts.isBinaryExpression(node) &&
|
|
708
|
+
node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
709
|
+
node.operatorToken.kind <= ts.SyntaxKind.LastAssignment) {
|
|
710
|
+
record(node.left, node.right, []);
|
|
711
|
+
}
|
|
712
|
+
else if (ts.isDeleteExpression(node)) {
|
|
713
|
+
record(node.expression, undefined, []);
|
|
714
|
+
}
|
|
715
|
+
else if ((ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
716
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
717
|
+
node.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
718
|
+
record(node.operand, undefined, []);
|
|
719
|
+
}
|
|
720
|
+
else if (ts.isForOfStatement(node) || ts.isForInStatement(node)) {
|
|
721
|
+
// for-in supplies string keys, but defaults in its binding pattern can still run.
|
|
722
|
+
if (!ts.isVariableDeclarationList(node.initializer))
|
|
723
|
+
record(node.initializer, ts.isForOfStatement(node) ? node.expression : undefined, [
|
|
724
|
+
undefined,
|
|
725
|
+
]);
|
|
726
|
+
}
|
|
727
|
+
ts.forEachChild(node, scan);
|
|
728
|
+
}
|
|
729
|
+
scan(file);
|
|
730
|
+
if (pending.length)
|
|
731
|
+
for (const call of possibleCalls)
|
|
732
|
+
calls.scan(call);
|
|
733
|
+
for (const { receiver, key, source, selection } of pending) {
|
|
734
|
+
const indexed = new Set();
|
|
735
|
+
function append(frame, call) {
|
|
736
|
+
return frame ? calls.frame(frame.call, append(frame.outer, call)) : calls.frame(call);
|
|
737
|
+
}
|
|
738
|
+
function index(frame) {
|
|
739
|
+
if (indexed.has(frame))
|
|
740
|
+
return;
|
|
741
|
+
// One pair of helpers can call each other on exponentially many paths.
|
|
742
|
+
// Join their possible arguments when exact call-site indexing exceeds the file budget.
|
|
743
|
+
if (frame?.outer && indexed.size >= MAX_CALL_CONTEXTS) {
|
|
744
|
+
index(null);
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
indexed.add(frame);
|
|
748
|
+
const missing = new Set();
|
|
749
|
+
locations(receiver, [], true, (root, location, instance) => {
|
|
750
|
+
if (!source) {
|
|
751
|
+
mutationOnly.add(root);
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
const path = key === null ? location : [...location, key];
|
|
755
|
+
const values = members.get(root);
|
|
756
|
+
const entry = { path, source, selection, frame, instance };
|
|
757
|
+
if (values)
|
|
758
|
+
values.push(entry);
|
|
759
|
+
else
|
|
760
|
+
members.set(root, [entry]);
|
|
761
|
+
return false;
|
|
762
|
+
}, frame, (fn) => {
|
|
763
|
+
if (calls.enclosing(frame, fn))
|
|
764
|
+
return;
|
|
765
|
+
for (const call of calls.sites(fn))
|
|
766
|
+
missing.add(call);
|
|
767
|
+
});
|
|
768
|
+
for (const call of missing)
|
|
769
|
+
index(append(frame ?? undefined, call));
|
|
770
|
+
}
|
|
771
|
+
const owner = calls.owner(receiver);
|
|
772
|
+
const sites = owner && calls.sites(owner);
|
|
773
|
+
if (sites?.length)
|
|
774
|
+
for (const call of sites)
|
|
775
|
+
index(calls.frame(call));
|
|
776
|
+
else
|
|
777
|
+
index();
|
|
778
|
+
}
|
|
779
|
+
const writes = {
|
|
780
|
+
calls,
|
|
781
|
+
selected,
|
|
782
|
+
bindingStep,
|
|
783
|
+
arguments: argumentSources,
|
|
784
|
+
bindings,
|
|
785
|
+
members: memberSources,
|
|
786
|
+
hasMemberWrite: (name) => (members.size > 0 || mutationOnly.size > 0) &&
|
|
787
|
+
locations(name, [], false, (root) => members.has(root) || mutationOnly.has(root)),
|
|
788
|
+
};
|
|
789
|
+
writesByFile.set(file, { checker, writes });
|
|
790
|
+
return writes;
|
|
791
|
+
}
|
|
792
|
+
export function hasLocalMemberWrite(name, checker) {
|
|
793
|
+
return localWrites(name.getSourceFile(), checker).hasMemberWrite(name);
|
|
794
|
+
}
|
|
795
|
+
/** Inspect syntax and the declarations and assignments supplying its values in this module.
|
|
796
|
+
* Returning false skips a subtree; undefined follows its children and local values. */
|
|
797
|
+
export function walkLocalValues(root, checker, inspect) {
|
|
798
|
+
const file = root.getSourceFile();
|
|
799
|
+
const states = new Map();
|
|
800
|
+
function walk(node, frame, path) {
|
|
801
|
+
let state = states.get(frame);
|
|
802
|
+
if (!state) {
|
|
803
|
+
state = { seen: new Set(), followed: new Set(), selections: new Map() };
|
|
804
|
+
states.set(frame, state);
|
|
805
|
+
}
|
|
806
|
+
const { seen, followed, selections } = state;
|
|
807
|
+
if (path?.length && ts.isExpression(node)) {
|
|
808
|
+
const writes = localWrites(file, checker);
|
|
809
|
+
const value = writes.selected(node, path);
|
|
810
|
+
if (value !== undefined) {
|
|
811
|
+
// A source can supply multiple fields, including through cyclic assignments.
|
|
812
|
+
let paths = selections.get(node);
|
|
813
|
+
if (!paths)
|
|
814
|
+
selections.set(node, (paths = new Set()));
|
|
815
|
+
// A branching chain of destructuring assignments can supply exponentially
|
|
816
|
+
// many paths. Join them into a whole-object read after 64 selections.
|
|
817
|
+
const key = paths.size < 64 ? JSON.stringify(path) : 'all';
|
|
818
|
+
if (paths.has(key))
|
|
819
|
+
return false;
|
|
820
|
+
paths.add(key);
|
|
821
|
+
if (key === 'all')
|
|
822
|
+
return writes.members(node, walk, frame, []) || walk(node, frame);
|
|
823
|
+
return writes.members(node, walk, frame, path) || (!!value && walk(value, frame));
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
const visit = (child) => walk(child, frame);
|
|
827
|
+
if (seen.has(node))
|
|
828
|
+
return false;
|
|
829
|
+
seen.add(node);
|
|
830
|
+
const result = inspect(node, visit);
|
|
831
|
+
if (result !== undefined)
|
|
832
|
+
return result;
|
|
833
|
+
if (ts.isCallExpression(node)) {
|
|
834
|
+
const writes = localWrites(file, checker);
|
|
835
|
+
const fn = writes.calls.targets.get(node);
|
|
836
|
+
if (fn?.body) {
|
|
837
|
+
if (writes.calls.forwardedArgument(node))
|
|
838
|
+
return node.arguments.some(visit);
|
|
839
|
+
const context = frame === null || states.size > MAX_CALL_CONTEXTS || writes.calls.enclosing(frame, fn)
|
|
840
|
+
? null
|
|
841
|
+
: writes.calls.frame(node, frame);
|
|
842
|
+
return walk(fn.body, context) || node.arguments.some(visit);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (ts.isIdentifier(node) || isMember(node)) {
|
|
846
|
+
const writes = localWrites(file, checker);
|
|
847
|
+
if (writes.members(node, walk, frame))
|
|
848
|
+
return true;
|
|
849
|
+
if (ts.isIdentifier(node)) {
|
|
850
|
+
const symbol = localSymbol(checker, node);
|
|
851
|
+
const declaration = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
|
|
852
|
+
const actual = frame !== undefined && declaration
|
|
853
|
+
? writes.arguments(node, declaration, frame, walk)
|
|
854
|
+
: undefined;
|
|
855
|
+
if (actual)
|
|
856
|
+
return true;
|
|
857
|
+
if (symbol && declaration?.getSourceFile() === file && !followed.has(symbol)) {
|
|
858
|
+
followed.add(symbol);
|
|
859
|
+
if ((actual === undefined && visit(declaration)) ||
|
|
860
|
+
writes.bindings
|
|
861
|
+
.get(symbol)
|
|
862
|
+
?.some(({ source, selection }) => walk(source, frame, sourcePath(selection, [], false))))
|
|
863
|
+
return true;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
// Destructuring stores defaults on individual bindings; its value comes from the owner.
|
|
868
|
+
let owner = node;
|
|
869
|
+
let selectedPath;
|
|
870
|
+
while (ts.isBindingElement(owner)) {
|
|
871
|
+
if (owner.initializer && walk(owner.initializer, frame, selectedPath))
|
|
872
|
+
return true;
|
|
873
|
+
selectedPath = sourcePath([localWrites(file, checker).bindingStep(owner)], selectedPath ?? [], false);
|
|
874
|
+
owner = owner.parent.parent;
|
|
875
|
+
}
|
|
876
|
+
if (ts.isVariableDeclaration(owner) || ts.isParameter(owner)) {
|
|
877
|
+
if (owner.initializer && walk(owner.initializer, frame, selectedPath))
|
|
878
|
+
return true;
|
|
879
|
+
const list = owner.parent;
|
|
880
|
+
// for-of (including for-await-of) yields iterable values; for-in yields property names.
|
|
881
|
+
if (ts.isVariableDeclarationList(list) &&
|
|
882
|
+
ts.isForOfStatement(list.parent) &&
|
|
883
|
+
visit(list.parent.expression))
|
|
884
|
+
return true;
|
|
885
|
+
}
|
|
886
|
+
return !!ts.forEachChild(node, visit);
|
|
887
|
+
}
|
|
888
|
+
return walk(root);
|
|
889
|
+
}
|
|
890
|
+
export function portableArguments(checker, typeOf) {
|
|
891
|
+
function argument(expr, imported) {
|
|
892
|
+
if (ts.isParenthesizedExpression(expr) ||
|
|
893
|
+
ts.isSatisfiesExpression(expr) ||
|
|
894
|
+
ts.isNonNullExpression(expr))
|
|
895
|
+
return argument(expr.expression, imported);
|
|
896
|
+
if (ts.isAsExpression(expr) || ts.isTypeAssertionExpression(expr))
|
|
897
|
+
return argument(expr.expression, imported);
|
|
898
|
+
if (ts.isConditionalExpression(expr))
|
|
899
|
+
return argument(expr.whenTrue, imported) && argument(expr.whenFalse, imported);
|
|
900
|
+
if (ts.isBinaryExpression(expr)) {
|
|
901
|
+
const op = expr.operatorToken.kind;
|
|
902
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
903
|
+
return scalar(typeOf(expr.left)) && argument(expr.right, imported);
|
|
904
|
+
if (op === ts.SyntaxKind.BarBarToken || op === ts.SyntaxKind.QuestionQuestionToken)
|
|
905
|
+
return argument(expr.left, imported) && argument(expr.right, imported);
|
|
906
|
+
if (op === ts.SyntaxKind.CommaToken)
|
|
907
|
+
return argument(expr.right, imported);
|
|
908
|
+
}
|
|
909
|
+
if (ts.isArrayLiteralExpression(expr))
|
|
910
|
+
return expr.elements.every((el) => argument(el, imported));
|
|
911
|
+
if (ts.isObjectLiteralExpression(expr))
|
|
912
|
+
return expr.properties.every((p) => ts.isShorthandPropertyAssignment(p)
|
|
913
|
+
? !p.objectAssignmentInitializer && argument(p.name, imported)
|
|
914
|
+
: ts.isPropertyAssignment(p) &&
|
|
915
|
+
!ts.isComputedPropertyName(p.name) &&
|
|
916
|
+
argument(p.initializer, imported));
|
|
917
|
+
if (ts.isIdentifier(expr)) {
|
|
918
|
+
const symbol = localSymbol(checker, expr);
|
|
919
|
+
const declaration = symbol?.valueDeclaration;
|
|
920
|
+
if (declaration && ts.isVariableDeclaration(declaration)) {
|
|
921
|
+
return (!!declaration.initializer &&
|
|
922
|
+
declaration.end <= expr.pos &&
|
|
923
|
+
ts.isVariableDeclarationList(declaration.parent) &&
|
|
924
|
+
!!(declaration.parent.flags & ts.NodeFlags.Const) &&
|
|
925
|
+
scalar(typeOf(expr)) &&
|
|
926
|
+
argument(declaration.initializer, imported));
|
|
927
|
+
}
|
|
928
|
+
const d = imported && symbol?.declarations?.[0];
|
|
929
|
+
if (imported &&
|
|
930
|
+
d &&
|
|
931
|
+
ts.isImportSpecifier(d) &&
|
|
932
|
+
!d.isTypeOnly &&
|
|
933
|
+
!d.parent.parent.isTypeOnly) {
|
|
934
|
+
const importDeclaration = d.parent.parent.parent;
|
|
935
|
+
const spec = importDeclaration.moduleSpecifier;
|
|
936
|
+
if (ts.isStringLiteral(spec)) {
|
|
937
|
+
const verdict = imported(spec.text, (d.propertyName ?? d.name).text);
|
|
938
|
+
if (verdict === 'reject')
|
|
939
|
+
return false;
|
|
940
|
+
if (verdict && !importDeclaration.attributes)
|
|
941
|
+
return true;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (ts.isCallExpression(expr) && !expr.arguments.every((arg) => argument(arg, imported)))
|
|
946
|
+
return false;
|
|
947
|
+
return scalar(typeOf(expr));
|
|
948
|
+
}
|
|
949
|
+
const clean = new Map();
|
|
950
|
+
return (expr, imported) => {
|
|
951
|
+
if (!argument(expr, imported))
|
|
952
|
+
return false;
|
|
953
|
+
let safe = clean.get(expr);
|
|
954
|
+
if (safe === undefined) {
|
|
955
|
+
safe = !walkLocalValues(expr, checker, (n) => ts.isJsxElement(n) || ts.isJsxSelfClosingElement(n) || ts.isJsxFragment(n)
|
|
956
|
+
? true
|
|
957
|
+
: undefined);
|
|
958
|
+
clean.set(expr, safe);
|
|
959
|
+
}
|
|
960
|
+
return safe;
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
/** Whether this value is only named as a JSX component or a module export. */
|
|
964
|
+
export function componentTagOrExport(ref) {
|
|
965
|
+
const p = ref.parent;
|
|
966
|
+
return ts.isJsxOpeningElement(p) || ts.isJsxClosingElement(p) || ts.isJsxSelfClosingElement(p)
|
|
967
|
+
? p.tagName === ref
|
|
968
|
+
: ts.isExportSpecifier(p) || ts.isExportAssignment(p);
|
|
969
|
+
}
|
|
970
|
+
export function localUses(file, checker) {
|
|
971
|
+
const refs = new Map();
|
|
972
|
+
function visit(node) {
|
|
973
|
+
if (ts.isIdentifier(node)) {
|
|
974
|
+
const binding = localSymbol(checker, node);
|
|
975
|
+
if (binding) {
|
|
976
|
+
const ids = refs.get(binding);
|
|
977
|
+
if (ids)
|
|
978
|
+
ids.push(node);
|
|
979
|
+
else
|
|
980
|
+
refs.set(binding, [node]);
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
ts.forEachChild(node, visit);
|
|
984
|
+
}
|
|
985
|
+
visit(file);
|
|
986
|
+
return (id) => (refs.get(localSymbol(checker, id)) ?? []).filter((ref) => ref !== id);
|
|
987
|
+
}
|