-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-async-in-computed-properties.js
146 lines (129 loc) · 4 KB
/
no-async-in-computed-properties.js
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
/**
* @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',
categories: ['vue3-essential', 'essential'],
url: 'https://eslint.vuejs.org/rules/no-async-in-computed-properties.html'
},
fixable: null,
schema: []
},
create (context) {
const computedPropertiesMap = new Map()
let scopeStack = { upper: null, body: null }
const expressionTypes = {
promise: 'asynchronous action',
await: 'await operator',
async: 'async function declaration',
new: 'Promise object',
timed: 'timed function'
}
function onFunctionEnter (node, { node: vueNode }) {
if (node.async) {
verify(node, node.body, 'async', computedPropertiesMap.get(vueNode))
}
scopeStack = { upper: scopeStack, body: node.body }
}
function onFunctionExit () {
scopeStack = scopeStack.upper
}
function verify (node, targetBody, type, computedProperties) {
computedProperties.forEach(cp => {
if (
cp.value &&
node.loc.start.line >= cp.value.loc.start.line &&
node.loc.end.line <= cp.value.loc.end.line &&
targetBody === cp.value
) {
context.report({
node: node,
message: 'Unexpected {{expressionName}} in "{{propertyName}}" computed property.',
data: {
expressionName: expressionTypes[type],
propertyName: cp.key
}
})
}
})
}
return utils.defineVueVisitor(context, {
ObjectExpression (node, { node: vueNode }) {
if (node !== vueNode) {
return
}
computedPropertiesMap.set(node, utils.getComputedProperties(node))
},
':function': onFunctionEnter,
':function:exit': onFunctionExit,
NewExpression (node, { node: vueNode }) {
if (node.callee.name === 'Promise') {
verify(node, scopeStack.body, 'new', computedPropertiesMap.get(vueNode))
}
},
CallExpression (node, { node: vueNode }) {
if (isPromise(node)) {
verify(node, scopeStack.body, 'promise', computedPropertiesMap.get(vueNode))
} else if (isTimedFunction(node)) {
verify(node, scopeStack.body, 'timed', computedPropertiesMap.get(vueNode))
}
},
AwaitExpression (node, { node: vueNode }) {
verify(node, scopeStack.body, 'await', computedPropertiesMap.get(vueNode))
}
})
}
}