From 828be55a4544e2e216b3bfe17affaaa398511a72 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 16 Dec 2014 23:29:14 +0000 Subject: [PATCH 1/2] Source code formatting * .editorconfig settings * new `gulp jscs` task to check JS code style * both `jshint` and `jscs` are now being run before `default` build task * pre-commit hook to check code format before committing to `master` Closes #31 --- .editorconfig | 11 +++++++++++ .git-hooks/pre-commit | 18 ++++++++++++++++++ .jscsrc | 26 ++++++++++++++++++++++++++ README.md | 15 ++++++++++++++- gulpfile.js | 11 +++++++++-- package.json | 3 ++- 6 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 .editorconfig create mode 100755 .git-hooks/pre-commit create mode 100644 .jscsrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..f5207af2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + diff --git a/.git-hooks/pre-commit b/.git-hooks/pre-commit new file mode 100755 index 00000000..8cb91aca --- /dev/null +++ b/.git-hooks/pre-commit @@ -0,0 +1,18 @@ +#!/bin/bash + +if [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]; then + exit 0; +else + gulp jshint &> /dev/null + if [ "$?" != "0" ]; then + echo "Found some JS errors. Check with 'gulp jshint' before committing." + exit 1; + fi + + gulp jscs &> /dev/null + if [ "$?" != "0" ]; then + echo "Unstyled JS. Check with 'gulp jscs' before committing." + exit 1; + fi +fi + diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 00000000..6128bee7 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,26 @@ +{ + "requireCurlyBraces": ["for", "while", "do", "try", "catch"], + "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"], + "requireSpacesInFunctionExpression": { + "beforeOpeningCurlyBrace": true + }, + "disallowMultipleVarDecl": "exceptUndefined", + "requireSpacesInsideObjectBrackets": "allButNested", + "disallowSpacesInsideArrayBrackets": true, + "disallowSpacesInsideParentheses": true, + "disallowSpaceAfterObjectKeys": true, + "disallowQuotedKeysInObjects": true, + "requireSpaceBeforeBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], + "disallowSpaceAfterBinaryOperators": ["!"], + "requireSpaceAfterBinaryOperators": ["?", ",", "+", "/", "*", ":", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="], + "disallowSpaceBeforeBinaryOperators": [","], + "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"], + "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], + "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], + "requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], + "disallowImplicitTypeConversion": ["numeric", "binary", "string"], + "disallowKeywords": ["with", "eval"], + "disallowMultipleLineBreaks": true, + "requireLineFeedAtFileEnd": true, + "validateIndentation": 2 +} diff --git a/README.md b/README.md index b1416acb..1e2c0e84 100755 --- a/README.md +++ b/README.md @@ -7,6 +7,17 @@ 3. `bower install` 4. `npm install` +If you plan on modifying source code, be a good citizen and: + +1. Install [EditorConfig plugin](http://editorconfig.org/#download) for your favourite browser. + The plugin should automatically pick up the [.editorconfig](.editorconfig) settings. +2. Add pre-commit git hook: + ``` + cp .git-hooks/pre-commit .git/hooks && chmod +x .git/hooks/pre-commit + ``` + + This will check for JS errors and code style before committing to the `master` branch. + ### Running Start a web server in `app/` or server via App Engine dev server. @@ -15,4 +26,6 @@ Start a web server in `app/` or server via App Engine dev server. ### Building -Run `gulp`. Then hit `http://localhost:/dist/app/`. The unbuilt version is still viewable at `http://localhost:/app/` but will not contain minfied JS or vulcanized HTML Imports. +Run `gulp`. Then hit `http://localhost:/dist/app/`. The unbuilt version is still viewable at `http://localhost:/app/` but will not contain minfied JS or vulcanized HTML Imports. + +**Note**: Build won't succeed if either `gulp jshint` or `gulp jscs` reports errors. diff --git a/gulpfile.js b/gulpfile.js index d6845462..b16d7249 100755 --- a/gulpfile.js +++ b/gulpfile.js @@ -151,13 +151,20 @@ gulp.task('vulcanize-elements', ['clean', 'compass'], function() { // Lint JavaScript gulp.task('jshint', function() { - return gulp.src(APP_DIR + '/scripts/**/*.js') + return gulp.src([APP_DIR + '/scripts/**/*.js', '!**/third_party/**']) .pipe(reload({stream: true, once: true})) .pipe($.jshint()) .pipe($.jshint.reporter('jshint-stylish')) .pipe($.if(!browserSync.active, $.jshint.reporter('fail'))); }); +// Check JS style +gulp.task('jscs', function() { + return gulp.src([APP_DIR + '/scripts/**/*.js', '!**/third_party/**']) + .pipe(reload({stream: true, once: true})) + .pipe($.jscs()); +}); + // Crush JS gulp.task('uglify', function() { return gulp.src(APP_DIR + '/scripts/**/*.js') @@ -210,7 +217,7 @@ gulp.task('serve', ['compass'], function () { gulp.task('vulcanize', ['vulcanize-elements']); -gulp.task('js', ['jshint', 'uglify']); +gulp.task('js', ['jshint', 'jscs', 'uglify']); gulp.task('default', ['clean'], function(cb) { runSequence('compass', 'vulcanize', ['js', 'images', 'fonts', 'copy-assets'], cb); diff --git a/package.json b/package.json index c5118e75..6e2fb5a8 100755 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "through2": "0.6.3", "vinyl-map": "1.0.1", "sprintf-js": "1.0.2", - "yargs": "1.3.3" + "yargs": "1.3.3", + "gulp-jscs": "^1.3.1" } } From bf69fb400f68b300fbc45742706be31f32f70e62 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 17 Dec 2014 00:15:35 +0000 Subject: [PATCH 2/2] Require space before/after '%' binary operator --- .jscsrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.jscsrc b/.jscsrc index 6128bee7..95c8a846 100644 --- a/.jscsrc +++ b/.jscsrc @@ -16,8 +16,8 @@ "disallowSpaceBeforeBinaryOperators": [","], "disallowSpaceAfterPrefixUnaryOperators": ["++", "--", "+", "-", "~", "!"], "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"], - "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], - "requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!=="], + "requireSpaceBeforeBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", "%"], + "requireSpaceAfterBinaryOperators": ["+", "-", "/", "*", "=", "==", "===", "!=", "!==", "%"], "disallowImplicitTypeConversion": ["numeric", "binary", "string"], "disallowKeywords": ["with", "eval"], "disallowMultipleLineBreaks": true,