Skip to content

Commit

Permalink
CLI: Added a few helpful comments to static code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 31, 2016
1 parent 24b293c commit 0cda72a
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 433 deletions.
9 changes: 7 additions & 2 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function slightlyBeautify(generatedCode) {
.replace(/(!?[=<>|&%?:]+)([^\s])/g, " $1 $2") // a!==b, a&&b, a?b:c etc.
.replace(/\b([+-])\b/g," $1 ") // a+b
.replace(/\b(if|else|else if|for|while|do|switch)\(/g, "$1 (") // if(a)
.replace(/\breturn"/g, "return \"") // return"error"
.replace(/\b(return|case)"/g, "$1 \"") // return"error", case"one"
.replace(/([;,])([^\s])/g, "$1 $2") // cond(var a=0;a<b;++b), var a=1,b;
.replace(/{$/mg, " {")
.replace(/\br\b/g, "reader")
Expand All @@ -148,7 +148,12 @@ function slightlyBeautify(generatedCode) {
.replace(/\bl\b/g, "length")
.replace(/\bc\b/g, "end")
.replace(/\bk\b/g, "key")
.replace(/\bks\b/g, "keys");
.replace(/\bks\b/g, "keys")
.replace(/\bs\b/g, "reason")
.replace(/(uint32)\((\d+)\)/g, function($0, $1, $2) {
var tag = parseInt($2, 10);
return $1 + "(" + tag + "/*= id " + (tag >>> 3) + ", wireType " + (tag & 7) + " */)";
});
}

function buildFunction(type, functionName, gen, scope) {
Expand Down
10 changes: 5 additions & 5 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $root.A = (function() {
A.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.whatever !== undefined && message.whatever !== "")
writer.uint32(10).string(message.whatever)
writer.uint32(10/*= id 1, wireType 2 */).string(message.whatever)
return writer
}})($protobuf.Writer, $protobuf.util, [null]); /* eslint-enable */

Expand Down Expand Up @@ -161,7 +161,7 @@ $root.B = (function() {
B.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.A !== undefined && message.A !== null)
types[0].encode(message.A, writer.uint32(10).fork()).ldelim()
types[0].encode(message.A, writer.uint32(10/*= id 1, wireType 2 */).fork()).ldelim()
return writer
}})($protobuf.Writer, $protobuf.util, ["A"]); /* eslint-enable */

Expand Down Expand Up @@ -217,9 +217,9 @@ $root.B = (function() {
*/
B.verify = /* eslint-disable */ (function(util, types) { $lazyTypes.push(types); return function verify(message) {
if (message.A !== undefined && message.A !== null) {
var s;
if (s = types[0].verify(message.A))
return s
var reason;
if (reason = types[0].verify(message.A))
return reason
}
return null
}})($protobuf.util, ["A"]); /* eslint-enable */
Expand Down
54 changes: 27 additions & 27 deletions tests/data/mapbox/vector_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ $root.vector_tile = (function() {
writer || (writer = Writer.create())
if (message.layers)
for (var i = 0; i < message.layers.length; ++i)
types[0].encode(message.layers[i], writer.uint32(26).fork()).ldelim()
types[0].encode(message.layers[i], writer.uint32(26/*= id 3, wireType 2 */).fork()).ldelim()
return writer
}})($protobuf.Writer, $protobuf.util, ["vector_tile.Tile.Layer"]); /* eslint-enable */

Expand Down Expand Up @@ -122,9 +122,9 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.layers))
return "invalid value for field .vector_tile.Tile.layers (array expected)"
for (var i = 0; i < message.layers.length; ++i) {
var s;
if (s = types[0].verify(message.layers[i]))
return s
var reason;
if (reason = types[0].verify(message.layers[i]))
return reason
}
}
return null
Expand Down Expand Up @@ -223,19 +223,19 @@ $root.vector_tile = (function() {
Value.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.stringValue !== undefined && message.stringValue !== "")
writer.uint32(10).string(message.stringValue)
writer.uint32(10/*= id 1, wireType 2 */).string(message.stringValue)
if (message.floatValue !== undefined && message.floatValue !== 0)
writer.uint32(21).float(message.floatValue)
writer.uint32(21/*= id 2, wireType 5 */).float(message.floatValue)
if (message.doubleValue !== undefined && message.doubleValue !== 0)
writer.uint32(25).double(message.doubleValue)
writer.uint32(25/*= id 3, wireType 1 */).double(message.doubleValue)
if (message.intValue !== undefined && message.intValue !== null && util.longNe(message.intValue, 0, 0))
writer.uint32(32).int64(message.intValue)
writer.uint32(32/*= id 4, wireType 0 */).int64(message.intValue)
if (message.uintValue !== undefined && message.uintValue !== null && util.longNe(message.uintValue, 0, 0))
writer.uint32(40).uint64(message.uintValue)
writer.uint32(40/*= id 5, wireType 0 */).uint64(message.uintValue)
if (message.sintValue !== undefined && message.sintValue !== null && util.longNe(message.sintValue, 0, 0))
writer.uint32(48).sint64(message.sintValue)
writer.uint32(48/*= id 6, wireType 0 */).sint64(message.sintValue)
if (message.boolValue !== undefined && message.boolValue !== false)
writer.uint32(56).bool(message.boolValue)
writer.uint32(56/*= id 7, wireType 0 */).bool(message.boolValue)
return writer
}})($protobuf.Writer, $protobuf.util, [null, null, null, null, null, null, null]); /* eslint-enable */

Expand Down Expand Up @@ -404,17 +404,17 @@ $root.vector_tile = (function() {
Feature.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.id !== undefined && message.id !== null && util.longNe(message.id, 0, 0))
writer.uint32(8).uint64(message.id)
writer.uint32(8/*= id 1, wireType 0 */).uint64(message.id)
if (message.tags && message.tags.length) {
writer.uint32(18).fork()
writer.uint32(18/*= id 2, wireType 2 */).fork()
for (var i = 0; i < message.tags.length; ++i)
writer.uint32(message.tags[i])
writer.ldelim()
}
if (message.type !== undefined && message.type !== "UNKNOWN")
writer.uint32(24).uint32(message.type)
writer.uint32(24/*= id 3, wireType 0 */).uint32(message.type)
if (message.geometry && message.geometry.length) {
writer.uint32(34).fork()
writer.uint32(34/*= id 4, wireType 2 */).fork()
for (var i = 0; i < message.geometry.length; ++i)
writer.uint32(message.geometry[i])
writer.ldelim()
Expand Down Expand Up @@ -604,19 +604,19 @@ $root.vector_tile = (function() {
*/
Layer.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
writer.uint32(120).uint32(message.version)
writer.uint32(10).string(message.name)
writer.uint32(120/*= id 15, wireType 0 */).uint32(message.version)
writer.uint32(10/*= id 1, wireType 2 */).string(message.name)
if (message.features)
for (var i = 0; i < message.features.length; ++i)
types[2].encode(message.features[i], writer.uint32(18).fork()).ldelim()
types[2].encode(message.features[i], writer.uint32(18/*= id 2, wireType 2 */).fork()).ldelim()
if (message.keys)
for (var i = 0; i < message.keys.length; ++i)
writer.uint32(26).string(message.keys[i])
writer.uint32(26/*= id 3, wireType 2 */).string(message.keys[i])
if (message.values)
for (var i = 0; i < message.values.length; ++i)
types[4].encode(message.values[i], writer.uint32(34).fork()).ldelim()
types[4].encode(message.values[i], writer.uint32(34/*= id 4, wireType 2 */).fork()).ldelim()
if (message.extent !== undefined && message.extent !== 4096)
writer.uint32(40).uint32(message.extent)
writer.uint32(40/*= id 5, wireType 0 */).uint32(message.extent)
return writer
}})($protobuf.Writer, $protobuf.util, [null, null, "vector_tile.Tile.Feature", null, "vector_tile.Tile.Value", null]); /* eslint-enable */

Expand Down Expand Up @@ -697,9 +697,9 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.features))
return "invalid value for field .vector_tile.Tile.Layer.features (array expected)"
for (var i = 0; i < message.features.length; ++i) {
var s;
if (s = types[2].verify(message.features[i]))
return s
var reason;
if (reason = types[2].verify(message.features[i]))
return reason
}
}
if (message.keys !== undefined) {
Expand All @@ -714,9 +714,9 @@ $root.vector_tile = (function() {
if (!Array.isArray(message.values))
return "invalid value for field .vector_tile.Tile.Layer.values (array expected)"
for (var i = 0; i < message.values.length; ++i) {
var s;
if (s = types[4].verify(message.values[i]))
return s
var reason;
if (reason = types[4].verify(message.values[i]))
return reason
}
}
if (message.extent !== undefined) {
Expand Down
52 changes: 26 additions & 26 deletions tests/data/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,63 +142,63 @@ $root.Package = (function() {
Package.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.name !== undefined && message.name !== "")
writer.uint32(10).string(message.name)
writer.uint32(10/*= id 1, wireType 2 */).string(message.name)
if (message.version !== undefined && message.version !== "")
writer.uint32(18).string(message.version)
writer.uint32(18/*= id 2, wireType 2 */).string(message.version)
if (message.description !== undefined && message.description !== "")
writer.uint32(26).string(message.description)
writer.uint32(26/*= id 3, wireType 2 */).string(message.description)
if (message.author !== undefined && message.author !== "")
writer.uint32(34).string(message.author)
writer.uint32(34/*= id 4, wireType 2 */).string(message.author)
if (message.license !== undefined && message.license !== "")
writer.uint32(42).string(message.license)
writer.uint32(42/*= id 5, wireType 2 */).string(message.license)
if (message.repository !== undefined && message.repository !== null)
types[5].encode(message.repository, writer.uint32(50).fork()).ldelim()
types[5].encode(message.repository, writer.uint32(50/*= id 6, wireType 2 */).fork()).ldelim()
if (message.bugs !== undefined && message.bugs !== "")
writer.uint32(58).string(message.bugs)
writer.uint32(58/*= id 7, wireType 2 */).string(message.bugs)
if (message.homepage !== undefined && message.homepage !== "")
writer.uint32(66).string(message.homepage)
writer.uint32(66/*= id 8, wireType 2 */).string(message.homepage)
if (message.keywords)
for (var i = 0; i < message.keywords.length; ++i)
writer.uint32(74).string(message.keywords[i])
writer.uint32(74/*= id 9, wireType 2 */).string(message.keywords[i])
if (message.main !== undefined && message.main !== "")
writer.uint32(82).string(message.main)
writer.uint32(82/*= id 10, wireType 2 */).string(message.main)
if (message.bin && message.bin !== util.emptyObject) {
for (var keys = Object.keys(message.bin), i = 0; i < keys.length; ++i) {
writer.uint32(90).fork().uint32(10).string(keys[i])
writer.uint32(18).string(message.bin[keys[i]])
writer.uint32(90/*= id 11, wireType 2 */).fork().uint32(10/*= id 1, wireType 2 */).string(keys[i])
writer.uint32(18/*= id 2, wireType 2 */).string(message.bin[keys[i]])
writer.ldelim()
}
}
if (message.scripts && message.scripts !== util.emptyObject) {
for (var keys = Object.keys(message.scripts), i = 0; i < keys.length; ++i) {
writer.uint32(98).fork().uint32(10).string(keys[i])
writer.uint32(18).string(message.scripts[keys[i]])
writer.uint32(98/*= id 12, wireType 2 */).fork().uint32(10/*= id 1, wireType 2 */).string(keys[i])
writer.uint32(18/*= id 2, wireType 2 */).string(message.scripts[keys[i]])
writer.ldelim()
}
}
if (message.dependencies && message.dependencies !== util.emptyObject) {
for (var keys = Object.keys(message.dependencies), i = 0; i < keys.length; ++i) {
writer.uint32(106).fork().uint32(10).string(keys[i])
writer.uint32(18).string(message.dependencies[keys[i]])
writer.uint32(106/*= id 13, wireType 2 */).fork().uint32(10/*= id 1, wireType 2 */).string(keys[i])
writer.uint32(18/*= id 2, wireType 2 */).string(message.dependencies[keys[i]])
writer.ldelim()
}
}
if (message.optionalDependencies && message.optionalDependencies !== util.emptyObject) {
for (var keys = Object.keys(message.optionalDependencies), i = 0; i < keys.length; ++i) {
writer.uint32(114).fork().uint32(10).string(keys[i])
writer.uint32(18).string(message.optionalDependencies[keys[i]])
writer.uint32(114/*= id 14, wireType 2 */).fork().uint32(10/*= id 1, wireType 2 */).string(keys[i])
writer.uint32(18/*= id 2, wireType 2 */).string(message.optionalDependencies[keys[i]])
writer.ldelim()
}
}
if (message.devDependencies && message.devDependencies !== util.emptyObject) {
for (var keys = Object.keys(message.devDependencies), i = 0; i < keys.length; ++i) {
writer.uint32(122).fork().uint32(10).string(keys[i])
writer.uint32(18).string(message.devDependencies[keys[i]])
writer.uint32(122/*= id 15, wireType 2 */).fork().uint32(10/*= id 1, wireType 2 */).string(keys[i])
writer.uint32(18/*= id 2, wireType 2 */).string(message.devDependencies[keys[i]])
writer.ldelim()
}
}
if (message.types !== undefined && message.types !== "")
writer.uint32(138).string(message.types)
writer.uint32(138/*= id 17, wireType 2 */).string(message.types)
return writer
}})($protobuf.Writer, $protobuf.util, [null, null, null, null, null, "Package.Repository", null, null, null, null, null, null, null, null, null, null]); /* eslint-enable */

Expand Down Expand Up @@ -355,9 +355,9 @@ $root.Package = (function() {
return "invalid value for field .Package.license (string expected)"
}
if (message.repository !== undefined && message.repository !== null) {
var s;
if (s = types[5].verify(message.repository))
return s
var reason;
if (reason = types[5].verify(message.repository))
return reason
}
if (message.bugs !== undefined) {
if (!util.isString(message.bugs))
Expand Down Expand Up @@ -481,9 +481,9 @@ $root.Package = (function() {
Repository.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.type !== undefined && message.type !== "")
writer.uint32(10).string(message.type)
writer.uint32(10/*= id 1, wireType 2 */).string(message.type)
if (message.url !== undefined && message.url !== "")
writer.uint32(18).string(message.url)
writer.uint32(18/*= id 2, wireType 2 */).string(message.url)
return writer
}})($protobuf.Writer, $protobuf.util, [null, null]); /* eslint-enable */

Expand Down
4 changes: 2 additions & 2 deletions tests/data/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ $root.MyRequest = (function() {
MyRequest.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.path !== undefined && message.path !== "")
writer.uint32(10).string(message.path)
writer.uint32(10/*= id 1, wireType 2 */).string(message.path)
return writer
}})($protobuf.Writer, $protobuf.util, [null]); /* eslint-enable */

Expand Down Expand Up @@ -253,7 +253,7 @@ $root.MyResponse = (function() {
MyResponse.encode = /* eslint-disable */ (function(Writer, util, types) { $lazyTypes.push(types); return function encode(message, writer) {
writer || (writer = Writer.create())
if (message.status !== undefined && message.status !== 0)
writer.uint32(16).int32(message.status)
writer.uint32(16/*= id 2, wireType 0 */).int32(message.status)
return writer
}})($protobuf.Writer, $protobuf.util, [null]); /* eslint-enable */

Expand Down
2 changes: 1 addition & 1 deletion tests/data/test.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// $> pbts --out tests/data/test.d.ts --no-comments tests/data/test.js
// Generated Sat, 31 Dec 2016 02:51:42 UTC
// Generated Sat, 31 Dec 2016 03:08:36 UTC

import * as $protobuf from "../..";

Expand Down
Loading

0 comments on commit 0cda72a

Please sign in to comment.