Skip to content

Commit c3f0a21

Browse files
committed
CLI: Added --strict-long option to pbjs to always emit 'Long' instead of 'number|Long' (only relevant with long.js), see #718
1 parent e1dd1bc commit c3f0a21

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

cli/pbjs.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ 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" ],
34+
boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long" ],
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
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
4749
}
4850
});
4951

@@ -107,6 +109,7 @@ exports.main = function main(args, callback) {
107109
" --no-delimited Does not generate delimited encode/decode functions.",
108110
" --no-beautify Does not beautify generated code.",
109111
" --no-comments Does not output any JSDoc comments.",
112+
" --strict-long Forces s-/u-/int64 and s-/fixed64 types to 'Long' only (no numbers).",
110113
"",
111114
"usage: " + chalk.bold.green("pbjs") + " [options] file1.proto file2.json ..." + chalk.gray(" (or) ") + "other | " + chalk.bold.green("pbjs") + " [options] -",
112115
""

cli/targets/static.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ function buildFunction(type, functionName, gen, scope) {
250250
push("};");
251251
}
252252

253-
function toJsType(field, isReturn) {
253+
function toJsType(field) {
254254
switch (field.type) {
255255
case "double":
256256
case "float":
@@ -265,7 +265,7 @@ function toJsType(field, isReturn) {
265265
case "sint64":
266266
case "fixed64":
267267
case "sfixed64":
268-
return isReturn ? "Long" : "number|Long";
268+
return config["strict-long"] ? "Long" : "number|Long";
269269
case "bool":
270270
return "boolean";
271271
case "string":

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"postinstall": "node scripts/postinstall",
3737
"prof": "node bench/prof",
3838
"test": "tape -r ./lib/tape-adapter tests/*.js tests/node/*.js | tap-spec",
39-
"types": "node bin/pbts --main --global protobuf --out index.d.ts src/ lib/aspromise/index.js lib/base64/index.js lib/codegen/index.js lib/eventemitter/index.js lib/fetch/index.js lib/inquire/index.js lib/path/index.js lib/pool/index.js lib/utf8/index.js && tsc tests/comp_typescript.ts --lib es2015 --noEmit --strictNullChecks && tsc tests/data/test.ts --lib es2015 --noEmit --strictNullChecks && tsc tests/data/rpc.ts --lib es2015 --noEmit --strictNullChecks",
39+
"test-types": "tsc tests/comp_typescript.ts --lib es2015 --noEmit --strictNullChecks && tsc tests/data/test.ts --lib es2015 --noEmit --strictNullChecks && tsc tests/data/rpc.ts --lib es2015 --noEmit --strictNullChecks",
40+
"types": "node bin/pbts --main --global protobuf --out index.d.ts src/ lib/aspromise/index.js lib/base64/index.js lib/codegen/index.js lib/eventemitter/index.js lib/fetch/index.js lib/inquire/index.js lib/path/index.js lib/pool/index.js lib/utf8/index.js && npm run test-types",
4041
"zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js",
4142
"zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js",
4243
"make": "npm run test && npm run types && npm run build && npm run lint",

0 commit comments

Comments
 (0)