@vestra-player/sdk 1.8.0 → 1.9.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.
Files changed (86) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +2 -2
  3. package/bin/vestra-plugin.mjs +11 -100
  4. package/capability-scan.json +3 -1
  5. package/dist/api-requests.d.ts +11 -36
  6. package/dist/api-requests.js +18 -47
  7. package/dist/capability-scan.d.ts +1 -1
  8. package/dist/capability-scan.js +8 -9
  9. package/dist/docs-tree.json.gz +0 -0
  10. package/dist/docs.d.ts +1 -1
  11. package/dist/docs.js +1 -1
  12. package/dist/index.d.ts +4 -5
  13. package/dist/manifest.d.ts +5 -0
  14. package/dist/manifest.js +9 -24
  15. package/dist/member-uses.d.ts +61 -0
  16. package/dist/member-uses.js +160 -0
  17. package/dist/player-runtime.d.ts +1 -2
  18. package/dist/player-runtime.js +1 -2
  19. package/dist/plugin/appearance.d.ts +1 -1
  20. package/dist/plugin/appearance.js +2 -2
  21. package/dist/plugin/bindings.d.ts +1 -1
  22. package/dist/plugin/bindings.js +2 -2
  23. package/dist/plugin/commands.js +1 -12
  24. package/dist/plugin/connection.d.ts +26 -7
  25. package/dist/plugin/connection.js +68 -36
  26. package/dist/plugin/drops.js +1 -1
  27. package/dist/plugin/index.js +9 -15
  28. package/dist/plugin/media.js +0 -12
  29. package/dist/plugin/mpv.js +0 -7
  30. package/dist/plugin/packages.js +1 -2
  31. package/dist/plugin/player/events.d.ts +4 -3
  32. package/dist/plugin/player/events.js +7 -6
  33. package/dist/plugin/player.d.ts +21 -13
  34. package/dist/plugin/player.js +56 -21
  35. package/dist/plugin/services.d.ts +4 -4
  36. package/dist/plugin/services.js +7 -29
  37. package/dist/plugin/settings.js +1 -1
  38. package/dist/plugin/shaders.js +0 -2
  39. package/dist/plugin/stream.d.ts +2 -1
  40. package/dist/plugin/stream.js +2 -2
  41. package/dist/plugin/testing/commands.d.ts +3 -10
  42. package/dist/plugin/testing/commands.js +6 -15
  43. package/dist/plugin/testing/index.d.ts +26 -116
  44. package/dist/plugin/testing/index.js +30 -104
  45. package/dist/plugin/testing/processing.js +1 -1
  46. package/dist/plugin/testing/services.js +0 -1
  47. package/dist/plugin/timeline.d.ts +2 -3
  48. package/dist/plugin/timeline.js +4 -8
  49. package/dist/plugin/types/base.d.ts +40 -86
  50. package/dist/plugin/types/commands.d.ts +10 -67
  51. package/dist/plugin/types/packages.d.ts +17 -55
  52. package/dist/plugin/types/playback.d.ts +126 -144
  53. package/dist/plugin/types/plugin-wire.d.ts +6 -36
  54. package/dist/plugin/types/processing.d.ts +10 -49
  55. package/dist/plugin/types/services.d.ts +11 -70
  56. package/dist/plugin/types/snapshots.d.ts +7 -60
  57. package/dist/plugin/types/sources.d.ts +2 -11
  58. package/dist/plugin/types/subtitles.d.ts +5 -26
  59. package/dist/plugin/types/ui-content.d.ts +1 -16
  60. package/dist/plugin/types/ui-layout.d.ts +53 -79
  61. package/dist/plugin/types/ui-locale.d.ts +5 -23
  62. package/dist/plugin/types/ui-shell.d.ts +13 -59
  63. package/dist/plugin/view/layout.js +1 -1
  64. package/dist/plugin/view.js +0 -2
  65. package/dist/plugin/window.d.ts +1 -1
  66. package/dist/plugin/window.js +1 -1
  67. package/dist/plugin/wire-facade.js +1 -2
  68. package/dist/processing.d.ts +2 -3
  69. package/dist/protocol.d.ts +14 -0
  70. package/dist/protocol.js +14 -0
  71. package/dist/request-queue.js +1 -1
  72. package/dist/subtitles.d.ts +1 -1
  73. package/dist/ts-plugin.d.ts +8 -15
  74. package/dist/ts-plugin.js +3 -141
  75. package/dist/wire.d.ts +7 -12
  76. package/dist/wire.js +6 -8
  77. package/llms.txt +4 -4
  78. package/package.json +1 -2
  79. package/dist/capabilities.d.ts +0 -107
  80. package/dist/capabilities.js +0 -69
  81. package/dist/capability-check.d.ts +0 -176
  82. package/dist/capability-check.js +0 -461
  83. package/dist/capability-rules.d.ts +0 -125
  84. package/dist/capability-rules.js +0 -192
  85. package/dist/host-capabilities.d.ts +0 -10
  86. package/dist/host-capabilities.js +0 -492
@@ -1,176 +0,0 @@
1
- /**
2
- * The capability checker: every use of a plugin API member whose capability
3
- * the plugin's `manifest.json` does not declare. `vestra-plugin check` (and
4
- * so `build` and `pack`) and the TypeScript language-service plugin
5
- * (`@vestra-player/sdk/ts-plugin`) both run it; the rule is
6
- * `capability-rules.ts` (the player's table and the SDK's requests).
7
- *
8
- * It is type-aware: a use is a property access, an element access with a
9
- * literal name or a destructured property whose symbol is the SDK's
10
- * declaration of a member in the table, however the object was reached
11
- * (`plugin.player.seek`, `const { player } = plugin; player.seek`, a
12
- * parameter typed `Player`, a `Panel` from `panels.mount`). Not detected:
13
- * access with a computed name (`player[name]`), values typed `any` or
14
- * `unknown`, plain JavaScript without types, members inherited from `View`
15
- * (their object comes from a call that is checked) and options that need
16
- * more (a `processed` capture, a file resolver).
17
- *
18
- * Members whose capability depends on an argument (`plugin.player.on` and
19
- * `plugin.wire.on` by event name, `plugin.wire.request` by method name,
20
- * `ARGUMENT_APIS`) are checked by that argument when its type fixes it to one
21
- * string (a literal, a constant); otherwise (a computed name, a union of
22
- * several, the member taken by destructuring or as a value) the call is
23
- * reported as unchecked ({@link checkCapabilities}), and the player still
24
- * checks it.
25
- *
26
- * The TypeScript module is passed in, never imported: the CLI uses the
27
- * SDK's own compiler, the editor its TypeScript server's.
28
- *
29
- * @internal
30
- */
31
- import type TS from "typescript";
32
- import type { CapabilityRules } from "./capability-rules.ts";
33
- /** @internal The module plugins import the API types from. */
34
- export declare const SDK_MODULE = "@vestra-player/sdk";
35
- /** @internal The diagnostic code the editor shows (and its quick fix keys on). */
36
- export declare const CAPABILITY_DIAGNOSTIC = 95301;
37
- /**
38
- * @internal The warning code the editor shows for a call whose capability
39
- * the checker cannot read (`plugin.wire.request` with a computed method).
40
- */
41
- export declare const CAPABILITY_UNCHECKED = 95302;
42
- /** @internal One use of a member whose capability is not declared. */
43
- export interface CapabilityFinding {
44
- /** The source file, as the program names it. */
45
- readonly file: string;
46
- /** 1-based line and column of the member's name. */
47
- readonly line: number;
48
- readonly column: number;
49
- /** Offset and length of the member's name in the file. */
50
- readonly start: number;
51
- readonly length: number;
52
- /** The member, `Interface.member` (`Player.seek`). */
53
- readonly api: string;
54
- /** The capability to declare. */
55
- readonly capability: string;
56
- /** The request that needs it. */
57
- readonly method: string;
58
- /** The declared-or-needed capability whose `requires` names it. */
59
- readonly requiredBy?: string;
60
- /** One line for people. */
61
- readonly message: string;
62
- }
63
- /** @internal */
64
- export interface CheckOptions {
65
- /** The files to check (others in the program are only read for types). */
66
- readonly files: readonly string[];
67
- /** The manifest's `capabilities`. */
68
- readonly capabilities: readonly string[];
69
- /** Default: the SDK's rules. */
70
- readonly rules?: CapabilityRules;
71
- }
72
- /**
73
- * @internal A call whose capability the checker cannot tell: a member of
74
- * `ARGUMENT_APIS` (`plugin.wire.request`, `player.on`, `wire.on`) whose name
75
- * argument is not a string the types fix, or a method the player's table does
76
- * not know. The player still checks it at run time.
77
- */
78
- export interface UncheckedCall {
79
- readonly file: string;
80
- /** 1-based line and column of the member's name. */
81
- readonly line: number;
82
- readonly column: number;
83
- /** Offset and length of the member's name in the file. */
84
- readonly start: number;
85
- readonly length: number;
86
- /** The member, `Interface.member` (`Wire.request`). */
87
- readonly api: string;
88
- /** One line for people. */
89
- readonly message: string;
90
- }
91
- /**
92
- * Every use, in `options.files`, of a member whose capability the manifest
93
- * does not declare, in file and position order.
94
- *
95
- * @internal
96
- */
97
- export declare function findUndeclaredCapabilities(ts: typeof TS, program: TS.Program, options: CheckOptions): CapabilityFinding[];
98
- /**
99
- * The capability check: the uses of members whose capability the manifest does
100
- * not declare (`findings`, as {@link findUndeclaredCapabilities}), and the
101
- * calls whose capability depends on a name the checker cannot read
102
- * (`unchecked`).
103
- *
104
- * A call of `plugin.player.on` or `plugin.wire.on` is checked by its event
105
- * name, one of `plugin.wire.request` by its method name and the string
106
- * fields of its parameters (`{ event: "…" }`), when the types fix them.
107
- *
108
- * @internal
109
- */
110
- export declare function checkCapabilities(ts: typeof TS, program: TS.Program, options: CheckOptions): {
111
- findings: CapabilityFinding[];
112
- unchecked: UncheckedCall[];
113
- };
114
- /**
115
- * Whether a file belongs to the plugin's production code, given the
116
- * directory of its `manifest.json`: not in `tests/`, `node_modules/`,
117
- * `dist/`, `vendor/` or a dot directory (what `vestra-plugin build` leaves
118
- * out), and not a declaration file.
119
- *
120
- * @internal
121
- */
122
- export declare function isPluginSource(directory: string, file: string): boolean;
123
- /**
124
- * The edit that adds a capability to a manifest's `capabilities` array, in
125
- * the array's own layout; `undefined` when the text has no such array.
126
- *
127
- * @internal
128
- */
129
- export declare function addCapabilityEdit(ts: typeof TS, text: string, capability: string): {
130
- start: number;
131
- length: number;
132
- newText: string;
133
- } | undefined;
134
- /** @internal One use of an SDK member, where {@link findMemberUses} found it. */
135
- export interface MemberUse {
136
- readonly file: string;
137
- /** 1-based line and column of the member's name. */
138
- readonly line: number;
139
- readonly column: number;
140
- /** The member, as it was asked for (`Clipboard.readText`). */
141
- readonly api: string;
142
- }
143
- /**
144
- * @internal A request sent by name (`plugin.wire.request("clipboard.readText")`):
145
- * the method when the types fix it to one string, else `undefined` (a
146
- * computed name, a union of several, the member taken as a value).
147
- */
148
- export interface RequestUse {
149
- readonly file: string;
150
- readonly line: number;
151
- readonly column: number;
152
- readonly method: string | undefined;
153
- }
154
- /**
155
- * Every use, in `files`, of the SDK members named by `apis`
156
- * (`Interface.member`), and every request sent by name
157
- * (`ARGUMENT_APIS`' `method` members: `Wire.request`), in file and position
158
- * order. The same type-aware resolution as {@link checkCapabilities}, for
159
- * any list of members: a use is a property access, an element access with
160
- * a literal name or a destructured property whose symbol is the SDK's
161
- * declaration of the member. It reads no manifest and no capability rule
162
- * (the capability scan, `capability-scan.ts`, gives it its own lists).
163
- *
164
- * `resolved` is `false` when the program has no import of
165
- * `@vestra-player/sdk` that resolves: nothing can be found by type then.
166
- *
167
- * @internal
168
- */
169
- export declare function findMemberUses(ts: typeof TS, program: TS.Program, options: {
170
- readonly files: readonly string[];
171
- readonly apis: Iterable<string>;
172
- }): {
173
- resolved: boolean;
174
- uses: MemberUse[];
175
- requests: RequestUse[];
176
- };
@@ -1,461 +0,0 @@
1
- import { ARGUMENT_APIS } from "./api-requests.js";
2
- import { argumentRequests, memberNeeds, requestsNeed, RULES, } from "./capability-rules.js";
3
- /** @internal The module plugins import the API types from. */
4
- export const SDK_MODULE = "@vestra-player/sdk";
5
- /** @internal The diagnostic code the editor shows (and its quick fix keys on). */
6
- export const CAPABILITY_DIAGNOSTIC = 95_301;
7
- /**
8
- * @internal The warning code the editor shows for a call whose capability
9
- * the checker cannot read (`plugin.wire.request` with a computed method).
10
- */
11
- export const CAPABILITY_UNCHECKED = 95_302;
12
- const memberCache = new WeakMap();
13
- const argumentCache = new WeakMap();
14
- /**
15
- * The module `@vestra-player/sdk` as the program resolved it: from the
16
- * first import or export of it in the program's own (non-declaration) files.
17
- */
18
- function sdkModule(ts, program, checker) {
19
- for (const file of program.getSourceFiles()) {
20
- if (file.isDeclarationFile || program.isSourceFileFromExternalLibrary(file))
21
- continue;
22
- for (const statement of file.statements) {
23
- const specifier = ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)
24
- ? statement.moduleSpecifier
25
- : undefined;
26
- if (specifier &&
27
- ts.isStringLiteral(specifier) &&
28
- specifier.text === SDK_MODULE) {
29
- const module = checker.getSymbolAtLocation(specifier);
30
- if (module)
31
- return module;
32
- }
33
- }
34
- }
35
- return undefined;
36
- }
37
- /** The SDK's declarations of the members named (`Interface.member`). */
38
- function resolveMembers(ts, program, apis) {
39
- const members = new Map();
40
- const checker = program.getTypeChecker();
41
- const module = sdkModule(ts, program, checker);
42
- if (!module)
43
- return members;
44
- const exports = new Map();
45
- for (const symbol of checker.getExportsOfModule(module))
46
- exports.set(symbol.name, symbol.flags & ts.SymbolFlags.Alias
47
- ? checker.getAliasedSymbol(symbol)
48
- : symbol);
49
- for (const api of apis) {
50
- const [head, ...path] = api.split(".");
51
- const root = exports.get(head);
52
- if (!root || !path.length)
53
- continue;
54
- let type = checker.getDeclaredTypeOfSymbol(root);
55
- let property;
56
- for (const name of path) {
57
- property = type?.getProperty(name);
58
- type = property && checker.getTypeOfSymbol(property);
59
- }
60
- for (const declaration of property?.declarations ?? [])
61
- if (!members.has(declaration))
62
- members.set(declaration, api);
63
- }
64
- return members;
65
- }
66
- /** The SDK's declarations of the members in the table. */
67
- function sdkMembers(ts, program, rules) {
68
- const cached = memberCache.get(program);
69
- if (cached && rules === RULES)
70
- return cached;
71
- const members = resolveMembers(ts, program, Object.keys(rules.apis));
72
- if (rules === RULES)
73
- memberCache.set(program, members);
74
- return members;
75
- }
76
- /** The SDK's declarations of the members of `ARGUMENT_APIS`. */
77
- function argumentMembers(ts, program) {
78
- let members = argumentCache.get(program);
79
- if (!members) {
80
- members = resolveMembers(ts, program, Object.keys(ARGUMENT_APIS));
81
- argumentCache.set(program, members);
82
- }
83
- return members;
84
- }
85
- /**
86
- * The strings an expression's type fixes: a string literal, a constant of
87
- * one, or a union of them (the caller takes one only); `undefined` when
88
- * the type allows any other.
89
- */
90
- function literalStrings(ts, checker, node) {
91
- if (!node)
92
- return undefined;
93
- if (ts.isStringLiteralLike(node))
94
- return [node.text];
95
- const type = checker.getTypeAtLocation(node);
96
- const parts = type.isUnion() ? type.types : [type];
97
- const values = [];
98
- for (const part of parts) {
99
- if (!part.isStringLiteral())
100
- return undefined;
101
- values.push(part.value);
102
- }
103
- return values.length ? values : undefined;
104
- }
105
- /**
106
- * The string fields of a request's parameters that the player's rules may
107
- * read (`event`, `stage`): those of an object literal whose value is one
108
- * string the types fix.
109
- */
110
- function stringFields(ts, checker, node) {
111
- if (!node || !ts.isObjectLiteralExpression(node))
112
- return undefined;
113
- const fields = {};
114
- for (const property of node.properties) {
115
- if (!ts.isPropertyAssignment(property))
116
- continue;
117
- const name = property.name;
118
- const key = ts.isIdentifier(name) || ts.isStringLiteralLike(name)
119
- ? name.text
120
- : undefined;
121
- const values = literalStrings(ts, checker, property.initializer);
122
- if (key !== undefined && values?.length === 1)
123
- fields[key] = values[0];
124
- }
125
- return Object.keys(fields).length ? fields : undefined;
126
- }
127
- /**
128
- * Every use, in `options.files`, of a member whose capability the manifest
129
- * does not declare, in file and position order.
130
- *
131
- * @internal
132
- */
133
- export function findUndeclaredCapabilities(ts, program, options) {
134
- return checkCapabilities(ts, program, options).findings;
135
- }
136
- /**
137
- * The capability check: the uses of members whose capability the manifest does
138
- * not declare (`findings`, as {@link findUndeclaredCapabilities}), and the
139
- * calls whose capability depends on a name the checker cannot read
140
- * (`unchecked`).
141
- *
142
- * A call of `plugin.player.on` or `plugin.wire.on` is checked by its event
143
- * name, one of `plugin.wire.request` by its method name and the string
144
- * fields of its parameters (`{ event: "…" }`), when the types fix them.
145
- *
146
- * @internal
147
- */
148
- export function checkCapabilities(ts, program, options) {
149
- const rules = options.rules ?? RULES;
150
- const findings = [];
151
- const unchecked = [];
152
- const files = options.files
153
- .map((file) => program.getSourceFile(file))
154
- .filter((file) => !!file);
155
- if (!files.length)
156
- return { findings, unchecked };
157
- const members = sdkMembers(ts, program, rules);
158
- const calls = argumentMembers(ts, program);
159
- if (!members.size && !calls.size)
160
- return { findings, unchecked };
161
- const checker = program.getTypeChecker();
162
- const apiOf = (table, symbol) => {
163
- for (const declaration of symbol?.declarations ?? []) {
164
- const api = table.get(declaration);
165
- if (api)
166
- return api;
167
- }
168
- return undefined;
169
- };
170
- for (const file of files) {
171
- const position = (name) => {
172
- const start = name.getStart(file);
173
- const { line, character } = file.getLineAndCharacterOfPosition(start);
174
- return {
175
- file: file.fileName,
176
- line: line + 1,
177
- column: character + 1,
178
- start,
179
- length: name.getEnd() - start,
180
- };
181
- };
182
- const missing = (name, api, needs, shown = api) => {
183
- for (const need of needs) {
184
- if (options.capabilities.includes(need.capability))
185
- continue;
186
- findings.push({
187
- ...position(name),
188
- api,
189
- capability: need.capability,
190
- method: need.method,
191
- ...(need.requiredBy ? { requiredBy: need.requiredBy } : {}),
192
- message: need.requiredBy
193
- ? `${shown} needs the capability "${need.capability}" (required by "${need.requiredBy}"), which manifest.json does not declare`
194
- : `${shown} needs the capability "${need.capability}", which manifest.json does not declare`,
195
- });
196
- }
197
- };
198
- const report = (name, symbol) => {
199
- const api = apiOf(members, symbol);
200
- if (!api)
201
- return;
202
- missing(name, api, memberNeeds(api, options.capabilities, rules) ?? []);
203
- };
204
- /** A call of a member of `ARGUMENT_APIS`, by its name argument. */
205
- const call = (node) => {
206
- const callee = node.expression;
207
- const name = ts.isPropertyAccessExpression(callee)
208
- ? callee.name
209
- : ts.isElementAccessExpression(callee) &&
210
- ts.isStringLiteralLike(callee.argumentExpression)
211
- ? callee.argumentExpression
212
- : undefined;
213
- if (!name)
214
- return;
215
- const api = apiOf(calls, checker.getSymbolAtLocation(name));
216
- if (!api)
217
- return;
218
- const kind = ARGUMENT_APIS[api];
219
- const values = literalStrings(ts, checker, node.arguments[0]);
220
- // One name, or not checked: a union could be any of its names at run
221
- // time, and none of them is known to be used.
222
- if (values?.length !== 1) {
223
- unchecked.push({
224
- ...position(name),
225
- api,
226
- message: `${api} with ${kind === "event" ? "an event" : "a method"} name that is not one literal: its capability is not checked here (the player checks it)`,
227
- });
228
- return;
229
- }
230
- const params = kind === "method"
231
- ? stringFields(ts, checker, node.arguments[1])
232
- : undefined;
233
- for (const value of values) {
234
- const requests = argumentRequests(kind, value, params, rules);
235
- const shown = `${api}(${JSON.stringify(value)})`;
236
- if (!requests) {
237
- unchecked.push({
238
- ...position(name),
239
- api,
240
- message: `${shown}: the player's capability table has no such method; not checked`,
241
- });
242
- continue;
243
- }
244
- missing(name, api, requestsNeed(requests, options.capabilities, rules), shown);
245
- }
246
- };
247
- /**
248
- * A member of `ARGUMENT_APIS` taken other than by a direct call (`const
249
- * { request } = plugin.wire`, `const on = plugin.player.on`): its calls
250
- * cannot be followed, so it is unchecked where it is taken.
251
- */
252
- const taken = (name, symbol) => {
253
- const api = apiOf(calls, symbol);
254
- if (!api)
255
- return;
256
- const access = name.parent;
257
- const parent = access?.parent;
258
- if (access &&
259
- (ts.isPropertyAccessExpression(access) ||
260
- ts.isElementAccessExpression(access)) &&
261
- parent &&
262
- ts.isCallExpression(parent) &&
263
- parent.expression === access)
264
- return; // a direct call: `call` checks it
265
- unchecked.push({
266
- ...position(name),
267
- api,
268
- message: `${api} taken other than by a direct call: the calls of it are not checked here (the player checks them)`,
269
- });
270
- };
271
- const visit = (node) => {
272
- if (ts.isCallExpression(node))
273
- call(node);
274
- if (ts.isPropertyAccessExpression(node)) {
275
- const symbol = checker.getSymbolAtLocation(node.name);
276
- report(node.name, symbol);
277
- taken(node.name, symbol);
278
- }
279
- else if (ts.isElementAccessExpression(node) &&
280
- ts.isStringLiteralLike(node.argumentExpression)) {
281
- const symbol = checker.getSymbolAtLocation(node.argumentExpression);
282
- report(node.argumentExpression, symbol);
283
- taken(node.argumentExpression, symbol);
284
- }
285
- else if (ts.isBindingElement(node) &&
286
- ts.isObjectBindingPattern(node.parent)) {
287
- const name = node.propertyName ?? node.name;
288
- const text = ts.isIdentifier(name) || ts.isStringLiteralLike(name)
289
- ? name.text
290
- : undefined;
291
- if (text !== undefined) {
292
- const symbol = checker
293
- .getTypeAtLocation(node.parent)
294
- .getProperty(text);
295
- report(name, symbol);
296
- taken(name, symbol);
297
- }
298
- }
299
- ts.forEachChild(node, visit);
300
- };
301
- visit(file);
302
- }
303
- return { findings, unchecked };
304
- }
305
- /**
306
- * Whether a file belongs to the plugin's production code, given the
307
- * directory of its `manifest.json`: not in `tests/`, `node_modules/`,
308
- * `dist/`, `vendor/` or a dot directory (what `vestra-plugin build` leaves
309
- * out), and not a declaration file.
310
- *
311
- * @internal
312
- */
313
- export function isPluginSource(directory, file) {
314
- const normal = (path) => path.replaceAll("\\", "/").replace(/\/$/, "");
315
- const base = normal(directory);
316
- const path = normal(file);
317
- const caseless = /^[a-z]:\//i.test(path);
318
- const same = (a, b) => caseless ? a.toLowerCase() === b.toLowerCase() : a === b;
319
- if (!same(path.slice(0, base.length + 1), `${base}/`))
320
- return false;
321
- if (/\.d\.[mc]?ts$/.test(path) || !/\.[mc]?tsx?$/.test(path))
322
- return false;
323
- const parts = path
324
- .slice(base.length + 1)
325
- .split("/")
326
- .slice(0, -1);
327
- return !parts.some((part) => part.startsWith(".") ||
328
- ["tests", "node_modules", "dist", "vendor"].includes(part));
329
- }
330
- /**
331
- * The edit that adds a capability to a manifest's `capabilities` array, in
332
- * the array's own layout; `undefined` when the text has no such array.
333
- *
334
- * @internal
335
- */
336
- export function addCapabilityEdit(ts, text, capability) {
337
- const json = ts.parseJsonText("manifest.json", text);
338
- const root = json.statements[0]?.expression;
339
- if (!root || !ts.isObjectLiteralExpression(root))
340
- return undefined;
341
- const property = root.properties.find((each) => ts.isPropertyAssignment(each) &&
342
- ts.isStringLiteral(each.name) &&
343
- each.name.text === "capabilities");
344
- const array = property?.initializer;
345
- if (!array || !ts.isArrayLiteralExpression(array))
346
- return undefined;
347
- const quoted = JSON.stringify(capability);
348
- const last = array.elements.at(-1);
349
- if (!last) {
350
- const start = array.getStart(json);
351
- return {
352
- start,
353
- length: array.getEnd() - start,
354
- newText: `[${quoted}]`,
355
- };
356
- }
357
- const first = array.elements[0];
358
- const lineStart = text.lastIndexOf("\n", first.getStart(json)) + 1;
359
- const multiline = text
360
- .slice(array.getStart(json), first.getStart(json))
361
- .includes("\n");
362
- const indent = text.slice(lineStart, first.getStart(json));
363
- return {
364
- start: last.getEnd(),
365
- length: 0,
366
- // The manifest's own line endings (CRLF on many Windows checkouts).
367
- newText: multiline
368
- ? `,${text.includes("\r\n") ? "\r\n" : "\n"}${indent}${quoted}`
369
- : `, ${quoted}`,
370
- };
371
- }
372
- /**
373
- * Every use, in `files`, of the SDK members named by `apis`
374
- * (`Interface.member`), and every request sent by name
375
- * (`ARGUMENT_APIS`' `method` members: `Wire.request`), in file and position
376
- * order. The same type-aware resolution as {@link checkCapabilities}, for
377
- * any list of members: a use is a property access, an element access with
378
- * a literal name or a destructured property whose symbol is the SDK's
379
- * declaration of the member. It reads no manifest and no capability rule
380
- * (the capability scan, `capability-scan.ts`, gives it its own lists).
381
- *
382
- * `resolved` is `false` when the program has no import of
383
- * `@vestra-player/sdk` that resolves: nothing can be found by type then.
384
- *
385
- * @internal
386
- */
387
- export function findMemberUses(ts, program, options) {
388
- const uses = [];
389
- const requests = [];
390
- const members = resolveMembers(ts, program, options.apis);
391
- const senders = resolveMembers(ts, program, Object.entries(ARGUMENT_APIS)
392
- .filter(([, kind]) => kind === "method")
393
- .map(([api]) => api));
394
- const resolved = members.size > 0 || senders.size > 0;
395
- if (!resolved)
396
- return { resolved, uses, requests };
397
- const checker = program.getTypeChecker();
398
- const apiOf = (table, symbol) => {
399
- for (const declaration of symbol?.declarations ?? []) {
400
- const api = table.get(declaration);
401
- if (api)
402
- return api;
403
- }
404
- return undefined;
405
- };
406
- for (const fileName of options.files) {
407
- const file = program.getSourceFile(fileName);
408
- if (!file)
409
- continue;
410
- const position = (name) => {
411
- const { line, character } = file.getLineAndCharacterOfPosition(name.getStart(file));
412
- return { file: file.fileName, line: line + 1, column: character + 1 };
413
- };
414
- const seen = (name, symbol) => {
415
- const api = apiOf(members, symbol);
416
- if (api)
417
- uses.push({ ...position(name), api });
418
- if (!apiOf(senders, symbol))
419
- return;
420
- // A direct call with one literal name is a request of that name;
421
- // anything else (a computed name, the member taken as a value) is a
422
- // request the scan cannot name.
423
- const access = name.parent;
424
- const call = access?.parent;
425
- const direct = access &&
426
- (ts.isPropertyAccessExpression(access) ||
427
- ts.isElementAccessExpression(access)) &&
428
- call &&
429
- ts.isCallExpression(call) &&
430
- call.expression === access
431
- ? call
432
- : undefined;
433
- const values = direct
434
- ? literalStrings(ts, checker, direct.arguments[0])
435
- : undefined;
436
- requests.push({
437
- ...position(name),
438
- method: values?.length === 1 ? values[0] : undefined,
439
- });
440
- };
441
- const visit = (node) => {
442
- if (ts.isPropertyAccessExpression(node))
443
- seen(node.name, checker.getSymbolAtLocation(node.name));
444
- else if (ts.isElementAccessExpression(node) &&
445
- ts.isStringLiteralLike(node.argumentExpression))
446
- seen(node.argumentExpression, checker.getSymbolAtLocation(node.argumentExpression));
447
- else if (ts.isBindingElement(node) &&
448
- ts.isObjectBindingPattern(node.parent)) {
449
- const name = node.propertyName ?? node.name;
450
- const text = ts.isIdentifier(name) || ts.isStringLiteralLike(name)
451
- ? name.text
452
- : undefined;
453
- if (text !== undefined)
454
- seen(name, checker.getTypeAtLocation(node.parent).getProperty(text));
455
- }
456
- ts.forEachChild(node, visit);
457
- };
458
- visit(file);
459
- }
460
- return { resolved, uses, requests };
461
- }