@swipt/cli 0.1.0-nightly.77 → 0.1.0-nightly.79
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/dist/availability.d.ts +5 -0
- package/dist/availability.js +349 -0
- package/dist/availability.js.map +1 -0
- package/dist/client.d.ts +25 -0
- package/dist/client.js +248 -0
- package/dist/client.js.map +1 -0
- package/dist/connection.d.ts +24 -0
- package/dist/connection.js +293 -0
- package/dist/connection.js.map +1 -0
- package/dist/coredevice.d.ts +22 -0
- package/dist/coredevice.js +329 -0
- package/dist/coredevice.js.map +1 -0
- package/dist/device.d.ts +130 -0
- package/dist/device.js +539 -0
- package/dist/device.js.map +1 -0
- package/dist/i18n.d.ts +2 -0
- package/dist/i18n.js +57 -0
- package/dist/i18n.js.map +1 -0
- package/dist/ignore.d.ts +8 -0
- package/dist/ignore.js +112 -0
- package/dist/ignore.js.map +1 -0
- package/dist/metro.d.ts +10 -0
- package/dist/metro.js +349 -0
- package/dist/metro.js.map +1 -0
- package/dist/output.d.ts +20 -0
- package/dist/output.js +116 -0
- package/dist/output.js.map +1 -0
- package/dist/project.d.ts +18 -0
- package/dist/project.js +696 -0
- package/dist/project.js.map +1 -0
- package/dist/proto/common_pb.js +34 -0
- package/dist/proto/common_pb.js.map +1 -0
- package/dist/proto/logs_pb.js +15 -0
- package/dist/proto/logs_pb.js.map +1 -0
- package/dist/proto/repl_pb.js +15 -0
- package/dist/proto/repl_pb.js.map +1 -0
- package/dist/proto/swiplets_pb.js +28 -0
- package/dist/proto/swiplets_pb.js.map +1 -0
- package/dist/proto/system_pb.js +15 -0
- package/dist/proto/system_pb.js.map +1 -0
- package/dist/sideload.d.ts +17 -0
- package/dist/sideload.js +1098 -0
- package/dist/sideload.js.map +1 -0
- package/dist/swipt.d.ts +1 -0
- package/dist/swipt.js +631 -0
- package/dist/swipt.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +15 -0
- package/dist/version.js.map +1 -0
- package/package.json +21 -7
- package/availability.mjs +0 -642
- package/client.mjs +0 -375
- package/connection.mjs +0 -450
- package/coredevice.mjs +0 -478
- package/device.mjs +0 -837
- package/i18n.mjs +0 -71
- package/ignore.mjs +0 -149
- package/metro.mjs +0 -432
- package/output.mjs +0 -177
- package/project.mjs +0 -1013
- package/proto/common_pb.js +0 -98
- package/proto/logs_pb.js +0 -34
- package/proto/repl_pb.js +0 -55
- package/proto/swiplets_pb.js +0 -179
- package/proto/system_pb.js +0 -27
- package/sideload.mjs +0 -1696
- package/swipt.mjs +0 -726
- package/version.mjs +0 -16
package/availability.mjs
DELETED
|
@@ -1,642 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
import { NodeFlags, SyntaxKind } from "typescript/unstable/ast";
|
|
5
|
-
import { isTypeNode } from "typescript/unstable/ast/is";
|
|
6
|
-
import { API, SymbolFlags } from "typescript/unstable/sync";
|
|
7
|
-
|
|
8
|
-
import { language, tr } from "./i18n.mjs";
|
|
9
|
-
import { parseVersion, versionAtLeast } from "./version.mjs";
|
|
10
|
-
|
|
11
|
-
const requirements = {
|
|
12
|
-
apiLevel: {
|
|
13
|
-
parse: (text) => (/^[1-9]\d*$/.test(text) ? Number(text) : undefined),
|
|
14
|
-
covers: (level, needed) => level >= needed,
|
|
15
|
-
describe: (level) => tr("API level {level}", { level }),
|
|
16
|
-
},
|
|
17
|
-
iOS: {
|
|
18
|
-
parse: parseVersion,
|
|
19
|
-
covers: versionAtLeast,
|
|
20
|
-
describe: (version) => `iOS ${version.join(".")}`,
|
|
21
|
-
},
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const lists = new Intl.ListFormat(language, { type: "conjunction" });
|
|
25
|
-
|
|
26
|
-
const merge = (into, from) => {
|
|
27
|
-
for (const [name, needed] of Object.entries(from)) {
|
|
28
|
-
if (
|
|
29
|
-
into[name] === undefined ||
|
|
30
|
-
!requirements[name].covers(into[name], needed)
|
|
31
|
-
) {
|
|
32
|
-
into[name] = needed;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return into;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const describe = (needed, missing) =>
|
|
39
|
-
lists.format(
|
|
40
|
-
missing.map((name) => requirements[name].describe(needed[name])),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
function parseAnnotation(text) {
|
|
44
|
-
const requirement = {};
|
|
45
|
-
for (const entry of text.split(",").map((part) => part.trim())) {
|
|
46
|
-
const [kind, value, ...rest] = entry.split(/\s+/);
|
|
47
|
-
const parsed =
|
|
48
|
-
rest.length === 0 && Object.hasOwn(requirements, kind)
|
|
49
|
-
? requirements[kind].parse(value ?? "")
|
|
50
|
-
: undefined;
|
|
51
|
-
if (parsed === undefined) {
|
|
52
|
-
return { requirement, invalid: entry };
|
|
53
|
-
}
|
|
54
|
-
if (requirement[kind] !== undefined) {
|
|
55
|
-
return { requirement, repeated: kind };
|
|
56
|
-
}
|
|
57
|
-
requirement[kind] = parsed;
|
|
58
|
-
}
|
|
59
|
-
return { requirement };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const tagText = (tag) =>
|
|
63
|
-
typeof tag.comment === "string"
|
|
64
|
-
? tag.comment
|
|
65
|
-
: (tag.comment ?? []).map((part) => part.text).join("");
|
|
66
|
-
|
|
67
|
-
const annotationTags = (node) =>
|
|
68
|
-
(node.jsDoc ?? []).flatMap((comment) =>
|
|
69
|
-
(comment.tags ?? []).filter((tag) => tag.tagName.text === "available"),
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const exits = (statement) =>
|
|
73
|
-
statement.kind === SyntaxKind.ReturnStatement ||
|
|
74
|
-
statement.kind === SyntaxKind.ThrowStatement ||
|
|
75
|
-
statement.kind === SyntaxKind.BreakStatement ||
|
|
76
|
-
statement.kind === SyntaxKind.ContinueStatement ||
|
|
77
|
-
(statement.kind === SyntaxKind.Block &&
|
|
78
|
-
statement.statements.length > 0 &&
|
|
79
|
-
exits(statement.statements.at(-1)));
|
|
80
|
-
|
|
81
|
-
const hasModifier = (node, kind) =>
|
|
82
|
-
node.modifiers?.some((modifier) => modifier.kind === kind) ?? false;
|
|
83
|
-
|
|
84
|
-
const functions = new Set([
|
|
85
|
-
SyntaxKind.FunctionDeclaration,
|
|
86
|
-
SyntaxKind.FunctionExpression,
|
|
87
|
-
SyntaxKind.ArrowFunction,
|
|
88
|
-
SyntaxKind.MethodDeclaration,
|
|
89
|
-
SyntaxKind.GetAccessor,
|
|
90
|
-
SyntaxKind.SetAccessor,
|
|
91
|
-
SyntaxKind.Constructor,
|
|
92
|
-
]);
|
|
93
|
-
|
|
94
|
-
const classes = new Set([
|
|
95
|
-
SyntaxKind.ClassDeclaration,
|
|
96
|
-
SyntaxKind.ClassExpression,
|
|
97
|
-
]);
|
|
98
|
-
|
|
99
|
-
const valueHolders = new Set([
|
|
100
|
-
SyntaxKind.VariableDeclaration,
|
|
101
|
-
SyntaxKind.PropertyDeclaration,
|
|
102
|
-
SyntaxKind.PropertyAssignment,
|
|
103
|
-
]);
|
|
104
|
-
|
|
105
|
-
const objectMembers = new Set([
|
|
106
|
-
SyntaxKind.PropertyAssignment,
|
|
107
|
-
SyntaxKind.ShorthandPropertyAssignment,
|
|
108
|
-
SyntaxKind.MethodDeclaration,
|
|
109
|
-
SyntaxKind.GetAccessor,
|
|
110
|
-
SyntaxKind.SetAccessor,
|
|
111
|
-
]);
|
|
112
|
-
|
|
113
|
-
const inert = new Set([
|
|
114
|
-
SyntaxKind.ImportDeclaration,
|
|
115
|
-
SyntaxKind.ExportDeclaration,
|
|
116
|
-
SyntaxKind.InterfaceDeclaration,
|
|
117
|
-
SyntaxKind.TypeAliasDeclaration,
|
|
118
|
-
SyntaxKind.JsxClosingElement,
|
|
119
|
-
]);
|
|
120
|
-
|
|
121
|
-
function sourceFilesOf(root, metafiles) {
|
|
122
|
-
const files = new Set();
|
|
123
|
-
for (const metafile of metafiles) {
|
|
124
|
-
for (const input of Object.keys(metafile.inputs)) {
|
|
125
|
-
const file = path.resolve(input);
|
|
126
|
-
if (
|
|
127
|
-
!/^[a-z][\w-]+:/.test(input) &&
|
|
128
|
-
/\.[cm]?[jt]sx?$/.test(file) &&
|
|
129
|
-
!path.relative(root, file).startsWith("..") &&
|
|
130
|
-
!file.split(path.sep).includes("node_modules")
|
|
131
|
-
) {
|
|
132
|
-
files.add(fs.realpathSync(file));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
return [...files];
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export class AvailabilityChecker {
|
|
140
|
-
#api;
|
|
141
|
-
#open = new Set();
|
|
142
|
-
|
|
143
|
-
check(project, metafiles) {
|
|
144
|
-
const files = sourceFilesOf(project.root, metafiles);
|
|
145
|
-
if (files.length === 0 && this.#open.size === 0) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
this.#api ??= new API({ cwd: project.root });
|
|
149
|
-
// An open file keeps the content it was opened with, whatever fileChanges says
|
|
150
|
-
// (typescript 7.0.2).
|
|
151
|
-
if (this.#open.size > 0) {
|
|
152
|
-
this.#api.updateSnapshot({ closeFiles: [...this.#open] });
|
|
153
|
-
}
|
|
154
|
-
const snapshot = this.#api.updateSnapshot({
|
|
155
|
-
openFiles: files,
|
|
156
|
-
fileChanges: { invalidateAll: true },
|
|
157
|
-
});
|
|
158
|
-
this.#open = new Set(files);
|
|
159
|
-
if (files.length === 0) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
const problems = { lines: new Set(), guarding: false };
|
|
163
|
-
const groups = Map.groupBy(files, (file) =>
|
|
164
|
-
snapshot.getDefaultProjectForFile(file),
|
|
165
|
-
);
|
|
166
|
-
for (const [typescript, owned] of groups) {
|
|
167
|
-
new Checker(typescript, project, files, problems).check(owned);
|
|
168
|
-
}
|
|
169
|
-
if (problems.lines.size > 0) {
|
|
170
|
-
throw new Error(
|
|
171
|
-
[
|
|
172
|
-
...problems.lines,
|
|
173
|
-
...(problems.guarding
|
|
174
|
-
? [
|
|
175
|
-
tr(
|
|
176
|
-
'check these with available(), mark the code that uses them with @available, or raise "swiplet.minimumAPILevel" or "swiplet.minimumIOSVersion" in package.json',
|
|
177
|
-
),
|
|
178
|
-
]
|
|
179
|
-
: []),
|
|
180
|
-
].join("\n"),
|
|
181
|
-
);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
close() {
|
|
186
|
-
this.#api?.close();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
class Checker {
|
|
191
|
-
#project;
|
|
192
|
-
#checker;
|
|
193
|
-
#root;
|
|
194
|
-
#files;
|
|
195
|
-
#problems;
|
|
196
|
-
#minimum;
|
|
197
|
-
#entries;
|
|
198
|
-
#available;
|
|
199
|
-
#symbols = new Map();
|
|
200
|
-
#requirements = new Map();
|
|
201
|
-
#annotations = new Map();
|
|
202
|
-
#facts = [new Map(), new Map()];
|
|
203
|
-
#types = new Map();
|
|
204
|
-
|
|
205
|
-
constructor(typescript, project, files, problems) {
|
|
206
|
-
this.#project = typescript;
|
|
207
|
-
this.#checker = typescript.checker;
|
|
208
|
-
this.#root = project.root;
|
|
209
|
-
this.#files = new Set(files);
|
|
210
|
-
this.#problems = problems;
|
|
211
|
-
this.#minimum = {
|
|
212
|
-
apiLevel: project.config.minimumAPILevel ?? project.apiLevel,
|
|
213
|
-
iOS: parseVersion(
|
|
214
|
-
project.config.minimumIOSVersion ?? project.minimumIOSVersion,
|
|
215
|
-
),
|
|
216
|
-
};
|
|
217
|
-
this.#entries = new Set(
|
|
218
|
-
[project.app, project.extension]
|
|
219
|
-
.filter(Boolean)
|
|
220
|
-
.map((entry) => fs.realpathSync(entry)),
|
|
221
|
-
);
|
|
222
|
-
this.#available = this.#availableSymbol(project.apiDirectory);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
check(files) {
|
|
226
|
-
for (const file of files) {
|
|
227
|
-
const source = this.#project.program.getSourceFile(file);
|
|
228
|
-
const { identifiers, properties } = this.#collect(source);
|
|
229
|
-
const symbols = this.#checker.getSymbolAtLocation(identifiers);
|
|
230
|
-
for (const [index, node] of identifiers.entries()) {
|
|
231
|
-
this.#use(
|
|
232
|
-
node,
|
|
233
|
-
node.parent.kind === SyntaxKind.ShorthandPropertyAssignment
|
|
234
|
-
? this.#checker.getShorthandAssignmentValueSymbol(node.parent)
|
|
235
|
-
: symbols[index],
|
|
236
|
-
);
|
|
237
|
-
}
|
|
238
|
-
for (const node of properties) {
|
|
239
|
-
this.#use(node, this.#propertySymbol(node));
|
|
240
|
-
}
|
|
241
|
-
if (this.#entries.has(file)) {
|
|
242
|
-
for (const statement of source.statements) {
|
|
243
|
-
if (
|
|
244
|
-
statement.name !== undefined &&
|
|
245
|
-
hasModifier(statement, SyntaxKind.DefaultKeyword)
|
|
246
|
-
) {
|
|
247
|
-
this.#useEntry(statement.name);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
#collect(source) {
|
|
255
|
-
const identifiers = [];
|
|
256
|
-
const properties = [];
|
|
257
|
-
const visit = (node, checked) => {
|
|
258
|
-
for (const tag of annotationTags(node)) {
|
|
259
|
-
this.#parse(tagText(tag), tag);
|
|
260
|
-
}
|
|
261
|
-
const extended =
|
|
262
|
-
node.kind === SyntaxKind.ExpressionWithTypeArguments &&
|
|
263
|
-
node.parent.token === SyntaxKind.ExtendsKeyword &&
|
|
264
|
-
classes.has(node.parent.parent.kind);
|
|
265
|
-
if (inert.has(node.kind) || (isTypeNode(node) && !extended)) {
|
|
266
|
-
checked = false;
|
|
267
|
-
}
|
|
268
|
-
if (checked && node.kind === SyntaxKind.Identifier) {
|
|
269
|
-
const { parent } = node;
|
|
270
|
-
const property =
|
|
271
|
-
parent.kind === SyntaxKind.JsxAttribute ||
|
|
272
|
-
(objectMembers.has(parent.kind) &&
|
|
273
|
-
parent.parent.kind === SyntaxKind.ObjectLiteralExpression &&
|
|
274
|
-
parent.name === node) ||
|
|
275
|
-
(parent.kind === SyntaxKind.BindingElement &&
|
|
276
|
-
parent.parent.kind === SyntaxKind.ObjectBindingPattern &&
|
|
277
|
-
(parent.propertyName ?? parent.name) === node);
|
|
278
|
-
if (property) {
|
|
279
|
-
properties.push(node);
|
|
280
|
-
}
|
|
281
|
-
if (
|
|
282
|
-
parent.kind === SyntaxKind.ShorthandPropertyAssignment ||
|
|
283
|
-
(!property &&
|
|
284
|
-
parent.propertyName !== node &&
|
|
285
|
-
(parent.name !== node ||
|
|
286
|
-
parent.kind === SyntaxKind.PropertyAccessExpression))
|
|
287
|
-
) {
|
|
288
|
-
identifiers.push(node);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
node.forEachChild((child) => visit(child, checked));
|
|
292
|
-
};
|
|
293
|
-
source.forEachChild((child) => visit(child, true));
|
|
294
|
-
return { identifiers, properties };
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
#use(node, symbol) {
|
|
298
|
-
if (symbol === undefined) {
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
const needed = this.#requirementOf(symbol);
|
|
302
|
-
const missing = Object.keys(needed).filter(
|
|
303
|
-
(name) => !this.#covered(name, needed[name], node),
|
|
304
|
-
);
|
|
305
|
-
if (missing.length > 0) {
|
|
306
|
-
this.#problems.guarding = true;
|
|
307
|
-
this.#problems.lines.add(
|
|
308
|
-
tr("{location}: {name} needs {requirement}", {
|
|
309
|
-
location: this.#location(node),
|
|
310
|
-
name: node.text,
|
|
311
|
-
requirement: describe(needed, missing),
|
|
312
|
-
}),
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
#useEntry(name) {
|
|
318
|
-
const symbol = this.#checker.getSymbolAtLocation(name);
|
|
319
|
-
const needed = symbol === undefined ? {} : this.#requirementOf(symbol);
|
|
320
|
-
const missing = Object.keys(needed).filter(
|
|
321
|
-
(kind) => !requirements[kind].covers(this.#minimum[kind], needed[kind]),
|
|
322
|
-
);
|
|
323
|
-
if (missing.length > 0) {
|
|
324
|
-
this.#problems.lines.add(
|
|
325
|
-
tr(
|
|
326
|
-
'{location}: {name} is an entry of the swiplet and needs {requirement}; raise "swiplet.minimumAPILevel" or "swiplet.minimumIOSVersion" in package.json',
|
|
327
|
-
{
|
|
328
|
-
location: this.#location(name),
|
|
329
|
-
name: name.text,
|
|
330
|
-
requirement: describe(needed, missing),
|
|
331
|
-
},
|
|
332
|
-
),
|
|
333
|
-
);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
#location(node) {
|
|
338
|
-
const source = sourceFileOf(node);
|
|
339
|
-
const { line, character } = source.getLineAndCharacterOfPosition(
|
|
340
|
-
node.getStart(source),
|
|
341
|
-
);
|
|
342
|
-
return `${path.relative(this.#root, source.fileName)}:${line + 1}:${character + 1}`;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
#parse(text, node) {
|
|
346
|
-
const { requirement, invalid, repeated } = parseAnnotation(text);
|
|
347
|
-
if (invalid !== undefined) {
|
|
348
|
-
this.#problems.lines.add(
|
|
349
|
-
tr(
|
|
350
|
-
'{location}: @available has "{entry}"; it takes "apiLevel <level>" and "iOS <version>", separated by commas',
|
|
351
|
-
{ location: this.#location(node), entry: invalid },
|
|
352
|
-
),
|
|
353
|
-
);
|
|
354
|
-
} else if (repeated !== undefined) {
|
|
355
|
-
this.#problems.lines.add(
|
|
356
|
-
tr("{location}: @available names {kind} twice", {
|
|
357
|
-
location: this.#location(node),
|
|
358
|
-
kind: repeated,
|
|
359
|
-
}),
|
|
360
|
-
);
|
|
361
|
-
}
|
|
362
|
-
return requirement;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
#target(symbol) {
|
|
366
|
-
return symbol.flags & SymbolFlags.Alias
|
|
367
|
-
? this.#checker.getAliasedSymbol(symbol)
|
|
368
|
-
: symbol;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
#symbolOf(node) {
|
|
372
|
-
if (!this.#symbols.has(node)) {
|
|
373
|
-
const symbol = this.#checker.getSymbolAtLocation(node);
|
|
374
|
-
this.#symbols.set(
|
|
375
|
-
node,
|
|
376
|
-
symbol === undefined ? undefined : this.#target(symbol),
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
return this.#symbols.get(node);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
#requirementOf(symbol) {
|
|
383
|
-
const target = this.#target(symbol);
|
|
384
|
-
if (!this.#requirements.has(target.id)) {
|
|
385
|
-
const requirement = {};
|
|
386
|
-
const declaration = (
|
|
387
|
-
target.valueDeclaration ?? target.declarations[0]
|
|
388
|
-
)?.resolve(this.#project);
|
|
389
|
-
const external =
|
|
390
|
-
declaration !== undefined &&
|
|
391
|
-
!this.#files.has(sourceFileOf(declaration).fileName);
|
|
392
|
-
for (const tag of this.#checker.getJsDocTagsOfSymbol(target)) {
|
|
393
|
-
if (tag.name === "available") {
|
|
394
|
-
merge(
|
|
395
|
-
requirement,
|
|
396
|
-
external
|
|
397
|
-
? this.#parse(tag.text ?? "", declaration)
|
|
398
|
-
: parseAnnotation(tag.text ?? "").requirement,
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
this.#requirements.set(target.id, requirement);
|
|
403
|
-
}
|
|
404
|
-
return this.#requirements.get(target.id);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
#annotationOf(node) {
|
|
408
|
-
if (!this.#annotations.has(node)) {
|
|
409
|
-
const requirement = {};
|
|
410
|
-
const symbol =
|
|
411
|
-
node.name?.kind === SyntaxKind.Identifier
|
|
412
|
-
? this.#checker.getSymbolAtLocation(node.name)
|
|
413
|
-
: undefined;
|
|
414
|
-
if (symbol !== undefined) {
|
|
415
|
-
merge(requirement, this.#requirementOf(symbol));
|
|
416
|
-
}
|
|
417
|
-
for (const tag of annotationTags(node)) {
|
|
418
|
-
merge(requirement, parseAnnotation(tagText(tag)).requirement);
|
|
419
|
-
}
|
|
420
|
-
this.#annotations.set(node, requirement);
|
|
421
|
-
}
|
|
422
|
-
return this.#annotations.get(node);
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
#propertySymbol(name) {
|
|
426
|
-
const member = name.parent;
|
|
427
|
-
const container = member.parent;
|
|
428
|
-
if (!this.#types.has(container)) {
|
|
429
|
-
this.#types.set(
|
|
430
|
-
container,
|
|
431
|
-
member.kind === SyntaxKind.BindingElement
|
|
432
|
-
? this.#checker.getTypeAtLocation(container)
|
|
433
|
-
: this.#checker.getContextualType(container),
|
|
434
|
-
);
|
|
435
|
-
}
|
|
436
|
-
const type = this.#types.get(container);
|
|
437
|
-
if (type === undefined) {
|
|
438
|
-
return undefined;
|
|
439
|
-
}
|
|
440
|
-
for (const candidate of [type, ...(type.getTypes?.() ?? [])]) {
|
|
441
|
-
const property = this.#checker.getPropertyOfType(candidate, name.text);
|
|
442
|
-
if (property !== undefined) {
|
|
443
|
-
return property;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
return undefined;
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
#covered(name, needed, reference) {
|
|
450
|
-
const { covers } = requirements[name];
|
|
451
|
-
if (covers(this.#minimum[name], needed)) {
|
|
452
|
-
return true;
|
|
453
|
-
}
|
|
454
|
-
let crossed = false;
|
|
455
|
-
for (
|
|
456
|
-
let child = reference, parent = reference.parent;
|
|
457
|
-
parent;
|
|
458
|
-
child = parent, parent = parent.parent
|
|
459
|
-
) {
|
|
460
|
-
const proven = this.#provenBy(parent, child, crossed)[name];
|
|
461
|
-
if (proven !== undefined && covers(proven, needed)) {
|
|
462
|
-
return true;
|
|
463
|
-
}
|
|
464
|
-
crossed ||= functions.has(parent.kind);
|
|
465
|
-
}
|
|
466
|
-
return false;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
#provenBy(parent, child, crossed) {
|
|
470
|
-
const proven = {};
|
|
471
|
-
switch (parent.kind) {
|
|
472
|
-
case SyntaxKind.IfStatement:
|
|
473
|
-
if (child === parent.thenStatement) {
|
|
474
|
-
merge(proven, this.#factsOf(parent.expression, true));
|
|
475
|
-
} else if (child === parent.elseStatement) {
|
|
476
|
-
merge(proven, this.#factsOf(parent.expression, false));
|
|
477
|
-
}
|
|
478
|
-
break;
|
|
479
|
-
case SyntaxKind.ConditionalExpression:
|
|
480
|
-
if (child === parent.whenTrue) {
|
|
481
|
-
merge(proven, this.#factsOf(parent.condition, true));
|
|
482
|
-
} else if (child === parent.whenFalse) {
|
|
483
|
-
merge(proven, this.#factsOf(parent.condition, false));
|
|
484
|
-
}
|
|
485
|
-
break;
|
|
486
|
-
case SyntaxKind.BinaryExpression:
|
|
487
|
-
if (child === parent.right) {
|
|
488
|
-
const operator = parent.operatorToken.kind;
|
|
489
|
-
if (operator === SyntaxKind.AmpersandAmpersandToken) {
|
|
490
|
-
merge(proven, this.#factsOf(parent.left, true));
|
|
491
|
-
} else if (operator === SyntaxKind.BarBarToken) {
|
|
492
|
-
merge(proven, this.#factsOf(parent.left, false));
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
break;
|
|
496
|
-
}
|
|
497
|
-
for (const statement of parent.statements ?? []) {
|
|
498
|
-
if (statement === child) {
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
if (
|
|
502
|
-
statement.kind === SyntaxKind.IfStatement &&
|
|
503
|
-
statement.elseStatement === undefined &&
|
|
504
|
-
exits(statement.thenStatement)
|
|
505
|
-
) {
|
|
506
|
-
merge(proven, this.#factsOf(statement.expression, false));
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
if (
|
|
510
|
-
functions.has(parent.kind) ||
|
|
511
|
-
(classes.has(parent.kind) &&
|
|
512
|
-
(crossed ||
|
|
513
|
-
(child.kind === SyntaxKind.PropertyDeclaration &&
|
|
514
|
-
!hasModifier(child, SyntaxKind.StaticKeyword)))) ||
|
|
515
|
-
(valueHolders.has(parent.kind) && crossed && child === parent.initializer)
|
|
516
|
-
) {
|
|
517
|
-
merge(proven, this.#annotationOf(parent));
|
|
518
|
-
}
|
|
519
|
-
return proven;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
#factsOf(node, outcome) {
|
|
523
|
-
const cache = this.#facts[Number(outcome)];
|
|
524
|
-
if (!cache.has(node)) {
|
|
525
|
-
cache.set(node, {});
|
|
526
|
-
cache.set(node, this.#computeFacts(node, outcome));
|
|
527
|
-
}
|
|
528
|
-
return cache.get(node);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
#computeFacts(node, outcome) {
|
|
532
|
-
switch (node.kind) {
|
|
533
|
-
case SyntaxKind.Identifier: {
|
|
534
|
-
const value = this.#constant(node);
|
|
535
|
-
return value === node ? {} : this.#factsOf(value, outcome);
|
|
536
|
-
}
|
|
537
|
-
case SyntaxKind.ParenthesizedExpression:
|
|
538
|
-
return this.#factsOf(node.expression, outcome);
|
|
539
|
-
case SyntaxKind.PrefixUnaryExpression:
|
|
540
|
-
return node.operator === SyntaxKind.ExclamationToken
|
|
541
|
-
? this.#factsOf(node.operand, !outcome)
|
|
542
|
-
: {};
|
|
543
|
-
case SyntaxKind.CallExpression:
|
|
544
|
-
return outcome && this.#isAvailable(node.expression)
|
|
545
|
-
? this.#requirementsOf(node.arguments[0])
|
|
546
|
-
: {};
|
|
547
|
-
case SyntaxKind.BinaryExpression: {
|
|
548
|
-
const operator = node.operatorToken.kind;
|
|
549
|
-
if (operator === SyntaxKind.AmpersandAmpersandToken) {
|
|
550
|
-
return outcome
|
|
551
|
-
? merge(
|
|
552
|
-
merge({}, this.#factsOf(node.left, true)),
|
|
553
|
-
this.#factsOf(node.right, true),
|
|
554
|
-
)
|
|
555
|
-
: {};
|
|
556
|
-
}
|
|
557
|
-
if (operator === SyntaxKind.BarBarToken) {
|
|
558
|
-
return outcome
|
|
559
|
-
? {}
|
|
560
|
-
: merge(
|
|
561
|
-
merge({}, this.#factsOf(node.left, false)),
|
|
562
|
-
this.#factsOf(node.right, false),
|
|
563
|
-
);
|
|
564
|
-
}
|
|
565
|
-
return {};
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
return {};
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
#constant(node) {
|
|
572
|
-
for (const seen = new Set(); node.kind === SyntaxKind.Identifier;) {
|
|
573
|
-
const declaration = this.#symbolOf(node)?.valueDeclaration?.resolve(
|
|
574
|
-
this.#project,
|
|
575
|
-
);
|
|
576
|
-
if (
|
|
577
|
-
declaration?.kind !== SyntaxKind.VariableDeclaration ||
|
|
578
|
-
declaration.initializer === undefined ||
|
|
579
|
-
!(declaration.parent.flags & NodeFlags.Const) ||
|
|
580
|
-
seen.has(declaration)
|
|
581
|
-
) {
|
|
582
|
-
break;
|
|
583
|
-
}
|
|
584
|
-
seen.add(declaration);
|
|
585
|
-
node = declaration.initializer;
|
|
586
|
-
}
|
|
587
|
-
return node;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
#isAvailable(node) {
|
|
591
|
-
const name =
|
|
592
|
-
node.kind === SyntaxKind.PropertyAccessExpression ? node.name : node;
|
|
593
|
-
return (
|
|
594
|
-
this.#available !== undefined &&
|
|
595
|
-
name.kind === SyntaxKind.Identifier &&
|
|
596
|
-
this.#symbolOf(name)?.id === this.#available
|
|
597
|
-
);
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
#requirementsOf(argument) {
|
|
601
|
-
const proven = {};
|
|
602
|
-
for (const property of argument?.kind === SyntaxKind.ObjectLiteralExpression
|
|
603
|
-
? argument.properties
|
|
604
|
-
: []) {
|
|
605
|
-
const value =
|
|
606
|
-
property.kind === SyntaxKind.PropertyAssignment &&
|
|
607
|
-
Object.hasOwn(requirements, property.name.text)
|
|
608
|
-
? this.#constant(property.initializer)
|
|
609
|
-
: undefined;
|
|
610
|
-
if (value?.kind === SyntaxKind.NumericLiteral) {
|
|
611
|
-
const parsed = requirements[property.name.text].parse(
|
|
612
|
-
String(Number(value.text)),
|
|
613
|
-
);
|
|
614
|
-
if (parsed !== undefined) {
|
|
615
|
-
proven[property.name.text] = parsed;
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
return proven;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
#availableSymbol(apiDirectory) {
|
|
623
|
-
const source = this.#project.program.getSourceFile(
|
|
624
|
-
fs.realpathSync(path.join(apiDirectory, "index.d.ts")),
|
|
625
|
-
);
|
|
626
|
-
const declaration = source?.statements.find(
|
|
627
|
-
(statement) =>
|
|
628
|
-
statement.kind === SyntaxKind.FunctionDeclaration &&
|
|
629
|
-
statement.name?.text === "available",
|
|
630
|
-
);
|
|
631
|
-
return declaration === undefined
|
|
632
|
-
? undefined
|
|
633
|
-
: this.#checker.getSymbolAtLocation(declaration.name)?.id;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
function sourceFileOf(node) {
|
|
638
|
-
while (node.kind !== SyntaxKind.SourceFile) {
|
|
639
|
-
node = node.parent;
|
|
640
|
-
}
|
|
641
|
-
return node;
|
|
642
|
-
}
|