From c812cef0eff26998f14c9d58d4486464ad7b2bbc Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 3 Apr 2017 13:47:38 +0200 Subject: [PATCH] CLI: Added --strict-message option to pbjs to strictly reference message instances instead of $Properties, see #741 --- cli/pbjs.js | 82 ++++++++++++++++++++++--------------------- cli/targets/static.js | 20 +++++++---- package.json | 4 +-- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/cli/pbjs.js b/cli/pbjs.js index 61e495f10..6e991ffe3 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -31,21 +31,22 @@ exports.main = function main(args, callback) { lint : "l" }, string: [ "target", "out", "path", "wrap", "root", "lint" ], - boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long" ], + boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long", "strict-message" ], default: { - target : "json", - create : true, - encode : true, - decode : true, - verify : true, - convert : true, - delimited : true, - beautify : true, - comments : true, - es6 : null, - lint : lintDefault, - "keep-case" : false, - "strict-long": false + target : "json", + create : true, + encode : true, + decode : true, + verify : true, + convert : true, + delimited : true, + beautify : true, + comments : true, + es6 : null, + lint : lintDefault, + "keep-case" : false, + "strict-long" : false, + "strict-message": false } }); @@ -68,48 +69,49 @@ exports.main = function main(args, callback) { "", chalk.bold.white("Translates between file formats and generates static code."), "", - " -t, --target Specifies the target format. Also accepts a path to require a custom target.", + " -t, --target Specifies the target format. Also accepts a path to require a custom target.", "", descs.join("\n"), "", - " -p, --path Adds a directory to the include path.", + " -p, --path Adds a directory to the include path.", "", - " -o, --out Saves to a file instead of writing to stdout.", + " -o, --out Saves to a file instead of writing to stdout.", "", - " --sparse Exports only those types referenced from a main file (experimental).", + " --sparse Exports only those types referenced from a main file (experimental).", "", - chalk.bold.gray(" Module targets only:"), + chalk.bold.gray(" Module targets only:"), "", - " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.", + " -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.", "", - " default Default wrapper supporting both CommonJS and AMD", - " commonjs CommonJS wrapper", - " amd AMD wrapper", - " es6 ES6 wrapper (implies --es6)", + " default Default wrapper supporting both CommonJS and AMD", + " commonjs CommonJS wrapper", + " amd AMD wrapper", + " es6 ES6 wrapper (implies --es6)", "", - " -r, --root Specifies an alternative protobuf.roots name.", + " -r, --root Specifies an alternative protobuf.roots name.", "", - " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:", + " -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:", "", - " " + lintDefault, + " " + lintDefault, "", - " --es6 Enables ES6 syntax (const/let instead of var)", + " --es6 Enables ES6 syntax (const/let instead of var)", "", - chalk.bold.gray(" Proto sources only:"), + chalk.bold.gray(" Proto sources only:"), "", - " --keep-case Keeps field casing instead of converting to camel case.", + " --keep-case Keeps field casing instead of converting to camel case.", "", - chalk.bold.gray(" Static targets only:"), + chalk.bold.gray(" Static targets only:"), "", - " --no-create Does not generate create functions used for reflection compatibility.", - " --no-encode Does not generate encode functions.", - " --no-decode Does not generate decode functions.", - " --no-verify Does not generate verify functions.", - " --no-convert Does not generate convert functions like from/toObject", - " --no-delimited Does not generate delimited encode/decode functions.", - " --no-beautify Does not beautify generated code.", - " --no-comments Does not output any JSDoc comments.", - " --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).", + " --no-create Does not generate create functions used for reflection compatibility.", + " --no-encode Does not generate encode functions.", + " --no-decode Does not generate decode functions.", + " --no-verify Does not generate verify functions.", + " --no-convert Does not generate convert functions like from/toObject", + " --no-delimited Does not generate delimited encode/decode functions.", + " --no-beautify Does not beautify generated code.", + " --no-comments Does not output any JSDoc comments.", + " --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).", + " --strict-message Forces message types to actual instances (no plain objects).", "", "usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -", "" diff --git a/cli/targets/static.js b/cli/targets/static.js index f9ef88346..bbb813b06 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -328,7 +328,7 @@ function toJsType(field) { if (field.resolve().resolvedType instanceof Enum) 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 + type = messageRef(field.resolvedType.fullName.substring(1)); // reference the interface else type = "*"; // should not happen break; @@ -338,13 +338,19 @@ function toJsType(field) { : type; } +function messageRef(fullName) { + return config["strict-message"] || !config.comments + ? fullName + : fullName + "$Properties"; +} + function buildType(ref, type) { var fullName = type.fullName.substring(1); - if (config.comments) { + if (config.comments && !config["strict-message"]) { var typeDef = [ "Properties of " + aOrAn(type.name) + ".", - "@typedef " + fullName + "$Properties", + "@typedef " + messageRef(fullName), "@type {Object}" ]; type.fieldsArray.forEach(function(field) { @@ -363,7 +369,7 @@ function buildType(ref, type) { type.comment ? "@classdesc " + type.comment : null, "@exports " + fullName, "@constructor", - "@param {" + fullName + "$Properties=} [" + (config.beautify ? "properties" : "p") + "] Properties to set" + "@param {" + messageRef(fullName) + "=} [" + (config.beautify ? "properties" : "p") + "] Properties to set" ]); buildFunction(type, type.name, Class.generate(type)); @@ -427,7 +433,7 @@ function buildType(ref, type) { push(""); pushComment([ "Creates a new " + type.name + " instance using the specified properties.", - "@param {" + fullName + "$Properties=} [properties] Properties to set", + "@param {" + messageRef(fullName) + "=} [properties] Properties to set", "@returns {" + fullName + "} " + type.name + " instance" ]); push(name(type.name) + ".create = function create(properties) {"); @@ -441,7 +447,7 @@ function buildType(ref, type) { push(""); pushComment([ "Encodes the specified " + type.name + " message. Does not implicitly {@link " + fullName + ".verify|verify} messages.", - "@param {" + fullName + "$Properties} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode", + "@param {" + messageRef(fullName) + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode", "@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to", "@returns {$protobuf.Writer} Writer" ]); @@ -451,7 +457,7 @@ function buildType(ref, type) { push(""); pushComment([ "Encodes the specified " + type.name + " message, length delimited. Does not implicitly {@link " + fullName + ".verify|verify} messages.", - "@param {" + fullName + "$Properties} message " + type.name + " message or plain object to encode", + "@param {" + messageRef(fullName) + "} message " + type.name + " message or plain object to encode", "@param {$protobuf.Writer} [writer] Writer to encode to", "@returns {$protobuf.Writer} Writer" ]); diff --git a/package.json b/package.json index 1197b5380..a358145c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "protobufjs", - "version": "6.7.0", + "version": "6.7.1", "versionScheme": "~", "description": "Protocol Buffers for JavaScript (& TypeScript).", "author": "Daniel Wirtz ", @@ -90,7 +90,7 @@ "tmp": "0.0.31", "tslint": "^5.0.0", "typescript": "^2.2.2", - "uglify-js": "^2.8.20", + "uglify-js": "^2.8.21", "vinyl-buffer": "^1.0.0", "vinyl-fs": "^2.4.4", "vinyl-source-stream": "^1.1.0"