Permalink
Browse files

Better TypeScript definition support for @property-annotated objects …

…[ci skip]
  • Loading branch information...
dcodeIO committed Dec 16, 2016
1 parent 3cb1b7e commit fc383d0721d83f66b2d941f0d9361621839327e9
Showing with 218 additions and 17 deletions.
  1. +1 −1 README.md
  2. +13 −2 lib/tsd-jsdoc/publish.js
  3. +1 −1 src/root.js
  4. +65 −2 src/types.js
  5. +1 −1 src/util/runtime.js
  6. +137 −10 types/protobuf.js.d.ts
@@ -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
--------
@@ -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), ";");
}
}
@@ -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;
}
@@ -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.<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
*/
types.basic = bake([
/* double */ 1,
@@ -58,6 +74,22 @@ types.basic = bake([
/**
* Basic type defaults.
* @type {Object.<string,*>}
* @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.<number>} 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.<string,number>}
* @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.<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
*/
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.<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
*/
types.packed = bake([
/* double */ 1,
@@ -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;
@@ -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.<string,number>}
*/
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.<string,*>}
*/
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.<number>} 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.<string,number>}
* @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.<string,number>}
*/
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.<string,number>}
*/
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
};
}
/**

0 comments on commit fc383d0

Please sign in to comment.