@ng-native/migrate 0.12.0 → 0.14.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/engine-node-tag.cjs +602 -0
- package/migrations.cjs +7 -0
- package/package.json +1 -1
- package/scope.cjs +116 -0
- package/tracking-available-getter.cjs +3 -102
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.14.0 renamed the field an engine node keeps its element's tag in, `view` or `text-input`, from
|
|
3
|
+
* `name` to `tag`, so that `name` is free for the DOM's attribute. `StyleTarget`, `ViewNameNode`
|
|
4
|
+
* and `NamedHostNode` from `@ng-native/fabric`, and `BrowserNode` from `@ng-native/web`, which
|
|
5
|
+
* mirror it, have `tag` too.
|
|
6
|
+
*
|
|
7
|
+
* `.name` is everywhere, and a file's syntax does not say what most of it is read from, so a
|
|
8
|
+
* `.name` (or `['name']`) is rewritten only where what it is read from is known to be one of those
|
|
9
|
+
* nodes: a name whose nearest declaration is typed as one, or initialised with one; `this.` a field
|
|
10
|
+
* typed as one, or `this` in a class that implements one; `x as EngineNode`; a node's `parent`, an
|
|
11
|
+
* item of its `children`, and the item a `for...of` or a `forEach`, `map`, `filter`, `some`,
|
|
12
|
+
* `every` or `find` callback takes from them; and `nativeElement` of an `ElementRef<EngineNode>`.
|
|
13
|
+
* A type counts when it names one of the node types, as imported (an alias or a namespace import
|
|
14
|
+
* included), or an interface or type in the file built on one. `{ name }` destructured from a node
|
|
15
|
+
* becomes `{ tag: name }`, and an object literal written as one of these types has its `name` key
|
|
16
|
+
* become `tag`.
|
|
17
|
+
*
|
|
18
|
+
* Everything else is left as it is. In a file that imports one of the types, a `.name` read from
|
|
19
|
+
* something it cannot tell is noted with its file and line, and so is a `nativeElement.name`
|
|
20
|
+
* anywhere, because an `ElementRef` holds `any` unless it is told otherwise. So is a `name` read
|
|
21
|
+
* from, or taken out of, a node cast to another type: `(node as { name?: string }).name` still
|
|
22
|
+
* compiles once the field is gone, and reads `undefined`.
|
|
23
|
+
*/
|
|
24
|
+
const { applied, scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
25
|
+
const { files } = require('./host.cjs');
|
|
26
|
+
const { ELSE, isStatic, nearestBinding, thisOf } = require('./scope.cjs');
|
|
27
|
+
|
|
28
|
+
/** The types whose `name` became `tag`, by the entry point that exports them. */
|
|
29
|
+
const TYPES = {
|
|
30
|
+
'@ng-native/fabric': ['EngineNode', 'StyleTarget', 'ViewNameNode', 'NamedHostNode'],
|
|
31
|
+
'@ng-native/web': ['BrowserNode'],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** What an expression is, as far as this migration needs to know. */
|
|
35
|
+
const NODE = 'node';
|
|
36
|
+
const NODES = 'nodes';
|
|
37
|
+
const REF = 'element ref';
|
|
38
|
+
/** Anything else, as a type written out says. */
|
|
39
|
+
const OTHER = 'other';
|
|
40
|
+
|
|
41
|
+
/** Types that say nothing of what a value is. */
|
|
42
|
+
const LOOSE = /^(any|unknown|object|Object|\{\s*\})$/;
|
|
43
|
+
|
|
44
|
+
/** Expressions whose value is never a node: literals, functions and constructed objects. */
|
|
45
|
+
const VALUES = [
|
|
46
|
+
'isObjectLiteralExpression',
|
|
47
|
+
'isArrayLiteralExpression',
|
|
48
|
+
'isNewExpression',
|
|
49
|
+
'isClassExpression',
|
|
50
|
+
'isFunctionExpression',
|
|
51
|
+
'isArrowFunction',
|
|
52
|
+
'isStringLiteral',
|
|
53
|
+
'isNoSubstitutionTemplateLiteral',
|
|
54
|
+
'isTemplateExpression',
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
/** `expression` without the parentheses and `!` around it. */
|
|
58
|
+
function unwrapped(ts, expression) {
|
|
59
|
+
let inner = expression;
|
|
60
|
+
while (ts.isParenthesizedExpression(inner) || ts.isNonNullExpression(inner)) {
|
|
61
|
+
inner = inner.expression;
|
|
62
|
+
}
|
|
63
|
+
return inner;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** `x as T`, `<T>x` or `x satisfies T`. */
|
|
67
|
+
const isAssertion = (ts, expression) =>
|
|
68
|
+
ts.isAsExpression(expression) ||
|
|
69
|
+
ts.isTypeAssertionExpression(expression) ||
|
|
70
|
+
ts.isSatisfiesExpression(expression);
|
|
71
|
+
|
|
72
|
+
/** Array methods whose callback takes an item as its first parameter. */
|
|
73
|
+
const EACH = new Set(['forEach', 'map', 'flatMap', 'filter', 'some', 'every', 'find', 'findLast']);
|
|
74
|
+
/** Array methods that answer one item. */
|
|
75
|
+
const ITEM = new Set(['at', 'find', 'findLast']);
|
|
76
|
+
/** Array methods that answer some of the items, as an array. */
|
|
77
|
+
const SOME = new Set(['filter', 'slice', 'toReversed', 'toSorted']);
|
|
78
|
+
|
|
79
|
+
/** The names the node types go by in a file: as imported, aliased, or through a namespace. */
|
|
80
|
+
function imported(ts, source) {
|
|
81
|
+
const names = new Set();
|
|
82
|
+
for (const statement of source.statements) {
|
|
83
|
+
const wanted = ts.isImportDeclaration(statement) && TYPES[statement.moduleSpecifier.text];
|
|
84
|
+
const bindings = wanted && statement.importClause?.namedBindings;
|
|
85
|
+
if (!bindings) continue;
|
|
86
|
+
if (ts.isNamespaceImport(bindings)) {
|
|
87
|
+
for (const type of wanted) names.add(`${bindings.name.text}.${type}`);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
for (const element of bindings.elements) {
|
|
91
|
+
if (wanted.includes((element.propertyName ?? element.name).text)) {
|
|
92
|
+
names.add(element.name.text);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return names;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The types a declared type can be, less parentheses, `null` and `undefined`. */
|
|
100
|
+
function variants(ts, type) {
|
|
101
|
+
if (!type) return [];
|
|
102
|
+
if (ts.isParenthesizedTypeNode(type)) return variants(ts, type.type);
|
|
103
|
+
if (ts.isUnionTypeNode(type)) return type.types.flatMap((member) => variants(ts, member));
|
|
104
|
+
const empty =
|
|
105
|
+
type.kind === ts.SyntaxKind.UndefinedKeyword ||
|
|
106
|
+
(ts.isLiteralTypeNode(type) && type.literal.kind === ts.SyntaxKind.NullKeyword);
|
|
107
|
+
return empty ? [] : [type];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Whether every type `type` can be is true of `test`, and it can be one at all. */
|
|
111
|
+
function always(ts, type, test) {
|
|
112
|
+
const all = variants(ts, type);
|
|
113
|
+
return all.length > 0 && all.every(test);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether a type is a node: one of the node types, or an intersection with one. */
|
|
117
|
+
function isNodeType(ts, type, names) {
|
|
118
|
+
return always(ts, type, (one) => {
|
|
119
|
+
if (ts.isTypeReferenceNode(one)) return names.has(one.typeName.getText());
|
|
120
|
+
return ts.isIntersectionTypeNode(one) && one.types.some((part) => isNodeType(ts, part, names));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The item type of an array type, `T[]`, `readonly T[]`, `Array<T>` or `ReadonlyArray<T>`. */
|
|
125
|
+
function itemType(ts, type) {
|
|
126
|
+
if (ts.isTypeOperatorNode(type)) return itemType(ts, type.type);
|
|
127
|
+
if (ts.isArrayTypeNode(type)) return type.elementType;
|
|
128
|
+
const generic =
|
|
129
|
+
ts.isTypeReferenceNode(type) && /^(Readonly)?Array$/.test(type.typeName.getText());
|
|
130
|
+
return generic ? type.typeArguments?.[0] : undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** `ElementRef<T>`'s `T`. */
|
|
134
|
+
function refType(ts, type) {
|
|
135
|
+
const ref = ts.isTypeReferenceNode(type) && type.typeName.getText() === 'ElementRef';
|
|
136
|
+
return ref ? type.typeArguments?.[0] : undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** What a declared type says a value is: a node, an array of nodes, an element ref to one. */
|
|
140
|
+
function typeKind(ts, type, names) {
|
|
141
|
+
if (isNodeType(ts, type, names)) return NODE;
|
|
142
|
+
if (always(ts, type, (one) => isNodeType(ts, itemType(ts, one), names))) return NODES;
|
|
143
|
+
if (always(ts, type, (one) => isNodeType(ts, refType(ts, one), names))) return REF;
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Whether a class says it implements one of the node types. */
|
|
148
|
+
const implementsNode = (ts, cls, names) =>
|
|
149
|
+
(cls.heritageClauses ?? []).some(
|
|
150
|
+
(clause) =>
|
|
151
|
+
clause.token === ts.SyntaxKind.ImplementsKeyword &&
|
|
152
|
+
clause.types.some((type) => names.has(type.expression.getText())),
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
/** What the file knows of each expression it asks about. */
|
|
156
|
+
class Reader {
|
|
157
|
+
constructor(ts, names) {
|
|
158
|
+
this.ts = ts;
|
|
159
|
+
this.names = names;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
type(type) {
|
|
163
|
+
return typeKind(this.ts, type, this.names);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** What `expression` is: `NODE`, `NODES`, `REF`, or null when this cannot tell. */
|
|
167
|
+
kind(expression, depth = 0) {
|
|
168
|
+
const ts = this.ts;
|
|
169
|
+
const inner = unwrapped(ts, expression);
|
|
170
|
+
if (depth > 32) return null;
|
|
171
|
+
if (isAssertion(ts, inner)) {
|
|
172
|
+
const said = this.type(inner.type);
|
|
173
|
+
if (said || !ts.isSatisfiesExpression(inner)) return said;
|
|
174
|
+
return this.kind(inner.expression, depth + 1);
|
|
175
|
+
}
|
|
176
|
+
if (ts.isIdentifier(inner)) return this.binding(nearestBinding(ts, inner), depth);
|
|
177
|
+
if (inner.kind === ts.SyntaxKind.ThisKeyword) return this.self(inner);
|
|
178
|
+
return this.access(inner, depth);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** What a member, an item or a call answers. */
|
|
182
|
+
access(expression, depth) {
|
|
183
|
+
const ts = this.ts;
|
|
184
|
+
if (ts.isPropertyAccessExpression(expression)) return this.member(expression, depth);
|
|
185
|
+
if (ts.isElementAccessExpression(expression)) {
|
|
186
|
+
return this.kind(expression.expression, depth + 1) === NODES ? NODE : null;
|
|
187
|
+
}
|
|
188
|
+
return ts.isCallExpression(expression) ? this.call(expression, depth) : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** `this`, which is a node in a class that implements a node type. */
|
|
192
|
+
self(expression) {
|
|
193
|
+
const self = thisOf(this.ts, expression);
|
|
194
|
+
return self && !self.static && implementsNode(this.ts, self.cls, this.names) ? NODE : null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** `x.parent`, `x.children`, `ref.nativeElement`, or `this.field`. */
|
|
198
|
+
member(expression, depth) {
|
|
199
|
+
const ts = this.ts;
|
|
200
|
+
const name = expression.name.text;
|
|
201
|
+
if (expression.expression.kind === ts.SyntaxKind.ThisKeyword) {
|
|
202
|
+
const self = thisOf(ts, expression);
|
|
203
|
+
const field = self && this.field(self.cls, name, self.static);
|
|
204
|
+
if (field) return field;
|
|
205
|
+
}
|
|
206
|
+
const of = this.kind(expression.expression, depth + 1);
|
|
207
|
+
if (of === NODE && name === 'parent') return NODE;
|
|
208
|
+
if (of === NODE && name === 'children') return NODES;
|
|
209
|
+
return of === REF && name === 'nativeElement' ? NODE : null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** An array method's answer, or `inject<ElementRef<EngineNode>>(ElementRef)`. */
|
|
213
|
+
call(expression, depth) {
|
|
214
|
+
const ts = this.ts;
|
|
215
|
+
const callee = expression.expression;
|
|
216
|
+
if (ts.isIdentifier(callee) && callee.text === 'inject') {
|
|
217
|
+
return this.type(expression.typeArguments?.[0]) === REF ? REF : null;
|
|
218
|
+
}
|
|
219
|
+
if (!ts.isPropertyAccessExpression(callee)) return null;
|
|
220
|
+
if (this.kind(callee.expression, depth + 1) !== NODES) return null;
|
|
221
|
+
if (ITEM.has(callee.name.text)) return NODE;
|
|
222
|
+
return SOME.has(callee.name.text) ? NODES : null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** What a class's field or getter `name` holds, from its type or its initialiser. */
|
|
226
|
+
field(cls, name, onClass) {
|
|
227
|
+
const declaration = this.fieldDeclaration(cls, name, onClass);
|
|
228
|
+
if (!declaration) return null;
|
|
229
|
+
if (declaration.type) return this.type(declaration.type);
|
|
230
|
+
return declaration.initializer ? this.kind(declaration.initializer, 1) : null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* The declaration of a class's field or getter `name`, a constructor parameter declared as a
|
|
235
|
+
* field (`constructor(readonly node: EngineNode)`) included.
|
|
236
|
+
*/
|
|
237
|
+
fieldDeclaration(cls, name, onClass) {
|
|
238
|
+
const ts = this.ts;
|
|
239
|
+
const named = (one) => one.name && ts.isIdentifier(one.name) && one.name.text === name;
|
|
240
|
+
const member = cls.members.find(
|
|
241
|
+
(one) =>
|
|
242
|
+
named(one) &&
|
|
243
|
+
isStatic(ts, one) === onClass &&
|
|
244
|
+
(ts.isPropertyDeclaration(one) || ts.isGetAccessorDeclaration(one)),
|
|
245
|
+
);
|
|
246
|
+
if (member || onClass) return member;
|
|
247
|
+
const constructor = cls.members.find((one) => ts.isConstructorDeclaration(one));
|
|
248
|
+
return constructor?.parameters.find((one) => ts.getModifiers(one)?.length && named(one));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The field `this.name` reads, where `this` is a class's. */
|
|
252
|
+
thisField(expression) {
|
|
253
|
+
const self = thisOf(this.ts, expression);
|
|
254
|
+
return self ? this.fieldDeclaration(self.cls, expression.name.text, self.static) : undefined;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** What a variable or parameter holds, from its type, its loop, its callback or its value. */
|
|
258
|
+
binding(declaration, depth) {
|
|
259
|
+
if (!declaration || declaration === ELSE) return null;
|
|
260
|
+
if (declaration.type) return this.type(declaration.type);
|
|
261
|
+
const items = this.iterated(declaration);
|
|
262
|
+
if (items) return this.kind(items, depth + 1) === NODES ? NODE : null;
|
|
263
|
+
return declaration.initializer ? this.kind(declaration.initializer, depth + 1) : null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* The array a declaration takes one item of: a `for...of` variable's, or the first parameter of
|
|
268
|
+
* a callback an array method is given.
|
|
269
|
+
*/
|
|
270
|
+
iterated(declaration) {
|
|
271
|
+
const ts = this.ts;
|
|
272
|
+
if (!ts.isParameter(declaration)) {
|
|
273
|
+
const loop = declaration.parent?.parent;
|
|
274
|
+
const item = loop && ts.isForOfStatement(loop) && loop.initializer === declaration.parent;
|
|
275
|
+
return item ? loop.expression : null;
|
|
276
|
+
}
|
|
277
|
+
const callback = declaration.parent;
|
|
278
|
+
const call = callback.parent;
|
|
279
|
+
if (callback.parameters[0] !== declaration || !call || !ts.isCallExpression(call)) return null;
|
|
280
|
+
const callee = call.expression;
|
|
281
|
+
if (call.arguments[0] !== callback || !ts.isPropertyAccessExpression(callee)) return null;
|
|
282
|
+
return EACH.has(callee.name.text) ? callee.expression : null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** What an explicit type says: its kind, `OTHER` for any other type, null when it says nothing. */
|
|
286
|
+
says(type) {
|
|
287
|
+
if (!type || LOOSE.test(type.getText())) return null;
|
|
288
|
+
return this.type(type) ?? OTHER;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Whether `expression` is a node cast to another type, `node as { name?: string }`: the type it
|
|
293
|
+
* is read as hides the node, and a `name` that type declares still compiles after the rename.
|
|
294
|
+
*/
|
|
295
|
+
castNode(expression, depth = 0) {
|
|
296
|
+
const ts = this.ts;
|
|
297
|
+
const inner = unwrapped(ts, expression);
|
|
298
|
+
if (depth > 32 || !isAssertion(ts, inner) || this.type(inner.type)) return false;
|
|
299
|
+
return (
|
|
300
|
+
this.kind(inner.expression, depth + 1) === NODE || this.castNode(inner.expression, depth + 1)
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Whether `expression` is certainly not a node, so a `name` read from it is no tag. */
|
|
305
|
+
notNode(expression, depth = 0) {
|
|
306
|
+
const ts = this.ts;
|
|
307
|
+
const inner = unwrapped(ts, expression);
|
|
308
|
+
if (depth > 32) return false;
|
|
309
|
+
if (VALUES.some((is) => ts[is](inner))) return true;
|
|
310
|
+
if (inner.kind === ts.SyntaxKind.ThisKeyword) return Boolean(thisOf(ts, inner));
|
|
311
|
+
if (isAssertion(ts, inner)) return this.says(inner.type) === OTHER;
|
|
312
|
+
if (ts.isIdentifier(inner)) return this.notNodeBinding(nearestBinding(ts, inner), depth);
|
|
313
|
+
if (ts.isElementAccessExpression(inner)) return this.notNodes(inner.expression, depth + 1);
|
|
314
|
+
if (ts.isPropertyAccessExpression(inner)) return this.notNodeMember(inner, depth);
|
|
315
|
+
const items = methodOf(ts, inner, ITEM);
|
|
316
|
+
return Boolean(items) && this.notNodes(items, depth + 1);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Whether `x.y` is certainly not a node: a constructor, or a member typed otherwise. */
|
|
320
|
+
notNodeMember(access, depth) {
|
|
321
|
+
const ts = this.ts;
|
|
322
|
+
if (access.name.text === 'constructor') return true;
|
|
323
|
+
const of = unwrapped(ts, access.expression);
|
|
324
|
+
if (of.kind === ts.SyntaxKind.ThisKeyword) {
|
|
325
|
+
const field = this.thisField(access);
|
|
326
|
+
return Boolean(field) && this.notNodeBinding(field, depth);
|
|
327
|
+
}
|
|
328
|
+
const literal = isAssertion(ts, of) && ts.isTypeLiteralNode(of.type) && of.type;
|
|
329
|
+
const member =
|
|
330
|
+
literal && literal.members.find((one) => one.name?.getText() === access.name.text);
|
|
331
|
+
return Boolean(member) && this.says(member.type) === OTHER;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Whether a variable, parameter or field is certainly not a node. */
|
|
335
|
+
notNodeBinding(declaration, depth) {
|
|
336
|
+
if (!declaration) return true;
|
|
337
|
+
if (declaration === ELSE) return false;
|
|
338
|
+
const said = this.says(declaration.type);
|
|
339
|
+
if (said) return said === OTHER;
|
|
340
|
+
const items = this.iterated(declaration);
|
|
341
|
+
if (items) return this.notNodes(items, depth + 1);
|
|
342
|
+
return Boolean(declaration.initializer) && this.notNode(declaration.initializer, depth + 1);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Whether `expression` is certainly not an array of nodes. */
|
|
346
|
+
notNodes(expression, depth) {
|
|
347
|
+
const ts = this.ts;
|
|
348
|
+
const inner = unwrapped(ts, expression);
|
|
349
|
+
if (depth > 32) return false;
|
|
350
|
+
if (isAssertion(ts, inner)) return ![null, NODES].includes(this.says(inner.type));
|
|
351
|
+
if (ts.isCallExpression(inner)) {
|
|
352
|
+
const items = methodOf(ts, inner, SOME);
|
|
353
|
+
return Boolean(items) && this.notNodes(items, depth + 1);
|
|
354
|
+
}
|
|
355
|
+
const declaration = this.declarationOf(inner);
|
|
356
|
+
if (!declaration) return true;
|
|
357
|
+
if (declaration === ELSE) return false;
|
|
358
|
+
const said = this.says(declaration.type);
|
|
359
|
+
if (said) return said !== NODES;
|
|
360
|
+
return Boolean(declaration.initializer) && this.notNodes(declaration.initializer, depth + 1);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The declaration of a name, or of `this.field`, as `nearestBinding` answers; `ELSE` else. */
|
|
364
|
+
declarationOf(expression) {
|
|
365
|
+
const ts = this.ts;
|
|
366
|
+
if (ts.isIdentifier(expression)) return nearestBinding(ts, expression);
|
|
367
|
+
const own =
|
|
368
|
+
ts.isPropertyAccessExpression(expression) &&
|
|
369
|
+
expression.expression.kind === ts.SyntaxKind.ThisKeyword;
|
|
370
|
+
return (own && this.thisField(expression)) || ELSE;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** What an object literal is written as: the type it is declared, asserted or returned as. */
|
|
374
|
+
written(literal) {
|
|
375
|
+
const ts = this.ts;
|
|
376
|
+
let at = literal;
|
|
377
|
+
while (ts.isParenthesizedExpression(at.parent)) at = at.parent;
|
|
378
|
+
const parent = at.parent;
|
|
379
|
+
if (ts.isArrayLiteralExpression(parent)) return this.written(parent) === NODES ? NODE : null;
|
|
380
|
+
return this.type(givenType(ts, parent, at));
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The type `parent` gives the value `at` in it: as a declaration, an assertion or a return. */
|
|
385
|
+
function givenType(ts, parent, at) {
|
|
386
|
+
if (isAssertion(ts, parent)) return parent.type;
|
|
387
|
+
if (ts.isReturnStatement(parent)) return ts.getContainingFunction(parent)?.type;
|
|
388
|
+
if (ts.isArrowFunction(parent)) return parent.body === at ? parent.type : undefined;
|
|
389
|
+
const declared = ts.isVariableDeclaration(parent) || ts.isPropertyDeclaration(parent);
|
|
390
|
+
return declared && parent.initializer === at ? parent.type : undefined;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** The array `call` calls one of `methods` on, as `nodes.find(...)` does. */
|
|
394
|
+
function methodOf(ts, call, methods) {
|
|
395
|
+
const callee = ts.isCallExpression(call) && call.expression;
|
|
396
|
+
const named = callee && ts.isPropertyAccessExpression(callee) && methods.has(callee.name.text);
|
|
397
|
+
return named ? callee.expression : null;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The edit that renames a property name node `name` to `tag`, keeping a string's quotes. */
|
|
401
|
+
function renamed(name) {
|
|
402
|
+
const start = name.getStart();
|
|
403
|
+
const quoted = name.getText().length > 4;
|
|
404
|
+
return { start: quoted ? start + 1 : start, end: name.getEnd() - (quoted ? 1 : 0), text: 'tag' };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** `{ name }` taken from a node as `{ tag: name }`, and `{ name: n }` as `{ tag: n }`. */
|
|
408
|
+
function destructured(ts, pattern, edits) {
|
|
409
|
+
for (const element of pattern.elements) {
|
|
410
|
+
const key = element.propertyName ?? (element.dotDotDotToken ? null : element.name);
|
|
411
|
+
if (!key || !ts.isIdentifier(key) || key.text !== 'name') continue;
|
|
412
|
+
if (element.propertyName) edits.push(renamed(key));
|
|
413
|
+
else edits.push({ start: key.getStart(), end: key.getStart(), text: 'tag: ' });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** Whether a destructuring pattern takes `name`. */
|
|
418
|
+
const takesName = (ts, pattern) =>
|
|
419
|
+
pattern.elements.some((element) => {
|
|
420
|
+
const key = element.propertyName ?? (element.dotDotDotToken ? null : element.name);
|
|
421
|
+
return key && ts.isIdentifier(key) && key.text === 'name';
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
/** An object literal written as a node with its `name` key as `tag`. */
|
|
425
|
+
function literal(ts, object, edits) {
|
|
426
|
+
for (const property of object.properties) {
|
|
427
|
+
const key = property.name;
|
|
428
|
+
const named = key && (ts.isIdentifier(key) || ts.isStringLiteral(key)) && key.text === 'name';
|
|
429
|
+
if (!named) continue;
|
|
430
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
431
|
+
edits.push({ start: key.getStart(), end: key.getStart(), text: 'tag: ' });
|
|
432
|
+
} else if (ts.isPropertyAssignment(property)) {
|
|
433
|
+
edits.push(renamed(key));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** Where a node is in its file, as a note gives it. */
|
|
439
|
+
const lineOf = (source, node) => source.getLineAndCharacterOfPosition(node.getStart()).line + 1;
|
|
440
|
+
|
|
441
|
+
/** The note for a `.name` this migration cannot tell is a node's. */
|
|
442
|
+
function note(file, source, access) {
|
|
443
|
+
const read = access.getText().replace(/\s+/g, ' ');
|
|
444
|
+
const instead = read.replace(/name(['"]?\]?)$/, 'tag$1');
|
|
445
|
+
return (
|
|
446
|
+
`${file}:${lineOf(source, access)}: ${read} may be an engine node's element tag, which is ` +
|
|
447
|
+
`now tag. If it is, read ${instead} instead.`
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** The note for a `name` taken from a node cast to another type, which still compiles. */
|
|
452
|
+
function castNote(file, source, node, verb) {
|
|
453
|
+
const text = node.getText().replace(/\s+/g, ' ');
|
|
454
|
+
return (
|
|
455
|
+
`${file}:${lineOf(source, node)}: ${text} ${verb}s name from an engine node read as another ` +
|
|
456
|
+
`type. Its element tag is now tag: ${verb} tag, and say so in the type.`
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** A `.name` or `['name']`: what it is read from, and the node naming the property. */
|
|
461
|
+
function nameRead(ts, node) {
|
|
462
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === 'name') {
|
|
463
|
+
return { receiver: node.expression, key: node.name };
|
|
464
|
+
}
|
|
465
|
+
const argument = ts.isElementAccessExpression(node) && node.argumentExpression;
|
|
466
|
+
if (argument && ts.isStringLiteralLike(argument) && argument.text === 'name') {
|
|
467
|
+
return { receiver: node.expression, key: argument };
|
|
468
|
+
}
|
|
469
|
+
return null;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** Whether a `.name` is read from an `ElementRef`'s `nativeElement`. */
|
|
473
|
+
const fromNativeElement = (ts, receiver) =>
|
|
474
|
+
ts.isPropertyAccessExpression(receiver) && receiver.name.text === 'nativeElement';
|
|
475
|
+
|
|
476
|
+
/** One read of `name`: an edit where it is a node's, a note where it may be. */
|
|
477
|
+
function read(context, node) {
|
|
478
|
+
const { ts, reader, edits, notes, file, source, typed } = context;
|
|
479
|
+
const found = nameRead(ts, node);
|
|
480
|
+
if (!found) return;
|
|
481
|
+
if (reader.kind(found.receiver) === NODE) {
|
|
482
|
+
edits.push(renamed(found.key));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (reader.castNode(found.receiver)) {
|
|
486
|
+
notes.push(castNote(file, source, node, 'read'));
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const unsure = fromNativeElement(ts, found.receiver) || typed;
|
|
490
|
+
const said = unsure && !reader.notNode(found.receiver) && note(file, source, node);
|
|
491
|
+
if (said && !notes.includes(said)) {
|
|
492
|
+
notes.push(said);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** The destructuring patterns and object literals that name `name` as a key of a node. */
|
|
497
|
+
function keys(context, node) {
|
|
498
|
+
const { ts, reader, edits, notes, file, source } = context;
|
|
499
|
+
const pattern =
|
|
500
|
+
(ts.isVariableDeclaration(node) || ts.isParameter(node)) &&
|
|
501
|
+
ts.isObjectBindingPattern(node.name);
|
|
502
|
+
if (pattern && reader.binding(node, 0) === NODE) destructured(ts, node.name, edits);
|
|
503
|
+
else if (
|
|
504
|
+
pattern &&
|
|
505
|
+
node.initializer &&
|
|
506
|
+
reader.castNode(node.initializer) &&
|
|
507
|
+
takesName(ts, node.name)
|
|
508
|
+
) {
|
|
509
|
+
notes.push(castNote(file, source, node, 'take'));
|
|
510
|
+
}
|
|
511
|
+
if (ts.isObjectLiteralExpression(node) && reader.written(node) === NODE) {
|
|
512
|
+
literal(ts, node, edits);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** A `name` field or getter of a class that implements a node type, as `tag`. */
|
|
517
|
+
function ownField(context, node) {
|
|
518
|
+
const { ts, reader, edits } = context;
|
|
519
|
+
const field = ts.isPropertyDeclaration(node) || ts.isGetAccessorDeclaration(node);
|
|
520
|
+
const own = field && ts.isIdentifier(node.name) && node.name.text === 'name';
|
|
521
|
+
if (own && ts.isClassLike(node.parent) && implementsNode(ts, node.parent, reader.names)) {
|
|
522
|
+
edits.push(renamed(node.name));
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** Rewrites one file's reads of a node's `name`, and answers its notes. */
|
|
527
|
+
function rewrite(ts, host, file, text) {
|
|
528
|
+
const source = ts.createSourceFile(
|
|
529
|
+
file,
|
|
530
|
+
text,
|
|
531
|
+
ts.ScriptTarget.Latest,
|
|
532
|
+
true,
|
|
533
|
+
scriptKind(ts, file),
|
|
534
|
+
);
|
|
535
|
+
const names = imported(ts, source);
|
|
536
|
+
localTypes(ts, source, names);
|
|
537
|
+
const context = {
|
|
538
|
+
ts,
|
|
539
|
+
reader: new Reader(ts, names),
|
|
540
|
+
edits: [],
|
|
541
|
+
notes: [],
|
|
542
|
+
file,
|
|
543
|
+
source,
|
|
544
|
+
typed: names.size > 0,
|
|
545
|
+
};
|
|
546
|
+
const visit = (node) => {
|
|
547
|
+
read(context, node);
|
|
548
|
+
keys(context, node);
|
|
549
|
+
ownField(context, node);
|
|
550
|
+
ts.forEachChild(node, visit);
|
|
551
|
+
};
|
|
552
|
+
visit(source);
|
|
553
|
+
if (context.edits.length) host.write(file, applied(text, context.edits));
|
|
554
|
+
return context.notes;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/** An interface or type in the file built on a node type, which is one too. */
|
|
558
|
+
function localType(ts, node, names) {
|
|
559
|
+
if (ts.isInterfaceDeclaration(node)) {
|
|
560
|
+
const bases = (node.heritageClauses ?? []).flatMap((clause) => clause.types);
|
|
561
|
+
return bases.some((base) => names.has(base.expression.getText())) ? node.name.text : null;
|
|
562
|
+
}
|
|
563
|
+
const alias = ts.isTypeAliasDeclaration(node) && isNodeType(ts, node.type, names);
|
|
564
|
+
return alias ? node.name.text : null;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** Adds the file's own node types to `names`, until one built on another is found too. */
|
|
568
|
+
function localTypes(ts, source, names) {
|
|
569
|
+
if (!names.size) return;
|
|
570
|
+
let grew = true;
|
|
571
|
+
while (grew) {
|
|
572
|
+
grew = false;
|
|
573
|
+
const visit = (node) => {
|
|
574
|
+
const name = localType(ts, node, names);
|
|
575
|
+
if (name && !names.has(name)) {
|
|
576
|
+
names.add(name);
|
|
577
|
+
grew = true;
|
|
578
|
+
}
|
|
579
|
+
ts.forEachChild(node, visit);
|
|
580
|
+
};
|
|
581
|
+
visit(source);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** Whether a file can read a node's `name` at all, before it is parsed. */
|
|
586
|
+
const mentions = (text) =>
|
|
587
|
+
text !== null &&
|
|
588
|
+
/\bname\b/.test(text) &&
|
|
589
|
+
(Object.keys(TYPES).some((entry) => text.includes(entry)) || text.includes('nativeElement'));
|
|
590
|
+
|
|
591
|
+
/** @param {import('./host.cjs').Host} host */
|
|
592
|
+
function engineNodeTag(host) {
|
|
593
|
+
const ts = typescript();
|
|
594
|
+
const notes = [];
|
|
595
|
+
for (const file of files(host)) {
|
|
596
|
+
const text = SOURCE.test(file) ? host.read(file) : null;
|
|
597
|
+
if (mentions(text)) notes.push(...rewrite(ts, host, file, text));
|
|
598
|
+
}
|
|
599
|
+
return notes;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
module.exports = { engineNodeTag };
|
package/migrations.cjs
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
* what is left for the developer to do, one line each.
|
|
19
19
|
*/
|
|
20
20
|
const { deviceReactNativeImport } = require('./device-react-native-import.cjs');
|
|
21
|
+
const { engineNodeTag } = require('./engine-node-tag.cjs');
|
|
21
22
|
const { splitStoreAndPlayer } = require('./split-store-and-player.cjs');
|
|
22
23
|
const { syncAppVersions } = require('./sync-app-versions.cjs');
|
|
23
24
|
const { trackingAvailableGetter } = require('./tracking-available-getter.cjs');
|
|
@@ -52,6 +53,12 @@ const migrations = [
|
|
|
52
53
|
'Note each import of reactNative or ReactNative from @ng-native/device, which no longer exports them.',
|
|
53
54
|
run: deviceReactNativeImport,
|
|
54
55
|
},
|
|
56
|
+
{
|
|
57
|
+
name: 'engine-node-tag',
|
|
58
|
+
version: '0.14.0',
|
|
59
|
+
description: "Read an engine node's element tag as tag rather than name.",
|
|
60
|
+
run: engineNodeTag,
|
|
61
|
+
},
|
|
55
62
|
];
|
|
56
63
|
|
|
57
64
|
module.exports = { migrations };
|
package/package.json
CHANGED
package/scope.cjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a name or a `this` refers to at a point in a file, read from the syntax alone: the nearest
|
|
3
|
+
* declaration of the name in the scopes around it, and the class whose instance (or the class
|
|
4
|
+
* itself) `this` is. A migration that rewrites a member only where it can tell what it is read
|
|
5
|
+
* from builds on these; one that cannot tell leaves the code alone.
|
|
6
|
+
*/
|
|
7
|
+
const isStatic = (ts, node) =>
|
|
8
|
+
Boolean(ts.getModifiers(node)?.some((m) => m.kind === ts.SyntaxKind.StaticKeyword));
|
|
9
|
+
|
|
10
|
+
/** A class member whose own `this` is the class's: a method, accessor, constructor or field. */
|
|
11
|
+
const isMember = (ts, node) =>
|
|
12
|
+
(ts.isClassDeclaration(node.parent) || ts.isClassExpression(node.parent)) &&
|
|
13
|
+
(ts.isMethodDeclaration(node) ||
|
|
14
|
+
ts.isConstructorDeclaration(node) ||
|
|
15
|
+
ts.isGetAccessorDeclaration(node) ||
|
|
16
|
+
ts.isSetAccessorDeclaration(node) ||
|
|
17
|
+
ts.isPropertyDeclaration(node) ||
|
|
18
|
+
ts.isClassStaticBlockDeclaration(node));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The class `this` is in at `node`, and whether it is the class itself rather than an instance, or
|
|
22
|
+
* null where `this` is something else: inside a plain function or an object literal's method, which
|
|
23
|
+
* bind a `this` of their own. An arrow function keeps the `this` around it.
|
|
24
|
+
*/
|
|
25
|
+
function thisOf(ts, node) {
|
|
26
|
+
for (let at = node.parent; at; at = at.parent) {
|
|
27
|
+
if (isMember(ts, at)) {
|
|
28
|
+
return { cls: at.parent, static: ts.isClassStaticBlockDeclaration(at) || isStatic(ts, at) };
|
|
29
|
+
}
|
|
30
|
+
if (ts.isFunctionLike(at) && !ts.isArrowFunction(at)) return null;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** What a binding of a name is when only its shape is known: a pattern, a catch, a function. */
|
|
36
|
+
const ELSE = Symbol('something else');
|
|
37
|
+
|
|
38
|
+
/** Whether a binding name, or any name inside a destructuring pattern, is `name`. */
|
|
39
|
+
function binds(ts, nameNode, name) {
|
|
40
|
+
if (ts.isIdentifier(nameNode)) return nameNode.text === name;
|
|
41
|
+
return nameNode.elements.some(
|
|
42
|
+
(element) => !ts.isOmittedExpression(element) && binds(ts, element.name, name),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A variable or parameter binding `name`: itself if bound plainly, `ELSE` if destructured. */
|
|
47
|
+
const bound = (ts, declaration, name) => (ts.isIdentifier(declaration.name) ? declaration : ELSE);
|
|
48
|
+
|
|
49
|
+
/** The bindings of `name` a list of variable declarations makes, as `bound` answers. */
|
|
50
|
+
function inDeclarations(ts, list, name) {
|
|
51
|
+
const found = list.declarations.find((d) => binds(ts, d.name, name));
|
|
52
|
+
return found && bound(ts, found);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The bindings of `name` a block's statements make: variables, and functions and classes. */
|
|
56
|
+
function inStatements(ts, statements, name) {
|
|
57
|
+
for (const statement of statements) {
|
|
58
|
+
if (ts.isVariableStatement(statement)) {
|
|
59
|
+
const found = inDeclarations(ts, statement.declarationList, name);
|
|
60
|
+
if (found) return found;
|
|
61
|
+
}
|
|
62
|
+
const named = ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement);
|
|
63
|
+
if (named && statement.name?.text === name) return ELSE;
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A function's own name, if it is an expression's, and its parameters. */
|
|
69
|
+
function inFunction(ts, node, name) {
|
|
70
|
+
if (ts.isFunctionExpression(node) && node.name?.text === name) return ELSE;
|
|
71
|
+
const parameter = node.parameters.find((p) => binds(ts, p.name, name));
|
|
72
|
+
return parameter && bound(ts, parameter);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A `for`, `for...of` or `for...in` loop's own variables. */
|
|
76
|
+
function inLoop(ts, node, name) {
|
|
77
|
+
const { initializer } = node;
|
|
78
|
+
if (!initializer || !ts.isVariableDeclarationList(initializer)) return undefined;
|
|
79
|
+
return inDeclarations(ts, initializer, name);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A `catch` clause's binding. */
|
|
83
|
+
const inCatch = (ts, node, name) =>
|
|
84
|
+
node.variableDeclaration && binds(ts, node.variableDeclaration.name, name) ? ELSE : undefined;
|
|
85
|
+
|
|
86
|
+
const isLoop = (ts, node) =>
|
|
87
|
+
ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node);
|
|
88
|
+
|
|
89
|
+
const isBlock = (ts, node) => ts.isSourceFile(node) || ts.isBlock(node) || ts.isModuleBlock(node);
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The binding of `name` the scope `node` opens, if it makes one there: the declaration when it is a
|
|
93
|
+
* plain variable or parameter, `ELSE` for one only its shape is known of (a destructured name, a
|
|
94
|
+
* catch binding, a function or class), whose type the syntax does not give.
|
|
95
|
+
*/
|
|
96
|
+
function declaredIn(ts, node, name) {
|
|
97
|
+
if (ts.isFunctionLike(node)) return inFunction(ts, node, name);
|
|
98
|
+
if (isLoop(ts, node)) return inLoop(ts, node, name);
|
|
99
|
+
if (ts.isCatchClause(node)) return inCatch(ts, node, name);
|
|
100
|
+
return isBlock(ts, node) ? inStatements(ts, node.statements, name) : undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The nearest binding of the name `identifier` reads: its declaration when it is a plain variable
|
|
105
|
+
* or parameter, `ELSE` when only its shape is known, and undefined when no scope in the file
|
|
106
|
+
* declares it, as for an import or a global.
|
|
107
|
+
*/
|
|
108
|
+
function nearestBinding(ts, identifier) {
|
|
109
|
+
for (let at = identifier.parent; at; at = at.parent) {
|
|
110
|
+
const declaration = declaredIn(ts, at, identifier.text);
|
|
111
|
+
if (declaration) return declaration;
|
|
112
|
+
}
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
module.exports = { ELSE, isStatic, nearestBinding, thisOf };
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
const { applied, scriptKind, SOURCE, typescript } = require('./move-imports.cjs');
|
|
14
14
|
const { files } = require('./host.cjs');
|
|
15
|
+
const { ELSE, isStatic, nearestBinding, thisOf } = require('./scope.cjs');
|
|
15
16
|
|
|
16
17
|
const ENTRY = '@ng-native/expo/tracking';
|
|
17
18
|
|
|
@@ -46,9 +47,6 @@ function holds(ts, declaration, types) {
|
|
|
46
47
|
return Boolean(initializer && isInjected(ts, initializer, types));
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
const isStatic = (ts, node) =>
|
|
50
|
-
Boolean(ts.getModifiers(node)?.some((m) => m.kind === ts.SyntaxKind.StaticKeyword));
|
|
51
|
-
|
|
52
50
|
/**
|
|
53
51
|
* The members of a class that hold a `Tracking`, apart by whether `this` reaches them from an
|
|
54
52
|
* instance or from the class itself: properties, and constructor parameter properties.
|
|
@@ -72,107 +70,10 @@ function classHolders(ts, cls, types) {
|
|
|
72
70
|
return names;
|
|
73
71
|
}
|
|
74
72
|
|
|
75
|
-
/** A class member whose own `this` is the class's: a method, accessor, constructor or field. */
|
|
76
|
-
const isMember = (ts, node) =>
|
|
77
|
-
(ts.isClassDeclaration(node.parent) || ts.isClassExpression(node.parent)) &&
|
|
78
|
-
(ts.isMethodDeclaration(node) ||
|
|
79
|
-
ts.isConstructorDeclaration(node) ||
|
|
80
|
-
ts.isGetAccessorDeclaration(node) ||
|
|
81
|
-
ts.isSetAccessorDeclaration(node) ||
|
|
82
|
-
ts.isPropertyDeclaration(node) ||
|
|
83
|
-
ts.isClassStaticBlockDeclaration(node));
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The class `this` is in at `node`, and whether it is the class itself rather than an instance, or
|
|
87
|
-
* null where `this` is something else: inside a plain function or an object literal's method, which
|
|
88
|
-
* bind a `this` of their own. An arrow function keeps the `this` around it.
|
|
89
|
-
*/
|
|
90
|
-
function thisOf(ts, node) {
|
|
91
|
-
for (let at = node.parent; at; at = at.parent) {
|
|
92
|
-
if (isMember(ts, at)) {
|
|
93
|
-
return { cls: at.parent, static: ts.isClassStaticBlockDeclaration(at) || isStatic(ts, at) };
|
|
94
|
-
}
|
|
95
|
-
if (ts.isFunctionLike(at) && !ts.isArrowFunction(at)) return null;
|
|
96
|
-
}
|
|
97
|
-
return null;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/** What a binding of a name is when only its shape is known: a pattern, a catch, a function. */
|
|
101
|
-
const ELSE = Symbol('something else');
|
|
102
|
-
|
|
103
|
-
/** Whether a binding name, or any name inside a destructuring pattern, is `name`. */
|
|
104
|
-
function binds(ts, nameNode, name) {
|
|
105
|
-
if (ts.isIdentifier(nameNode)) return nameNode.text === name;
|
|
106
|
-
return nameNode.elements.some(
|
|
107
|
-
(element) => !ts.isOmittedExpression(element) && binds(ts, element.name, name),
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** A variable or parameter binding `name`: itself if bound plainly, `ELSE` if destructured. */
|
|
112
|
-
const bound = (ts, declaration, name) => (ts.isIdentifier(declaration.name) ? declaration : ELSE);
|
|
113
|
-
|
|
114
|
-
/** The bindings of `name` a list of variable declarations makes, as `bound` answers. */
|
|
115
|
-
function inDeclarations(ts, list, name) {
|
|
116
|
-
const found = list.declarations.find((d) => binds(ts, d.name, name));
|
|
117
|
-
return found && bound(ts, found);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** The bindings of `name` a block's statements make: variables, and functions and classes. */
|
|
121
|
-
function inStatements(ts, statements, name) {
|
|
122
|
-
for (const statement of statements) {
|
|
123
|
-
if (ts.isVariableStatement(statement)) {
|
|
124
|
-
const found = inDeclarations(ts, statement.declarationList, name);
|
|
125
|
-
if (found) return found;
|
|
126
|
-
}
|
|
127
|
-
const named = ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement);
|
|
128
|
-
if (named && statement.name?.text === name) return ELSE;
|
|
129
|
-
}
|
|
130
|
-
return undefined;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** A function's own name, if it is an expression's, and its parameters. */
|
|
134
|
-
function inFunction(ts, node, name) {
|
|
135
|
-
if (ts.isFunctionExpression(node) && node.name?.text === name) return ELSE;
|
|
136
|
-
const parameter = node.parameters.find((p) => binds(ts, p.name, name));
|
|
137
|
-
return parameter && bound(ts, parameter);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/** A `for`, `for...of` or `for...in` loop's own variables. */
|
|
141
|
-
function inLoop(ts, node, name) {
|
|
142
|
-
const { initializer } = node;
|
|
143
|
-
if (!initializer || !ts.isVariableDeclarationList(initializer)) return undefined;
|
|
144
|
-
return inDeclarations(ts, initializer, name);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** A `catch` clause's binding. */
|
|
148
|
-
const inCatch = (ts, node, name) =>
|
|
149
|
-
node.variableDeclaration && binds(ts, node.variableDeclaration.name, name) ? ELSE : undefined;
|
|
150
|
-
|
|
151
|
-
const isLoop = (ts, node) =>
|
|
152
|
-
ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node);
|
|
153
|
-
|
|
154
|
-
const isBlock = (ts, node) => ts.isSourceFile(node) || ts.isBlock(node) || ts.isModuleBlock(node);
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* The binding of `name` the scope `node` opens, if it makes one there: the declaration when it is a
|
|
158
|
-
* plain variable or parameter, `ELSE` for one only its shape is known of (a destructured name, a
|
|
159
|
-
* catch binding, a function or class), which is never a `Tracking` this can tell.
|
|
160
|
-
*/
|
|
161
|
-
function declaredIn(ts, node, name) {
|
|
162
|
-
if (ts.isFunctionLike(node)) return inFunction(ts, node, name);
|
|
163
|
-
if (isLoop(ts, node)) return inLoop(ts, node, name);
|
|
164
|
-
if (ts.isCatchClause(node)) return inCatch(ts, node, name);
|
|
165
|
-
return isBlock(ts, node) ? inStatements(ts, node.statements, name) : undefined;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
73
|
/** Whether the nearest binding of the name `identifier` reads holds a `Tracking`. */
|
|
169
74
|
function identifierHolds(ts, identifier, types) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (declaration === ELSE) return false;
|
|
173
|
-
if (declaration) return holds(ts, declaration, types);
|
|
174
|
-
}
|
|
175
|
-
return false;
|
|
75
|
+
const declaration = nearestBinding(ts, identifier);
|
|
76
|
+
return Boolean(declaration) && declaration !== ELSE && holds(ts, declaration, types);
|
|
176
77
|
}
|
|
177
78
|
|
|
178
79
|
/** Whether `receiver` is a `Tracking`, as this file can tell. */
|