jsstyle clean

This commit is contained in:
Trent Mick 2012-04-27 17:40:56 -07:00
parent 1a916f6ad7
commit a1c3c17b84
4 changed files with 96 additions and 32 deletions

View file

@ -57,12 +57,12 @@ test06:
.PHONY: check-jsstyle .PHONY: check-jsstyle
check-jsstyle: $(JSSTYLE_FILES) check-jsstyle: $(JSSTYLE_FILES)
./tools/jsstyle -o indent=2,doxygen,unparenthesized-return=0,blank-after-start-comment=0 $(JSSTYLE_FILES) ./tools/jsstyle -o indent=2,doxygen,unparenthesized-return=0,blank-after-start-comment=0,leading-right-paren-ok $(JSSTYLE_FILES)
.PHONY: check .PHONY: check
check: check-jsstyle check: check-jsstyle
@echo "Check ok." @echo "Check ok."
.PHONY: prepush .PHONY: prepush
prepush: check test prepush: check testall
@echo "Okay to push." @echo "Okay to push."

View file

@ -57,7 +57,9 @@ test('--bogus', function (t) {
test('simple.log', function (t) { test('simple.log', function (t) {
exec(BUNYAN + ' corpus/simple.log', function (err, stdout, stderr) { exec(BUNYAN + ' corpus/simple.log', function (err, stdout, stderr) {
t.error(err) t.error(err)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: My message\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: '
+ 'My message\n');
t.end(); t.end();
}); });
}); });
@ -65,7 +67,9 @@ test('cat simple.log', function (t) {
exec(format('cat corpus/simple.log | %s', BUNYAN), exec(format('cat corpus/simple.log | %s', BUNYAN),
function (err, stdout, stderr) { function (err, stdout, stderr) {
t.error(err) t.error(err)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: My message\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: '
+ 'My message\n');
t.end(); t.end();
} }
); );
@ -73,7 +77,9 @@ test('cat simple.log', function (t) {
test('simple.log with color', function (t) { test('simple.log with color', function (t) {
exec(BUNYAN + ' --color corpus/simple.log', function (err, stdout, stderr) { exec(BUNYAN + ' --color corpus/simple.log', function (err, stdout, stderr) {
t.error(err) t.error(err)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] \u001b[36m INFO\u001b[39m: myservice/123 on example.com: \u001b[36mMy message\u001b[39m\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] \u001b[36m INFO\u001b[39m: myservice/123 '
+ 'on example.com: \u001b[36mMy message\u001b[39m\n');
t.end(); t.end();
}); });
}); });
@ -81,14 +87,20 @@ test('simple.log with color', function (t) {
test('extrafield.log', function (t) { test('extrafield.log', function (t) {
exec(BUNYAN + ' corpus/extrafield.log', function (err, stdout, stderr) { exec(BUNYAN + ' corpus/extrafield.log', function (err, stdout, stderr) {
t.error(err) t.error(err)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: My message (extra=field)\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: '
+ 'My message (extra=field)\n');
t.end(); t.end();
}); });
}); });
test('extrafield.log with color', function (t) { test('extrafield.log with color', function (t) {
exec(BUNYAN + ' --color corpus/extrafield.log', function (err, stdout, stderr) { exec(BUNYAN + ' --color corpus/extrafield.log',
function (err, stdout, stderr) {
t.error(err) t.error(err)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] \u001b[36m INFO\u001b[39m: myservice/123 on example.com: \u001b[36mMy message\u001b[39m\u001b[90m (extra=field)\u001b[39m\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] \u001b[36m INFO\u001b[39m: myservice/123 '
+ 'on example.com: \u001b[36mMy message\u001b[39m'
+ '\u001b[90m (extra=field)\u001b[39m\n');
t.end(); t.end();
}); });
}); });
@ -122,13 +134,17 @@ test('simple.log doesnotexist1.log doesnotexist2.log', function (t) {
function (err, stdout, stderr) { function (err, stdout, stderr) {
t.ok(err) t.ok(err)
t.equal(err.code, 2) t.equal(err.code, 2)
t.equal(stdout, '[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: My message\n'); t.equal(stdout,
'[2012-02-08T22:56:52.856Z] INFO: myservice/123 on example.com: '
+ 'My message\n');
// Note: node v0.6.10: // Note: node v0.6.10:
// ENOENT, no such file or directory 'asdf.log' // ENOENT, no such file or directory 'asdf.log'
// but node v0.6.14: // but node v0.6.14:
// ENOENT, open 'asdf.log' // ENOENT, open 'asdf.log'
// Somewhat annoying change. // Somewhat annoying change.
t.equal(stderr, "bunyan: ENOENT, open 'doesnotexist1.log'\nbunyan: ENOENT, open 'doesnotexist2.log'\n"); t.equal(stderr,
'bunyan: ENOENT, open \'doesnotexist1.log\'\nbunyan: ENOENT, '
+ 'open \'doesnotexist2.log\'\n');
t.end(); t.end();
} }
); );

View file

@ -38,12 +38,18 @@ test('ensure Logger creation options', function (t) {
options = {name: 'foo', serializers: 'a string'}; options = {name: 'foo', serializers: 'a string'};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {name: 'foo', serializers: [1,2,3]}; options = {name: 'foo', serializers: [1, 2, 3]};
t.throws(function () { new Logger(options); }, t.throws(function () { new Logger(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();
@ -78,12 +84,18 @@ test('ensure Logger creation options (createLogger)', function (t) {
options = {name: 'foo', serializers: 'a string'}; options = {name: 'foo', serializers: 'a string'};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {name: 'foo', serializers: [1,2,3]}; options = {name: 'foo', serializers: [1, 2, 3]};
t.throws(function () { bunyan.createLogger(options); }, t.throws(function () { bunyan.createLogger(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();
@ -100,7 +112,10 @@ test('ensure Logger child() options', function (t) {
'empty options should be fine too'); 'empty options should be fine too');
t.throws(function () { log.child({name: 'foo'}); }, t.throws(function () { log.child({name: 'foo'}); },
{name: 'TypeError', message: 'invalid options.name: child cannot set logger name'}, {
name: 'TypeError',
message: 'invalid options.name: child cannot set logger name'
},
'child cannot change name'); 'child cannot change name');
var options = {stream: process.stdout, streams: []}; var options = {stream: process.stdout, streams: []};
@ -117,14 +132,20 @@ test('ensure Logger child() options', function (t) {
{name: 'TypeError', message: 'invalid options.streams: must be an array'}, {name: 'TypeError', message: 'invalid options.streams: must be an array'},
'"streams" must be an array'); '"streams" must be an array');
options = {serializers: "a string"}; options = {serializers: 'a string'};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be a string'); '"serializers" cannot be a string');
options = {serializers: [1,2,3]}; options = {serializers: [1, 2, 3]};
t.throws(function () { log.child(options); }, t.throws(function () { log.child(options); },
{name: 'TypeError', message: 'invalid options.serializers: must be an object'}, {
name: 'TypeError',
message: 'invalid options.serializers: must be an object'
},
'"serializers" cannot be an array'); '"serializers" cannot be an array');
t.end(); t.end();

View file

@ -107,6 +107,9 @@ my %config = (
"unparenthesized-return" => 1, "unparenthesized-return" => 1,
"literal-string-quote" => "single", # 'single' or 'double' "literal-string-quote" => "single", # 'single' or 'double'
"blank-after-start-comment" => 1, "blank-after-start-comment" => 1,
"continuation-at-front" => 0,
"leading-right-paren-ok" => 0,
"strict-indent" => 0
); );
sub add_config_var ($$) { sub add_config_var ($$) {
my ($scope, $str) = @_; my ($scope, $str) = @_;
@ -128,7 +131,14 @@ sub add_config_var ($$) {
} elsif ($name eq "doxygen" || # boolean vars } elsif ($name eq "doxygen" || # boolean vars
$name eq "splint" || $name eq "splint" ||
$name eq "unparenthesized-return" || $name eq "unparenthesized-return" ||
$name eq "continuation-at-front" ||
$name eq "leading-right-paren-ok" ||
$name eq "leading-comma-ok" ||
$name eq "uncuddled-else-ok" ||
$name eq "whitespace-after-left-paren-ok" ||
$name eq "strict-indent" ||
$name eq "blank-after-start-comment") { $name eq "blank-after-start-comment") {
if ($value != 1 && $value != 0) { if ($value != 1 && $value != 0) {
die "$scope: invalid '$name': don't give a value"; die "$scope: invalid '$name': don't give a value";
} }
@ -483,7 +493,11 @@ line: while (<$filehandle>) {
my $indent = $1; my $indent = $1;
if (length($indent) < $config{"indent"}) { if (length($indent) < $config{"indent"}) {
err("indent of " . length($indent) . err("indent of " . length($indent) .
" space(s) instead of " . $config{'indent'}); " space(s) instead of " . $config{"indent"});
} elsif ($config{"strict-indent"} &&
length($indent) % $config{"indent"} != 0) {
err("indent is " . length($indent) .
" not a multiple of " . $config{'indent'} . " spaces");
} }
} }
if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) { if (/^\t+ [^ \t\*]/ || /^\t+ \S/ || /^\t+ \S/) {
@ -566,17 +580,26 @@ line: while (<$filehandle>) {
!/\/.*?\{\d+,?\d*\}.*?\//) { !/\/.*?\{\d+,?\d*\}.*?\//) {
err("comma or semicolon followed by non-blank"); err("comma or semicolon followed by non-blank");
} }
# allow "for" statements to have empty "while" clauses # check for commas preceded by blanks
if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) { if ((!$config{"leading-comma-ok"} && /^\s*,/) || (!/^\s*,/ && /\s,/)) {
err("comma or semicolon preceded by blank"); err("comma preceded by blank");
} }
if (/^\s*(&&|\|\|)/) { # check for semicolons preceded by blanks
# allow "for" statements to have empty "while" clauses
if (/\s;/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) {
err("semicolon preceded by blank");
}
if (!$config{"continuation-at-front"} && /^\s*(&&|\|\|)/) {
err("improper boolean continuation"); err("improper boolean continuation");
} elsif ($config{"continuation-at-front"} && /(&&|\|\||\+)$/) {
err("improper continuation");
} }
if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) { if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) {
err("more than one space around boolean operator"); err("more than one space around boolean operator");
} }
if (/\b(delete|typeof|instanceOf|throw|with|catch|new|function|in|for|if|while|switch|return|case)\(/) { # We allow methods which look like obj.delete() but not keywords without
# spaces ala: delete(obj)
if (/(?<!\.)\b(delete|typeof|instanceof|throw|with|catch|new|function|in|for|if|while|switch|return|case)\(/) {
err("missing space between keyword and paren"); err("missing space between keyword and paren");
} }
if (/(\b(catch|for|if|with|while|switch|return)\b.*){2,}/) { if (/(\b(catch|for|if|with|while|switch|return)\b.*){2,}/) {
@ -612,12 +635,16 @@ line: while (<$filehandle>) {
if (/\btypeof\b/ && !/\btypeof\s*\(.*\)/) { if (/\btypeof\b/ && !/\btypeof\s*\(.*\)/) {
err("unparenthesized typeof expression"); err("unparenthesized typeof expression");
} }
if (/\(\s/) { if (!$config{"whitespace-after-left-paren-ok"} && /\(\s/) {
err("whitespace after left paren"); err("whitespace after left paren");
} }
# allow "for" statements to have empty "continue" clauses # allow "for" statements to have empty "continue" clauses
if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) { if (/\s\)/ && !/^\s*for \([^;]*;[^;]*; \)/) {
err("whitespace before right paren"); if ($config{"leading-right-paren-ok"} && /^\s+\)/) {
# this is allowed
} else {
err("whitespace before right paren");
}
} }
if (/^\s*\(void\)[^ ]/) { if (/^\s*\(void\)[^ ]/) {
err("missing space after (void) cast"); err("missing space after (void) cast");
@ -654,7 +681,7 @@ line: while (<$filehandle>) {
} }
if (/^\s*else\W/) { if (/^\s*else\W/) {
if ($prev =~ /^\s*}$/) { if (!$config{"uncuddled-else-ok"} && $prev =~ /^\s*}$/) {
err_prefix($prev, err_prefix($prev,
"else and right brace should be on same line"); "else and right brace should be on same line");
} }