reffy 20.0.13 → 20.0.14

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 (78) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +151 -151
  3. package/index.js +29 -29
  4. package/package.json +3 -3
  5. package/reffy.js +324 -324
  6. package/schemas/browserlib/extract-algorithms.json +52 -52
  7. package/schemas/browserlib/extract-cssdfn.json +108 -108
  8. package/schemas/browserlib/extract-dfns.json +90 -90
  9. package/schemas/browserlib/extract-elements.json +17 -17
  10. package/schemas/browserlib/extract-events.json +31 -31
  11. package/schemas/browserlib/extract-headings.json +19 -19
  12. package/schemas/browserlib/extract-ids.json +7 -7
  13. package/schemas/browserlib/extract-links.json +12 -12
  14. package/schemas/browserlib/extract-refs.json +12 -12
  15. package/schemas/common.json +876 -876
  16. package/schemas/files/extracts/algorithms.json +12 -12
  17. package/schemas/files/extracts/css.json +16 -16
  18. package/schemas/files/extracts/dfns.json +12 -12
  19. package/schemas/files/extracts/elements.json +12 -12
  20. package/schemas/files/extracts/events.json +12 -12
  21. package/schemas/files/extracts/headings.json +12 -12
  22. package/schemas/files/extracts/ids.json +12 -12
  23. package/schemas/files/extracts/links.json +12 -12
  24. package/schemas/files/extracts/refs.json +12 -12
  25. package/schemas/files/index.json +59 -59
  26. package/schemas/postprocessing/events.json +50 -50
  27. package/schemas/postprocessing/idlnames-parsed.json +27 -27
  28. package/schemas/postprocessing/idlnames.json +17 -17
  29. package/schemas/postprocessing/idlparsed.json +67 -67
  30. package/src/browserlib/clone-and-clean.mjs +24 -24
  31. package/src/browserlib/create-outline.mjs +353 -353
  32. package/src/browserlib/extract-algorithms.mjs +723 -723
  33. package/src/browserlib/extract-cddl.mjs +125 -125
  34. package/src/browserlib/extract-dfns.mjs +1093 -1093
  35. package/src/browserlib/extract-headings.mjs +76 -76
  36. package/src/browserlib/extract-ids.mjs +28 -28
  37. package/src/browserlib/extract-links.mjs +45 -45
  38. package/src/browserlib/extract-references.mjs +308 -308
  39. package/src/browserlib/extract-webidl.mjs +89 -89
  40. package/src/browserlib/get-absolute-url.mjs +29 -29
  41. package/src/browserlib/get-code-elements.mjs +20 -20
  42. package/src/browserlib/get-generator.mjs +26 -26
  43. package/src/browserlib/get-lastmodified-date.mjs +13 -13
  44. package/src/browserlib/get-revision.mjs +12 -12
  45. package/src/browserlib/get-title.mjs +14 -14
  46. package/src/browserlib/informative-selector.mjs +24 -24
  47. package/src/browserlib/map-ids-to-headings.mjs +173 -173
  48. package/src/browserlib/reffy.json +85 -85
  49. package/src/browserlib/trim-spaces.mjs +35 -35
  50. package/src/cli/check-missing-dfns.js +587 -587
  51. package/src/cli/merge-crawl-results.js +132 -132
  52. package/src/cli/parse-webidl.js +447 -447
  53. package/src/lib/css-grammar-parse-tree.schema.json +109 -109
  54. package/src/lib/css-grammar-parser.js +440 -440
  55. package/src/lib/fetch.js +51 -51
  56. package/src/lib/markdown-report.js +360 -360
  57. package/src/lib/mock-server.js +218 -218
  58. package/src/lib/post-processor.js +322 -322
  59. package/src/lib/throttled-queue.js +129 -129
  60. package/src/postprocessing/annotate-links.js +41 -41
  61. package/src/postprocessing/csscomplete.js +48 -48
  62. package/src/postprocessing/idlnames.js +391 -391
  63. package/src/postprocessing/idlparsed.js +179 -179
  64. package/src/postprocessing/patch-dfns.js +51 -51
  65. package/src/specs/missing-css-rules.json +197 -197
  66. package/src/specs/spec-equivalents.json +149 -149
  67. package/src/browserlib/extract-editors.mjs~ +0 -14
  68. package/src/browserlib/extract-events.mjs~ +0 -3
  69. package/src/browserlib/generate-es-dfn-report.sh~ +0 -4
  70. package/src/browserlib/get-revision.mjs~ +0 -7
  71. package/src/cli/csstree-grammar-check.js +0 -28
  72. package/src/cli/csstree-grammar-check.js~ +0 -10
  73. package/src/cli/csstree-grammar-parser.js +0 -11
  74. package/src/cli/csstree-grammar-parser.js~ +0 -1
  75. package/src/cli/extract-editors.js~ +0 -38
  76. package/src/cli/process-specs.js~ +0 -28
  77. package/src/postprocessing/annotate-links.js~ +0 -8
  78. package/src/postprocessing/events.js~ +0 -245
@@ -1,447 +1,447 @@
1
- #!/usr/bin/env node
2
- /**
3
- * The WebIDL parser takes the URL of a spec as input and outputs a JSON
4
- * structure that describes the WebIDL term definitions and references that the
5
- * spec contains.
6
- *
7
- * The WebIDL parser uses the [WebIDL extractor]{@link module:webidlExtractor}
8
- * to fetch and extract the WebIDL definitions contained in the given spec, and
9
- * analyzes that spec with [WebIDL2]{@link
10
- * https://github.com/darobin/webidl2.js}
11
- *
12
- * The WebIDL parser can be called directly through:
13
- *
14
- * `node parse-webidl.js [url]`
15
- *
16
- * where `url` is the URL of the spec to fetch and parse.
17
- *
18
- * @module webidlParser
19
- */
20
-
21
- import * as WebIDL2 from 'webidl2';
22
- import { fileURLToPath } from 'node:url';
23
- import process from 'node:process';
24
-
25
-
26
- /**
27
- * Update obsolete WebIDL constructs to make them compatible with latest
28
- * version of WebIDL
29
- */
30
- function normalizeWebIDL1to2(idl) {
31
- return idl
32
- // Use "FrozenArray" instead of "[]"
33
- .replace(/attribute +([^\[ ]*)\[\]/g, "attribute FrozenArray<$1>")
34
-
35
- // Use the default toJSON operation instead of serializers
36
- .replace(/serializer\s*=\s*{[^}]*}/g, "[Default] object toJSON()");
37
- }
38
-
39
-
40
- /**
41
- * Checks whether the given IDL uses WebIDL Level 1 constructs that are no
42
- * longer valid in WebIDL Level 2.
43
- *
44
- * Note a smarter method would typically return the list of obsolete constructs
45
- * instead of just a boolean flag. To be considered...
46
- *
47
- * @function
48
- * @public
49
- * @param {String} idl IDL content to check
50
- * @return {boolean} True when the IDL string contains obsolete constructs,
51
- * false otherwise.
52
- */
53
- function hasObsoleteIdl(idl) {
54
- return (idl !== normalizeWebIDL1to2(idl));
55
- }
56
-
57
-
58
- /**
59
- * Serialize an IDL AST node to a JSON object that contains both a textual
60
- * serialization of the IDL fragment and a tree serialization of the AST node
61
- *
62
- * @function
63
- * @private
64
- * @param {Object} def The parsed IDL node returned by the webidl2.js parser
65
- * @return {Object} An object with a "value" and "parsedValue" properties
66
- */
67
- function serialize(def) {
68
- return Object.assign(
69
- { fragment: WebIDL2.write([def]).trim() },
70
- JSON.parse(JSON.stringify(def)));
71
- }
72
-
73
-
74
- /**
75
- * Main method that takes IDL definitions and parses that IDL to compute
76
- * the list of internal/external dependencies.
77
- *
78
- * @function
79
- * @public
80
- * @param {String} idl IDL content, typically returned by the WebIDL extractor
81
- * @return {Promise} The promise to a parsed IDL structure that includes
82
- * information about dependencies, both at the interface level and at the
83
- * global level.
84
- */
85
- function parse(idl) {
86
- idl = normalizeWebIDL1to2(idl);
87
- return new Promise(function (resolve, reject) {
88
- var idlTree;
89
- var idlReport = {
90
- // List of names available to global interfaces, either as
91
- // objects that can be constructed or as objects that can be
92
- // returned from functions
93
- jsNames: {
94
- constructors: {},
95
- functions: {}
96
- },
97
-
98
- // List of IDL names defined in the IDL content, indexed by name
99
- idlNames: {},
100
-
101
- // List of partial IDL name definitions, indexed by name
102
- idlExtendedNames: {},
103
-
104
- // List of globals defined by the IDL content and the name of the
105
- // underlying interfaces (e.g. { "Worker":
106
- // ["DedicatedWorkerGlobalScope", "SharedWorkerGlobalScope"] })
107
- globals: {},
108
-
109
- // List of globals on which interfaces are defined, along with the
110
- // name of the underlying interfaces (e.g. { "Window":
111
- // ["ServiceWorker", "ServiceWorkerRegistration", ...]})
112
- exposed: {},
113
-
114
- // List of dependencies (both internal and external) per interface
115
- dependencies: {},
116
-
117
- // IDL names referenced by the IDL content but defined elsewhere
118
- externalDependencies: []
119
- };
120
- try {
121
- idlTree = WebIDL2.parse(idl);
122
- } catch (e) {
123
- return reject(e);
124
- }
125
- idlTree.forEach(parseIdlAstTree(idlReport));
126
- idlReport.externalDependencies = idlReport.externalDependencies
127
- .filter(n => !idlReport.idlNames[n]);
128
- resolve(idlReport);
129
- });
130
- }
131
-
132
-
133
- /**
134
- * Function that generates a parsing method that may be applied to nodes of
135
- * the IDL AST tree generated by the webidl2 and that completes the tree
136
- * structure with dependencies information.
137
- *
138
- * Note that the method recursively calls itself when it parses interfaces or
139
- * dictionaries.
140
- *
141
- * @function
142
- * @private
143
- * @param {Object} idlReport The IDL report to fill out, see structure
144
- * definition in parse function
145
- * @param {String} contextName The current interface context, used to compute
146
- * dependencies at the interface level
147
- * @return A function that can be applied to all nodes of an IDL AST tree and
148
- * that fills up the above sets.
149
- */
150
- function parseIdlAstTree(idlReport, contextName) {
151
- const { idlNames, idlExtendedNames, dependencies, externalDependencies } = idlReport;
152
-
153
- return function (def) {
154
- switch(def.type) {
155
- case "namespace":
156
- case "interface":
157
- case "interface mixin":
158
- case "dictionary":
159
- case "callback interface":
160
- parseInterfaceOrDictionary(def, idlReport);
161
- break;
162
- case "enum":
163
- idlNames[def.name] = serialize(def);
164
- break;
165
- case "operation":
166
- if (def.stringifier || (def.special && def.special === 'stringifier')) return;
167
- parseType(def.idlType, idlReport, contextName);
168
- def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
169
- break;
170
- case "attribute":
171
- case "field":
172
- parseType(def.idlType, idlReport, contextName);
173
- break;
174
- case 'constructor':
175
- def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
176
- break;
177
- case "includes":
178
- case "implements":
179
- parseType(def.target, idlReport);
180
- parseType(def[def.type], idlReport);
181
- if (!dependencies[def.target]) {
182
- dependencies[def.target] = [];
183
- }
184
- addDependency(def[def.type], {}, dependencies[def.target]);
185
- if (!idlExtendedNames[def.target]) {
186
- idlExtendedNames[def.target] = [];
187
- }
188
- const mixin = {name: def.target, type: "interface", includes: def.includes};
189
- idlExtendedNames[def.target].push(serialize(def));
190
- break;
191
- case "typedef":
192
- parseType(def.idlType, idlReport);
193
- idlNames[def.name] = serialize(def);
194
- break;
195
- case "callback":
196
- idlNames[def.name] = serialize(def);
197
- def.arguments.forEach(a => parseType(a.idlType, idlReport));
198
- break;
199
- case "iterable":
200
- case "async_iterable":
201
- case "setlike":
202
- case "maplike":
203
- var type = def.idlType;
204
- if (!Array.isArray(type)) {
205
- type = [def.idlType];
206
- }
207
- type.forEach(a => parseType(a, idlReport, contextName));
208
- break;
209
- case "serializer":
210
- case "stringifier":
211
- case "const":
212
- case "eof":
213
- break;
214
- default:
215
- throw new Error("Unhandled IDL type: " + def.type + " in " +JSON.stringify(def));
216
- }
217
- };
218
- }
219
-
220
-
221
- /**
222
- * Parse an IDL AST node that defines an interface or dictionary, and compute
223
- * dependencies information.
224
- *
225
- * @function
226
- * @private
227
- * @param {Object} def The IDL AST node to parse
228
- * @see parseIdlAstTree for other parameters
229
- * @return {void} The function updates the contents of its parameters and does
230
- * not return anything
231
- */
232
- function parseInterfaceOrDictionary(def, idlReport) {
233
- const { idlNames, idlExtendedNames, globals, exposed, jsNames, dependencies, externalDependencies } = idlReport;
234
-
235
- if (!dependencies[def.name]) {
236
- dependencies[def.name] = [];
237
- }
238
- if (def.partial) {
239
- if (!idlExtendedNames[def.name]) {
240
- idlExtendedNames[def.name] = [];
241
- }
242
- idlExtendedNames[def.name].push(serialize(def));
243
- addDependency(def.name, idlNames, externalDependencies);
244
- }
245
- else {
246
- idlNames[def.name] = serialize(def);
247
- }
248
- if (def.inheritance) {
249
- addDependency(def.inheritance, idlNames, externalDependencies);
250
- addDependency(def.inheritance, {}, dependencies[def.name]);
251
- }
252
-
253
- const globalEA = def.extAttrs.find(ea => ea.name === "Global");
254
- if (globalEA && globalEA.rhs) {
255
- const globalNames = (globalEA.rhs.type === "identifier") ?
256
- [globalEA.rhs.value] : globalEA.rhs.value.map(c => c.value);
257
- globalNames.forEach(name => {
258
- if (!globals[name]) {
259
- globals[name] = [];
260
- }
261
- globals[name].push(def.name);
262
- });
263
- }
264
-
265
- const exposedEA = def.extAttrs.find(ea => ea.name === "Exposed");
266
- if (exposedEA && exposedEA.rhs) {
267
- let exposedNames = [];
268
- if (exposedEA.rhs.type === "*") {
269
- exposedNames.push("*");
270
- } else if (exposedEA.rhs.type === "identifier") {
271
- exposedNames.push(exposedEA.rhs.value);
272
- } else {
273
- exposedNames = exposedEA.rhs.value.map(c => c.value);
274
- }
275
- exposedNames.forEach(name => {
276
- if (!exposed[name]) {
277
- exposed[name] = [];
278
- }
279
- exposed[name].push(def.name);
280
- });
281
- }
282
- if (def.extAttrs.some(ea => ea.name === "Constructor")) {
283
- addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
284
- def.extAttrs.filter(ea => ea.name === "Constructor").forEach(function(constructor) {
285
- if (constructor.arguments) {
286
- constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
287
- }
288
- });
289
- } else if (def.extAttrs.some(ea => ea.name === "NamedConstructor")) {
290
- def.extAttrs.filter(ea => ea.name === "NamedConstructor").forEach(function(constructor) {
291
- idlNames[constructor.rhs.value] = constructor;
292
- addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
293
- if (constructor.arguments) {
294
- constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
295
- }
296
- });
297
- } else if (def.members.find(member => member.type === 'constructor')) {
298
- addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
299
- } else if (def.type === "interface") {
300
- addToJSContext(def.extAttrs, jsNames, def.name, "functions");
301
- }
302
- def.members.forEach(parseIdlAstTree(idlReport, def.name));
303
- }
304
-
305
-
306
- /**
307
- * Add the given IDL name to the set of objects that are exposed to JS, for the
308
- * right contexts.
309
- *
310
- * @function
311
- * @private
312
- * @param {Object} eas The extended attributes that may qualify that IDL name
313
- * @param {Object} jsNames See parseIdlAstTree params
314
- * @param {String} name The IDL name to add to the jsNames set
315
- * @param {String} type The type of exposure (constructor, function, object)
316
- * @return {void} The function updates jsNames
317
- */
318
- function addToJSContext(eas, jsNames, name, type) {
319
- var contexts = [];
320
- var exposed = eas && eas.some(ea => ea.name === "Exposed");
321
- if (exposed) {
322
- var exposedEa = eas.find(ea => ea.name === "Exposed");
323
- if (exposedEa.rhs.type === "*") {
324
- contexts = ["*"];
325
- } else if (exposedEa.rhs.type === "identifier") {
326
- contexts = [exposedEa.rhs.value];
327
- } else {
328
- contexts = exposedEa.rhs.value.map(c => c.value);
329
- }
330
- }
331
- contexts.forEach(c => { if (!jsNames[type][c]) jsNames[type][c] = []; jsNames[type][c].push(name)});
332
- }
333
-
334
-
335
- /**
336
- * Parse the given IDL type and update external dependencies accordingly
337
- *
338
- * @function
339
- * @private
340
- * @param {Object} idltype The IDL AST node that defines/references the type
341
- * @see parseIdlAstTree for other parameters
342
- * @return {void} The function updates externalDependencies
343
- */
344
- function parseType(idltype, idlReport, contextName) {
345
- // For some reasons, webidl2 sometimes returns the name of the IDL type
346
- // instead of an IDL construct for array constructs. For example:
347
- // Constructor(DOMString[] urls) interface toto;
348
- // ... will create an array IDL node that directly point to "DOMString" and
349
- // not to a node that describes the "DOMString" type.
350
- if (isString(idltype)) {
351
- idltype = { idlType: 'DOMString' };
352
- }
353
- if (idltype.union || (idltype.generic && Array.isArray(idltype.idlType))) {
354
- idltype.idlType.forEach(t => parseType(t, idlReport, contextName));
355
- return;
356
- }
357
- if (idltype.sequence || idltype.array || idltype.generic) {
358
- parseType(idltype.idlType, idlReport, contextName);
359
- return;
360
- }
361
- var wellKnownTypes = [
362
- "undefined", "any",
363
- "boolean",
364
- "byte", "octet",
365
- "short", "unsigned short",
366
- "long", "unsigned long", "long long", "unsigned long long",
367
- "float", "unrestricted float", "double", "unrestricted double",
368
- "bigint",
369
- "DOMString", "ByteString", "USVString",
370
- "object"
371
- ];
372
- if (wellKnownTypes.indexOf(idltype.idlType) === -1) {
373
- addDependency(idltype.idlType, idlReport.idlNames, idlReport.externalDependencies);
374
- if (contextName) {
375
- addDependency(idltype.idlType, {}, idlReport.dependencies[contextName]);
376
- }
377
- }
378
- }
379
-
380
-
381
- /**
382
- * Returns true if given object is a String
383
- *
384
- * @function
385
- * @private
386
- * @param {any} obj The object to test
387
- * @return {bool} true is object is a String, false otherwise
388
- */
389
- function isString(obj) {
390
- return Object.prototype.toString.call(obj) === '[object String]';
391
- }
392
-
393
-
394
- /**
395
- * Add the given name to the list of external dependencies, unless it already
396
- * appears in the set of IDL names defined by the IDL content
397
- *
398
- * @function
399
- * @private
400
- * @param {String} name The IDL name to consider as a potential external dependency
401
- * @param {Array(String)} idlNames The set of IDL names set by the IDL content
402
- * @param {Array(String)} externalDependencies The set of external dependencies
403
- * @return {void} The function updates externalDependencies as needed
404
- */
405
- function addDependency(name, idlNames, externalDependencies) {
406
- if ((Object.keys(idlNames).indexOf(name) === -1) &&
407
- (externalDependencies.indexOf(name) === -1)) {
408
- externalDependencies.push(name);
409
- }
410
- }
411
-
412
-
413
- /**************************************************
414
- Export the Web IDL parser
415
- **************************************************/
416
- const webidlParser = {
417
- parse,
418
- hasObsoleteIdl
419
- };
420
- export {
421
- webidlParser as default,
422
- parse,
423
- hasObsoleteIdl
424
- };
425
-
426
-
427
- /**************************************************
428
- Code run if the code is run as a stand-alone module
429
- **************************************************/
430
- if (process.argv[1] === fileURLToPath(import.meta.url)) {
431
- const fs = await import('node:fs');
432
- const idlFile = process.argv[2];
433
- if (!idlFile) {
434
- console.error("No IDL file to parse");
435
- process.exit(2);
436
- }
437
-
438
- const idl = fs.readFileSync(idlFile, "utf8");
439
- parse(idl)
440
- .then(function (data) {
441
- console.log(JSON.stringify(data, null, 2));
442
- })
443
- .catch(function (err) {
444
- console.error(err, err.stack);
445
- process.exit(64);
446
- });
447
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The WebIDL parser takes the URL of a spec as input and outputs a JSON
4
+ * structure that describes the WebIDL term definitions and references that the
5
+ * spec contains.
6
+ *
7
+ * The WebIDL parser uses the [WebIDL extractor]{@link module:webidlExtractor}
8
+ * to fetch and extract the WebIDL definitions contained in the given spec, and
9
+ * analyzes that spec with [WebIDL2]{@link
10
+ * https://github.com/darobin/webidl2.js}
11
+ *
12
+ * The WebIDL parser can be called directly through:
13
+ *
14
+ * `node parse-webidl.js [url]`
15
+ *
16
+ * where `url` is the URL of the spec to fetch and parse.
17
+ *
18
+ * @module webidlParser
19
+ */
20
+
21
+ import * as WebIDL2 from 'webidl2';
22
+ import { fileURLToPath } from 'node:url';
23
+ import process from 'node:process';
24
+
25
+
26
+ /**
27
+ * Update obsolete WebIDL constructs to make them compatible with latest
28
+ * version of WebIDL
29
+ */
30
+ function normalizeWebIDL1to2(idl) {
31
+ return idl
32
+ // Use "FrozenArray" instead of "[]"
33
+ .replace(/attribute +([^\[ ]*)\[\]/g, "attribute FrozenArray<$1>")
34
+
35
+ // Use the default toJSON operation instead of serializers
36
+ .replace(/serializer\s*=\s*{[^}]*}/g, "[Default] object toJSON()");
37
+ }
38
+
39
+
40
+ /**
41
+ * Checks whether the given IDL uses WebIDL Level 1 constructs that are no
42
+ * longer valid in WebIDL Level 2.
43
+ *
44
+ * Note a smarter method would typically return the list of obsolete constructs
45
+ * instead of just a boolean flag. To be considered...
46
+ *
47
+ * @function
48
+ * @public
49
+ * @param {String} idl IDL content to check
50
+ * @return {boolean} True when the IDL string contains obsolete constructs,
51
+ * false otherwise.
52
+ */
53
+ function hasObsoleteIdl(idl) {
54
+ return (idl !== normalizeWebIDL1to2(idl));
55
+ }
56
+
57
+
58
+ /**
59
+ * Serialize an IDL AST node to a JSON object that contains both a textual
60
+ * serialization of the IDL fragment and a tree serialization of the AST node
61
+ *
62
+ * @function
63
+ * @private
64
+ * @param {Object} def The parsed IDL node returned by the webidl2.js parser
65
+ * @return {Object} An object with a "value" and "parsedValue" properties
66
+ */
67
+ function serialize(def) {
68
+ return Object.assign(
69
+ { fragment: WebIDL2.write([def]).trim() },
70
+ JSON.parse(JSON.stringify(def)));
71
+ }
72
+
73
+
74
+ /**
75
+ * Main method that takes IDL definitions and parses that IDL to compute
76
+ * the list of internal/external dependencies.
77
+ *
78
+ * @function
79
+ * @public
80
+ * @param {String} idl IDL content, typically returned by the WebIDL extractor
81
+ * @return {Promise} The promise to a parsed IDL structure that includes
82
+ * information about dependencies, both at the interface level and at the
83
+ * global level.
84
+ */
85
+ function parse(idl) {
86
+ idl = normalizeWebIDL1to2(idl);
87
+ return new Promise(function (resolve, reject) {
88
+ var idlTree;
89
+ var idlReport = {
90
+ // List of names available to global interfaces, either as
91
+ // objects that can be constructed or as objects that can be
92
+ // returned from functions
93
+ jsNames: {
94
+ constructors: {},
95
+ functions: {}
96
+ },
97
+
98
+ // List of IDL names defined in the IDL content, indexed by name
99
+ idlNames: {},
100
+
101
+ // List of partial IDL name definitions, indexed by name
102
+ idlExtendedNames: {},
103
+
104
+ // List of globals defined by the IDL content and the name of the
105
+ // underlying interfaces (e.g. { "Worker":
106
+ // ["DedicatedWorkerGlobalScope", "SharedWorkerGlobalScope"] })
107
+ globals: {},
108
+
109
+ // List of globals on which interfaces are defined, along with the
110
+ // name of the underlying interfaces (e.g. { "Window":
111
+ // ["ServiceWorker", "ServiceWorkerRegistration", ...]})
112
+ exposed: {},
113
+
114
+ // List of dependencies (both internal and external) per interface
115
+ dependencies: {},
116
+
117
+ // IDL names referenced by the IDL content but defined elsewhere
118
+ externalDependencies: []
119
+ };
120
+ try {
121
+ idlTree = WebIDL2.parse(idl);
122
+ } catch (e) {
123
+ return reject(e);
124
+ }
125
+ idlTree.forEach(parseIdlAstTree(idlReport));
126
+ idlReport.externalDependencies = idlReport.externalDependencies
127
+ .filter(n => !idlReport.idlNames[n]);
128
+ resolve(idlReport);
129
+ });
130
+ }
131
+
132
+
133
+ /**
134
+ * Function that generates a parsing method that may be applied to nodes of
135
+ * the IDL AST tree generated by the webidl2 and that completes the tree
136
+ * structure with dependencies information.
137
+ *
138
+ * Note that the method recursively calls itself when it parses interfaces or
139
+ * dictionaries.
140
+ *
141
+ * @function
142
+ * @private
143
+ * @param {Object} idlReport The IDL report to fill out, see structure
144
+ * definition in parse function
145
+ * @param {String} contextName The current interface context, used to compute
146
+ * dependencies at the interface level
147
+ * @return A function that can be applied to all nodes of an IDL AST tree and
148
+ * that fills up the above sets.
149
+ */
150
+ function parseIdlAstTree(idlReport, contextName) {
151
+ const { idlNames, idlExtendedNames, dependencies, externalDependencies } = idlReport;
152
+
153
+ return function (def) {
154
+ switch(def.type) {
155
+ case "namespace":
156
+ case "interface":
157
+ case "interface mixin":
158
+ case "dictionary":
159
+ case "callback interface":
160
+ parseInterfaceOrDictionary(def, idlReport);
161
+ break;
162
+ case "enum":
163
+ idlNames[def.name] = serialize(def);
164
+ break;
165
+ case "operation":
166
+ if (def.stringifier || (def.special && def.special === 'stringifier')) return;
167
+ parseType(def.idlType, idlReport, contextName);
168
+ def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
169
+ break;
170
+ case "attribute":
171
+ case "field":
172
+ parseType(def.idlType, idlReport, contextName);
173
+ break;
174
+ case 'constructor':
175
+ def.arguments.forEach(a => parseType(a.idlType, idlReport, contextName));
176
+ break;
177
+ case "includes":
178
+ case "implements":
179
+ parseType(def.target, idlReport);
180
+ parseType(def[def.type], idlReport);
181
+ if (!dependencies[def.target]) {
182
+ dependencies[def.target] = [];
183
+ }
184
+ addDependency(def[def.type], {}, dependencies[def.target]);
185
+ if (!idlExtendedNames[def.target]) {
186
+ idlExtendedNames[def.target] = [];
187
+ }
188
+ const mixin = {name: def.target, type: "interface", includes: def.includes};
189
+ idlExtendedNames[def.target].push(serialize(def));
190
+ break;
191
+ case "typedef":
192
+ parseType(def.idlType, idlReport);
193
+ idlNames[def.name] = serialize(def);
194
+ break;
195
+ case "callback":
196
+ idlNames[def.name] = serialize(def);
197
+ def.arguments.forEach(a => parseType(a.idlType, idlReport));
198
+ break;
199
+ case "iterable":
200
+ case "async_iterable":
201
+ case "setlike":
202
+ case "maplike":
203
+ var type = def.idlType;
204
+ if (!Array.isArray(type)) {
205
+ type = [def.idlType];
206
+ }
207
+ type.forEach(a => parseType(a, idlReport, contextName));
208
+ break;
209
+ case "serializer":
210
+ case "stringifier":
211
+ case "const":
212
+ case "eof":
213
+ break;
214
+ default:
215
+ throw new Error("Unhandled IDL type: " + def.type + " in " +JSON.stringify(def));
216
+ }
217
+ };
218
+ }
219
+
220
+
221
+ /**
222
+ * Parse an IDL AST node that defines an interface or dictionary, and compute
223
+ * dependencies information.
224
+ *
225
+ * @function
226
+ * @private
227
+ * @param {Object} def The IDL AST node to parse
228
+ * @see parseIdlAstTree for other parameters
229
+ * @return {void} The function updates the contents of its parameters and does
230
+ * not return anything
231
+ */
232
+ function parseInterfaceOrDictionary(def, idlReport) {
233
+ const { idlNames, idlExtendedNames, globals, exposed, jsNames, dependencies, externalDependencies } = idlReport;
234
+
235
+ if (!dependencies[def.name]) {
236
+ dependencies[def.name] = [];
237
+ }
238
+ if (def.partial) {
239
+ if (!idlExtendedNames[def.name]) {
240
+ idlExtendedNames[def.name] = [];
241
+ }
242
+ idlExtendedNames[def.name].push(serialize(def));
243
+ addDependency(def.name, idlNames, externalDependencies);
244
+ }
245
+ else {
246
+ idlNames[def.name] = serialize(def);
247
+ }
248
+ if (def.inheritance) {
249
+ addDependency(def.inheritance, idlNames, externalDependencies);
250
+ addDependency(def.inheritance, {}, dependencies[def.name]);
251
+ }
252
+
253
+ const globalEA = def.extAttrs.find(ea => ea.name === "Global");
254
+ if (globalEA && globalEA.rhs) {
255
+ const globalNames = (globalEA.rhs.type === "identifier") ?
256
+ [globalEA.rhs.value] : globalEA.rhs.value.map(c => c.value);
257
+ globalNames.forEach(name => {
258
+ if (!globals[name]) {
259
+ globals[name] = [];
260
+ }
261
+ globals[name].push(def.name);
262
+ });
263
+ }
264
+
265
+ const exposedEA = def.extAttrs.find(ea => ea.name === "Exposed");
266
+ if (exposedEA && exposedEA.rhs) {
267
+ let exposedNames = [];
268
+ if (exposedEA.rhs.type === "*") {
269
+ exposedNames.push("*");
270
+ } else if (exposedEA.rhs.type === "identifier") {
271
+ exposedNames.push(exposedEA.rhs.value);
272
+ } else {
273
+ exposedNames = exposedEA.rhs.value.map(c => c.value);
274
+ }
275
+ exposedNames.forEach(name => {
276
+ if (!exposed[name]) {
277
+ exposed[name] = [];
278
+ }
279
+ exposed[name].push(def.name);
280
+ });
281
+ }
282
+ if (def.extAttrs.some(ea => ea.name === "Constructor")) {
283
+ addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
284
+ def.extAttrs.filter(ea => ea.name === "Constructor").forEach(function(constructor) {
285
+ if (constructor.arguments) {
286
+ constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
287
+ }
288
+ });
289
+ } else if (def.extAttrs.some(ea => ea.name === "NamedConstructor")) {
290
+ def.extAttrs.filter(ea => ea.name === "NamedConstructor").forEach(function(constructor) {
291
+ idlNames[constructor.rhs.value] = constructor;
292
+ addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
293
+ if (constructor.arguments) {
294
+ constructor.arguments.forEach(a => parseType(a.idlType, idlReport, def.name));
295
+ }
296
+ });
297
+ } else if (def.members.find(member => member.type === 'constructor')) {
298
+ addToJSContext(def.extAttrs, jsNames, def.name, "constructors");
299
+ } else if (def.type === "interface") {
300
+ addToJSContext(def.extAttrs, jsNames, def.name, "functions");
301
+ }
302
+ def.members.forEach(parseIdlAstTree(idlReport, def.name));
303
+ }
304
+
305
+
306
+ /**
307
+ * Add the given IDL name to the set of objects that are exposed to JS, for the
308
+ * right contexts.
309
+ *
310
+ * @function
311
+ * @private
312
+ * @param {Object} eas The extended attributes that may qualify that IDL name
313
+ * @param {Object} jsNames See parseIdlAstTree params
314
+ * @param {String} name The IDL name to add to the jsNames set
315
+ * @param {String} type The type of exposure (constructor, function, object)
316
+ * @return {void} The function updates jsNames
317
+ */
318
+ function addToJSContext(eas, jsNames, name, type) {
319
+ var contexts = [];
320
+ var exposed = eas && eas.some(ea => ea.name === "Exposed");
321
+ if (exposed) {
322
+ var exposedEa = eas.find(ea => ea.name === "Exposed");
323
+ if (exposedEa.rhs.type === "*") {
324
+ contexts = ["*"];
325
+ } else if (exposedEa.rhs.type === "identifier") {
326
+ contexts = [exposedEa.rhs.value];
327
+ } else {
328
+ contexts = exposedEa.rhs.value.map(c => c.value);
329
+ }
330
+ }
331
+ contexts.forEach(c => { if (!jsNames[type][c]) jsNames[type][c] = []; jsNames[type][c].push(name)});
332
+ }
333
+
334
+
335
+ /**
336
+ * Parse the given IDL type and update external dependencies accordingly
337
+ *
338
+ * @function
339
+ * @private
340
+ * @param {Object} idltype The IDL AST node that defines/references the type
341
+ * @see parseIdlAstTree for other parameters
342
+ * @return {void} The function updates externalDependencies
343
+ */
344
+ function parseType(idltype, idlReport, contextName) {
345
+ // For some reasons, webidl2 sometimes returns the name of the IDL type
346
+ // instead of an IDL construct for array constructs. For example:
347
+ // Constructor(DOMString[] urls) interface toto;
348
+ // ... will create an array IDL node that directly point to "DOMString" and
349
+ // not to a node that describes the "DOMString" type.
350
+ if (isString(idltype)) {
351
+ idltype = { idlType: 'DOMString' };
352
+ }
353
+ if (idltype.union || (idltype.generic && Array.isArray(idltype.idlType))) {
354
+ idltype.idlType.forEach(t => parseType(t, idlReport, contextName));
355
+ return;
356
+ }
357
+ if (idltype.sequence || idltype.array || idltype.generic) {
358
+ parseType(idltype.idlType, idlReport, contextName);
359
+ return;
360
+ }
361
+ var wellKnownTypes = [
362
+ "undefined", "any",
363
+ "boolean",
364
+ "byte", "octet",
365
+ "short", "unsigned short",
366
+ "long", "unsigned long", "long long", "unsigned long long",
367
+ "float", "unrestricted float", "double", "unrestricted double",
368
+ "bigint",
369
+ "DOMString", "ByteString", "USVString",
370
+ "object"
371
+ ];
372
+ if (wellKnownTypes.indexOf(idltype.idlType) === -1) {
373
+ addDependency(idltype.idlType, idlReport.idlNames, idlReport.externalDependencies);
374
+ if (contextName) {
375
+ addDependency(idltype.idlType, {}, idlReport.dependencies[contextName]);
376
+ }
377
+ }
378
+ }
379
+
380
+
381
+ /**
382
+ * Returns true if given object is a String
383
+ *
384
+ * @function
385
+ * @private
386
+ * @param {any} obj The object to test
387
+ * @return {bool} true is object is a String, false otherwise
388
+ */
389
+ function isString(obj) {
390
+ return Object.prototype.toString.call(obj) === '[object String]';
391
+ }
392
+
393
+
394
+ /**
395
+ * Add the given name to the list of external dependencies, unless it already
396
+ * appears in the set of IDL names defined by the IDL content
397
+ *
398
+ * @function
399
+ * @private
400
+ * @param {String} name The IDL name to consider as a potential external dependency
401
+ * @param {Array(String)} idlNames The set of IDL names set by the IDL content
402
+ * @param {Array(String)} externalDependencies The set of external dependencies
403
+ * @return {void} The function updates externalDependencies as needed
404
+ */
405
+ function addDependency(name, idlNames, externalDependencies) {
406
+ if ((Object.keys(idlNames).indexOf(name) === -1) &&
407
+ (externalDependencies.indexOf(name) === -1)) {
408
+ externalDependencies.push(name);
409
+ }
410
+ }
411
+
412
+
413
+ /**************************************************
414
+ Export the Web IDL parser
415
+ **************************************************/
416
+ const webidlParser = {
417
+ parse,
418
+ hasObsoleteIdl
419
+ };
420
+ export {
421
+ webidlParser as default,
422
+ parse,
423
+ hasObsoleteIdl
424
+ };
425
+
426
+
427
+ /**************************************************
428
+ Code run if the code is run as a stand-alone module
429
+ **************************************************/
430
+ if (process.argv[1] === fileURLToPath(import.meta.url)) {
431
+ const fs = await import('node:fs');
432
+ const idlFile = process.argv[2];
433
+ if (!idlFile) {
434
+ console.error("No IDL file to parse");
435
+ process.exit(2);
436
+ }
437
+
438
+ const idl = fs.readFileSync(idlFile, "utf8");
439
+ parse(idl)
440
+ .then(function (data) {
441
+ console.log(JSON.stringify(data, null, 2));
442
+ })
443
+ .catch(function (err) {
444
+ console.error(err, err.stack);
445
+ process.exit(64);
446
+ });
447
+ }