Skip to content

Commit

Permalink
Update rules to new ESLint rule format (#54)
Browse files Browse the repository at this point in the history
* Update rules to new ESLint rule format

See: <http://eslint.org/blog/2016/07/eslint-new-rule-format>

This was done automatically using
[eslint-transforms](https://github.com/eslint/eslint-transforms).

* Fix `object-shorthand` linting errors caused by automatic transform

* Fix `max-len` linting error
  • Loading branch information
SaladFork authored and michalsnik committed Apr 21, 2017
1 parent 4eb5158 commit a9bc107
Show file tree
Hide file tree
Showing 17 changed files with 511 additions and 408 deletions.
60 changes: 33 additions & 27 deletions lib/rules/alias-model-in-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@ const ember = require('../utils/ember');
// Controllers - Alias your model
//------------------------------------------------------------------------------

module.exports = function (context) {
const message = 'Alias your model';
module.exports = {
meta: {
docs: {}
},

const report = function (node) {
context.report(node, message);
};
create(context) {
const message = 'Alias your model';

const filePath = context.getFilename();
const report = function (node) {
context.report(node, message);
};

return {
CallExpression(node) {
if (!ember.isEmberController(node, filePath)) return;
const filePath = context.getFilename();

const properties = ember.getModuleProperties(node);
let aliasPresent = false;
return {
CallExpression(node) {
if (!ember.isEmberController(node, filePath)) return;

properties.forEach((property) => {
const parsedCallee = utils.parseCallee(property.value);
const parsedArgs = utils.parseArgs(property.value);
const properties = ember.getModuleProperties(node);
let aliasPresent = false;

if (
parsedCallee.length &&
parsedCallee.pop() === 'alias' &&
parsedArgs[0] === 'model'
) {
aliasPresent = true;
}
});
properties.forEach((property) => {
const parsedCallee = utils.parseCallee(property.value);
const parsedArgs = utils.parseArgs(property.value);

if (
parsedCallee.length &&
parsedCallee.pop() === 'alias' &&
parsedArgs[0] === 'model'
) {
aliasPresent = true;
}
});


if (!aliasPresent) {
report(node);
}
},
};
if (!aliasPresent) {
report(node);
}
},
};
}
};
36 changes: 21 additions & 15 deletions lib/rules/closure-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ const utils = require('../utils/utils');
// Components - Closure actions
//------------------------------------------------------------------------------

module.exports = function (context) {
const message = 'Use closure actions, unless you need bubbling';
module.exports = {
meta: {
docs: {}
},

const report = function (node) {
context.report(node, message);
};
create(context) {
const message = 'Use closure actions, unless you need bubbling';

return {
MemberExpression(node) {
const isSendAction = utils.isThisExpression(node.object) &&
utils.isIdentifier(node.property) &&
node.property.name === 'sendAction';
const report = function (node) {
context.report(node, message);
};

if (isSendAction) {
report(node);
}
},
};
return {
MemberExpression(node) {
const isSendAction = utils.isThisExpression(node.object) &&
utils.isIdentifier(node.property) &&
node.property.name === 'sendAction';

if (isSendAction) {
report(node);
}
},
};
}
};
88 changes: 47 additions & 41 deletions lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,52 @@ const ember = require('../utils/ember');
// General rule - Don’t use jQuery without Ember Run Loop
//------------------------------------------------------------------------------

module.exports = function (context) {
const message = 'Don\'t use jQuery without Ember Run Loop';

const report = function (node) {
context.report(node, message);
};

const isJqueryUsed = function (node) {
return utils.isMemberExpression(node) &&
utils.isCallExpression(node.object) &&
ember.isModule(node.object, '$');
};

const isRunUsed = function (node) {
return ember.isModule(node, 'run');
};

return {
CallExpression(node) {
const callee = node.callee;
const fnNodes = utils.findNodes(node.arguments, 'ArrowFunctionExpression');

if (isJqueryUsed(callee) && fnNodes.length) {
fnNodes.forEach((fnNode) => {
const fnBody = fnNode.body.body;
const fnExpressions = utils.findNodes(fnBody, 'ExpressionStatement');

fnExpressions.forEach((fnExpression) => {
const expression = fnExpression.expression;

if (
utils.isCallExpression(expression) &&
utils.isMemberExpression(expression.callee) &&
!isRunUsed(expression)
) {
report(expression.callee);
}
module.exports = {
meta: {
docs: {}
},

create(context) {
const message = 'Don\'t use jQuery without Ember Run Loop';

const report = function (node) {
context.report(node, message);
};

const isJqueryUsed = function (node) {
return utils.isMemberExpression(node) &&
utils.isCallExpression(node.object) &&
ember.isModule(node.object, '$');
};

const isRunUsed = function (node) {
return ember.isModule(node, 'run');
};

return {
CallExpression(node) {
const callee = node.callee;
const fnNodes = utils.findNodes(node.arguments, 'ArrowFunctionExpression');

if (isJqueryUsed(callee) && fnNodes.length) {
fnNodes.forEach((fnNode) => {
const fnBody = fnNode.body.body;
const fnExpressions = utils.findNodes(fnBody, 'ExpressionStatement');

fnExpressions.forEach((fnExpression) => {
const expression = fnExpression.expression;

if (
utils.isCallExpression(expression) &&
utils.isMemberExpression(expression.callee) &&
!isRunUsed(expression)
) {
report(expression.callee);
}
});
});
});
}
},
};
}
},
};
}
};
76 changes: 41 additions & 35 deletions lib/rules/local-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,46 @@ const utils = require('../utils/utils');
// General rule - Create local version of Ember.* and DS.*
//------------------------------------------------------------------------------

module.exports = function (context) {
const message = 'Create local version of ';

const report = function (node, name) {
const msg = `${message + name}.${node.property.name}`;
context.report(node, msg);
};

const allowedEmberProperties = ['$', 'Object', 'Router', 'String'];
const allowedDSProperties = [];

const isExpressionForbidden = function (objectName, node, allowedProperties) {
return node.object.name === objectName &&
node.property.name.length &&
allowedProperties.indexOf(node.property.name) === -1;
};

return {
CallExpression(node) {
const callee = node.callee;
const obj = utils.isMemberExpression(callee.object) ? callee.object : callee;

if (
utils.isIdentifier(obj.object) &&
utils.isIdentifier(obj.property)
) {
if (isExpressionForbidden('Ember', obj, allowedEmberProperties)) {
report(obj, 'Ember');
module.exports = {
meta: {
docs: {}
},

create(context) {
const message = 'Create local version of ';

const report = function (node, name) {
const msg = `${message + name}.${node.property.name}`;
context.report(node, msg);
};

const allowedEmberProperties = ['$', 'Object', 'Router', 'String'];
const allowedDSProperties = [];

const isExpressionForbidden = function (objectName, node, allowedProperties) {
return node.object.name === objectName &&
node.property.name.length &&
allowedProperties.indexOf(node.property.name) === -1;
};

return {
CallExpression(node) {
const callee = node.callee;
const obj = utils.isMemberExpression(callee.object) ? callee.object : callee;

if (
utils.isIdentifier(obj.object) &&
utils.isIdentifier(obj.property)
) {
if (isExpressionForbidden('Ember', obj, allowedEmberProperties)) {
report(obj, 'Ember');
}

if (isExpressionForbidden('DS', obj, allowedDSProperties)) {
report(obj, 'DS');
}
}

if (isExpressionForbidden('DS', obj, allowedDSProperties)) {
report(obj, 'DS');
}
}
},
};
},
};
}
};
66 changes: 36 additions & 30 deletions lib/rules/named-functions-in-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,41 @@ const utils = require('../utils/utils');
// General rule - Use named functions defined on objects to handle promises
//------------------------------------------------------------------------------

module.exports = function (context) {
const message = 'Use named functions defined on objects to handle promises';

const report = function (node) {
context.report(node, message);
};

return {
CallExpression(node) {
const firstArg = node.arguments[0];

if (
hasPromiseExpression(node) &&
(
utils.isFunctionExpression(firstArg) ||
utils.isArrowFunctionExpression(firstArg)
)
) {
report(node);
}
},
};

function hasPromiseExpression(node) {
const callee = node.callee;
const promisesMethods = ['then', 'catch', 'finally'];

return utils.isCallExpression(callee.object) &&
utils.isIdentifier(callee.property) &&
promisesMethods.indexOf(callee.property.name) > -1;
module.exports = {
meta: {
docs: {}
},

create(context) {
const message = 'Use named functions defined on objects to handle promises';

const report = function (node) {
context.report(node, message);
};

return {
CallExpression(node) {
const firstArg = node.arguments[0];

if (
hasPromiseExpression(node) &&
(
utils.isFunctionExpression(firstArg) ||
utils.isArrowFunctionExpression(firstArg)
)
) {
report(node);
}
},
};

function hasPromiseExpression(node) {
const callee = node.callee;
const promisesMethods = ['then', 'catch', 'finally'];

return utils.isCallExpression(callee.object) &&
utils.isIdentifier(callee.property) &&
promisesMethods.indexOf(callee.property.name) > -1;
}
}
};
Loading

0 comments on commit a9bc107

Please sign in to comment.