forked from ember-cli/eslint-plugin-ember
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW RULE] Adding no-async-actions rule
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
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}] | ||
}, | ||
|
||
] | ||
}); |