reffy 6.2.0 → 6.2.1

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