Skip to content

Commit 39016d1

Browse files
committed
Fix tests & add checks for promises
1 parent 886b45d commit 39016d1

File tree

2 files changed

+97
-54
lines changed

2 files changed

+97
-54
lines changed

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

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,38 @@
66

77
const utils = require('../utils')
88

9-
// const methodNames = [
10-
// 'then',
11-
// 'catch'
12-
// ]
9+
var PROMISE_FUNCTIONS = [
10+
'then',
11+
'catch',
12+
'finally'
13+
]
1314

14-
/**
15-
* Checks if a node has a promise method called.
16-
*/
17-
// function hasPromiseMethod (node) {
18-
// if (node.type === 'CallExpression' && node.callee) {
19-
// var property = node.callee.property
20-
// if (property && methodNames.indexOf(property.name) >= 0) {
21-
// return property.name
22-
// }
23-
// }
24-
// return false
25-
// }
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+
}
2637

2738
function create (context) {
2839
const forbiddenNodes = []
2940

30-
function onFunctionExit (node) {
31-
}
32-
3341
function onFunctionEnter (node) {
3442
if (node.async) {
3543
forbiddenNodes.push({
@@ -42,13 +50,19 @@ function create (context) {
4250
return Object.assign({},
4351
{
4452
FunctionDeclaration: onFunctionEnter,
45-
'FunctionDeclaration:exit': onFunctionExit,
4653

4754
FunctionExpression: onFunctionEnter,
48-
'FunctionExpression:exit': onFunctionExit,
4955

5056
ArrowFunctionExpression: onFunctionEnter,
51-
'ArrowFunctionExpression:exit': onFunctionExit,
57+
58+
CallExpression (node) {
59+
if (isPromise(node)) {
60+
forbiddenNodes.push({
61+
node: node,
62+
type: 'promise'
63+
})
64+
}
65+
},
5266

5367
YieldExpression (node) {
5468
// await nodes are YieldExpression's with babel-eslint < 7.0.0
@@ -74,10 +88,27 @@ function create (context) {
7488
node.loc.start.line >= cp.value.loc.start.line &&
7589
node.loc.end.line <= cp.value.loc.end.line
7690
) {
77-
context.report({
78-
node: node,
79-
message: `Unexpected ${type} effect in "${cp.key}" computed property.`
80-
})
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+
}
81112
}
82113
})
83114
})

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

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,30 @@ const rule = require('../../../lib/rules/no-async-in-computed-properties')
1515
// Tests
1616
// ------------------------------------------------------------------------------
1717

18-
const ruleTester = new RuleTester({
19-
parser: 'vue-eslint-parser',
20-
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }
21-
})
18+
const ruleTester = new RuleTester()
2219

2320
ruleTester.run('no-async-in-computed-properties', rule, {
2421

2522
valid: [
26-
{
27-
filename: 'test.vue',
28-
code: 'computed: { foo: function () { return this.aaa } }'
29-
},
3023
{
3124
filename: 'test.vue',
3225
code: `
33-
computed: {
34-
foo: function () {
35-
var bar = 0
36-
try {
37-
bar = bar / 0
38-
} catch (e) {
39-
return e
40-
} finally {
41-
return bar
26+
export default {
27+
computed: {
28+
foo: function () {
29+
var bar = 0
30+
try {
31+
bar = bar / 0
32+
} catch (e) {
33+
return e
34+
} finally {
35+
return bar
36+
}
4237
}
4338
}
4439
}
45-
`
40+
`,
41+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
4642
}
4743
],
4844

@@ -58,7 +54,11 @@ ruleTester.run('no-async-in-computed-properties', rule, {
5854
}
5955
}
6056
`,
61-
errors: ['Computed properies cannot have side effect.']
57+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
58+
errors: [
59+
'Unexpected await in "foo" computed property.',
60+
'Unexpected async in "foo" computed property.'
61+
]
6262
},
6363
{
6464
filename: 'test.vue',
@@ -71,7 +71,11 @@ ruleTester.run('no-async-in-computed-properties', rule, {
7171
}
7272
}
7373
`,
74-
errors: ['Computed properies cannot have side effect.']
74+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
75+
errors: [
76+
'Unexpected asynchronous action in "foo" computed property.',
77+
'Unexpected async in "foo" computed property.'
78+
]
7579
},
7680
{
7781
filename: 'test.vue',
@@ -84,7 +88,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
8488
}
8589
}
8690
`,
87-
errors: ['Computed properies cannot have side effect.']
91+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
92+
errors: ['Unexpected asynchronous action in "foo" computed property.']
8893
},
8994
{
9095
filename: 'test.vue',
@@ -97,7 +102,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
97102
}
98103
}
99104
`,
100-
errors: ['Computed properies cannot have side effect.']
105+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
106+
errors: ['Unexpected asynchronous action in "foo" computed property.']
101107
},
102108
{
103109
filename: 'test.vue',
@@ -110,7 +116,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
110116
}
111117
}
112118
`,
113-
errors: ['Computed properies cannot have side effect.']
119+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
120+
errors: ['Unexpected asynchronous action in "foo" computed property.']
114121
},
115122
{
116123
filename: 'test.vue',
@@ -123,7 +130,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
123130
}
124131
}
125132
`,
126-
errors: ['Computed properies cannot have side effect.']
133+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
134+
errors: ['Unexpected asynchronous action in "foo" computed property.']
127135
},
128136
{
129137
filename: 'test.vue',
@@ -136,7 +144,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
136144
}
137145
}
138146
`,
139-
errors: ['Computed properies cannot have side effect.']
147+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
148+
errors: ['Unexpected asynchronous action in "foo" computed property.']
140149
},
141150
{
142151
filename: 'test.vue',
@@ -149,7 +158,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
149158
}
150159
}
151160
`,
152-
errors: ['Computed properies cannot have side effect.']
161+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
162+
errors: ['Unexpected asynchronous action in "foo" computed property.']
153163
},
154164
{
155165
filename: 'test.vue',
@@ -162,7 +172,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
162172
}
163173
}
164174
`,
165-
errors: ['Computed properies cannot have side effect.']
175+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
176+
errors: ['Unexpected asynchronous action in "foo" computed property.']
166177
},
167178
{
168179
filename: 'test.vue',
@@ -177,7 +188,8 @@ ruleTester.run('no-async-in-computed-properties', rule, {
177188
}
178189
}
179190
`,
180-
errors: ['Computed properies cannot have side effect.']
191+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
192+
errors: ['Unexpected asynchronous action in "foo" computed property.']
181193
}
182194
]
183195
})

0 commit comments

Comments
 (0)