diff --git a/README.md b/README.md index 2e76a62716..3c4f6f4669 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ -# Airbnb JavaScript Style Guide() { +# JavaScript Style Guide() { *A mostly reasonable approach to JavaScript* +This version of the style guide includes my own personal style preferences. The main differences are style asthetics. + ## Table of Contents @@ -23,6 +25,7 @@ 1. [Naming Conventions](#naming-conventions) 1. [Accessors](#accessors) 1. [Constructors](#constructors) + 1. [Events](#events) 1. [Modules](#modules) 1. [jQuery](#jquery) @@ -40,9 +43,9 @@ - **Primitives**: When you access a primitive type you work directly on its value - + `string` - + `number` - + `boolean` + + `String` + + `Number` + + `Boolean` + `null` + `undefined` @@ -56,9 +59,9 @@ ``` - **Complex**: When you access a complex type you work on a reference to its value - + `object` - + `array` - + `function` + + `Object` + + `Array` + + `Function` ```javascript var foo = [1, 2], @@ -69,7 +72,7 @@ console.log(foo[0], bar[0]); // => 9, 9 ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Objects @@ -117,7 +120,7 @@ type: 'alien' }; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Arrays @@ -169,7 +172,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Strings @@ -215,7 +218,7 @@ 'fast.'; ``` - - When programatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2). + - When programmatically building up a string, use Array#join instead of string concatenation. Mostly for IE: [jsPerf](http://jsperf.com/string-vs-array-concat/2). ```javascript var items, @@ -259,7 +262,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Functions @@ -316,7 +319,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** @@ -352,7 +355,7 @@ var isJedi = getProp('jedi'); ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Variables @@ -461,7 +464,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Hoisting @@ -522,7 +525,7 @@ console.log('Flying'); }; } - + // the same is true when the function name // is the same as the variable name. function example() { @@ -550,7 +553,7 @@ - For more information refer to [JavaScript Scoping & Hoisting](http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting) by [Ben Cherry](http://www.adequatelygood.com/) - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** @@ -560,10 +563,10 @@ - Conditional expressions are evaluated using coercion with the `ToBoolean` method and always follow these simple rules: + **Objects** evaluate to **true** - + **Undefined** evaluates to **false** - + **Null** evaluates to **false** + + `undefined` evaluates to **false** + + `null` evaluates to **false** + **Booleans** evaluate to **the value of the boolean** - + **Numbers** evalute to **false** if **+0, -0, or NaN**, otherwise **true** + + **Numbers** evaluate to **false** if **+0, -0, or NaN**, otherwise **true** + **Strings** evaluate to **false** if an empty string `''`, otherwise **true** ```javascript @@ -599,7 +602,7 @@ - For more information see [Truth Equality and JavaScript](http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/#more-2108) by Angus Croll - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Blocks @@ -628,7 +631,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Comments @@ -640,10 +643,9 @@ // make() returns a new element // based on the passed in tag name // - // @param tag - // @return element + // @param {string} tag Tag to create + // @return {element} element New element function make(tag) { - // ...stuff... return element; @@ -654,11 +656,10 @@ * make() returns a new element * based on the passed in tag name * - * @param tag - * @return element + * @param {string} tag Tag to create + * @return {element} element New element */ function make(tag) { - // ...stuff... return element; @@ -721,7 +722,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Whitespace @@ -744,6 +745,7 @@ ∙∙var name; } ``` + - Place 1 space before the leading brace. ```javascript @@ -769,6 +771,75 @@ breed: 'Bernese Mountain Dog' }); ``` + + - Place 1 space inside leading and trailing braces. + + ```javascript + // bad + doSomething({key: 'value'}); + + // good + doSomething({ key: 'value' }); + ``` + + - Place 1 space after commas in a series. + + ```javascript + // bad + var myArray = [1,2,3,4]; + + // good + var myArray = [1, 2, 3, 4]; + ``` + + - Pad multi-line logic with empty newlines + + ```javascript + // bad + var myFunc = function myFunc() { + var myVar = [1, 2, 3], + i = myVar.length; + while (i--) { + // ... + } + doSomething(); + } + + // good + var myFunc = function myFunc() { + var myVar = [1, 2, 3], + i = myVar.length; + + while (i--) { + // ... + } + + doSomething(); + } + ``` + + - Place an empty newline before `return` statements. + + ```javascript + // bad + Jedi.prototype.slash = function(target) { + this.target = target; + return this; + } + + // good + Jedi.prototype.slash = function(target) { + this.target = target; + + return this; + } + + // acceptable + Jedi.prototype.getTarget = function(target) { + return this.target; + } + ``` + - Place an empty newline at the end of the file. ```javascript @@ -817,7 +888,7 @@ .call(tron.led); ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Commas @@ -879,7 +950,7 @@ ]; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Semicolons @@ -890,23 +961,44 @@ // bad (function() { var name = 'Skywalker' + return name })() // good (function() { var name = 'Skywalker'; + return name; })(); // good ;(function() { var name = 'Skywalker'; + return name; })(); ``` + - No hanging semicolons - **[[⬆]](#TOC)** + ```javascript + // bad + (function() { + var name = 'Skywalker'; + + return name; + })() + ; + + // good + (function() { + var name = 'Skywalker'; + + return name; + })(); + ``` + + **[[TOC]](#TOC)** ## Type Casting & Coercion @@ -981,7 +1073,7 @@ var hasAge = !!age; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Naming Conventions @@ -1095,7 +1187,7 @@ }; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Accessors @@ -1149,7 +1241,7 @@ }; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Constructors @@ -1188,6 +1280,7 @@ // bad Jedi.prototype.jump = function() { this.jumping = true; + return true; }; @@ -1202,11 +1295,13 @@ // good Jedi.prototype.jump = function() { this.jumping = true; + return this; }; Jedi.prototype.setHeight = function(height) { this.height = height; + return this; }; @@ -1234,7 +1329,7 @@ }; ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Events @@ -1265,7 +1360,7 @@ }); ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Modules @@ -1296,7 +1391,7 @@ }(this); ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## jQuery @@ -1361,14 +1456,14 @@ $($sidebar[0]).find('ul'); ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## ECMAScript 5 Compatibility - Refer to [Kangax](https://twitter.com/kangax/)'s ES5 [compatibility table](http://kangax.github.com/es5-compat-table/) - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Testing @@ -1381,7 +1476,7 @@ } ``` - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Performance @@ -1395,7 +1490,7 @@ - [Long String Concatenation](http://jsperf.com/ya-string-concat) - Loading... - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## Resources @@ -1445,7 +1540,7 @@ - [Dustin Diaz](http://dustindiaz.com/) - [nettuts](http://net.tutsplus.com/?s=javascript) - **[[⬆]](#TOC)** + **[[TOC]](#TOC)** ## In the Wild @@ -1512,7 +1607,7 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -**[[⬆]](#TOC)** +**[[TOC]](#TOC)** # }; diff --git a/linters/SublimeLinter/SublimeLinter.sublime-settings b/linters/SublimeLinter/SublimeLinter.sublime-settings index 1e12f7c6bb..79ac01800a 100644 --- a/linters/SublimeLinter/SublimeLinter.sublime-settings +++ b/linters/SublimeLinter/SublimeLinter.sublime-settings @@ -22,6 +22,9 @@ // Define globals exposed by modern browsers. "browser": true, + // Define globals exposed by Dojo Toolkit. + "dojo": true, + // Define globals exposed by jQuery. "jquery": true, @@ -37,21 +40,37 @@ // with underscores. "camelcase": true, - // Prohibit use of == and != in favor of === and !==. + // Require curly braces around blocks in loops and conditionals. + "curly": true, + + // Prohibit the use of == and != in favor of === and !==. "eqeqeq": true, // Suppress warnings about == null comparisons. "eqnull": true, - // Enforce tab width of 2 spaces. + // Prohibit the use of immediate function invocations without wrapping + // them in parentheses. + "immed": true, + + // Enforce a tab width of 2 spaces. "indent": 2, // Prohibit use of a variable before it is defined. "latedef": true, + // Suppress warnings about functions inside of loops. + "loopfunc": false, + // Require capitalized names for constructor functions. "newcap": true, + // This option suppresses warnings about mixed tabs and spaces when the latter are used for alignmnent only. + "smarttabs": false, + + // Disable 'use strict' message. + "strict": false, + // Enforce use of single quotation marks for strings. "quotmark": "single",