forked from eslint-community/eslint-plugin-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-nesting.js
114 lines (105 loc) · 3.19 KB
/
no-nesting.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
'use strict'
const rule = require('../rules/no-nesting')
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})
const errorMessage = 'Avoid nesting promises.'
ruleTester.run('no-nesting', rule, {
valid: [
// resolve and reject are sometimes okay
'Promise.resolve(4).then(function(x) { return x })',
'Promise.reject(4).then(function(x) { return x })',
'Promise.resolve(4).then(function() {})',
'Promise.reject(4).then(function() {})',
// throw and return are fine
'doThing().then(function() { return 4 })',
'doThing().then(function() { throw 4 })',
'doThing().then(null, function() { return 4 })',
'doThing().then(null, function() { throw 4 })',
'doThing().catch(null, function() { return 4 })',
'doThing().catch(null, function() { throw 4 })',
// arrow functions and other things
'doThing().then(() => 4)',
'doThing().then(() => { throw 4 })',
'doThing().then(()=>{}, () => 4)',
'doThing().then(()=>{}, () => { throw 4 })',
'doThing().catch(() => 4)',
'doThing().catch(() => { throw 4 })',
// random functions and callback methods
'var x = function() { return Promise.resolve(4) }',
'function y() { return Promise.resolve(4) }',
'function then() { return Promise.reject() }',
'doThing(function(x) { return Promise.reject(x) })',
// this should be allowed basically, we're mostly raging against using then()
'doThing().then(function() { return Promise.all([a,b,c]) })',
'doThing().then(function() { return Promise.resolve(4) })',
'doThing().then(() => Promise.resolve(4))',
'doThing().then(() => Promise.all([a]))',
// references vars in closure
`doThing()
.then(a => getB(a)
.then(b => getC(a, b))
)`,
`doThing()
.then(a => {
const c = a * 2;
return getB(c).then(b => getC(c, b))
})`,
],
invalid: [
{
code: 'doThing().then(function() { a.then() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(function() { b.catch() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(function() { return a.then() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(function() { return b.catch() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(() => { a.then() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(() => { b.catch() })',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(() => a.then())',
errors: [{ message: errorMessage }],
},
{
code: 'doThing().then(() => b.catch())',
errors: [{ message: errorMessage }],
},
// references vars in closure
{
code: `
doThing()
.then(a => getB(a)
.then(b => getC(b))
)`,
errors: [{ message: errorMessage, line: 4 }],
},
{
code: `
doThing()
.then(a => getB(a)
.then(b => getC(a, b)
.then(c => getD(a, c))
)
)`,
errors: [{ message: errorMessage, line: 5 }],
},
],
})