Skip to content

Commit 2267b18

Browse files
committed
[Expiremental] Adding rule to black list 2.0.0 hooks
1 parent 4905ff1 commit 2267b18

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

config/expiremental.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
parserOptions: {
3+
ecmaVersion: 6,
4+
sourceType: 'module'
5+
},
6+
env: {
7+
'browser': true
8+
},
9+
plugins: [
10+
'ember-best-practices'
11+
],
12+
extends: require.resolve('./recommended.js'),
13+
rules: {
14+
// Expiremental rules
15+
'ember-best-practices/no-2.0.0-hooks': 2
16+
}
17+
};

lib/rules/no-2.0.0-hooks.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// TODO
2+
// Write docs for this once we feel it should be recommended.
3+
const MESSAGE = 'Do not use the 2.0.0 hooks as they are not conducive to the programming model.';
4+
const MISTAKE_HOOKS = ['didInitAttrs', 'didReceiveAttrs', 'didUpdateAttrs', 'didRender', 'willUpdate', 'didUpdate', 'willRender'];
5+
6+
module.exports = {
7+
meta: {
8+
message: MESSAGE
9+
},
10+
create(context) {
11+
return {
12+
ObjectExpression(node) {
13+
node.properties.forEach((property) => {
14+
let name = property.key.name;
15+
if (MISTAKE_HOOKS.indexOf(name) > -1) {
16+
context.report(property, MESSAGE);
17+
}
18+
});
19+
}
20+
};
21+
}
22+
};

tests/lib/rules/no-2.0.0-hooks.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
const rule = require('../../../lib/rules/no-2.0.0-hooks');
2+
const MESSAGE = rule.meta.message;
3+
const RuleTester = require('eslint').RuleTester;
4+
const ruleTester = new RuleTester();
5+
6+
ruleTester.run('no-2.0.0-hooks', rule, {
7+
valid: [
8+
{
9+
code: `
10+
export default Ember.Component({
11+
foo: 'bar',
12+
baz: false,
13+
actions: {
14+
userInput() {
15+
this.set('baz', true);
16+
}
17+
}
18+
});`,
19+
parserOptions: {
20+
ecmaVersion: 6,
21+
sourceType: 'module'
22+
}
23+
}
24+
],
25+
invalid: [
26+
{
27+
code: `
28+
export default Ember.Component({
29+
didInitAttrs() {
30+
this._super(...arguments);
31+
this.nope = true;
32+
}
33+
});`,
34+
parserOptions: {
35+
ecmaVersion: 6,
36+
sourceType: 'module'
37+
},
38+
errors: [{
39+
message: MESSAGE
40+
}]
41+
},
42+
{
43+
code: `
44+
export default Ember.Component({
45+
didReceiveAttrs() {
46+
this._super(...arguments);
47+
this.nope = true;
48+
}
49+
});`,
50+
parserOptions: {
51+
ecmaVersion: 6,
52+
sourceType: 'module'
53+
},
54+
errors: [{
55+
message: MESSAGE
56+
}]
57+
},
58+
{
59+
code: `
60+
export default Ember.Component({
61+
didUpdateAttrs() {
62+
this._super(...arguments);
63+
this.nope = true;
64+
}
65+
});`,
66+
parserOptions: {
67+
ecmaVersion: 6,
68+
sourceType: 'module'
69+
},
70+
errors: [{
71+
message: MESSAGE
72+
}]
73+
},
74+
{
75+
code: `
76+
export default Ember.Component({
77+
didRender() {
78+
this._super(...arguments);
79+
this.nope = true;
80+
}
81+
});`,
82+
parserOptions: {
83+
ecmaVersion: 6,
84+
sourceType: 'module'
85+
},
86+
errors: [{
87+
message: MESSAGE
88+
}]
89+
},
90+
{
91+
code: `
92+
export default Ember.Component({
93+
didUpdate() {
94+
this._super(...arguments);
95+
this.nope = true;
96+
}
97+
});`,
98+
parserOptions: {
99+
ecmaVersion: 6,
100+
sourceType: 'module'
101+
},
102+
errors: [{
103+
message: MESSAGE
104+
}]
105+
},
106+
{
107+
code: `
108+
export default Ember.Component({
109+
willUpdate() {
110+
this._super(...arguments);
111+
this.nope = true;
112+
}
113+
});`,
114+
parserOptions: {
115+
ecmaVersion: 6,
116+
sourceType: 'module'
117+
},
118+
errors: [{
119+
message: MESSAGE
120+
}]
121+
},
122+
{
123+
code: `
124+
export default Ember.Component({
125+
willRender() {
126+
this._super(...arguments);
127+
this.nope = true;
128+
}
129+
});`,
130+
parserOptions: {
131+
ecmaVersion: 6,
132+
sourceType: 'module'
133+
},
134+
errors: [{
135+
message: MESSAGE
136+
}]
137+
}
138+
]
139+
});

0 commit comments

Comments
 (0)