Skip to content

Commit

Permalink
[NEW RULE] Adding no-async-actions rule
Browse files Browse the repository at this point in the history
Fixes ember-cli#418

[DO NOT MERGE]
1. Working for normal async actions
2. With decorators it is throwing errors, need to fix this.
  • Loading branch information
rajasegar committed May 25, 2019
1 parent 54c5f9c commit 24ebede
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
61 changes: 61 additions & 0 deletions lib/rules/no-async-actions.js
@@ -0,0 +1,61 @@
'use strict';

const utils = require('../utils/utils');


//------------------------------------------------------------------------------
// General rule - Don't use async actions
//------------------------------------------------------------------------------


const message = 'Do not use async actions';

module.exports = {
meta: {
docs: {
description: 'Disallow usage of async actions in components',
category: 'Possible Errors',
recommended: true,
url: 'http://ember-concurrency.com/docs/tutorial'
},
fixable: null,
},

create(context) {
return {
Property(node) {
if (node.key.name === 'actions') {
const props = node.value.properties;

props.forEach((p) => {
const body = p.value.body.body;
if (p.value.async) {
context.report({
node: p,
message,
});
} else if (body.length === 1 && utils.isReturnStatement(body[0])) {
const retSt = body[0];
if (retSt.argument.type === 'CallExpression' &&
retSt.argument.callee.property.name === 'then') {
context.report({
node: retSt,
message,
});
}
}
});
} else if (node.decorators) {
if (node.decorators.find(d => d.expression.name === 'action')) {
if (node.value.async) {
context.report({
node,
message: 'Do not use async actions'
});
}
}
}
}
};
}
};
96 changes: 96 additions & 0 deletions tests/lib/rules/no-async-actions.js
@@ -0,0 +1,96 @@
'use strict';

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-async-actions.js');
const RuleTester = require('eslint').RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const message = 'Do not use async actions';

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
}
});

ruleTester.run('no-async-actions', rule, {
valid: [
{
code: `
Component.extend({
actions: {
handleClick() {
// ...
}
}
});`,
}

],

invalid: [
{
code: `Component.extend({
actions: {
async handleClick() {
// ...
}
}
});`,
output: null,
errors: [{
message,
}]
},
{
code: `Component.extend({
actions: {
handleClick() {
return something.then(() => {
let hello = "world";
});
}
}
});`,
output: null,
errors: [{
message,
}]
},
{
code: `Component.extend({
@action
async handleClick() {
// ...
}
});`,
output: null,
errors: [{
message,
}]
},
{
code: `Component.extend({
@action
handleClick() {
return something.then(() => {
let hello = "world";
});
}
});`,
output: null,
errors: [{
message,
}]
},

]
});

0 comments on commit 24ebede

Please sign in to comment.