@ng-native/migrate 0.16.0 → 0.17.1
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-fields.cjs +25 -0
- package/engine-node-tag.cjs +10 -595
- package/library-typecheck-declaration-map.cjs +40 -0
- package/migrations.cjs +22 -0
- package/native-typecheck-target.cjs +49 -0
- package/node-fields.cjs +617 -0
- package/package.json +1 -1
- package/sync-app-versions.cjs +1 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0.17.0 renamed the fields of an engine node that have the name of a DOM element's member, so
|
|
3
|
+
* a node seen as an element does not hide that member: `children` is `childList`, `text` is
|
|
4
|
+
* `nodeText`, `kind` is `nodeKind`, `host` is `engine` and `sheet` is `templateSheet`.
|
|
5
|
+
* `HostNode`, `StyleTarget` and `ViewNameNode` from `@ng-native/fabric`, and `BrowserNode` from
|
|
6
|
+
* `@ng-native/web`, which mirror it, have the new names too. What is rewritten, and what is
|
|
7
|
+
* noted, is as `node-fields.cjs` says.
|
|
8
|
+
*/
|
|
9
|
+
const { nodeFieldRenames } = require('./node-fields.cjs');
|
|
10
|
+
|
|
11
|
+
const engineNodeFields = nodeFieldRenames({
|
|
12
|
+
types: {
|
|
13
|
+
'@ng-native/fabric': ['EngineNode', 'HostNode', 'StyleTarget', 'ViewNameNode'],
|
|
14
|
+
'@ng-native/web': ['BrowserNode'],
|
|
15
|
+
},
|
|
16
|
+
fields: {
|
|
17
|
+
children: { to: 'childList', what: 'child nodes' },
|
|
18
|
+
text: { to: 'nodeText', what: 'text' },
|
|
19
|
+
kind: { to: 'nodeKind', what: 'kind' },
|
|
20
|
+
host: { to: 'engine', what: 'host engine' },
|
|
21
|
+
sheet: { to: 'templateSheet', what: 'sheet' },
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
module.exports = { engineNodeFields };
|
package/engine-node-tag.cjs
CHANGED
|
@@ -2,601 +2,16 @@
|
|
|
2
2
|
* 0.14.0 renamed the field an engine node keeps its element's tag in, `view` or `text-input`, from
|
|
3
3
|
* `name` to `tag`, so that `name` is free for the DOM's attribute. `StyleTarget`, `ViewNameNode`
|
|
4
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`.
|
|
5
|
+
* mirror it, have `tag` too. What is rewritten, and what is noted, is as `node-fields.cjs` says.
|
|
23
6
|
*/
|
|
24
|
-
const {
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
'
|
|
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
|
-
}
|
|
7
|
+
const { nodeFieldRenames } = require('./node-fields.cjs');
|
|
8
|
+
|
|
9
|
+
const engineNodeTag = nodeFieldRenames({
|
|
10
|
+
types: {
|
|
11
|
+
'@ng-native/fabric': ['EngineNode', 'StyleTarget', 'ViewNameNode', 'NamedHostNode'],
|
|
12
|
+
'@ng-native/web': ['BrowserNode'],
|
|
13
|
+
},
|
|
14
|
+
fields: { name: { to: 'tag', what: 'element tag' } },
|
|
15
|
+
});
|
|
601
16
|
|
|
602
17
|
module.exports = { engineNodeTag };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns off `declarationMap` in each `tsconfig.typecheck.json` `@ng-native/nx:library` wrote.
|
|
3
|
+
*
|
|
4
|
+
* That config sets `composite: false` so `ngc` can run with `noEmit`, and Nx's TypeScript preset
|
|
5
|
+
* turns `declarationMap` on in `tsconfig.base.json`. With neither `composite` nor `declaration`,
|
|
6
|
+
* TypeScript stops with TS5069 before it checks anything, so `nx typecheck` fails. A config that
|
|
7
|
+
* sets `declarationMap` itself, either way, is left as it is.
|
|
8
|
+
*/
|
|
9
|
+
const { files } = require('./host.cjs');
|
|
10
|
+
const { formatted } = require('./sync-app-versions.cjs');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {import('./host.cjs').Host} host
|
|
14
|
+
* @returns {string[]} the configs that are not plain JSON, which are left for the developer
|
|
15
|
+
*/
|
|
16
|
+
function libraryTypecheckDeclarationMap(host) {
|
|
17
|
+
const notes = [];
|
|
18
|
+
for (const file of files(host)) {
|
|
19
|
+
if (file !== 'tsconfig.typecheck.json' && !file.endsWith('/tsconfig.typecheck.json')) continue;
|
|
20
|
+
const original = host.read(file) ?? '';
|
|
21
|
+
let config;
|
|
22
|
+
try {
|
|
23
|
+
config = JSON.parse(original);
|
|
24
|
+
} catch {
|
|
25
|
+
notes.push(
|
|
26
|
+
`${file}: not plain JSON, so it was left as it is. If ngc reports TS5069 for it, add ` +
|
|
27
|
+
'"declarationMap": false to its compilerOptions.',
|
|
28
|
+
);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
// The generator's: ngc's options, over the library's own config. An app's extends its own.
|
|
32
|
+
const generated = config.extends === './tsconfig.lib.json' && config.angularCompilerOptions;
|
|
33
|
+
if (!generated || 'declarationMap' in (config.compilerOptions ?? {})) continue;
|
|
34
|
+
config.compilerOptions = { ...config.compilerOptions, declarationMap: false };
|
|
35
|
+
host.write(file, formatted(config, original));
|
|
36
|
+
}
|
|
37
|
+
return notes;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { libraryTypecheckDeclarationMap };
|