Skip to content

Commit 6e81fcb

Browse files
committed
Other: Also lint cli utilities
1 parent 26d9fad commit 6e81fcb

35 files changed

+379
-288
lines changed

.codeclimate.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ engines:
44
ratings:
55
paths:
66
- "src/**.js"
7+
- "cli/**.js"
78
- "lib/aspromise/**.js"
89
- "lib/base64/**.js"
910
- "lib/eventemitter/**.js"
@@ -14,3 +15,4 @@ ratings:
1415
- "lib/utf8/**.js"
1516
exclude_paths:
1617
- "**/tests/**"
18+
- "cli/wrappers/**"

.eslintignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
bench/*
22
bin/*
3-
cli/*
3+
cli/wrappers/*
44
coverage/*
55
dist/*
66
docs/*
@@ -10,6 +10,7 @@ lib/prelude.js
1010
lib/polyfill.js
1111
lib/tape-adapter.js
1212
lib/tsd-jsdoc/*
13+
lib/*/tests/*
1314
sandbox/*
1415
scripts/*
15-
**/tests/*
16+
tests/*

cli/pbjs.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var targets = util.requireAll("./targets");
1919
* @param {function(?Error)} [callback] Optional completion callback
2020
* @returns {number|undefined} Exit code, if known
2121
*/
22-
exports.main = function(args, callback) {
22+
exports.main = function main(args, callback) {
2323
var lintDefault = "eslint-disable block-scoped-var, no-redeclare, no-control-regex, no-prototype-builtins";
2424
var argv = minimist(args, {
2525
alias: {
@@ -59,16 +59,16 @@ exports.main = function(args, callback) {
5959
return " " + util.pad(key, 14, true) + targets[key].description;
6060
});
6161
if (callback)
62-
callback(Error("usage"));
62+
callback(Error("usage")); // eslint-disable-line callback-return
6363
else
64-
console.error([
64+
process.stderr.write([
6565
"protobuf.js v" + pkg.version + " CLI for JavaScript",
6666
"",
6767
chalk.bold.white("Consolidates imports and converts between file formats."),
6868
"",
6969
" -t, --target Specifies the target format. Also accepts a path to require a custom target.",
7070
"",
71-
descs.join('\n'),
71+
descs.join("\n"),
7272
"",
7373
" -p, --path Adds a directory to the include path.",
7474
"",
@@ -184,8 +184,10 @@ exports.main = function(args, callback) {
184184
}
185185
callTarget();
186186
} catch (err) {
187-
if (callback)
188-
return callback(err);
187+
if (callback) {
188+
callback(err);
189+
return;
190+
}
189191
throw err;
190192
}
191193
});
@@ -280,4 +282,6 @@ exports.main = function(args, callback) {
280282
: undefined;
281283
});
282284
}
285+
286+
return undefined;
283287
};

cli/pbts.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ exports.main = function(args, callback) {
3838

3939
if (!files.length) {
4040
if (callback)
41-
callback(Error("usage"));
41+
callback(Error("usage")); // eslint-disable-line callback-return
4242
else
43-
console.error([
43+
process.stderr.write([
4444
"protobuf.js v" + pkg.version + " CLI for TypeScript",
4545
"",
4646
chalk.bold.white("Generates TypeScript definitions from annotated JavaScript files."),
@@ -59,8 +59,6 @@ exports.main = function(args, callback) {
5959
"",
6060
"usage: " + chalk.bold.green("pbts") + " [options] file1.js file2.js ..." + chalk.bold.gray(" (or) ") + "other | " + chalk.bold.green("pbts") + " [options] -"
6161
].join("\n"));
62-
if (callback)
63-
callback(Error("usage"));
6462
return 1;
6563
}
6664

@@ -113,17 +111,17 @@ exports.main = function(args, callback) {
113111
child.stderr.pipe(process.stderr);
114112
child.on("close", function(code) {
115113
// clean up temporary files, no matter what
116-
try { cleanup.forEach(fs.unlinkSync); } catch(e) {} cleanup = [];
114+
try { cleanup.forEach(fs.unlinkSync); } catch(e) {/**/} cleanup = [];
117115

118116
if (code) {
119-
out = out.join('').replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
117+
out = out.join("").replace(/\s*JSDoc \d+\.\d+\.\d+ [^$]+/, "");
120118
process.stderr.write(out);
121119
var err = Error("code " + code);
122-
if (callback)
120+
if (callback) {
123121
callback(err);
124-
else
125-
throw err;
126-
return;
122+
return;
123+
}
124+
throw err;
127125
}
128126

129127
var output = [];
@@ -137,20 +135,21 @@ exports.main = function(args, callback) {
137135
"import * as $protobuf from \"protobufjs\";",
138136
""
139137
);
140-
output = output.join('\n') + "\n" + out.join('');
138+
output = output.join("\n") + "\n" + out.join("");
141139

142140
try {
143141
if (argv.out)
144142
fs.writeFileSync(argv.out, output);
145143
else
146144
process.stdout.write(output, "utf8");
147145
if (callback)
148-
callback(null);
146+
callback(null); // eslint-disable-line callback-return
149147
} catch (err) {
150-
if (callback)
148+
if (callback) {
151149
callback(err);
152-
else
153-
throw err;
150+
return;
151+
}
152+
throw err;
154153
}
155154
});
156155
}

cli/targets/json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
module.exports = json_target;
33

4-
json_target.description = "JSON representation"
4+
json_target.description = "JSON representation";
55

66
function json_target(root, options, callback) {
77
callback(null, JSON.stringify(root, null, 2));

cli/targets/proto.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ function proto_target(root, options, callback) {
4646
first = false;
4747
try {
4848
buildRoot(root);
49-
callback(null, out.join('\n'));
49+
return callback(null, out.join("\n"));
5050
} catch (err) {
51-
callback(err);
51+
return callback(err);
5252
} finally {
5353
out = [];
5454
syntax = 3;
@@ -67,18 +67,18 @@ function push(line) {
6767
}
6868

6969
function escape(str) {
70-
return str.replace(/[\\"']/g, '\\$&')
71-
.replace(/\u0000/g, '\\0');
70+
return str.replace(/[\\"']/g, "\\$&")
71+
.replace(/\u0000/g, "\\0");
7272
}
7373

7474
function value(v) {
7575
switch (typeof v) {
76-
case 'boolean':
77-
return v ? 'true' : 'false';
78-
case 'number':
76+
case "boolean":
77+
return v ? "true" : "false";
78+
case "number":
7979
return v.toString();
8080
default:
81-
return '"' + escape(v + '') + '"';
81+
return "\"" + escape(String(v)) + "\"";
8282
}
8383
}
8484

@@ -178,10 +178,12 @@ function buildType(type) {
178178
}
179179

180180
function buildField(field, passExtend) {
181-
if (field.partOf || field.declaringField || (field.extend !== undefined && !passExtend))
181+
if (field.partOf || field.declaringField || !(field.extend === undefined || passExtend))
182182
return;
183-
if (first)
184-
first = false, push("");
183+
if (first) {
184+
first = false;
185+
push("");
186+
}
185187
if (field.resolvedType && field.resolvedType.group) {
186188
buildGroup(field);
187189
return;
@@ -277,8 +279,10 @@ function buildOneOf(oneof) {
277279
++indent; first = true;
278280
oneof.oneof.forEach(function(fieldName) {
279281
var field = oneof.parent.get(fieldName);
280-
if (first)
281-
push(""), first = false;
282+
if (first) {
283+
first = false;
284+
push("");
285+
}
282286
var opts = buildFieldOptions(field);
283287
push(field.type + " " + underScore(field.name) + " = " + field.id + (opts ? " " + opts : "") + ";");
284288
});
@@ -301,11 +305,13 @@ function buildMethod(method) {
301305

302306
function buildOptions(object) {
303307
if (!object.options)
304-
return
308+
return;
305309
first = true;
306310
Object.keys(object.options).forEach(function(key) {
307-
if (first)
308-
push(""), first = false;
311+
if (first) {
312+
first = false;
313+
push("");
314+
}
309315
var val = object.options[key];
310316
push("option " + key + " = " + JSON.stringify(val) + ";");
311317
});

cli/targets/static-module.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ static_module_target.description = "Static code without reflection as a module";
1414

1515
function static_module_target(root, options, callback) {
1616
require("./static")(root, options, function(err, output) {
17-
if (err)
18-
return callback(err);
17+
if (err) {
18+
callback(err);
19+
return;
20+
}
1921
try {
2022
output = util.wrap(output, protobuf.util.merge({ dependency: "protobufjs/minimal" }, options));
2123
} catch (e) {

cli/util.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
22
var fs = require("fs"),
33
path = require("path"),
4-
child_process = require("child_process"),
5-
semver;
4+
child_process = require("child_process");
5+
6+
var semver;
67

78
var protobuf = require("..");
89

@@ -18,7 +19,7 @@ function basenameCompare(a, b) {
1819
return 1;
1920
}
2021
return a.length < b.length ? -1 : 0;
21-
};
22+
}
2223

2324
exports.requireAll = function requireAll(dirname) {
2425
dirname = path.join(__dirname, dirname);
@@ -110,7 +111,7 @@ function modExists(name, version) {
110111
return semver
111112
? semver.satisfies(pkg.version, version)
112113
: parseInt(pkg.version, 10) === parseInt(version.replace(/^[\^~]/, ""), 10); // used for semver only
113-
} catch (e) {}
114+
} catch (e) {/**/}
114115
}
115116
return false;
116117
}
@@ -156,8 +157,8 @@ exports.wrap = function(OUTPUT, options) {
156157
// otherwise fetch the custom one
157158
wrap = fs.readFileSync(path.resolve(process.cwd(), name)).toString("utf8");
158159
}
159-
wrap = wrap.replace(/%DEPENDENCY%/g, JSON.stringify(options.dependency || "protobufjs"));
160-
wrap = wrap.replace(/( *)%OUTPUT%/, function($0, $1) {
160+
wrap = wrap.replace(/\$DEPENDENCY/g, JSON.stringify(options.dependency || "protobufjs"));
161+
wrap = wrap.replace(/( *)\$OUTPUT;/, function($0, $1) {
161162
return $1.length ? OUTPUT.replace(/^/mg, $1) : OUTPUT;
162163
});
163164
if (options.lint !== "")
@@ -183,7 +184,7 @@ exports.safeProp = protobuf.util.safeProp = (function(safeProp) {
183184
return !/^[$\w]+$/.test(name) || exports.reserved(name)
184185
? safeProp(name)
185186
: "." + name;
186-
}
187+
};
187188
})(protobuf.util.safeProp);
188189

189190
exports.jsonSafeProp = function(json) {

cli/wrappers/amd.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
define(["protobuf"], function($protobuf) {
22
"use strict";
33

4-
%OUTPUT%
4+
$OUTPUT;
55

66
return $root;
77
});

cli/wrappers/commonjs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

3-
var $protobuf = require(%DEPENDENCY%);
3+
var $protobuf = require($DEPENDENCY);
44

5-
%OUTPUT%
5+
$OUTPUT;
66

77
module.exports = $root;

cli/wrappers/default.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
define(["protobuf"], factory);
55

66
/* CommonJS */ else if (typeof require === 'function' && typeof module === 'object' && module && module.exports)
7-
module.exports = factory(require(%DEPENDENCY%));
7+
module.exports = factory(require($DEPENDENCY));
88

99
})(this, function($protobuf) {
1010
"use strict";
1111

12-
%OUTPUT%
12+
$OUTPUT;
1313

1414
return $root;
1515
});

cli/wrappers/es6.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as $protobuf from "protobufjs";
22

3-
%OUTPUT%
3+
$OUTPUT;
44

55
export { $root as default };

dist/light/protobuf.js

+1-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/light/protobuf.min.js.gz

-14 Bytes
Binary file not shown.

dist/light/protobuf.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)