Skip to content
This repository has been archived by the owner on Dec 5, 2022. It is now read-only.

Source code formatting #32

Merged
merged 2 commits into from
Dec 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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

18 changes: 18 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

if [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but we're only checking code into branches and submitting PRs. Is there a way we can modify this to be useful for that case too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case you probably don't want it to run all the times, when intentionally committing a WIP code.
We could change the trigger so that it runs only if a commit message contains something like "pre-submit check", but you'll have to remember it.

Normally, for merging PR, this would be a job of a CI. Like Travis adding its info above the "merge" button so that you know it passed all checks/tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so this hook is just a precaution for now, for a rare case where you happened to be on the master branch for some reason.

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

26 changes: 26 additions & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,4 +26,6 @@ Start a web server in `app/` or server via App Engine dev server.

### Building

Run `gulp`. Then hit `http://localhost:<PORT>/dist/app/`. The unbuilt version is still viewable at `http://localhost:<PORT>/app/` but will not contain minfied JS or vulcanized HTML Imports.
Run `gulp`. Then hit `http://localhost:<PORT>/dist/app/`. The unbuilt version is still viewable at `http://localhost:<PORT>/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.
11 changes: 9 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}