Skip to content

Commit 33bccc9

Browse files
authored
Merge pull request #11 from chadhietala/no-attrs-snapshot
Add no-attrs-snapshot rule
2 parents ca5e4cb + 6083569 commit 33bccc9

File tree

3 files changed

+130
-7
lines changed

3 files changed

+130
-7
lines changed

RULES.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
These are rules we would like to enforce:
44

5-
no-attrs-snapshots
6-
no-timers
7-
no-mixins
8-
no-sub-classes
9-
detect-stateless-service
10-
eager-injections
11-
no-send-action
5+
- no-mixins
6+
- no-sub-classes
7+
- detect-stateless-service
8+
- eager-injections
9+
- no-send-action

lib/rules/no-attrs-snapshot.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function hasAttrsSnapShot(node) {
2+
let methodName = node.parent.key.name;
3+
let hasParams = node.params.length > 0;
4+
return (methodName === 'didReceiveAttrs' || methodName === 'didUpdateAttrs') && hasParams;
5+
}
6+
7+
const MESSAGE = 'Do not use the attrs snapshot that is passed in `didReceiveAttrs` and `didUpdateAttrs`. Please see the following guide for more infromation: https://github.com/chadhietala/ember-best-practices/blob/master/guides/rules/no-attrs-snapshot.md';
8+
9+
module.exports = {
10+
meta: {
11+
message: MESSAGE
12+
},
13+
create(context) {
14+
return {
15+
FunctionExpression(node) {
16+
if (hasAttrsSnapShot(node)) {
17+
context.report(node, MESSAGE);
18+
}
19+
}
20+
};
21+
}
22+
};

tests/lib/rules/no-attrs-snapshot.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
const rule = require('../../../lib/rules/no-attrs-snapshot');
2+
const MESSAGE = rule.meta.message;
3+
const RuleTester = require('eslint').RuleTester;
4+
const ruleTester = new RuleTester();
5+
6+
ruleTester.run('no-attrs-snapshot', rule, {
7+
valid: [
8+
{
9+
code: `
10+
export default Ember.Component({
11+
init() {
12+
this._super(...arguments);
13+
this._valueCache = this.value;
14+
this.updated = false;
15+
},
16+
didReceiveAttrs() {
17+
if (this._valueCache !== this.value) {
18+
this._valueCache = this.value;
19+
this.set('updated', true);
20+
} else {
21+
this.set('updated', false);
22+
}
23+
}
24+
});`,
25+
parserOptions: {
26+
ecmaVersion: 6,
27+
sourceType: 'module'
28+
}
29+
},
30+
{
31+
code: `
32+
export default Ember.Component({
33+
init() {
34+
this._super(...arguments);
35+
this._valueCache = this.value;
36+
this.updated = false;
37+
},
38+
didUpdateAttrs() {
39+
if (this._valueCache !== this.value) {
40+
this._valueCache = this.value;
41+
this.set('updated', true);
42+
} else {
43+
this.set('updated', false);
44+
}
45+
}
46+
});`,
47+
parserOptions: {
48+
ecmaVersion: 6,
49+
sourceType: 'module'
50+
}
51+
}
52+
],
53+
invalid: [
54+
{
55+
code: `
56+
export default Ember.Component({
57+
init() {
58+
this._super(...arguments);
59+
this.updated = false;
60+
},
61+
didReceiveAttrs(attrs) {
62+
let { newAttrs, oldAttrs } = attrs;
63+
if ((newAttrs && oldAttrs) && newAttrs.value !== oldAttrs.value) {
64+
this.set('updated', true);
65+
} else {
66+
this.set('updated', false);
67+
}
68+
}
69+
});`,
70+
parserOptions: {
71+
ecmaVersion: 6,
72+
sourceType: 'module'
73+
},
74+
errors: [{
75+
message: MESSAGE
76+
}]
77+
},
78+
{
79+
code: `
80+
export default Ember.Component({
81+
init() {
82+
this._super(...arguments);
83+
this.updated = false;
84+
},
85+
didUpdateAttrs(attrs) {
86+
let { newAttrs, oldAttrs } = attrs;
87+
if ((newAttrs && oldAttrs) && newAttrs.value !== oldAttrs.value) {
88+
this.set('updated', true);
89+
} else {
90+
this.set('updated', false);
91+
}
92+
}
93+
});`,
94+
parserOptions: {
95+
ecmaVersion: 6,
96+
sourceType: 'module'
97+
},
98+
errors: [{
99+
message: MESSAGE
100+
}]
101+
}
102+
]
103+
});

0 commit comments

Comments
 (0)