-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathvalid-next-tick.js
211 lines (190 loc) · 5.74 KB
/
valid-next-tick.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @fileoverview enforce valid `nextTick` function calls
* @author Flo Edelmann
* @copyright 2021 Flo Edelmann. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const { findVariable } = require('@eslint-community/eslint-utils')
/**
* @param {Identifier} identifier
* @param {RuleContext} context
* @returns {ASTNode|undefined}
*/
function getVueNextTickNode(identifier, context) {
// Instance API: this.$nextTick()
if (
identifier.name === '$nextTick' &&
identifier.parent.type === 'MemberExpression' &&
utils.isThis(identifier.parent.object, context)
) {
return identifier.parent
}
// Vue 2 Global API: Vue.nextTick()
if (
identifier.name === 'nextTick' &&
identifier.parent.type === 'MemberExpression' &&
identifier.parent.object.type === 'Identifier' &&
identifier.parent.object.name === 'Vue'
) {
return identifier.parent
}
// Vue 3 Global API: import { nextTick as nt } from 'vue'; nt()
const variable = findVariable(utils.getScope(context, identifier), identifier)
if (variable != null && variable.defs.length === 1) {
const def = variable.defs[0]
if (
def.type === 'ImportBinding' &&
def.node.type === 'ImportSpecifier' &&
def.node.imported.type === 'Identifier' &&
def.node.imported.name === 'nextTick' &&
def.node.parent.type === 'ImportDeclaration' &&
def.node.parent.source.value === 'vue'
) {
return identifier
}
}
return undefined
}
/**
* @param {CallExpression} callExpression
* @returns {boolean}
*/
function isAwaitedPromise(callExpression) {
if (callExpression.parent.type === 'AwaitExpression') {
// cases like `await nextTick()`
return true
}
if (callExpression.parent.type === 'ReturnStatement') {
// cases like `return nextTick()`
return true
}
if (
callExpression.parent.type === 'ArrowFunctionExpression' &&
callExpression.parent.body === callExpression
) {
// cases like `() => nextTick()`
return true
}
if (
callExpression.parent.type === 'MemberExpression' &&
callExpression.parent.property.type === 'Identifier' &&
callExpression.parent.property.name === 'then'
) {
// cases like `nextTick().then()`
return true
}
if (
callExpression.parent.type === 'VariableDeclarator' ||
callExpression.parent.type === 'AssignmentExpression'
) {
// cases like `let foo = nextTick()` or `foo = nextTick()`
return true
}
if (
callExpression.parent.type === 'ArrayExpression' &&
callExpression.parent.parent.type === 'CallExpression' &&
callExpression.parent.parent.callee.type === 'MemberExpression' &&
callExpression.parent.parent.callee.object.type === 'Identifier' &&
callExpression.parent.parent.callee.object.name === 'Promise' &&
callExpression.parent.parent.callee.property.type === 'Identifier'
) {
// cases like `Promise.all([nextTick()])`
return true
}
return false
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce valid `nextTick` function calls',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/valid-next-tick.html'
},
fixable: 'code',
hasSuggestions: true,
schema: [],
messages: {
shouldBeFunction: '`nextTick` is a function.',
missingCallbackOrAwait:
'Await the Promise returned by `nextTick` or pass a callback function.',
addAwait: 'Add missing `await` statement.',
tooManyParameters: '`nextTick` expects zero or one parameters.',
eitherAwaitOrCallback:
'Either await the Promise or pass a callback function to `nextTick`.'
}
},
/** @param {RuleContext} context */
create(context) {
return utils.defineVueVisitor(context, {
/** @param {Identifier} node */
Identifier(node) {
const nextTickNode = getVueNextTickNode(node, context)
if (!nextTickNode || !nextTickNode.parent) {
return
}
let parentNode = nextTickNode.parent
// skip conditional expressions like `foo ? nextTick : bar`
if (parentNode.type === 'ConditionalExpression') {
parentNode = parentNode.parent
}
if (
parentNode.type === 'CallExpression' &&
parentNode.callee !== nextTickNode
) {
// cases like `foo.then(nextTick)` are allowed
return
}
if (
parentNode.type === 'VariableDeclarator' ||
parentNode.type === 'AssignmentExpression'
) {
// cases like `let foo = nextTick` or `foo = nextTick` are allowed
return
}
if (parentNode.type !== 'CallExpression') {
context.report({
node,
messageId: 'shouldBeFunction',
fix(fixer) {
return fixer.insertTextAfter(node, '()')
}
})
return
}
if (parentNode.arguments.length === 0) {
if (!isAwaitedPromise(parentNode)) {
context.report({
node,
messageId: 'missingCallbackOrAwait',
suggest: [
{
messageId: 'addAwait',
fix(fixer) {
return fixer.insertTextBefore(parentNode, 'await ')
}
}
]
})
}
return
}
if (parentNode.arguments.length > 1) {
context.report({
node,
messageId: 'tooManyParameters'
})
return
}
if (isAwaitedPromise(parentNode)) {
context.report({
node,
messageId: 'eitherAwaitOrCallback'
})
}
}
})
}
}