/
prefer-await-to-then.js
53 lines (49 loc) · 1.41 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
'use strict'
const rule = require('../rules/prefer-await-to-then')
const RuleTester = require('eslint').RuleTester
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 => {}) }',
],
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 }],
},
],
})