From a1c3c17b8484f863d022be272794fb7211ac6f0d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 27 Apr 2012 17:40:56 -0700 Subject: [PATCH] jsstyle clean --- Makefile | 4 ++-- test/cli.test.js | 32 +++++++++++++++++++++++-------- test/ctor.test.js | 43 ++++++++++++++++++++++++++++++----------- tools/jsstyle | 49 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 96 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 42b2b68..d1f8c1d 100644 --- a/Makefile +++ b/Makefile @@ -57,12 +57,12 @@ test06: .PHONY: check-jsstyle 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 check: check-jsstyle @echo "Check ok." .PHONY: prepush -prepush: check test +prepush: check testall @echo "Okay to push." diff --git a/test/cli.test.js b/test/cli.test.js index c6fee59..bf1fe9e 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -57,7 +57,9 @@ test('--bogus', function (t) { test('simple.log', function (t) { exec(BUNYAN + ' corpus/simple.log', function (err, stdout, stderr) { 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(); }); }); @@ -65,7 +67,9 @@ test('cat simple.log', function (t) { exec(format('cat corpus/simple.log | %s', BUNYAN), function (err, stdout, stderr) { 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(); } ); @@ -73,7 +77,9 @@ test('cat simple.log', function (t) { test('simple.log with color', function (t) { exec(BUNYAN + ' --color corpus/simple.log', function (err, stdout, stderr) { 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(); }); }); @@ -81,14 +87,20 @@ test('simple.log with color', function (t) { test('extrafield.log', function (t) { exec(BUNYAN + ' corpus/extrafield.log', function (err, stdout, stderr) { 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(); }); }); 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.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(); }); }); @@ -122,13 +134,17 @@ test('simple.log doesnotexist1.log doesnotexist2.log', function (t) { function (err, stdout, stderr) { t.ok(err) 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: // ENOENT, no such file or directory 'asdf.log' // but node v0.6.14: // ENOENT, open 'asdf.log' // 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(); } ); diff --git a/test/ctor.test.js b/test/ctor.test.js index abbfc20..9db59be 100644 --- a/test/ctor.test.js +++ b/test/ctor.test.js @@ -38,12 +38,18 @@ test('ensure Logger creation options', function (t) { options = {name: 'foo', serializers: 'a string'}; 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'); - options = {name: 'foo', serializers: [1,2,3]}; + options = {name: 'foo', serializers: [1, 2, 3]}; 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'); t.end(); @@ -78,12 +84,18 @@ test('ensure Logger creation options (createLogger)', function (t) { options = {name: 'foo', serializers: 'a string'}; 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'); - options = {name: 'foo', serializers: [1,2,3]}; + options = {name: 'foo', serializers: [1, 2, 3]}; 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'); t.end(); @@ -100,7 +112,10 @@ test('ensure Logger child() options', function (t) { 'empty options should be fine too'); 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'); 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'}, '"streams" must be an array'); - options = {serializers: "a string"}; + options = {serializers: 'a string'}; 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'); - options = {serializers: [1,2,3]}; + options = {serializers: [1, 2, 3]}; 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'); t.end(); diff --git a/tools/jsstyle b/tools/jsstyle index c0df3a9..9df33c9 100755 --- a/tools/jsstyle +++ b/tools/jsstyle @@ -64,11 +64,11 @@ Report bugs to . Options: -h print this help and exit -v verbose - + -c check continuation indentation inside functions -t specify tab width for line length calculation -C don't check anything in header block comments - + -f PATH path to a jsstyle config file -o OPTION1,OPTION2 @@ -107,6 +107,9 @@ my %config = ( "unparenthesized-return" => 1, "literal-string-quote" => "single", # 'single' or 'double' "blank-after-start-comment" => 1, + "continuation-at-front" => 0, + "leading-right-paren-ok" => 0, + "strict-indent" => 0 ); sub add_config_var ($$) { my ($scope, $str) = @_; @@ -128,7 +131,14 @@ sub add_config_var ($$) { } elsif ($name eq "doxygen" || # boolean vars $name eq "splint" || $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") { + if ($value != 1 && $value != 0) { die "$scope: invalid '$name': don't give a value"; } @@ -483,7 +493,11 @@ line: while (<$filehandle>) { my $indent = $1; if (length($indent) < $config{"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/) { @@ -566,17 +580,26 @@ line: while (<$filehandle>) { !/\/.*?\{\d+,?\d*\}.*?\//) { err("comma or semicolon followed by non-blank"); } - # allow "for" statements to have empty "while" clauses - if (/\s[,;]/ && !/^[\t]+;$/ && !/^\s*for \([^;]*; ;[^;]*\)/) { - err("comma or semicolon preceded by blank"); + # check for commas preceded by blanks + if ((!$config{"leading-comma-ok"} && /^\s*,/) || (!/^\s*,/ && /\s,/)) { + 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"); + } elsif ($config{"continuation-at-front"} && /(&&|\|\||\+)$/) { + err("improper continuation"); } if (/\S *(&&|\|\|)/ || /(&&|\|\|) *\S/) { 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 (/(?) { if (/\btypeof\b/ && !/\btypeof\s*\(.*\)/) { err("unparenthesized typeof expression"); } - if (/\(\s/) { + if (!$config{"whitespace-after-left-paren-ok"} && /\(\s/) { err("whitespace after left paren"); } # allow "for" statements to have empty "continue" clauses 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\)[^ ]/) { err("missing space after (void) cast"); @@ -654,7 +681,7 @@ line: while (<$filehandle>) { } if (/^\s*else\W/) { - if ($prev =~ /^\s*}$/) { + if (!$config{"uncuddled-else-ok"} && $prev =~ /^\s*}$/) { err_prefix($prev, "else and right brace should be on same line"); }