Skip to content

Commit 4a13457

Browse files
authored
Merge pull request #7 from chadhietala/change-events
Add rule for Ember.on usage
2 parents 69da37f + d23994d commit 4a13457

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

config/recommended.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
'ember-best-practices/no-action-cp': 2,
1515
'ember-best-practices/no-attrs': 2,
1616
'ember-best-practices/no-observers': 2,
17-
'ember-best-practices/require-dependent-keys': 2
17+
'ember-best-practices/require-dependent-keys': 2,
18+
'ember-best-practices/no-lifecycle-events': 2
1819
}
1920
};

lib/rules/no-lifecycle-events.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const MESSAGE = 'Do not use events for lifecycle hooks. Please use the actual hooks instead: https://github.com/chadhietala/ember-best-practices/blob/master/guides/rules/no-lifecycle-events.md';
2+
3+
function isOn(node) {
4+
return node.name === 'on';
5+
}
6+
7+
function isEmber(node) {
8+
return node.name === 'Ember';
9+
}
10+
11+
module.exports = {
12+
meta: {
13+
message: MESSAGE
14+
},
15+
create(context) {
16+
return {
17+
18+
ImportDeclaration(node) {
19+
if (node.source.value === '@ember/object/evented') {
20+
node.specifiers.forEach((specifier) => {
21+
if (isOn(specifier.imported)) {
22+
context.report(node, MESSAGE);
23+
}
24+
});
25+
}
26+
},
27+
28+
FunctionExpression(node) {
29+
if (node.parent.property && isOn(node.parent.property)) {
30+
context.report(node, MESSAGE);
31+
}
32+
},
33+
34+
MemberExpression(node) {
35+
if (isEmber(node.object) && isOn(node.property)) {
36+
context.report(node, MESSAGE);
37+
}
38+
},
39+
40+
CallExpression(node) {
41+
if (isOn(node.callee)) {
42+
context.getScope().variables.forEach((v) => {
43+
if (isOn(v)) {
44+
v.defs.forEach((defNode) => {
45+
if (defNode.node.type === 'VariableDeclarator' && isEmber(defNode.node.init)) {
46+
context.report(node, MESSAGE);
47+
}
48+
});
49+
}
50+
});
51+
}
52+
}
53+
};
54+
}
55+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const rule = require('../../../lib/rules/no-lifecycle-events');
2+
const MESSAGE = rule.meta.message;
3+
const RuleTester = require('eslint').RuleTester;
4+
const ruleTester = new RuleTester();
5+
6+
ruleTester.run('no-lifecycle-events', rule, {
7+
valid: [
8+
{
9+
code: `
10+
export default Ember.Component({
11+
didInsertElement() {
12+
this.$().on('focus', () => {
13+
alert('sss');
14+
});
15+
}
16+
});`,
17+
parserOptions: {
18+
ecmaVersion: 6,
19+
sourceType: 'module'
20+
}
21+
}
22+
],
23+
invalid: [
24+
{
25+
code: `
26+
export default Ember.Component({
27+
registerFocus: Ember.on('didInsertElement', function() {
28+
this.$().on('focus', () => {
29+
alert('sss');
30+
});
31+
})
32+
});`,
33+
parserOptions: {
34+
ecmaVersion: 6,
35+
sourceType: 'module'
36+
},
37+
errors: [{
38+
message: MESSAGE
39+
}]
40+
},
41+
{
42+
code: `
43+
const { on } = Ember;
44+
export default Ember.Component({
45+
registerFocus: on('didInsertElement', function() {
46+
this.$().on('focus', () => {
47+
alert('sss');
48+
});
49+
})
50+
});`,
51+
parserOptions: {
52+
ecmaVersion: 6,
53+
sourceType: 'module'
54+
},
55+
errors: [{
56+
message: MESSAGE
57+
}]
58+
},
59+
{
60+
code: `
61+
import { on } from '@ember/object/evented';
62+
export default Ember.Component({
63+
registerFocus: on('didInsertElement', function() {
64+
this.$().on('focus', () => {
65+
alert('sss');
66+
});
67+
})
68+
});`,
69+
parserOptions: {
70+
ecmaVersion: 6,
71+
sourceType: 'module'
72+
},
73+
errors: [{
74+
message: MESSAGE
75+
}]
76+
},
77+
{
78+
code: `
79+
export default Ember.Component({
80+
registerFocus: function() {
81+
this.$().on('focus', () => {
82+
alert('sss');
83+
});
84+
}.on('didInsertElement')
85+
});`,
86+
parserOptions: {
87+
ecmaVersion: 6,
88+
sourceType: 'module'
89+
},
90+
errors: [{
91+
message: MESSAGE
92+
}]
93+
}
94+
]
95+
});

0 commit comments

Comments
 (0)