forked from eslint-community/eslint-plugin-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefer-await-to-then.js
58 lines (54 loc) · 1.59 KB
/
prefer-await-to-then.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
'use strict'
const rule = require('../rules/prefer-await-to-then')
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
},
})
const message = 'Prefer await to then()/catch()/finally().'
ruleTester.run('prefer-await-to-then', rule, {
valid: [
'async function hi() { await thing() }',
'async function hi() { await thing().then() }',
'async function hi() { await thing().catch() }',
'a = async () => (await something())',
`a = async () => {
try { await something() } catch (error) { somethingElse() }
}`,
'something().then(async () => await somethingElse())',
'function foo() { hey.somethingElse(x => {}) }',
`const isThenable = (obj) => {
return obj && typeof obj.then === 'function';
};`,
`function isThenable(obj) {
return obj && typeof obj.then === 'function';
}`,
],
invalid: [
{
code: 'function foo() { hey.then(x => {}) }',
errors: [{ message }],
},
{
code: 'function foo() { hey.then(function() { }).then() }',
errors: [{ message }, { message }],
},
{
code: 'function foo() { hey.then(function() { }).then(x).catch() }',
errors: [{ message }, { message }, { message }],
},
{
code: 'async function a() { hey.then(function() { }).then(function() { }) }',
errors: [{ message }, { message }],
},
{
code: 'function foo() { hey.catch(x => {}) }',
errors: [{ message }],
},
{
code: 'function foo() { hey.finally(x => {}) }',
errors: [{ message }],
},
],
})