Skip to content

Commit

Permalink
suggested improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmurclar committed Oct 30, 2019
1 parent d36bcbc commit 320bf51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ The `--fix` option on the command line automatically fixes problems reported by
| | [classic-decorator-hooks](./docs/rules/classic-decorator-hooks.md) | Ensure correct hooks are used for both classic and non-classic classes |
| | [classic-decorator-no-classic-methods](./docs/rules/classic-decorator-no-classic-methods.md) | Prevent usage of classic APIs such as get/set in classes that aren't explicitly decorated with @classic |
| | [computed-property-getters](./docs/rules/computed-property-getters.md) | Enforce the consistent use of getters in computed properties |
| | [no-proxies](./docs/rules/no-proxies.md) | Disallows using array or object proxies |
| | [no-actions-hash](./docs/rules/no-actions-hash.md) | Disallows the actions hash in components, controllers and routes |
| | [no-proxies](./docs/rules/no-proxies.md) | Disallows using array or object proxies |


### Possible Errors
Expand Down
39 changes: 15 additions & 24 deletions lib/rules/no-actions-hash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ember = require('../utils/ember');
const utils = require('../utils/utils');

const ERROR_MESSAGE = 'Use the @action decorator instead of declaring an actions hash';

Expand All @@ -17,42 +18,32 @@ module.exports = {

create: context => {
const filePath = context.getFilename();
let inClassWhichCanContainActions = false;

function _inClassWhichCanContainActions(node, filePath) {
function inClassWhichCanContainActions(node, filePath) {
return (
inClassWhichCanContainActions ||
ember.isEmberComponent(node, filePath) ||
ember.isEmberController(node, filePath) ||
ember.isEmberRoute(node, filePath)
);
}

function reportActionsProp(properties) {
properties.forEach(property => {
if (ember.isActionsProp(property)) {
context.report(property, ERROR_MESSAGE);
}
});
}

return {
ClassDeclaration(node) {
inClassWhichCanContainActions = _inClassWhichCanContainActions(node, filePath);
},
CallExpression(node) {
inClassWhichCanContainActions = _inClassWhichCanContainActions(node, filePath);
},
ObjectExpression(node) {
if (!inClassWhichCanContainActions) {
return;
if (inClassWhichCanContainActions(node, filePath)) {
reportActionsProp(utils.findNodes(node.body.body, 'ClassProperty'));
}

node.properties.forEach(property => {
if (property.key.name === 'actions') {
context.report(node, ERROR_MESSAGE);
}
});
},
ClassProperty(node) {
if (!inClassWhichCanContainActions) {
return;
}

if (node.value.type === 'ObjectExpression' && node.key.name === 'actions') {
context.report(node, ERROR_MESSAGE);
CallExpression(node) {
if (inClassWhichCanContainActions(node, filePath)) {
reportActionsProp(ember.getModuleProperties(node));
}
},
};
Expand Down
18 changes: 12 additions & 6 deletions tests/lib/rules/no-actions-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ ruleTester.run('no-actions-hash', rule, {
}
}
`,
`
export default Service.extend({
actions: {
},
});
`,
],

invalid: [
Expand All @@ -68,7 +74,7 @@ ruleTester.run('no-actions-hash', rule, {
},
});
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'Property', message: ERROR_MESSAGE }],
},
{
code: `
Expand All @@ -79,7 +85,7 @@ ruleTester.run('no-actions-hash', rule, {
}
}
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'ClassProperty', message: ERROR_MESSAGE }],
},
{
code: `
Expand All @@ -88,7 +94,7 @@ ruleTester.run('no-actions-hash', rule, {
},
});
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'Property', message: ERROR_MESSAGE }],
},
{
code: `
Expand All @@ -99,7 +105,7 @@ ruleTester.run('no-actions-hash', rule, {
}
}
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'ClassProperty', message: ERROR_MESSAGE }],
},
{
code: `
Expand All @@ -108,7 +114,7 @@ ruleTester.run('no-actions-hash', rule, {
},
});
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'Property', message: ERROR_MESSAGE }],
},
{
code: `
Expand All @@ -119,7 +125,7 @@ ruleTester.run('no-actions-hash', rule, {
}
}
`,
errors: [{ message: ERROR_MESSAGE }],
errors: [{ type: 'ClassProperty', message: ERROR_MESSAGE }],
},
],
});

0 comments on commit 320bf51

Please sign in to comment.