From 2ff265b7b81aad1b3b1048bea1ab9410f3829016 Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sat, 28 Mar 2015 08:49:45 -0700 Subject: [PATCH 01/13] Own it --- README.md | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 516f0e2862..ecbdca65c2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/airbnb/javascript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +# AltSchool JavaScript Style Guide() { -# Airbnb JavaScript Style Guide() { +Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). *A mostly reasonable approach to JavaScript* diff --git a/package.json b/package.json index 2fe47fb268..7bf4477324 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/airbnb/javascript.git" + "url": "https://github.com/AltSchool/javascript.git" }, "keywords": [ "style guide", From 4f0f28a492a065d9d6e524eaabc20593c3a49acd Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sat, 28 Mar 2015 08:51:55 -0700 Subject: [PATCH 02/13] Always use `//` for comments --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ecbdca65c2..5cb6ae3c99 100644 --- a/README.md +++ b/README.md @@ -665,15 +665,17 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). ## Comments - - Use `/** ... */` for multiline comments. Include a description, specify types and values for all parameters and return values. + - Use `//` for multiline comments. Include a description, specify types and values for all parameters and return values. ```javascript // bad - // make() returns a new element - // based on the passed in tag name - // - // @param {String} tag - // @return {Element} element + /** + * make() returns a new element + * based on the passed in tag name + * + * @param {String} tag + * @return {Element} element + */ function make(tag) { // ...stuff... @@ -682,13 +684,11 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). } // good - /** - * make() returns a new element - * based on the passed in tag name - * - * @param {String} tag - * @return {Element} element - */ + // make() returns a new element + // based on the passed in tag name + // + // @param {String} tag + // @return {Element} element function make(tag) { // ...stuff... From 25bc6ad76697a89950e4bf4e7a8af980d5526857 Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sat, 28 Mar 2015 09:04:18 -0700 Subject: [PATCH 03/13] Add sections for Ember and ES6 --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index ecbdca65c2..1cc780c07f 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,10 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [Constructors](#constructors) 1. [Events](#events) 1. [Modules](#modules) + 1. [Ember](#ember) 1. [jQuery](#jquery) 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility) + 1. [ECMAScript 6](#ecmascript-6) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1431,6 +1433,13 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). **[⬆ back to top](#table-of-contents)** +## Ember + +Comming soon... + +**[⬆ back to top](#table-of-contents)** + + ## jQuery - Prefix jQuery object variables with a `$`. @@ -1500,6 +1509,12 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). **[⬆ back to top](#table-of-contents)** +## ECMAScript 6 + +Coming soon... + +**[⬆ back to top](#table-of-contents)** + ## Testing - **Yup.** From 74098faf07362320b8c1d7d49951d04f7e29f0ab Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sun, 29 Mar 2015 09:11:22 -0700 Subject: [PATCH 04/13] Fat arrows --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1cc780c07f..0d7162c152 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [jQuery](#jquery) 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility) 1. [ECMAScript 6](#ecmascript-6) + 1. [Fat Arrows](#fat-arrows) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1511,7 +1512,30 @@ Comming soon... ## ECMAScript 6 -Coming soon... +### Fat Arrows + +Use when defining inline function iterators and callbacks, especially when needing to preserve `this`. Don’t use as general replacement for `function`. + +```javascript +// bad +var self = this; +person.save().then(function() { + self.send('flash-message', 'Saved!'); +}); + +// bad +person.save().then(function() { + self.send('flash-message', 'Saved!'); +}.bind(this)); + +// good +var names = people.map((person) => { return person.name; }); + +// good +person.save().then(()=> { + this.send('flash-message', 'Saved!'); +}); +``` **[⬆ back to top](#table-of-contents)** From 1c8c958cc3decbebbd706e2883f77643854e1aff Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sun, 29 Mar 2015 09:27:59 -0700 Subject: [PATCH 05/13] Classes --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 0d7162c152..b780b8b06a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility) 1. [ECMAScript 6](#ecmascript-6) 1. [Fat Arrows](#fat-arrows) + 1. [Classes](#classes) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1537,6 +1538,10 @@ person.save().then(()=> { }); ``` +### Classes + +Class syntax looks great and works well with Ember. However, don’t use it yet. The style isn’t Ember idiomatic. Usage outside of Ember may be appropriate in certain circumstances. + **[⬆ back to top](#table-of-contents)** ## Testing From ab33b505951f38bef7fe76845a1629a66a3af6f1 Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sun, 29 Mar 2015 09:28:18 -0700 Subject: [PATCH 06/13] Enhanced object literals --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index b780b8b06a..22b3846e3c 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [ECMAScript 6](#ecmascript-6) 1. [Fat Arrows](#fat-arrows) 1. [Classes](#classes) + 1. [Enhanced Object Literals](#enhanced-object-literals) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1542,6 +1543,20 @@ person.save().then(()=> { Class syntax looks great and works well with Ember. However, don’t use it yet. The style isn’t Ember idiomatic. Usage outside of Ember may be appropriate in certain circumstances. +### Enhanced Object Literals + +For the most part these should be avoided, except for the case of actions objects in Ember object definitions where the method shorthand syntax can help with readability. + +```javascript +// good +export default Ember.Route.extend({ + actions: { + save() { + this.model.save(); + } + } +}); +``` **[⬆ back to top](#table-of-contents)** ## Testing From 860a4bf03d558ca6fb8bc7b9f97dec3ffa159ca0 Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sun, 29 Mar 2015 09:28:27 -0700 Subject: [PATCH 07/13] Template strings --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 22b3846e3c..3e1f809013 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [Fat Arrows](#fat-arrows) 1. [Classes](#classes) 1. [Enhanced Object Literals](#enhanced-object-literals) + 1. [Template Strings](#template-strings) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1557,6 +1558,20 @@ export default Ember.Route.extend({ } }); ``` + +### Template Strings + +Use with aplomb! + +```javascript +// bad +return 'Hi my name is ' + name + ' and my favorite color is ' + color; + +// good +return `Hi my name is ${name} and my favorite color is ${color}`; +``` + + **[⬆ back to top](#table-of-contents)** ## Testing From f799608d013d1110d345069bed3ec9a57967520f Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Mon, 30 Mar 2015 19:09:11 -0700 Subject: [PATCH 08/13] Add destructuring --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index d22b709b14..b838aa7edb 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [Classes](#classes) 1. [Enhanced Object Literals](#enhanced-object-literals) 1. [Template Strings](#template-strings) + 1. [Destructuring](#destructuring) 1. [Testing](#testing) 1. [Performance](#performance) 1. [Resources](#resources) @@ -1571,6 +1572,24 @@ return 'Hi my name is ' + name + ' and my favorite color is ' + color; return `Hi my name is ${name} and my favorite color is ${color}`; ``` +### Destructuring + +You should use destructuring with only a few caveats. Avoid complex destructuring, especially in function parameters. Also, know that jsHint is currently incompatible with this syntax so you'll need to surround code that uses it in `/* jshint ignore:start */` and ` +/* jshint ignore:end */` + +```javascript +// bad +function getName(person) { + return person && person.name; +} + +// good +function getName({ name: name }) { + return name; +} +``` + +See more examples [here](https://github.com/AltSchool/ember-cli-es6-testing/blob/master/tests/unit/destructuring-test.js) **[⬆ back to top](#table-of-contents)** From 05da7ce63a2218a88d5e35c34fe2e760fb90b4be Mon Sep 17 00:00:00 2001 From: Benji Shine Date: Mon, 6 Apr 2015 21:20:39 -0700 Subject: [PATCH 09/13] Directions on how to use Ember.computed --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b838aa7edb..80afa9a9db 100644 --- a/README.md +++ b/README.md @@ -1440,7 +1440,25 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). ## Ember -Comming soon... + - If you use `Ember.computed`, be sure that you define all the property dependencies through the parameters to that method: + + ```javascript + // good + description: Ember.computed('fullName', 'homePlanet', function () { + return `${this.get('fullName')} from ${this.get('homePlanet')}`; + }) + ``` + + - Reusable components should not use the prototype extensions API. Instead, use `Ember.computed`: + ```javascript + // bad + nowish: function () { return new Date().toString(); }.property(); + + // good + nowish: Ember.computed( function() { return new Date().toString(); } ); + ``` + +More coming soon... **[⬆ back to top](#table-of-contents)** From 7d5dc13341230d84d9217be10a0157d42a65c42d Mon Sep 17 00:00:00 2001 From: Benji Shine Date: Mon, 6 Apr 2015 21:33:25 -0700 Subject: [PATCH 10/13] Whitespace for calls to Ember.*.extend({..}); --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80afa9a9db..6ebdc29938 100644 --- a/README.md +++ b/README.md @@ -1440,6 +1440,16 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). ## Ember + - In calls to `Ember.*.extend`, leave a blank lines after any mixins, before properties: + ```javascript + export default Ember.ArrayController.extend( + SingleCellSelection, + AdjacentCellFinder, { + + someProperty: true + }); + ``` + - If you use `Ember.computed`, be sure that you define all the property dependencies through the parameters to that method: ```javascript @@ -1452,10 +1462,10 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). - Reusable components should not use the prototype extensions API. Instead, use `Ember.computed`: ```javascript // bad - nowish: function () { return new Date().toString(); }.property(); + nowish: function () { return new Date().toString(); }.property(), // good - nowish: Ember.computed( function() { return new Date().toString(); } ); + nowish: Ember.computed( function() { return new Date().toString(); }), ``` More coming soon... From f1fc0fb4a3b860a22c197206a6a00edeb2e385ac Mon Sep 17 00:00:00 2001 From: Benji Shine Date: Fri, 15 May 2015 15:35:25 -0700 Subject: [PATCH 11/13] Align calls to DS.attr in models --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 6ebdc29938..e2b7b07da2 100644 --- a/README.md +++ b/README.md @@ -1468,6 +1468,22 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). nowish: Ember.computed( function() { return new Date().toString(); }), ``` + - Align calls to `DS.attr` in models: + ```javascript + // bad + export default DS.Model.extend({ + favoriteColor: DS.attr('string'), + favoriteFlavor: DS.attr('string'), + age: DS.attr('number'), + + // good + export default DS.Model.extend({ + favoriteColor: DS.attr('string'), + favoriteFlavor: DS.attr('string'), + age: DS.attr('number'), + + ``` + More coming soon... **[⬆ back to top](#table-of-contents)** From 176c522f49034a6c131281d2f433d0331482f6c7 Mon Sep 17 00:00:00 2001 From: Benji Shine Date: Wed, 15 Jul 2015 21:24:01 -0700 Subject: [PATCH 12/13] Propose standard import sort order. --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index e2b7b07da2..e451671fca 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. [Constructors](#constructors) 1. [Events](#events) 1. [Modules](#modules) + 1. [Imports](#imports) 1. [Ember](#ember) 1. [jQuery](#jquery) 1. [ECMAScript 5 Compatibility](#ecmascript-5-compatibility) @@ -1437,6 +1438,39 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). **[⬆ back to top](#table-of-contents)** +## Imports + + - Always use ES6-style `import`. Never use `require`. + + ```javascript + import Foo from '/path/to/foo/'; // good + var Foo = require('/path/to/foo'); // bad + ``` + + - Group imports into at most three groups: + 1. ember imports + 1. other library imports + 1. imports from this application. + + - Within each group, sort by alphabetical order, case-insensitive, of the name you're assigning to the thing being imported. + - If you're importing multiple things, put those into alphabetical order, and sort by the name of the first thing you're importing. + - Put newlines between the three groups of imports. + - Put the ember import before the ember-data import, even though `DS` is before `Ember` in alphabetical order. + + For example: + ```javascript + import Ember from 'ember'; + import DS from 'ember-data'; + + import { afterEach, beforeEach } from 'mocha'; + import markdown from 'markdown'; + + import AloCompetency from 'vishnu/models/alo-competency'; + import DomainGroup from 'vishnu/models/domain-group'; + ``` + +**[⬆ back to top](#table-of-contents)** + ## Ember From 95b9d475a3b968d39b017a9f3f0f59878355febb Mon Sep 17 00:00:00 2001 From: Alex Gibbons Date: Sat, 12 Sep 2015 15:54:00 -0700 Subject: [PATCH 13/13] Naturally sort imports Closes #10 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e451671fca..d676208aa2 100644 --- a/README.md +++ b/README.md @@ -1453,7 +1453,7 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). 1. imports from this application. - Within each group, sort by alphabetical order, case-insensitive, of the name you're assigning to the thing being imported. - - If you're importing multiple things, put those into alphabetical order, and sort by the name of the first thing you're importing. + - If you're importing multiple things, put those into alphabetical order, after any single imports, by the name of the first thing being importing. - Put newlines between the three groups of imports. - Put the ember import before the ember-data import, even though `DS` is before `Ember` in alphabetical order. @@ -1462,8 +1462,8 @@ Forked from [Airbnb's Style Guide](https://github.com/airbnb/javascript). import Ember from 'ember'; import DS from 'ember-data'; - import { afterEach, beforeEach } from 'mocha'; import markdown from 'markdown'; + import { afterEach, beforeEach } from 'mocha'; import AloCompetency from 'vishnu/models/alo-competency'; import DomainGroup from 'vishnu/models/domain-group';