Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order beforeModel and afterModel around model #124

Merged
merged 9 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from 8 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
63 changes: 49 additions & 14 deletions docs/rules/order-in-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ ember/order-in-routes: [2, {
'service',
'inherited-property',
'property',
'single-line-function',
'multi-line-function',
'beforeModel',
'model',
'lifecycle-hook',
'afterModel',
'serialize',
'redirect',
'activate',
'setupController',
'renderTemplate',
'resetController',
'deactivate',
'actions',
['method', 'empty-method'],
]
Expand All @@ -25,7 +35,18 @@ order: [
'service',
['inherited-property', 'property'],
'model',
'lifecycle-hook',
[
'beforeModel',
'model',
'afterModel',
'serialize',
'redirect',
'activate',
'setupController',
'renderTemplate',
'resetController',
'deactivate'
],
'actions',
['method', 'empty-method'],
]
Expand All @@ -40,10 +61,12 @@ You should write code grouped and ordered in this way:
1. Services
2. Default route's properties
3. Custom properties
4. model() hook
5. Other route's methods (beforeModel etc.)
6. Actions
7. Custom / private methods
4. beforeModel() hook
5. model() hook
6. afterModel() hook
7. Other lifecycle hooks in execution order (serialize, redirect, etc)
8. Actions
9. Custom / private methods

```javascript
const { Route, inject: { service }, get } = Ember;
Expand All @@ -60,26 +83,38 @@ export default Route.extend({
// 3. Custom properties
customProp: 'test',

// 4. Model hook
model() {
return this.store.findAll('article');
},

// 5. Other route's methods
// 4. beforeModel hook
beforeModel() {
if (!get(this, 'currentUser.isAdmin')) {
this.transitionTo('index');
}
},

// 5. model hook
model() {
return this.store.findAll('article');
},

// 6. afterModel hook
afterModel(articles) {
articles.forEach((article) => {
article.set('foo', 'bar');
});
},

// 7. Other route's methods
setupController(controller) {
controller.set('foo', 'bar');
},

// 6. All actions
// 8. All actions
actions: {
sneakyAction() {
return this._secretMethod();
},
},

// 7. Custom / private methods
// 9. Custom / private methods
_secretMethod() {
// custom secret method logic
},
Expand Down
34 changes: 30 additions & 4 deletions lib/rules/order-in-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ const ORDER = [
'property',
'single-line-function',
'multi-line-function',
'beforeModel',
'model',
'lifecycle-hook',
'afterModel',
'serialize',
'redirect',
'activate',
'setupController',
'renderTemplate',
'resetController',
'deactivate',
'actions',
['method', 'empty-method'],
['method', 'empty-method']
];

//------------------------------------------------------------------------------
Expand All @@ -29,11 +37,29 @@ module.exports = {
category: 'Organizing',
recommended: true
},
fixable: null, // or "code" or "whitespace"
fixable: null // or "code" or "whitespace"
},

create(context) {
const options = context.options[0] || {};
let order = options.order || ORDER;
order = order.slice(0);
const indexOfLifecycleHook = order.indexOf('lifecycle-hook');

if (indexOfLifecycleHook !== -1) {
order.splice(indexOfLifecycleHook, 1, [
'beforeModel',
'afterModel',
'serialize',
'redirect',
'activate',
'setupController',
'renderTemplate',
'resetController',
'deactivate'
]);
}

const filePath = context.getFilename();
const order = options.order ? addBackwardsPosition(options.order, 'empty-method', 'method') : ORDER;

Expand All @@ -42,7 +68,7 @@ module.exports = {
if (!ember.isEmberRoute(node, filePath)) return;

reportUnorderedProperties(node, context, 'route', order);
},
}
};
}
};
55 changes: 7 additions & 48 deletions lib/utils/ember.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
isComputedProp,
isCustomProp,
isActionsProp,
isModelProp,
isRouteLifecycleHook,

isRelation,

Expand All @@ -36,8 +36,6 @@ module.exports = {
isComponentCustomFunction,

isRoute,
isRouteMethod,
isRouteDefaultMethod,
isRouteCustomFunction,
isRouteProperty,
isRouteDefaultProp,
Expand Down Expand Up @@ -177,8 +175,9 @@ function isCustomProp(property) {
utils.isTaggedTemplateExpression(value);
}

function isModelProp(property) {
return property.key.name === 'model' && utils.isFunctionExpression(property.value);
function isRouteLifecycleHook(property) {
return isFunctionExpression(property.value) &&
isRouteLifecycleHookName(property.key.name);
}

function isActionsProp(property) {
Expand Down Expand Up @@ -218,64 +217,24 @@ function isRoute(node) {
node.callee.property.name === 'route';
}

function isRouteMethod(name) {
function isRouteLifecycleHookName(name) {
return [
'activate',
'addObserver',
'afterModel',
'beforeModel',
'cacheFor',
'controllerFor',
'create',
'deactivate',
'decrementProperty',
'destroy',
'disconnectOutlet',
'extend',
'get',
'getProperties',
'getWithDefault',
'has',
'incrementProperty',
'init',
'intermediateTransitionTo',
'model',
'modelFor',
'notifyPropertyChange',
'off',
'on',
'one',
'paramsFor',
'redirect',
'refresh',
'removeObserver',
'render',
'renderTemplate',
'reopen',
'reopenClass',
'replaceWith',
'resetController',
'send',
'serialize',
'set',
'setProperties',
'setupController',
'toString',
'toggleProperty',
'transitionTo',
'trigger',
'willDestroy',
'setupController'
].indexOf(name) > -1;
}

function isRouteDefaultMethod(property) {
return isFunctionExpression(property.value) &&
isRouteMethod(property.key.name);
}

function isRouteCustomFunction(property) {
return isFunctionExpression(property.value) &&
!isRouteMethod(property.key.name);
!isRouteLifecycleHookName(property.key.name);
}

function isRouteProperty(name) {
Expand Down
25 changes: 17 additions & 8 deletions lib/utils/property-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const utils = require('./utils');
module.exports = {
determinePropertyType,
reportUnorderedProperties,
addBackwardsPosition,
addBackwardsPosition
};

const NAMES = {
Expand All @@ -15,15 +15,24 @@ const NAMES = {
'single-line-function': 'single-line function',
'multi-line-function': 'multi-line function',
observer: 'observer',
beforeModel: 'lifecycle hook',
model: '"model" hook',
afterModel: 'lifecycle hook',
serialize: 'lifecycle hook',
redirect: 'lifecycle hook',
activate: 'lifecycle hook',
setupController: 'lifecycle hook',
renderTemplate: 'lifecycle hook',
resetController: 'lifecycle hook',
deactivate: 'lifecycle hook',
'lifecycle-hook': 'lifecycle hook',
actions: 'actions hash',
method: 'method',
'empty-method': 'empty method',
unknown: 'unknown property type',
attribute: 'attribute',
relationship: 'relationship',
'query-params': 'property',
'query-params': 'property'
};

function determinePropertyType(node, parentType) {
Expand Down Expand Up @@ -56,10 +65,8 @@ function determinePropertyType(node, parentType) {
if (parentType === 'route') {
if (ember.isRouteDefaultProp(node)) {
return 'inherited-property';
} else if (ember.isModelProp(node)) {
return 'model';
} else if (ember.isRouteDefaultMethod(node)) {
return 'lifecycle-hook';
} else if (ember.isRouteLifecycleHook(node)) {
return node.key.name;
}
}

Expand Down Expand Up @@ -114,7 +121,9 @@ function getOrder(ORDER, type) {

function getName(type, node) {
let prefix;
if (!node.computed && type !== 'actions' && type !== 'model') {
if (!node.computed &&
type !== 'actions' &&
type !== 'model') {
if (node.key.type === 'Identifier') {
prefix = node.key.name;
} else if (node.key.type === 'Literal' && typeof node.key.value === 'string') {
Expand Down Expand Up @@ -142,7 +151,7 @@ function reportUnorderedProperties(node, context, parentType, ORDER) {

const info = {
node: property,
type,
type
};

// check if this property should be moved further upwards
Expand Down
Loading