forked from eslint-community/eslint-plugin-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-promise-in-callback.js
93 lines (83 loc) · 2.63 KB
/
no-promise-in-callback.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
'use strict'
const rule = require('../rules/no-promise-in-callback')
const { RuleTester } = require('./rule-tester')
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})
const errorMessage = 'Avoid using promises inside of callbacks.'
ruleTester.run('no-promise-in-callback', rule, {
valid: [
'go(function() { return Promise.resolve(4) })',
'go(function() { return a.then(b) })',
'go(function() { b.catch(c) })',
'go(function() { b.then(c, d) })',
// arrow functions and other things
'go(() => Promise.resolve(4))',
'go((errrr) => a.then(b))',
'go((helpers) => { b.catch(c) })',
'go((e) => { b.then(c, d) })',
// within promises it won't complain
'a.catch((err) => { b.then(c, d) })',
// random unrelated things
'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) })',
'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]))',
// weird case, we assume it's not a big deal if you return (even though you may be cheating)
'a(function(err) { return doThing().then(a) })',
],
invalid: [
{
code: 'a(function(err) { doThing().then(a) })',
errors: [{ message: errorMessage }],
},
{
code: 'a(function(error, zup, supa) { doThing().then(a) })',
errors: [{ message: errorMessage }],
},
{
code: 'a(function(error) { doThing().then(a) })',
errors: [{ message: errorMessage }],
},
// arrow function
{
code: 'a((error) => { doThing().then(a) })',
errors: [{ message: errorMessage }],
},
{
code: 'a((error) => doThing().then(a))',
errors: [{ message: errorMessage }],
},
{
code: 'a((err, data) => { doThing().then(a) })',
errors: [{ message: errorMessage }],
},
{
code: 'a((err, data) => doThing().then(a))',
errors: [{ message: errorMessage }],
},
// function decl. and similar (why not)
{
code: 'function x(err) { Promise.all() }',
errors: [{ message: errorMessage }],
},
{
code: 'function x(err) { Promise.allSettled() }',
errors: [{ message: errorMessage }],
},
{
code: 'function x(err) { Promise.any() }',
errors: [{ message: errorMessage }],
},
{
code: 'let x = (err) => doThingWith(err).then(a)',
errors: [{ message: errorMessage }],
},
],
})