Permalink
Browse files

CLI: Properly implement $Properties interface in JSDoc, see #723

  • Loading branch information...
dcodeIO committed Mar 24, 2017
1 parent a1f23e0 commit afefa3de09620f50346bdcfa04d52952824c3c8d
Showing with 1,353 additions and 159 deletions.
  1. +6 −3 README.md
  2. +32 −20 cli/targets/static.js
  3. +11 −0 tests/data/comments.js
  4. +37 −2 tests/data/convert.js
  5. +69 −1 tests/data/mapbox/vector_tile.js
  6. +79 −1 tests/data/package.js
  7. +2 −0 tests/data/rpc.d.ts
  8. +6 −0 tests/data/rpc.js
  9. +270 −64 tests/data/test.d.ts
  10. +841 −68 tests/data/test.js
@@ -452,11 +452,14 @@ var buffer = AwesomeMessage.encode(message).finish();
...
```
If you'd like to completely exclude long.js and/or node (Buffer) typings, there are two stubs available that can be referenced instead of the full type definitions:
**Note:** By default, the npm package ships with long.js including its typings and node typing as optional dependencies. However, where long.js and/or node Buffers are not required, there are two stubs available that can be referenced instead of the full type definitions:
```ts
/// <reference path="node_modules/protobufjs/stub-long.d.ts" />
/// <reference path="node_modules/protobufjs/stub-node.d.ts" />
/// <reference path="./node_modules/protobufjs/stub-long.d.ts" />
```
```ts
/// <reference path="./node_modules/protobufjs/stub-node.d.ts" />
```
Documentation
@@ -251,6 +251,7 @@ function buildFunction(type, functionName, gen, scope) {
}
function toJsType(field) {
var type;
switch (field.type) {
case "double":
case "float":
@@ -259,26 +260,36 @@ function toJsType(field) {
case "sint32":
case "fixed32":
case "sfixed32":
return "number";
type = "number";
break;
case "int64":
case "uint64":
case "sint64":
case "fixed64":
case "sfixed64":
return config["strict-long"] ? "Long" : "number|Long";
type = config["strict-long"] ? "Long" : "number|Long";
break;
case "bool":
return "boolean";
type = "boolean";
break;
case "string":
return "string";
type = "string";
break;
case "bytes":
return "Uint8Array";
type = "Uint8Array";
break;
default:
if (field.resolvedType instanceof Enum)
return "number";
if (field.resolvedType instanceof Type)
return field.resolvedType.fullName.substring(1) + "$Properties";
return "*"; // should not happen
type = field.resolvedType.fullName.substring(1); // reference the enum
else if (field.resolvedType instanceof Type)
type = field.resolvedType.fullName.substring(1) + "$Properties"; // reference the interface
else
type = "*"; // should not happen
break;
}
return field.repeated ? "Array.<" + type + ">"
: field.map ? "Object.<string," + type + ">"
: type;
}
function buildType(ref, type) {
@@ -291,14 +302,10 @@ function buildType(ref, type) {
"@type Object"
];
type.fieldsArray.forEach(function(field) {
var jsType = toJsType(field);
if (field.map)
jsType = "Object.<string," + jsType + ">";
else if (field.repeated)
jsType = "Array.<" + jsType + ">";
var name = util.safeProp(field.name);
name = name.substring(1, name.charAt(0) === "[" ? name.length - 1 : name.length);
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + name + "]" : field.name) + " " + (field.comment || type.name + " " + field.name + "."));
var jsType = toJsType(field),
prop = util.safeProp(field.name);
prop = prop.substring(1, prop.charAt(0) === "[" ? prop.length - 1 : prop.length);
typeDef.push("@property {" + jsType + "} " + (field.optional ? "[" + prop + "]" : field.name) + " " + (field.comment || type.name + " " + field.name + "."));
});
push("");
pushComment(typeDef);
@@ -320,14 +327,19 @@ function buildType(ref, type) {
type.fieldsArray.forEach(function(field) {
field.resolve();
var prop = util.safeProp(field.name);
if (firstField) {
if (config.comments) {
push("");
pushComment([
"@type {" + toJsType(field) + (field.optional ? "|undefined" : "") + "}"
]);
} else if (firstField) {
push("");
firstField = false;
}
if (field.repeated)
push(name(type.name) + ".prototype" + prop + " = $util.emptyArray;");
push(name(type.name) + ".prototype" + prop + " = $util.emptyArray;"); // overwritten in constructor
else if (field.map)
push(name(type.name) + ".prototype" + prop + " = $util.emptyObject;");
push(name(type.name) + ".prototype" + prop + " = $util.emptyObject;"); // overwritten in constructor
else if (field.long)
push(name(type.name) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
+ JSON.stringify(field.typeDefault.low) + ","
@@ -37,8 +37,19 @@ $root.Test1 = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {string|undefined}
*/
Test1.prototype.field1 = "";
/**
* @type {number|undefined}
*/
Test1.prototype.field2 = 0;
/**
* @type {boolean|undefined}
*/
Test1.prototype.field3 = false;
/**
@@ -21,8 +21,8 @@ $root.Message = (function() {
* @property {Array.<number|Long>} [uint64Repeated] Message uint64Repeated.
* @property {Uint8Array} [bytesVal] Message bytesVal.
* @property {Array.<Uint8Array>} [bytesRepeated] Message bytesRepeated.
* @property {number} [enumVal] Message enumVal.
* @property {Array.<number>} [enumRepeated] Message enumRepeated.
* @property {Message.SomeEnum} [enumVal] Message enumVal.
* @property {Array.<Message.SomeEnum>} [enumRepeated] Message enumRepeated.
* @property {Object.<string,number|Long>} [int64Map] Message int64Map.
*/
@@ -44,14 +44,49 @@ $root.Message = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {string|undefined}
*/
Message.prototype.stringVal = "";
/**
* @type {Array.<string>|undefined}
*/
Message.prototype.stringRepeated = $util.emptyArray;
/**
* @type {number|Long|undefined}
*/
Message.prototype.uint64Val = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
/**
* @type {Array.<number|Long>|undefined}
*/
Message.prototype.uint64Repeated = $util.emptyArray;
/**
* @type {Uint8Array|undefined}
*/
Message.prototype.bytesVal = $util.newBuffer([]);
/**
* @type {Array.<Uint8Array>|undefined}
*/
Message.prototype.bytesRepeated = $util.emptyArray;
/**
* @type {Message.SomeEnum|undefined}
*/
Message.prototype.enumVal = 1;
/**
* @type {Array.<Message.SomeEnum>|undefined}
*/
Message.prototype.enumRepeated = $util.emptyArray;
/**
* @type {Object.<string,number|Long>|undefined}
*/
Message.prototype.int64Map = $util.emptyObject;
/**
@@ -24,7 +24,7 @@ $root.vector_tile = (function() {
* Properties of a Tile.
* @typedef vector_tile.Tile$Properties
* @type Object
* @property {Array.<vector_tile.Tile.Layer>} [layers] Tile layers.
* @property {Array.<vector_tile.Tile.Layer$Properties>} [layers] Tile layers.
*/
/**
@@ -41,6 +41,9 @@ $root.vector_tile = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {Array.<vector_tile.Tile.Layer$Properties>|undefined}
*/
Tile.prototype.layers = $util.emptyArray;
/**
@@ -253,12 +256,39 @@ $root.vector_tile = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {string|undefined}
*/
Value.prototype.stringValue = "";
/**
* @type {number|undefined}
*/
Value.prototype.floatValue = 0;
/**
* @type {number|undefined}
*/
Value.prototype.doubleValue = 0;
/**
* @type {number|Long|undefined}
*/
Value.prototype.intValue = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
/**
* @type {number|Long|undefined}
*/
Value.prototype.uintValue = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
/**
* @type {number|Long|undefined}
*/
Value.prototype.sintValue = $util.Long ? $util.Long.fromBits(0,0,false) : 0;
/**
* @type {boolean|undefined}
*/
Value.prototype.boolValue = false;
/**
@@ -555,9 +585,24 @@ $root.vector_tile = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {number|Long|undefined}
*/
Feature.prototype.id = $util.Long ? $util.Long.fromBits(0,0,true) : 0;
/**
* @type {Array.<number>|undefined}
*/
Feature.prototype.tags = $util.emptyArray;
/**
* @type {vector_tile.Tile.GeomType|undefined}
*/
Feature.prototype.type = 0;
/**
* @type {Array.<number>|undefined}
*/
Feature.prototype.geometry = $util.emptyArray;
/**
@@ -861,11 +906,34 @@ $root.vector_tile = (function() {
this[keys[i]] = properties[keys[i]];
}
/**
* @type {number}
*/
Layer.prototype.version = 1;
/**
* @type {string}
*/
Layer.prototype.name = "";
/**
* @type {Array.<vector_tile.Tile.Feature$Properties>|undefined}
*/
Layer.prototype.features = $util.emptyArray;
/**
* @type {Array.<string>|undefined}
*/
Layer.prototype.keys = $util.emptyArray;
/**
* @type {Array.<vector_tile.Tile.Value$Properties>|undefined}
*/
Layer.prototype.values = $util.emptyArray;
/**
* @type {number|undefined}
*/
Layer.prototype.extent = 4096;
/**
Oops, something went wrong.

0 comments on commit afefa3d

Please sign in to comment.