Skip to content

Commit b3f7425

Browse files
authored
Merge pull request #44 from eddie-ruva/send-action
Implements rule to catch sendAction calls
2 parents 5b02780 + befbfd4 commit b3f7425

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

config/experimental.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
extends: require.resolve('./recommended.js'),
1313
rules: {
1414
// Experimental rules
15-
'ember-best-practices/no-2.0.0-hooks': 2
15+
'ember-best-practices/no-2.0.0-hooks': 2,
16+
'ember-best-practices/no-send-action': 2
1617
}
1718
};

guides/rules/no-send-action.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# No sendAction()
2+
3+
**TL;DR: Use closure actions instead of bubbling actions via sendAction**
4+
5+
Before v1.13 Ember's only way to deal with actions was through a bubbling system by calling `sendAction` or `send`. This changed when closure actions got introduced into the system and changed the paradigm to simpler JS function calls.
6+
7+
References:
8+
[Ember 1.13 release post](http://emberjs.com/blog/2015/06/12/ember-1-13-0-released.html#toc_closure-actions)
9+
[Ember Best Practices: Stop bubbling actions and use closure actions by Dan McClain](https://dockyard.com/blog/2015/10/29/ember-best-practice-stop-bubbling-and-use-closure-actions)

lib/rules/no-send-action.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @fileOverview Disallow use of sendAction
3+
* @author Edilberto Ruvalcaba
4+
*/
5+
'use strict';
6+
7+
const { get } = require('../utils/get');
8+
9+
function isSendActionFnCall(node) {
10+
return get(node, 'callee.property.name') === 'sendAction';
11+
}
12+
13+
const MESSAGE = 'Do not use send action. Consider using closure actions to work with JS functions instead of relying on the old action system. Please see following guide for more information: https://github.com/ember-best-practices/eslint-plugin-ember-best-practices/blob/master/guides/rules/no-send-action.md';
14+
15+
module.exports = {
16+
docs: {
17+
description: 'Disallow use of sendAction',
18+
category: 'Best Practices'
19+
},
20+
meta: {
21+
message: MESSAGE
22+
},
23+
create(context) {
24+
return {
25+
CallExpression(node) {
26+
if (isSendActionFnCall(node)) {
27+
context.report(node, MESSAGE);
28+
}
29+
}
30+
};
31+
}
32+
};

tests/lib/rules/no-send-action.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const rule = require('../../../lib/rules/no-send-action');
2+
const MESSAGE = rule.meta.message;
3+
const RuleTester = require('eslint').RuleTester;
4+
const ruleTester = new RuleTester();
5+
6+
ruleTester.run('no-send-action', rule, {
7+
valid: [
8+
{
9+
code: `
10+
function anotherFunction() {
11+
}
12+
13+
export default Ember.Component({
14+
actions: {
15+
userInput() {
16+
anotherFunction();
17+
this.myAction();
18+
}
19+
}
20+
});`,
21+
parserOptions: {
22+
ecmaVersion: 6,
23+
sourceType: 'module'
24+
}
25+
}
26+
],
27+
invalid: [
28+
{
29+
code: `
30+
export default Ember.Component({
31+
actions: {
32+
userInput() {
33+
this.sendAction('myAction');
34+
}
35+
}
36+
});`,
37+
parserOptions: {
38+
ecmaVersion: 6,
39+
sourceType: 'module'
40+
},
41+
errors: [{
42+
message: MESSAGE
43+
}]
44+
}
45+
]
46+
});

0 commit comments

Comments
 (0)