Skip to content

Commit

Permalink
CLI: Slightly beautify statically generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 31, 2016
1 parent 65637ff commit 24b293c
Show file tree
Hide file tree
Showing 10 changed files with 3,960 additions and 5,198 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ Generates TypeScript definitions from annotated JavaScript files.
-o, --out Saves to a file instead of writing to stdout.
-m, --main Whether building the main library without any imports.
-g, --global Name of the global object in browser environments, if any.
--no-comments Does not output any JSDoc comments.
usage: pbts [options] file1.js file2.js ...
Expand Down
4 changes: 4 additions & 0 deletions cli/pbts.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ exports.main = function(args, callback) {
"Generates TypeScript definitions from annotated JavaScript files.",
"",
" -n, --name Wraps everything in a module of the specified name.",
"",
" -o, --out Saves to a file instead of writing to stdout.",
"",
" -m, --main Whether building the main library without any imports.",
"",
" -g, --global Name of the global object in browser environments, if any.",
"",
" --no-comments Does not output any JSDoc comments.",
"",
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..."
Expand Down
46 changes: 30 additions & 16 deletions cli/targets/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,47 @@ function buildNamespace(ref, ns) {
}
}

function slightlyBeautify(generatedCode) {
return 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(/([;,])([^\s])/g, "$1 $2") // cond(var a=0;a<b;++b), var a=1,b;
.replace(/{$/mg, " {")
.replace(/\br\b/g, "reader")
.replace(/\bw\b/g, "writer")
.replace(/\bm\b/g, "message")
.replace(/\bt\b/g, "tag")
.replace(/\bl\b/g, "length")
.replace(/\bc\b/g, "end")
.replace(/\bk\b/g, "key")
.replace(/\bks\b/g, "keys");
}

function buildFunction(type, functionName, gen, scope) {
var lines = gen.str(functionName)
.replace("(this.getCtor())", " $root" + type.fullName)
.split(/\n/g);
push(name(type.name) + "." + functionName + " = (function() {");
++indent;
push("/* eslint-disable */");
Object.keys(scope).forEach(function(key) {
push("var " + key + " = " + scope[key] + ";");
});
push("var types; $lazyTypes.push(types = [" + type.fieldsArray.map(function(field) {
var lines = slightlyBeautify(gen.str(functionName)
.replace("(this.getCtor())", " $root" + type.fullName))
.split(/\n/g);

// add referenced types to scope
scope["types"] = "[" + type.fieldsArray.map(function(field) {
return field.resolve().resolvedType
? JSON.stringify(field.resolvedType.fullName.substring(1))
: "null";
}).join(",") + "]);");
push("return " + lines[0]);
lines.slice(1).forEach(function(line) {
}).join(", ") + "]";

// enclose all but the first and last line in an iife returning our properly scoped function
push(name(type.name) + "." + functionName + " = /* eslint-disable */ (function(" + Object.keys(scope).join(", ") + ") { $lazyTypes.push(types); return " + lines[0]);
lines.slice(1, lines.length - 1).forEach(function(line) {
var prev = indent;
var i = 0;
while (line.charAt(i++) === "\t")
++indent;
push(line.trim());
indent = prev;
});
push("/* eslint-enable */");
--indent;
push("})();");
push("}})(" + Object.keys(scope).map(function(key) { return scope[key]; }).join(", ") + "); /* eslint-enable */");
}

function buildType(ref, type) {
Expand Down
6 changes: 3 additions & 3 deletions src/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ function genVerifyValue(gen, field, fieldIndex, ref) {
("break")
("}");
} else if (field.resolvedType instanceof Type) gen
("var r;")
("if(r=types[%d].verify(%s))", fieldIndex, ref)
("return r");
("var s;")
("if(s=types[%d].verify(%s))", fieldIndex, ref)
("return s");
} else {
switch (field.type) {
case "int32":
Expand Down
146 changes: 53 additions & 93 deletions tests/data/ambiguous-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,12 @@ $root.A = (function() {
* @param {$protobuf.Writer} [writer] Writer to encode to
* @returns {$protobuf.Writer} Writer
*/
A.encode = (function() {
/* eslint-disable */
var Writer = $protobuf.Writer;
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function encode(m, w) {
w||(w=Writer.create())
if(m.whatever!==undefined&&m.whatever!=="")
w.uint32(10).string(m.whatever)
return w
}
/* eslint-enable */
})();
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)
return writer
}})($protobuf.Writer, $protobuf.util, [null]); /* eslint-enable */

/**
* Encodes the specified A, length delimited.
Expand All @@ -80,29 +73,22 @@ $root.A = (function() {
* @param {number} [length] Message length if known beforehand
* @returns {A} A
*/
A.decode = (function() {
/* eslint-disable */
var Reader = $protobuf.Reader;
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function decode(r, l) {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.A
while(r.pos<c){
var t=r.uint32()
switch(t>>>3){
case 1:
m.whatever=r.string()
break
default:
r.skipType(t&7)
break
}
A.decode = /* eslint-disable */ (function(Reader, util, types) { $lazyTypes.push(types); return function decode(reader, length) {
reader instanceof Reader || (reader = Reader.create(reader))
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.A
while (reader.pos < end) {
var tag = reader.uint32()
switch (tag >>> 3) {
case 1:
message.whatever = reader.string()
break
default:
reader.skipType(tag & 7)
break
}
return m
}
/* eslint-enable */
})();
return message
}})($protobuf.Reader, $protobuf.util, [null]); /* eslint-enable */

/**
* Decodes a A from the specified reader or buffer, length delimited.
Expand All @@ -120,19 +106,13 @@ $root.A = (function() {
* @param {A|Object} message A or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
A.verify = (function() {
/* eslint-disable */
var util = $protobuf.util;
var types; $lazyTypes.push(types = [null]);
return function verify(m) {
if(m.whatever!==undefined){
if(!util.isString(m.whatever))
return"invalid value for field .A.whatever (string expected)"
}
return null
A.verify = /* eslint-disable */ (function(util, types) { $lazyTypes.push(types); return function verify(message) {
if (message.whatever !== undefined) {
if (!util.isString(message.whatever))
return "invalid value for field .A.whatever (string expected)"
}
/* eslint-enable */
})();
return null
}})($protobuf.util, [null]); /* eslint-enable */

return A;
})();
Expand Down Expand Up @@ -178,19 +158,12 @@ $root.B = (function() {
* @param {$protobuf.Writer} [writer] Writer to encode to
* @returns {$protobuf.Writer} Writer
*/
B.encode = (function() {
/* eslint-disable */
var Writer = $protobuf.Writer;
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function encode(m, w) {
w||(w=Writer.create())
if(m.A!==undefined&&m.A!==null)
types[0].encode(m.A,w.uint32(10).fork()).ldelim()
return w
}
/* eslint-enable */
})();
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()
return writer
}})($protobuf.Writer, $protobuf.util, ["A"]); /* eslint-enable */

/**
* Encodes the specified B, length delimited.
Expand All @@ -209,29 +182,22 @@ $root.B = (function() {
* @param {number} [length] Message length if known beforehand
* @returns {B} B
*/
B.decode = (function() {
/* eslint-disable */
var Reader = $protobuf.Reader;
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function decode(r, l) {
r instanceof Reader||(r=Reader.create(r))
var c=l===undefined?r.len:r.pos+l,m=new $root.B
while(r.pos<c){
var t=r.uint32()
switch(t>>>3){
case 1:
m.A=types[0].decode(r,r.uint32())
break
default:
r.skipType(t&7)
break
}
B.decode = /* eslint-disable */ (function(Reader, util, types) { $lazyTypes.push(types); return function decode(reader, length) {
reader instanceof Reader || (reader = Reader.create(reader))
var end = length === undefined ? reader.len : reader.pos + length, message = new $root.B
while (reader.pos < end) {
var tag = reader.uint32()
switch (tag >>> 3) {
case 1:
message.A = types[0].decode(reader, reader.uint32())
break
default:
reader.skipType(tag & 7)
break
}
return m
}
/* eslint-enable */
})();
return message
}})($protobuf.Reader, $protobuf.util, ["A"]); /* eslint-enable */

/**
* Decodes a B from the specified reader or buffer, length delimited.
Expand All @@ -249,20 +215,14 @@ $root.B = (function() {
* @param {B|Object} message B or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
*/
B.verify = (function() {
/* eslint-disable */
var util = $protobuf.util;
var types; $lazyTypes.push(types = ["A"]);
return function verify(m) {
if(m.A!==undefined&&m.A!==null){
var r;
if(r=types[0].verify(m.A))
return r
}
return null
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
}
/* eslint-enable */
})();
return null
}})($protobuf.util, ["A"]); /* eslint-enable */

return B;
})();
Expand Down
Loading

0 comments on commit 24b293c

Please sign in to comment.