From fc383d0721d83f66b2d941f0d9361621839327e9 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 16 Dec 2016 16:36:42 +0100 Subject: [PATCH] Better TypeScript definition support for @property-annotated objects [ci skip] --- README.md | 2 +- lib/tsd-jsdoc/publish.js | 15 +++- src/root.js | 2 +- src/types.js | 67 +++++++++++++++++- src/util/runtime.js | 2 +- types/protobuf.js.d.ts | 147 ++++++++++++++++++++++++++++++++++++--- 6 files changed, 218 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 7789963db..4cba5f148 100644 --- a/README.md +++ b/README.md @@ -400,7 +400,7 @@ While .proto and JSON files require the full library (about 18kb gzipped), prett Static code, on the other hand, requires just the minimal runtime (about 5.5kb gzipped), but generates additional, albeit editable, source code without any reflection features. -When `new Function(...)` is supported (and it usually is), there is no difference performance-wise as the code generated statically is pretty much the same as generated at runtime. +Where `new Function(...)` is supported (it usually is), there is no difference performance-wise as the code generated statically is pretty much the same as generated at runtime. Building -------- diff --git a/lib/tsd-jsdoc/publish.js b/lib/tsd-jsdoc/publish.js index afed2288c..f63fa23cd 100644 --- a/lib/tsd-jsdoc/publish.js +++ b/lib/tsd-jsdoc/publish.js @@ -386,7 +386,7 @@ function handleClass(element, parent) { } // handles a namespace or class member -function handleMember(element, parent) { +function handleMember(element, parent, isProperty) { begin(element); if (element.isEnum) { @@ -416,7 +416,18 @@ function handleMember(element, parent) { } else write(element.kind === "constant" ? "const " : "var "); - writeln(element.name, ": ", getTypeOf(element), ";"); + write(element.name, ": "); + + if (/^Object\b/i.test(element.type.names[0]) && element.properties) { + writeln("{"); + ++indent; + element.properties.forEach(function(property, i) { + writeln(JSON.stringify(property.name), ": ", getTypeOf(property), i < element.properties.length - 1 ? "," : ""); + }); + --indent; + writeln("};"); + } else + writeln(getTypeOf(element), ";"); } } diff --git a/src/root.js b/src/root.js index 44ed9c506..4a34a738a 100644 --- a/src/root.js +++ b/src/root.js @@ -67,7 +67,7 @@ function SYNC() {} // eslint-disable-line no-empty-function * @returns {undefined} */ RootPrototype.load = function load(filename, options, callback) { - if (typeof options === 'function') { + if (typeof options === "function") { callback = options; options = undefined; } diff --git a/src/types.js b/src/types.js index a1d9ae521..0f00ffa4e 100644 --- a/src/types.js +++ b/src/types.js @@ -23,7 +23,8 @@ var s = [ "sfixed64", // 11 "bool", // 12 "string", // 13 - "bytes" // 14 + "bytes", // 14 + "message" // 15 ]; function bake(values, offset) { @@ -36,6 +37,21 @@ function bake(values, offset) { /** * Basic type wire types. * @type {Object.} + * @property {number} double=1 Fixed64 wire type + * @property {number} float=5 Fixed32 wire type + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type + * @property {number} string=2 Ldelim wire type + * @property {number} bytes=2 Ldelim wire type */ types.basic = bake([ /* double */ 1, @@ -58,6 +74,22 @@ types.basic = bake([ /** * Basic type defaults. * @type {Object.} + * @property {number} double=0 Double default + * @property {number} float=0 Float default + * @property {number} int32=0 Int32 default + * @property {number} uint32=0 Uint32 default + * @property {number} sint32=0 Sint32 default + * @property {number} fixed32=0 Fixed32 default + * @property {number} sfixed32=0 Sfixed32 default + * @property {number} int64=0 Int64 default + * @property {number} uint64=0 Uint64 default + * @property {number} sint64=0 Sint32 default + * @property {number} fixed64=0 Fixed64 default + * @property {number} sfixed64=0 Sfixed64 default + * @property {boolean} bool=false Bool default + * @property {string} string="" String default + * @property {Array.} bytes=Array(0) Bytes default + * @property {Message} message=null Message default */ types.defaults = bake([ /* double */ 0, @@ -74,12 +106,18 @@ types.defaults = bake([ /* sfixed64 */ 0, /* bool */ false, /* string */ "", - /* bytes */ util.emptyArray + /* bytes */ util.emptyArray, + /* message */ null ]); /** * Basic long type wire types. * @type {Object.} + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type */ types.long = bake([ /* int64 */ 0, @@ -92,6 +130,18 @@ types.long = bake([ /** * Allowed types for map keys with their associated wire type. * @type {Object.} + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type + * @property {number} string=2 Ldelim wire type */ types.mapKey = bake([ /* int32 */ 0, @@ -111,6 +161,19 @@ types.mapKey = bake([ /** * Allowed types for packed repeated fields with their associated wire type. * @type {Object.} + * @property {number} double=1 Fixed64 wire type + * @property {number} float=5 Fixed32 wire type + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type */ types.packed = bake([ /* double */ 1, diff --git a/src/util/runtime.js b/src/util/runtime.js index 90347637a..22cab9d74 100644 --- a/src/util/runtime.js +++ b/src/util/runtime.js @@ -113,7 +113,7 @@ util.longNeq = function longNeq(a, b) { * @returns {boolean} `true` if not equal */ util.longNe = function longNe(val, lo, hi) { - if (typeof val === 'object') // Long-like, null is invalid and throws + if (typeof val === "object") // Long-like, null is invalid and throws return val.low !== lo || val.high !== hi; var bits = util.LongBits.from(val); return bits.lo !== lo || bits.hi !== hi; diff --git a/types/protobuf.js.d.ts b/types/protobuf.js.d.ts index 14fac1de1..368297c10 100644 --- a/types/protobuf.js.d.ts +++ b/types/protobuf.js.d.ts @@ -1,5 +1,5 @@ // $> pbts --name protobufjs --out types/protobuf.js.d.ts src -// Generated Fri, 16 Dec 2016 14:38:04 UTC +// Generated Fri, 16 Dec 2016 15:34:54 UTC declare module "protobufjs" { /** @@ -1624,32 +1624,159 @@ declare module "protobufjs" { /** * Basic type wire types. * @type {Object.} - */ - var basic: { [k: string]: number }; + * @property {number} double=1 Fixed64 wire type + * @property {number} float=5 Fixed32 wire type + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type + * @property {number} string=2 Ldelim wire type + * @property {number} bytes=2 Ldelim wire type + */ + var basic: { + "double": number, + "float": number, + "int32": number, + "uint32": number, + "sint32": number, + "fixed32": number, + "sfixed32": number, + "int64": number, + "uint64": number, + "sint64": number, + "fixed64": number, + "sfixed64": number, + "bool": number, + "string": number, + "bytes": number + }; /** * Basic type defaults. * @type {Object.} - */ - var defaults: { [k: string]: any }; + * @property {number} double=0 Double default + * @property {number} float=0 Float default + * @property {number} int32=0 Int32 default + * @property {number} uint32=0 Uint32 default + * @property {number} sint32=0 Sint32 default + * @property {number} fixed32=0 Fixed32 default + * @property {number} sfixed32=0 Sfixed32 default + * @property {number} int64=0 Int64 default + * @property {number} uint64=0 Uint64 default + * @property {number} sint64=0 Sint32 default + * @property {number} fixed64=0 Fixed64 default + * @property {number} sfixed64=0 Sfixed64 default + * @property {boolean} bool=false Bool default + * @property {string} string="" String default + * @property {Array.} bytes=Array(0) Bytes default + * @property {Message} message=null Message default + */ + var defaults: { + "double": number, + "float": number, + "int32": number, + "uint32": number, + "sint32": number, + "fixed32": number, + "sfixed32": number, + "int64": number, + "uint64": number, + "sint64": number, + "fixed64": number, + "sfixed64": number, + "bool": boolean, + "string": string, + "bytes": number[], + "message": Message + }; /** * Basic long type wire types. * @type {Object.} + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type */ - var long: { [k: string]: number }; + var long: { + "int64": number, + "uint64": number, + "sint64": number, + "fixed64": number, + "sfixed64": number + }; /** * Allowed types for map keys with their associated wire type. * @type {Object.} - */ - var mapKey: { [k: string]: number }; + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type + * @property {number} string=2 Ldelim wire type + */ + var mapKey: { + "int32": number, + "uint32": number, + "sint32": number, + "fixed32": number, + "sfixed32": number, + "int64": number, + "uint64": number, + "sint64": number, + "fixed64": number, + "sfixed64": number, + "bool": number, + "string": number + }; /** * Allowed types for packed repeated fields with their associated wire type. * @type {Object.} - */ - var packed: { [k: string]: number }; + * @property {number} double=1 Fixed64 wire type + * @property {number} float=5 Fixed32 wire type + * @property {number} int32=0 Varint wire type + * @property {number} uint32=0 Varint wire type + * @property {number} sint32=0 Varint wire type + * @property {number} fixed32=5 Fixed32 wire type + * @property {number} sfixed32=5 Fixed32 wire type + * @property {number} int64=0 Varint wire type + * @property {number} uint64=0 Varint wire type + * @property {number} sint64=0 Varint wire type + * @property {number} fixed64=1 Fixed64 wire type + * @property {number} sfixed64=1 Fixed64 wire type + * @property {number} bool=0 Varint wire type + */ + var packed: { + "double": number, + "float": number, + "int32": number, + "uint32": number, + "sint32": number, + "fixed32": number, + "sfixed32": number, + "int64": number, + "uint64": number, + "sint64": number, + "fixed64": number, + "sfixed64": number, + "bool": number + }; } /**