1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* @fileoverview Check if there are no asynchronous actions inside computed properties.
* @author Armano
*/
'use strict'
const utils = require('../utils')
const PROMISE_FUNCTIONS = [
'then',
'catch',
'finally'
]
const PROMISE_METHODS = [
'all',
'race',
'reject',
'resolve'
]
const TIMED_FUNCTIONS = [
'setTimeout',
'setInterval',
'setImmediate',
'requestAnimationFrame'
]
function isTimedFunction (node) {
return ((
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
TIMED_FUNCTIONS.indexOf(node.callee.name) !== -1
) || (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'window' && (
TIMED_FUNCTIONS.indexOf(node.callee.property.name) !== -1
)
)) && node.arguments.length
}
function isPromise (node) {
if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression') {
return ( // hello.PROMISE_FUNCTION()
node.callee.property.type === 'Identifier' &&
PROMISE_FUNCTIONS.indexOf(node.callee.property.name) !== -1
) || ( // Promise.PROMISE_METHOD()
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'Promise' &&
PROMISE_METHODS.indexOf(node.callee.property.name) !== -1
)
}
return false
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow asynchronous actions in computed properties',
category: 'essential',
url: 'https://eslint.vuejs.org/rules/no-async-in-computed-properties.html'
},
fixable: null,
schema: []
},
create (context) {
const forbiddenNodes = []
const allowedScopes = []
const expressionTypes = {
promise: 'asynchronous action',
await: 'await operator',
async: 'async function declaration',
new: 'Promise object',
timed: 'timed function'
}
function onFunctionEnter (node) {
if (node.async) {
forbiddenNodes.push({
node: node,
type: 'async'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
}
return Object.assign({},
{
FunctionDeclaration: onFunctionEnter,
FunctionExpression: onFunctionEnter,
ArrowFunctionExpression: onFunctionEnter,
NewExpression (node) {
if (node.callee.name === 'Promise') {
forbiddenNodes.push({
node: node,
type: 'new'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},
CallExpression (node) {
if (isPromise(node)) {
forbiddenNodes.push({
node: node,
type: 'promise'
})
} else if (isTimedFunction(node)) {
forbiddenNodes.push({
node: node,
type: 'timed'
})
} else if (node.parent.type === 'ReturnStatement') {
allowedScopes.push(node)
}
},
AwaitExpression (node) {
forbiddenNodes.push({
node: node,
type: 'await'
})
},
'ReturnStatement' (node) {
if (
node.argument &&
(
node.argument.type === 'ObjectExpression' ||
node.argument.type === 'ArrayExpression'
)
) {
allowedScopes.push(node.argument)
}
}
},
utils.executeOnVue(context, (obj) => {
const computedProperties = utils.getComputedProperties(obj)
computedProperties.forEach(cp => {
forbiddenNodes.forEach(el => {
if (
cp.value &&
el.node.loc.start.line >= cp.value.loc.start.line &&
el.node.loc.end.line <= cp.value.loc.end.line &&
!allowedScopes.some(scope =>
scope.range[0] < el.node.range[0] &&
scope.range[1] > el.node.range[1]
)
) {
context.report({
node: el.node,
message: 'Unexpected {{expressionName}} in "{{propertyName}}" computed property.',
data: {
expressionName: expressionTypes[el.type],
propertyName: cp.key
}
})
}
})
})
})
)
}
}