Skip to content

Commit f197190

Browse files
authored
Merge pull request microsoft#625 from jwbay/api-descriptions-from-mdn
2 parents 1c0e7db + 2214698 commit f197190

11 files changed

+1043
-2
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ A "Living Standard" ([example](https://xhr.spec.whatwg.org/)) should be added he
7474

7575
- `browser.webidl.preprocessed.json`: a JSON file generated by Microsoft Edge. **Do not edit this file**.
7676
- Due to the different update schedules between Edge and TypeScript, this may not be the most up-to-date version of the spec.
77+
- `mdn/apiDescriptions.json`: a JSON file generated by fetching API descriptions from [MDN](https://developer.mozilla.org/en-US/docs/Web/API). **Do not edit this file**.
7778
- `addedTypes.json`: types that should exist in either browser or webworker but are missing from the Edge spec. The format of the file mimics that of `browser.webidl.preprocessed.json`.
7879
- `overridingTypes.json`: types that are defined in the spec file but has a better or more up-to-date definitions in the json files.
7980
- `removedTypes.json`: types that are defined in the spec file but should be removed.

baselines/dom.generated.d.ts

+422
Large diffs are not rendered by default.

baselines/webworker.generated.d.ts

+104
Large diffs are not rendered by default.

inputfiles/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Some files in this directory are generated.
44
Please do not edit them.
55
This specifically includes:
66

7-
* `browser.webidl.json`
7+
* `browser.webidl.preprocessed.json`
8+
* `idl/*`
9+
* `mdn/*`
810

911
Feel free to send a pull request with changes to any other files.

inputfiles/mdn/apiDescriptions.json

+442
Large diffs are not rendered by default.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"private": true,
44
"scripts": {
55
"build": "tsc --p ./tsconfig.json && node ./lib/index.js",
6-
"fetch": "tsc --p ./tsconfig.json && node ./lib/fetcher.js",
6+
"fetch-idl": "npm run build && node ./lib/idlfetcher.js",
7+
"fetch-mdn": "npm run build && node ./lib/mdnfetcher.js",
8+
"fetch": "echo This could take a few minutes... && npm run fetch-idl && npm run fetch-mdn",
79
"baseline-accept": "cpx \"generated\\*\" baselines\\",
810
"test": "tsc --p ./tsconfig.json && node ./lib/index.js && node ./lib/test.js"
911
},

src/emitter.ts

+4
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,10 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
831831
printer.printLineToStack(`interface ${processInterfaceType(i, i.name)} extends ${processedIName} {`);
832832
}
833833

834+
if (i.comment) {
835+
printer.printLine(`/** ${i.comment} */`);
836+
}
837+
834838
printer.print(`interface ${processInterfaceType(i, processedIName)}`);
835839

836840
const finalExtends = distinct([i.extends || "Object"].concat(i.implements || [])
File renamed without changes.

src/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function emitDom() {
4343
const overriddenItems = require(path.join(inputFolder, "overridingTypes.json"));
4444
const addedItems = require(path.join(inputFolder, "addedTypes.json"));
4545
const comments = require(path.join(inputFolder, "comments.json"));
46+
const documentationFromMDN = apiDescriptionsToIdl(require(path.join(inputFolder, 'mdn', 'apiDescriptions.json')));
4647
const removedItems = require(path.join(inputFolder, "removedTypes.json"));
4748
const idlSources: any[] = require(path.join(inputFolder, "idlSources.json"));
4849
const widlStandardTypes = idlSources.map(convertWidl);
@@ -60,6 +61,22 @@ function emitDom() {
6061
return result;
6162
}
6263

64+
function apiDescriptionsToIdl(descriptions: Record<string, string>) {
65+
const idl: Browser.WebIdl = {
66+
interfaces: {
67+
interface: {}
68+
}
69+
};
70+
71+
Object.keys(descriptions).forEach(name => {
72+
idl.interfaces!.interface[name] = {
73+
comment: descriptions[name],
74+
} as Browser.Interface;
75+
});
76+
77+
return idl;
78+
}
79+
6380
/// Load the input file
6481
let webidl: Browser.WebIdl = require(path.join(inputFolder, "browser.webidl.preprocessed.json"));
6582

@@ -106,6 +123,8 @@ function emitDom() {
106123
}
107124
}
108125
}
126+
127+
webidl = merge(webidl, documentationFromMDN);
109128
webidl = prune(webidl, removedItems);
110129
webidl = merge(webidl, addedItems);
111130
webidl = merge(webidl, overriddenItems);

src/mdnfetcher.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as fs from "fs";
2+
import fetch from "node-fetch";
3+
import { JSDOM } from "jsdom";
4+
5+
interface Page {
6+
summary: string;
7+
}
8+
9+
const knownBadDescriptions = [
10+
"CustomEvent", // too vague; references truncated data
11+
"IDBTransaction", // just a comment about Firefox 40
12+
"RTCDtmfSender", // is just 'tbd'
13+
"SVGMatrix", // too vague; references truncated data
14+
];
15+
16+
fetchInterfaceDescriptions();
17+
18+
async function fetchInterfaceDescriptions() {
19+
const webIdl = require("../inputfiles/browser.webidl.preprocessed.json");
20+
const interfaceNames = Object.keys(webIdl.interfaces.interface).sort();
21+
const descriptions: Record<string, string> = {};
22+
23+
await interfaceNames.reduce(async (previousRequest, name) => {
24+
// Issuing too many requests in parallel causes 504 gateway errors, so chain
25+
await previousRequest;
26+
27+
const response = await fetch(`https://developer.mozilla.org/en-US/docs/Web/API/${name}$json`);
28+
if (response.ok) {
29+
const page = await response.json();
30+
addDescription(name, page);
31+
} else if (response.status !== 404) {
32+
throw new Error(`Failed to fetch ${name}: ${response.statusText}`);
33+
}
34+
}, Promise.resolve());
35+
36+
function addDescription(name: string, page: Page) {
37+
if (page.summary && !knownBadDescriptions.includes(name)) {
38+
const fragment = JSDOM.fragment(page.summary);
39+
descriptions[name] = fragment.textContent!;
40+
}
41+
}
42+
43+
fs.writeFileSync("inputfiles/mdn/apiDescriptions.json", JSON.stringify(descriptions, null, 2));
44+
}

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export interface TypeParameter {
149149
export interface Interface {
150150
name: string;
151151
extends: string;
152+
comment?: string;
152153
constants?: {
153154
constant: Record<string, Constant>;
154155
}

0 commit comments

Comments
 (0)