Skip to content

Commit

Permalink
Better TypeScript definition support for @property-annotated objects …
Browse files Browse the repository at this point in the history
…[ci skip]
  • Loading branch information
dcodeIO committed Dec 16, 2016
1 parent 3cb1b7e commit fc383d0
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
15 changes: 13 additions & 2 deletions lib/tsd-jsdoc/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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), ";");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
67 changes: 65 additions & 2 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var s = [
"sfixed64", // 11
"bool", // 12
"string", // 13
"bytes" // 14
"bytes", // 14
"message" // 15
];

function bake(values, offset) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/util/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
147 changes: 137 additions & 10 deletions types/protobuf.js.d.ts
Original file line number Diff line number Diff line change
@@ -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" {

/**
Expand Down Expand Up @@ -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
};
}

/**
Expand Down

0 comments on commit fc383d0

Please sign in to comment.