From c3f0a2124c661bb9ba35f92c21a98a4405d30b47 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Fri, 24 Mar 2017 00:57:06 +0100 Subject: [PATCH] CLI: Added --strict-long option to pbjs to always emit 'Long' instead of 'number|Long' (only relevant with long.js), see #718 --- cli/pbjs.js | 27 +++++++++++++++------------ cli/targets/static.js | 4 ++-- package.json | 3 ++- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cli/pbjs.js b/cli/pbjs.js index 7a5e7f8fb..8210c0377 100644 --- a/cli/pbjs.js +++ b/cli/pbjs.js @@ -31,19 +31,21 @@ 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" ], + boolean: [ "keep-case", "create", "encode", "decode", "verify", "convert", "delimited", "beautify", "comments", "es6", "sparse", "strict-long" ], default: { - target : "json", - create : true, - encode : true, - decode : true, - verify : true, - convert : true, - delimited : true, - beautify : true, - comments : true, - es6 : null, - lint : lintDefault + 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 } }); @@ -107,6 +109,7 @@ exports.main = function main(args, callback) { " --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).", "", "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 b90927963..9997e1418 100644 --- a/cli/targets/static.js +++ b/cli/targets/static.js @@ -250,7 +250,7 @@ function buildFunction(type, functionName, gen, scope) { push("};"); } -function toJsType(field, isReturn) { +function toJsType(field) { switch (field.type) { case "double": case "float": @@ -265,7 +265,7 @@ function toJsType(field, isReturn) { case "sint64": case "fixed64": case "sfixed64": - return isReturn ? "Long" : "number|Long"; + return config["strict-long"] ? "Long" : "number|Long"; case "bool": return "boolean"; case "string": diff --git a/package.json b/package.json index f9b985037..5c04d1203 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "postinstall": "node scripts/postinstall", "prof": "node bench/prof", "test": "tape -r ./lib/tape-adapter tests/*.js tests/node/*.js | tap-spec", - "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", + "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", + "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", "zuul": "zuul --ui tape --no-coverage --concurrency 4 -- tests/*.js", "zuul-local": "zuul --ui tape --concurrency 1 --local 8080 --disable-tunnel -- tests/*.js", "make": "npm run test && npm run types && npm run build && npm run lint",