Skip to content

Commit f3d137b

Browse files
author
qucchia
committed
Allow TYPESCRIPT comments to include additional metadata
1 parent 4268e69 commit f3d137b

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

scripts/build_types.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,18 @@ function getAll(objects) {
282282

283283
/**
284284
* Return the declarations of custom types.
285-
* @param {string[]} types - The list of types defined in comments.
285+
* @param {object[]} types - The list of types defined in comments.
286+
* Of the form { declaration: string, implementation: string }.
286287
* @returns {string} The joined declarations.
287288
*/
288289
function getTypeDeclarations(types) {
289290
return (
290291
"// TYPES\n" +
291292
types
292-
.map((type) => type.replace(/\\\//g, "/").replace(/\\\\/g, "\\"))
293+
.filter((type) => !type.class)
294+
.map((type) =>
295+
type.declaration.replace(/\\\//g, "/").replace(/\\\\/g, "\\")
296+
)
293297
.join("")
294298
);
295299
}
@@ -299,9 +303,10 @@ function getTypeDeclarations(types) {
299303
* vanilla JavaScript, e.g. String, Array.
300304
* @param {string} name - The class's name.
301305
* @param {object} c - The class's data.
306+
* @param {object[]} types
302307
* @returns {string} The class's declaration.
303308
*/
304-
function getBuiltinClassDeclaration(name, c) {
309+
function getBuiltinClassDeclaration(name, c, types) {
305310
return (
306311
`interface ${name}Constructor {\n` +
307312
indent(
@@ -329,6 +334,7 @@ function getBuiltinClassDeclaration(name, c) {
329334
)}`.trim()
330335
)
331336
.concat(name === "Array" ? ["[index: number]: T"] : [])
337+
.concat(types.map((type) => type.declaration))
332338
.join("\n\n")
333339
) +
334340
`\n}\n\n${getDocumentation(c.object)}`) +
@@ -340,9 +346,10 @@ function getBuiltinClassDeclaration(name, c) {
340346
* Get the declaration of a class that is not builtin.
341347
* @param {string} name - The class's name.
342348
* @param {object} c - The class's data.
349+
* @param {object[]} types
343350
* @returns {string} The class's declaration.
344351
*/
345-
function getOtherClassDeclaration(name, c) {
352+
function getOtherClassDeclaration(name, c, types) {
346353
return (
347354
`${getDocumentation(c.object)}\ndeclare class ${
348355
c.object?.typescript || name
@@ -367,6 +374,7 @@ function getOtherClassDeclaration(name, c) {
367374
)}`.trim()
368375
)
369376
.concat(name === "ArrayBufferView" ? ["[index: number]: number"] : [])
377+
.concat(types.map((type) => type.declaration))
370378
.join("\n\n")
371379
) +
372380
"\n}"
@@ -376,17 +384,26 @@ function getOtherClassDeclaration(name, c) {
376384
/**
377385
* Return the class declarations (not including libraries).
378386
* @param {object} classes - The object of classes (see `getClasses`).
387+
* @param {object[]} types
379388
* @returns {string} The class declarations.
380389
*/
381-
function getClassDeclarations(classes) {
390+
function getClassDeclarations(classes, types) {
382391
return (
383392
"\n\n// CLASSES\n\n" +
384393
Object.entries(classes)
385394
.filter(([_, c]) => !c.library)
386395
.map(([name, c]) =>
387396
name in global
388-
? getBuiltinClassDeclaration(name, c)
389-
: getOtherClassDeclaration(name, c)
397+
? getBuiltinClassDeclaration(
398+
name,
399+
c,
400+
types.filter((type) => type.class === name)
401+
)
402+
: getOtherClassDeclaration(
403+
name,
404+
c,
405+
types.filter((type) => type.class === name)
406+
)
390407
)
391408
.join("\n\n")
392409
);
@@ -456,13 +473,13 @@ function getLibraryDeclarations(classes) {
456473
function buildTypes() {
457474
return new Promise((resolve) => {
458475
require("./common.js").readAllWrapperFiles(function (objects, types) {
459-
const [classes, globals] = getAll(objects);
476+
const [classes, globals] = getAll(objects, types);
460477

461478
resolve(
462479
"// NOTE: This file has been automatically generated.\n\n" +
463480
'/// <reference path="other.d.ts" />\n\n' +
464481
getTypeDeclarations(types) +
465-
getClassDeclarations(classes) +
482+
getClassDeclarations(classes, types) +
466483
getGlobalDeclarations(globals) +
467484
getLibraryDeclarations(classes)
468485
);

scripts/common.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,17 @@ exports.readWrapperFile = function(filename) {
9494
var comments = contents.match( /\/\*TYPESCRIPT(?:(?!\*\/).|[\n\r])*\*\//g );
9595
if (comments) comments.forEach(function(comment) {
9696
comment = comment.slice(12,-2);
97-
types.push(comment);
97+
var j = {};
98+
var declaration = comment;
99+
if (comment[0] === "{") {
100+
var endOfJson = comment.indexOf("\n}")+2;
101+
var json = comment.substr(0,endOfJson);
102+
j = new Builtin(JSON.parse(json));
103+
declaration = comment.substr(endOfJson).trim();
104+
}
105+
j.declaration = declaration;
106+
j.implementation = filename;
107+
types.push(j);
98108
});
99109
return [builtins, types];
100110
}

0 commit comments

Comments
 (0)