Skip to content

Commit 0c5e626

Browse files
committed
Fix issues from review
1 parent 7ad02e2 commit 0c5e626

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

docs/rules/no-async-in-computed-properties.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
# Check if there are no asynchronous action inside computed properties (no-async-in-computed-properties)
22

3-
Vue.js has a basic construct which lets computed properties collects dependencies and refreshes them thats why its really important to not use any of asynchronous action inside of them.
3+
Computed properties should be synchronous. Asynchronous actions inside them may not work as expected and can lead to an unexpected behaviour, that's why you should avoid them.
4+
If you need async computed properties you might want to consider using additional plugin [vue-async-computed]
45

56
## :book: Rule Details
67

78
This rule is aimed at preventing asynchronous methods from being called in computed properties.
89

9-
1010
:-1: Examples of **incorrect** code for this rule:
1111

1212
```js
1313
export default {
1414
computed: {
1515
pro () {
16-
setTimeout(() => { }, 0)
1716
return Promise.all([new Promise((resolve, reject) => {})])
1817
},
1918
foo: async function () {
20-
setInterval(() => { }, 0)
2119
return await someFunc()
2220
},
2321
bar () {
24-
requestAnimationFrame(() => {})
2522
return fetch(url).then(response => {})
2623
},
2724
yiel: function* () {
2825
yield 1
2926
yield* g1()
27+
},
28+
tim () {
29+
setTimeout(() => { }, 0)
30+
},
31+
inter () {
32+
setInterval(() => { }, 0)
33+
},
34+
anim () {
35+
requestAnimationFrame(() => {})
3036
}
3137
}
3238
}
@@ -54,3 +60,5 @@ export default {
5460
## :wrench: Options
5561

5662
Nothing.
63+
64+
[vue-async-computed]: https://github.com/foxbenjaminfox/vue-async-computed

lib/rules/no-async-in-computed-properties.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview Check if there are no async inside computed properties.
2+
* @fileoverview Check if there are no asynchronous actions inside computed properties.
33
* @author Armano
44
*/
55
'use strict'
@@ -28,21 +28,23 @@ const TIMED_FUNCTIONS = [
2828

2929
function isTimedFunction (node) {
3030
return (
31+
node.type === 'CallExpression' &&
3132
node.callee.type === 'Identifier' &&
3233
TIMED_FUNCTIONS.indexOf(node.callee.name) !== -1
3334
) || (
35+
node.type === 'CallExpression' &&
3436
node.callee.type === 'MemberExpression' &&
37+
node.callee.object.type === 'Identifier' &&
3538
node.callee.object.name === 'window' && (
36-
TIMED_FUNCTIONS.indexOf(node.callee.property.name) !== -1 ||
37-
TIMED_FUNCTIONS.indexOf(node.callee.property.value) !== -1
39+
TIMED_FUNCTIONS.indexOf(node.callee.property.name) !== -1
3840
)
3941
) && node.arguments.length
4042
}
4143

4244
function isPromise (node) {
43-
if (node.type === 'CallExpression' && node.callee && node.callee.type === 'MemberExpression') {
45+
if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') {
4446
return ( // hello.PROMISE_FUNCTION()
45-
node.callee.property &&
47+
node.callee.property.type === 'Identifier' &&
4648
PROMISE_FUNCTIONS.indexOf(node.callee.property.name) !== -1
4749
) || ( // Promise.PROMISE_METHOD()
4850
node.callee.object.type === 'Identifier' &&
@@ -58,6 +60,16 @@ function isPromise (node) {
5860
function create (context) {
5961
const forbiddenNodes = []
6062

63+
const messages = {
64+
promise: 'Unexpected asynchronous action in "{{name}}" computed property.',
65+
await: 'Unexpected await operator in "{{name}}" computed property.',
66+
yield: 'Unexpected yield keyword in "{{name}}" computed property.',
67+
generator: 'Unexpected generator function expression in "{{name}}" computed property.',
68+
async: 'Unexpected async function declaration in "{{name}}" computed property.',
69+
new: 'Unexpected Promise object in "{{name}}" computed property.',
70+
timed: 'Unexpected timed function in "{{name}}" computed property.'
71+
}
72+
6173
function onFunctionEnter (node) {
6274
if (node.async) {
6375
forbiddenNodes.push({
@@ -68,7 +80,7 @@ function create (context) {
6880
if (node.generator) {
6981
forbiddenNodes.push({
7082
node: node,
71-
type: 'yield*'
83+
type: 'generator'
7284
})
7385
}
7486
}
@@ -130,25 +142,9 @@ function create (context) {
130142
el.node.loc.start.line >= cp.value.loc.start.line &&
131143
el.node.loc.end.line <= cp.value.loc.end.line
132144
) {
133-
let message = `Unexpected asynchronous action in "{{name}}" computed property.`
134-
if (el.type === 'await') {
135-
message = `Unexpected await operator in "{{name}}" computed property.`
136-
} else if (el.type === 'yield') {
137-
message = `Unexpected yield keyword in "{{name}}" computed property.`
138-
} else if (el.type === 'yield*') {
139-
message = `Unexpected yield* expression in "{{name}}" computed property.`
140-
} else if (el.type === 'async') {
141-
message = `Unexpected async function declaration in "{{name}}" computed property.`
142-
} else if (el.type === 'promise') {
143-
} else if (el.type === 'new') {
144-
message = `Unexpected Promise object in "{{name}}" computed property.`
145-
} else if (el.type === 'timed') {
146-
message = `Unexpected timed function in "{{name}}" computed property.`
147-
}
148-
149145
context.report({
150146
node: el.node,
151-
message,
147+
message: messages[el.type],
152148
data: {
153149
name: cp.key
154150
}
@@ -168,7 +164,7 @@ module.exports = {
168164
create,
169165
meta: {
170166
docs: {
171-
description: 'Check if there are no async inside computed properties.',
167+
description: 'Check if there are no asynchronous actions inside computed properties.',
172168
category: 'Best Practices',
173169
recommended: false
174170
},

tests/lib/rules/no-async-in-computed-properties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ ruleTester.run('no-async-in-computed-properties', rule, {
276276
`,
277277
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
278278
errors: [{
279-
message: 'Unexpected yield* expression in "foo" computed property.',
279+
message: 'Unexpected generator function expression in "foo" computed property.',
280280
line: 4
281281
}, {
282282
message: 'Unexpected yield keyword in "foo" computed property.',

0 commit comments

Comments
 (0)