Skip to content

Commit 790decc

Browse files
committed
Add ywait & yield & async & promise
1 parent 0ddca03 commit 790decc

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

lib/recommended-rules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
"vue/html-no-self-closing": "off",
99
"vue/html-quotes": "off",
1010
"vue/jsx-uses-vars": "error",
11+
"vue/no-async-in-computed-properties": "off",
1112
"vue/no-confusing-v-for-v-if": "error",
1213
"vue/no-duplicate-attributes": "off",
1314
"vue/no-invalid-template-root": "error",
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/**
2+
* @fileoverview Check if there are no async inside computed properties
3+
* @author Armano
4+
*/
5+
'use strict'
6+
7+
const utils = require('../utils')
8+
9+
var PROMISE_FUNCTIONS = [
10+
'then',
11+
'catch',
12+
'finally'
13+
]
14+
15+
var PROMISE_METHODS = [
16+
'all',
17+
'race',
18+
'reject',
19+
'resolve'
20+
]
21+
22+
function isPromise (node) {
23+
if (node.type === 'CallExpression' && node.callee && node.callee.type === 'MemberExpression') {
24+
return ( // hello.then() || hello.catch()
25+
node.callee.property &&
26+
PROMISE_FUNCTIONS.indexOf(node.callee.property.name) !== -1
27+
) || ( // Promise.STATIC_METHOD()
28+
node.callee.object.type === 'Identifier' &&
29+
node.callee.object.name === 'Promise' &&
30+
PROMISE_METHODS.indexOf(node.callee.property.name) !== -1
31+
) || ( // somePromise.ANYTHING()
32+
node.callee.object && isPromise(node.callee.object)
33+
)
34+
}
35+
return false
36+
}
37+
38+
function create (context) {
39+
const forbiddenNodes = []
40+
41+
function onFunctionEnter (node) {
42+
if (node.async) {
43+
forbiddenNodes.push({
44+
node: node,
45+
type: 'async'
46+
})
47+
}
48+
}
49+
50+
return Object.assign({},
51+
{
52+
FunctionDeclaration: onFunctionEnter,
53+
54+
FunctionExpression: onFunctionEnter,
55+
56+
ArrowFunctionExpression: onFunctionEnter,
57+
58+
CallExpression (node) {
59+
if (isPromise(node)) {
60+
forbiddenNodes.push({
61+
node: node,
62+
type: 'promise'
63+
})
64+
}
65+
},
66+
67+
YieldExpression (node) {
68+
// await nodes are YieldExpression's with babel-eslint < 7.0.0
69+
forbiddenNodes.push({
70+
node: node,
71+
type: 'yield'
72+
})
73+
},
74+
75+
AwaitExpression (node) {
76+
forbiddenNodes.push({
77+
node: node,
78+
type: 'await'
79+
})
80+
}
81+
},
82+
utils.executeOnVueComponent(context, (properties) => {
83+
const computedProperties = utils.getComputedProperties(properties)
84+
85+
computedProperties.forEach(cp => {
86+
forbiddenNodes.forEach(({ type, node }) => {
87+
if (
88+
node.loc.start.line >= cp.value.loc.start.line &&
89+
node.loc.end.line <= cp.value.loc.end.line
90+
) {
91+
if (type === 'await') {
92+
context.report({
93+
node: node,
94+
message: `Unexpected await in "${cp.key}" computed property.`
95+
})
96+
} else if (type === 'yield') {
97+
context.report({
98+
node: node,
99+
message: `Unexpected yield in "${cp.key}" computed property.`
100+
})
101+
} else if (type === 'async') {
102+
context.report({
103+
node: node,
104+
message: `Unexpected async in "${cp.key}" computed property.`
105+
})
106+
} else if (type === 'promise') {
107+
context.report({
108+
node: node,
109+
message: `Unexpected asynchronous action in "${cp.key}" computed property.`
110+
})
111+
}
112+
}
113+
})
114+
})
115+
})
116+
)
117+
}
118+
119+
// ------------------------------------------------------------------------------
120+
// Rule Definition
121+
// ------------------------------------------------------------------------------
122+
123+
module.exports = {
124+
create,
125+
meta: {
126+
docs: {
127+
description: 'Check if there are no async inside computed properties',
128+
category: 'Best Practices',
129+
recommended: false
130+
},
131+
fixable: null,
132+
schema: []
133+
}
134+
}

0 commit comments

Comments
 (0)