Skip to content

Commit c812cef

Browse files
committed
CLI: Added --strict-message option to pbjs to strictly reference message instances instead of $Properties, see #741
1 parent 7a0b5b1 commit c812cef

File tree

3 files changed

+57
-49
lines changed

3 files changed

+57
-49
lines changed

Diff for: cli/pbjs.js

+42-40
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,22 @@ exports.main = function main(args, callback) {
3131
lint : "l"
3232
},
3333
string: [ "target", "out", "path", "wrap", "root", "lint" ],
34-
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long" ],
34+
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long", "strict-message" ],
3535
default: {
36-
target : "json",
37-
create : true,
38-
encode : true,
39-
decode : true,
40-
verify : true,
41-
convert : true,
42-
delimited : true,
43-
beautify : true,
44-
comments : true,
45-
es6 : null,
46-
lint : lintDefault,
47-
"keep-case" : false,
48-
"strict-long": false
36+
target : "json",
37+
create : true,
38+
encode : true,
39+
decode : true,
40+
verify : true,
41+
convert : true,
42+
delimited : true,
43+
beautify : true,
44+
comments : true,
45+
es6 : null,
46+
lint : lintDefault,
47+
"keep-case" : false,
48+
"strict-long" : false,
49+
"strict-message": false
4950
}
5051
});
5152

@@ -68,48 +69,49 @@ exports.main = function main(args, callback) {
6869
"",
6970
chalk.bold.white("Translates between file formats and generates static code."),
7071
"",
71-
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
72+
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
7273
"",
7374
descs.join("\n"),
7475
"",
75-
" -p, --path Adds a directory to the include path.",
76+
" -p, --path Adds a directory to the include path.",
7677
"",
77-
" -o, --out Saves to a file instead of writing to stdout.",
78+
" -o, --out Saves to a file instead of writing to stdout.",
7879
"",
79-
" --sparse Exports only those types referenced from a main file (experimental).",
80+
" --sparse Exports only those types referenced from a main file (experimental).",
8081
"",
81-
chalk.bold.gray(" Module targets only:"),
82+
chalk.bold.gray(" Module targets only:"),
8283
"",
83-
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
84+
" -w, --wrap Specifies the wrapper to use. Also accepts a path to require a custom wrapper.",
8485
"",
85-
" default Default wrapper supporting both CommonJS and AMD",
86-
" commonjs CommonJS wrapper",
87-
" amd AMD wrapper",
88-
" es6 ES6 wrapper (implies --es6)",
86+
" default Default wrapper supporting both CommonJS and AMD",
87+
" commonjs CommonJS wrapper",
88+
" amd AMD wrapper",
89+
" es6 ES6 wrapper (implies --es6)",
8990
"",
90-
" -r, --root Specifies an alternative protobuf.roots name.",
91+
" -r, --root Specifies an alternative protobuf.roots name.",
9192
"",
92-
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
93+
" -l, --lint Linter configuration. Defaults to protobuf.js-compatible rules:",
9394
"",
94-
" " + lintDefault,
95+
" " + lintDefault,
9596
"",
96-
" --es6 Enables ES6 syntax (const/let instead of var)",
97+
" --es6 Enables ES6 syntax (const/let instead of var)",
9798
"",
98-
chalk.bold.gray(" Proto sources only:"),
99+
chalk.bold.gray(" Proto sources only:"),
99100
"",
100-
" --keep-case Keeps field casing instead of converting to camel case.",
101+
" --keep-case Keeps field casing instead of converting to camel case.",
101102
"",
102-
chalk.bold.gray(" Static targets only:"),
103+
chalk.bold.gray(" Static targets only:"),
103104
"",
104-
" --no-create Does not generate create functions used for reflection compatibility.",
105-
" --no-encode Does not generate encode functions.",
106-
" --no-decode Does not generate decode functions.",
107-
" --no-verify Does not generate verify functions.",
108-
" --no-convert Does not generate convert functions like from/toObject",
109-
" --no-delimited Does not generate delimited encode/decode functions.",
110-
" --no-beautify Does not beautify generated code.",
111-
" --no-comments Does not output any JSDoc comments.",
112-
" --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).",
105+
" --no-create Does not generate create functions used for reflection compatibility.",
106+
" --no-encode Does not generate encode functions.",
107+
" --no-decode Does not generate decode functions.",
108+
" --no-verify Does not generate verify functions.",
109+
" --no-convert Does not generate convert functions like from/toObject",
110+
" --no-delimited Does not generate delimited encode/decode functions.",
111+
" --no-beautify Does not beautify generated code.",
112+
" --no-comments Does not output any JSDoc comments.",
113+
" --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).",
114+
" --strict-message Forces message types to actual instances (no plain objects).",
113115
"",
114116
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
115117
""

Diff for: cli/targets/static.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function toJsType(field) {
328328
if (field.resolve().resolvedType instanceof Enum)
329329
type = field.resolvedType.fullName.substring(1); // reference the enum
330330
else if (field.resolvedType instanceof Type)
331-
type = field.resolvedType.fullName.substring(1) + "$Properties"; // reference the interface
331+
type = messageRef(field.resolvedType.fullName.substring(1)); // reference the interface
332332
else
333333
type = "*"; // should not happen
334334
break;
@@ -338,13 +338,19 @@ function toJsType(field) {
338338
: type;
339339
}
340340

341+
function messageRef(fullName) {
342+
return config["strict-message"] || !config.comments
343+
? fullName
344+
: fullName + "$Properties";
345+
}
346+
341347
function buildType(ref, type) {
342348
var fullName = type.fullName.substring(1);
343349

344-
if (config.comments) {
350+
if (config.comments && !config["strict-message"]) {
345351
var typeDef = [
346352
"Properties of " + aOrAn(type.name) + ".",
347-
"@typedef " + fullName + "$Properties",
353+
"@typedef " + messageRef(fullName),
348354
"@type {Object}"
349355
];
350356
type.fieldsArray.forEach(function(field) {
@@ -363,7 +369,7 @@ function buildType(ref, type) {
363369
type.comment ? "@classdesc " + type.comment : null,
364370
"@exports " + fullName,
365371
"@constructor",
366-
"@param {" + fullName + "$Properties=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
372+
"@param {" + messageRef(fullName) + "=} [" + (config.beautify ? "properties" : "p") + "] Properties to set"
367373
]);
368374
buildFunction(type, type.name, Class.generate(type));
369375

@@ -427,7 +433,7 @@ function buildType(ref, type) {
427433
push("");
428434
pushComment([
429435
"Creates a new " + type.name + " instance using the specified properties.",
430-
"@param {" + fullName + "$Properties=} [properties] Properties to set",
436+
"@param {" + messageRef(fullName) + "=} [properties] Properties to set",
431437
"@returns {" + fullName + "} " + type.name + " instance"
432438
]);
433439
push(name(type.name) + ".create = function create(properties) {");
@@ -441,7 +447,7 @@ function buildType(ref, type) {
441447
push("");
442448
pushComment([
443449
"Encodes the specified " + type.name + " message. Does not implicitly {@link " + fullName + ".verify|verify} messages.",
444-
"@param {" + fullName + "$Properties} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
450+
"@param {" + messageRef(fullName) + "} " + (config.beautify ? "message" : "m") + " " + type.name + " message or plain object to encode",
445451
"@param {$protobuf.Writer} [" + (config.beautify ? "writer" : "w") + "] Writer to encode to",
446452
"@returns {$protobuf.Writer} Writer"
447453
]);
@@ -451,7 +457,7 @@ function buildType(ref, type) {
451457
push("");
452458
pushComment([
453459
"Encodes the specified " + type.name + " message, length delimited. Does not implicitly {@link " + fullName + ".verify|verify} messages.",
454-
"@param {" + fullName + "$Properties} message " + type.name + " message or plain object to encode",
460+
"@param {" + messageRef(fullName) + "} message " + type.name + " message or plain object to encode",
455461
"@param {$protobuf.Writer} [writer] Writer to encode to",
456462
"@returns {$protobuf.Writer} Writer"
457463
]);

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "protobufjs",
3-
"version": "6.7.0",
3+
"version": "6.7.1",
44
"versionScheme": "~",
55
"description": "Protocol Buffers for JavaScript (& TypeScript).",
66
"author": "Daniel Wirtz <[email protected]>",
@@ -90,7 +90,7 @@
9090
"tmp": "0.0.31",
9191
"tslint": "^5.0.0",
9292
"typescript": "^2.2.2",
93-
"uglify-js": "^2.8.20",
93+
"uglify-js": "^2.8.21",
9494
"vinyl-buffer": "^1.0.0",
9595
"vinyl-fs": "^2.4.4",
9696
"vinyl-source-stream": "^1.1.0"

0 commit comments

Comments
 (0)