Skip to content

Commit bb9f1fc

Browse files
authored
Merge pull request #801 from r-tock/master
Add support to generate Typescript types that work against JSON objects
2 parents 60fabe6 + 6504a50 commit bb9f1fc

19 files changed

+2903
-2883
lines changed

cli/lib/tsd-jsdoc/publish.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,35 @@ function handleMember(element, parent) {
574574
begin(element);
575575

576576
if (element.isEnum) {
577-
578-
writeln("enum ", element.name, " {");
579-
++indent;
580-
element.properties.forEach(function(property, i) {
581-
write(property.name);
582-
if (property.defaultvalue !== undefined)
583-
write(" = ", JSON.stringify(property.defaultvalue));
584-
if (i < element.properties.length - 1)
585-
writeln(",");
586-
else
587-
writeln();
577+
var stringEnum = false;
578+
element.properties.forEach(function(property) {
579+
if (isNaN(property.defaultvalue)) {
580+
stringEnum = true;
581+
}
588582
});
589-
--indent;
590-
writeln("}");
583+
if (stringEnum) {
584+
writeln("type ", element.name, " =");
585+
++indent;
586+
element.properties.forEach(function(property, i) {
587+
write(i === 0 ? "" : "| ", JSON.stringify(property.defaultvalue));
588+
});
589+
--indent;
590+
writeln(";");
591+
} else {
592+
writeln("enum ", element.name, " {");
593+
++indent;
594+
element.properties.forEach(function(property, i) {
595+
write(property.name);
596+
if (property.defaultvalue !== undefined)
597+
write(" = ", JSON.stringify(property.defaultvalue));
598+
if (i < element.properties.length - 1)
599+
writeln(",");
600+
else
601+
writeln();
602+
});
603+
--indent;
604+
writeln("}");
605+
}
591606

592607
} else {
593608

cli/pbjs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ exports.main = function main(args, callback) {
3434
"force-message": "strict-message"
3535
},
3636
string: [ "target", "out", "path", "wrap", "root", "lint" ],
37-
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-message" ],
37+
boolean: [ "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "keep-case", "force-long", "force-number", "force-enum-string", "force-message" ],
3838
default: {
3939
target: "json",
4040
create: true,
@@ -49,6 +49,8 @@ exports.main = function main(args, callback) {
4949
lint: lintDefault,
5050
"keep-case": false,
5151
"force-long": false,
52+
"force-number": false,
53+
"force-enum-string": false,
5254
"force-message": false
5355
}
5456
});
@@ -123,6 +125,7 @@ exports.main = function main(args, callback) {
123125
" --no-comments Does not output any JSDoc comments.",
124126
"",
125127
" --force-long Enfores the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.",
128+
" --force-number Enfores the use of 'number' for s-/u-/int64 and s-/fixed64 fields.",
126129
" --force-message Enfores the use of message instances instead of plain objects.",
127130
"",
128131
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",

cli/targets/static.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ function buildFunction(type, functionName, gen, scope) {
313313

314314
function toJsType(field) {
315315
var type;
316+
316317
switch (field.type) {
317318
case "double":
318319
case "float":
@@ -328,7 +329,7 @@ function toJsType(field) {
328329
case "sint64":
329330
case "fixed64":
330331
case "sfixed64":
331-
type = config.forceLong ? "Long" : "number|Long";
332+
type = config.forceLong ? "Long" : config.forceNumber ? "number" : "number|Long";
332333
break;
333334
case "bool":
334335
type = "boolean";
@@ -636,24 +637,25 @@ function buildEnum(ref, enm) {
636637
var comment = [
637638
enm.comment || enm.name + " enum.",
638639
enm.parent instanceof protobuf.Root ? "@exports " + escapeName(enm.name) : undefined,
639-
"@enum {number}",
640+
config.forceEnumString ? "@enum {number}" : "@enum {string}",
640641
];
641642
Object.keys(enm.values).forEach(function(key) {
642-
var val = enm.values[key];
643-
comment.push("@property {number} " + key + "=" + val + " " + (enm.comments[key] || key + " value"));
643+
var val = config.forceEnumString ? key : enm.values[key];
644+
comment.push((config.forceEnumString ? "@property {string} " : "@property {number} ") + key + "=" + val + " " + (enm.comments[key] || key + " value"));
644645
});
645646
pushComment(comment);
646647
push(escapeName(ref) + "." + escapeName(enm.name) + " = (function() {");
647648
++indent;
648649
push((config.es6 ? "const" : "var") + " valuesById = {}, values = Object.create(valuesById);");
649650
var aliased = [];
650651
Object.keys(enm.values).forEach(function(key) {
651-
var val = enm.values[key];
652-
if (aliased.indexOf(val) > -1)
652+
var valueId = enm.values[key];
653+
var val = config.forceEnumString ? JSON.stringify(key) : valueId;
654+
if (aliased.indexOf(valueId) > -1)
653655
push("values[" + JSON.stringify(key) + "] = " + val + ";");
654656
else {
655-
push("values[valuesById[" + val + "] = " + JSON.stringify(key) + "] = " + val + ";");
656-
aliased.push(val);
657+
push("values[valuesById[" + valueId + "] = " + JSON.stringify(key) + "] = " + val + ";");
658+
aliased.push(valueId);
657659
}
658660
});
659661
push("return values;");

0 commit comments

Comments
 (0)