This document outlines general guidelines, best practices and coding style rules for developers contributing to the project.
If you fixed or added something useful to the project, you can send a pull request. It will be reviewed by maintainer and accepted, or commented for rework, or declined.
If you found an error, typo or any other flaw in the project, please report about it using GitHub Issues. The more details you provide, the easier it could be reproduced and the faster it could be fixed. Unfortunately, sometimes the bug can only be reproduced in your project or in your environment, so maintainers cannot reproduce it. In this case we believe you can fix the bug and send us the fix.
If you've got an idea about a new feature, it's most likely that you'll have to implement it on your own. If you cannot implement the feature, but it is very important, you can create an issue at GitHub, but expect it to be declined by the maintainer.
This project's build process uses Node npm modules defined via the package.json file.
To add the latest version of new npm package to the project and automatically append it to package.json run:
npm install <pkg> --save-dev
If new packages have been added by other contributors then run:
npm update
To update all your local packages to the latest possible versions as constrained by package.json then run:
npm install
{} instead of new Object();[] instead of new Array();if statements.// Good vs //Bad.var to declare variables.var for multiple variables.// Good var aVariable = 0, aSecondVariable = null, A_CONSTANT = 'constant'; // Bad var a_variable = 0; var a_second_variable = null;
Our JavaScript code is compressed so don't try and save on space when writing code, focus on readability by trying to (however do not add unnecessary spaces).
if and after right parenthesis.if statement.if (foo === 'foo') { var array = ['one', 'two', 'three']; bar(array); foo = 'bar'; } function (arg1, arg2) { var foo = arg1; bar(foo); }
// Bad if ( foo ) { bar(); } function ( baz ) { bar(); }
// Good angular.module('foo') .controller('FooCtrl', function ($scope, $someReallyLongServiceName, $anotherService, $somethingElse) { $scope.bar = 'baz'; $scope.qux = 'quux'; });
This repo uses JSHint, defined in .jshintrc. Files are checked on save, and errors are visible in the console. Optionally this config file can also be used by your code editor.
The following guidelines are checked by JSHint.
' quotes for strings.=== and !== over == and !=camelCase for variables.This repo also uses JSCS, defined in .jscsrc. Files are checked on save, and errors are visible in the console. Optionally this config file can also be used by your code editor.
The following rules have been defined.
requireCurlyBracesUse curly braces after statements.
// Good if (x) { x++; } // Bad if (x) x++;
requireSpaceAfterKeywordsUse spaces after keywords:
// Good if (x) { x++; } // Bad if(x) { x++; }
requireSpaceBeforeBlockStatementsUse spaces after parenthesis, before curly brace:
// Good if (x) { x++; } // Bad if (x){ x++; }
requireSpacesInConditionalExpressionUse spaces around conditional expressions:
// Good var a = b ? c : d; // Bad var a=b?c:d;
requireSpacesInFunctionExpressionrequireSpacesInAnonymousFunctionExpressionrequireSpacesInNamedFunctionExpressionrequireSpacesInFunctionDeclarationUse spaces before and after parenthesis:
// Good function a () {} // Bad function a() {} function a (){}
requireMultipleVarDeclUse one var statement for multiple variables:
// Good var x = 1, y = 2; // Bad var x = 1; var y = 2;
requireBlocksOnNewlineUse new lines for multiple statements
// Good if (true) { doSomething(); doSomethingElse(); } // Bad if (true) { doSomething(); doSomethingElse(); }
disallowEmptyBlocksDon't use empty blocks
// Good if ( a === b ) { c = d; } // Bad if ( a !== b ) { } else { c = d; }
disallowSpacesInsideObjectBracketsDon't use a space after an opening or before a closing curly bracket
// Good var x = {a: 1}; // Bad var x = { a: 1 };
disallowSpacesInsideArrayBracketsDon't use a space after an opening or before a closing square bracket
// Good var x = [1]; // Bad var x = [ 1 ];
disallowSpacesInsideParenthesesDon't use a space after an opening or before a closing parenthesis
// Good var x = (1 + 2) * 3; // Bad var x = ( 1 + 2 ) * 3;
disallowSpaceAfterObjectKeysDon't use a space after a key in an object
// Good var x = {a: 1}; // Bad var x = {a : 1};
requireCommaBeforeLineBreakDon't put commas on the following line
// Good var x = { one: 1, two: 2 }; // Bad var x = { one: 1 , two: 2 };
requireOperatorBeforeLineBreakBreak lines after (not before) an operator
// Good x = y ? 1 : 2; // Bad x = y ? 1 : 2;
disallowLeftStickedOperatorsPut a space before an operator
// Good x = y ? 1 : 2; // Bad x = y? 1 : 2;
disallowRightStickedOperatorsPut a space after an operator
// Good x = y + 1; // Bad x = y +1;
requireLeftStickedOperatorsDon't put a space before a comma
// Good x = [1, 2]; // Bad x = [1 , 2];
disallowSpaceAfterPrefixUnaryOperatorsDon't put a space after a prefix unary operator
// Good x = !y; y = ++z; // Bad x = ! y; y = ++ z;
disallowSpaceBeforePostfixUnaryOperatorsDon't put a space after a postfix unary operator
// Good x = y++; // Bad x = y ++;
requireSpaceBeforeBinaryOperatorsPut a space before binary operators
// Good x !== y; // Bad x!== y;
requireSpaceAfterBinaryOperatorsPut a space after binary operators
// Good x + y; // Bad x +y;
disallowImplicitTypeConversionDon't use implicit type conversion
// Good x = Boolean(y); x = Number(y); x = String(y); x = s.indexOf('.') !== -1; // Bad x = !!y; x = +y; x = '' + y; x = ~s.indexOf('.');
requireCamelCaseOrUpperCaseIdentifiersUse camelCase or UPPERCASE_WITH_UNDERSCORES for variable names
// Good var camelCase = 0; var UPPER_CASE = 4; // Bad var lower_case = 1; var Mixed_case = 2; var mixed_Case = 3;
disallowKeywordsDisallow certain keywords from anywhere in the code base.
// Bad with (foo) { }
disallowMultipleLineBreaksDon't use multiple blank lines in a row
// Bad var x = 1; x++;
validateLineBreaksChecks line break character consistency:
// Good foo();<LF> // Bad foo();<CRLF> foo();<CR>
validateQuoteMarksCheck quote mark usage is single quotes.
// Good var foo = 'bar'; // Bad var foo = "bar";
validateIndentationCheck indentation is at 4 spaces, no tabs.
// Good function foo (bar) { console.log(bar); } // Bad function foo (bar) { console.log(bar); }
disallowMixedSpacesAndTabsCheck that spaces and tabs are not mixed.
disallowTrailingWhitespaceCheck that no line ends in a whitespace char.
// Good var foo = "blah blah";<LF> // Bad var foo = "blah blah"; <LF>
disallowTrailingCommaDon't use a comma at the end of an array or object
// Good var foo = [1, 2, 3]; var bar = {a: "a", b: "b"} // Bad var foo = [1, 2, 3,]; var bar = {a: "a", b: "b",}
disallowKeywordsOnNewLineDon't place else statements on their own line
// Good if (x < 0) { x++; } else { x--; } // Bad if (x < 0) { x++; } else { x--; }
maximumLineLengthChecks that no single line extends beyond 80 chars.
requireCapitalizedConstructorsConstructor variable names should be Capitalised.
// Good var foo = new Bar(); // Bad var foo = new bar();
safeContextKeywordWhen saving this context call the variable $this.
// Good var $this = this; // Bad var that = this; var self = this;
disallowYodaConditionsThe variable should be on the left in boolean comparisons.
// Good if (a === 1) { // ... } // Bad if (1 === a) { // ... }