Skip to content

Commit 6b175fd

Browse files
committed
Add no-side-effects-in-computed-properties rule
1 parent 8f5e2e8 commit 6b175fd

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Do not introduce side effects in computed properties (no-side-effects-in-computed-properties)
2+
3+
It is considered a very bad practice to introduce side effects inside computed properties. It makes the code not predictable and hard to understand.
4+
5+
6+
## Rule Details
7+
8+
Examples of **incorrect** code for this rule:
9+
10+
```js
11+
12+
export default {
13+
computed: {
14+
fullName() {
15+
this.firstName = 'lorem' // <- side effect
16+
return `${this.firstName} ${this.lastName}`
17+
},
18+
somethingReversed() {
19+
return this.something.reverse() // <- side effect
20+
}
21+
}
22+
}
23+
24+
```
25+
26+
Examples of **correct** code for this rule:
27+
28+
```js
29+
30+
export default {
31+
computed: {
32+
fullName() {
33+
return `${this.firstName} ${this.lastName}`
34+
},
35+
somethingReversed() {
36+
return this.something.slice(0).reverse()
37+
}
38+
}
39+
}
40+
41+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @fileoverview Don't introduce side effects in computed properties
3+
* @author Michał Sajnóg
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description: "Don't introduce side effects in computed properties",
15+
category: "Fill me in",
16+
recommended: false
17+
},
18+
fixable: null, // or "code" or "whitespace"
19+
schema: [
20+
// fill in your schema
21+
]
22+
},
23+
24+
create: function(context) {
25+
26+
// variables should be defined here
27+
28+
//----------------------------------------------------------------------
29+
// Helpers
30+
//----------------------------------------------------------------------
31+
32+
// any helper functions should go here or else delete this section
33+
34+
//----------------------------------------------------------------------
35+
// Public
36+
//----------------------------------------------------------------------
37+
38+
return {
39+
40+
// give me methods
41+
42+
};
43+
}
44+
};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @fileoverview Don&#39;t introduce side effects in computed properties
3+
* @author Michał Sajnóg
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
var rule = require("../../../lib/rules/no-side-effects-in-computed-properties"),
12+
13+
RuleTester = require("eslint").RuleTester;
14+
15+
16+
//------------------------------------------------------------------------------
17+
// Tests
18+
//------------------------------------------------------------------------------
19+
20+
var ruleTester = new RuleTester();
21+
ruleTester.run("no-side-effects-in-computed-properties", rule, {
22+
23+
valid: [
24+
25+
// give me some code that won't trigger a warning
26+
],
27+
28+
invalid: [
29+
{
30+
code: "",
31+
errors: [{
32+
message: "Fill me in.",
33+
type: "Me too"
34+
}]
35+
}
36+
]
37+
});

0 commit comments

Comments
 (0)