-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
always-return.js
134 lines (130 loc) · 3.56 KB
/
always-return.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'use strict'
const rule = require('../rules/always-return')
const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})
const message = 'Each then() should return a value or throw'
ruleTester.run('always-return', rule, {
valid: [
'hey.then(x => x)',
'hey.then(x => ({}))',
'hey.then(x => { return; })',
'hey.then(x => { return x ? x.id : null })',
'hey.then(x => { return x * 10 })',
'hey.then(function() { return 42; })',
'hey.then(function() { return new Promise(); })',
'hey.then(function() { return "x"; }).then(doSomethingWicked)',
'hey.then(x => x).then(function() { return "3" })',
'hey.then(function() { throw new Error("msg"); })',
'hey.then(function(x) { if (!x) { throw new Error("no x"); } return x; })',
'hey.then(function(x) { if (x) { return x; } throw new Error("no x"); })',
'hey.then(x => { throw new Error("msg"); })',
'hey.then(x => { if (!x) { throw new Error("no x"); } return x; })',
'hey.then(x => { if (x) { return x; } throw new Error("no x"); })',
'hey.then(x => { var f = function() { }; return f; })',
'hey.then(x => { if (x) { return x; } else { return x; } })',
'hey.then(x => { return x; var y = "unreachable"; })',
'hey.then(x => { return x; return "unreachable"; })',
'hey.then(x => { return; }, err=>{ log(err); })',
'hey.then(x => { return x && x(); }, err=>{ log(err); })',
'hey.then(x => { return x.y || x(); }, err=>{ log(err); })',
`hey.then(x => {
return anotherFunc({
nested: {
one: x === 1 ? 1 : 0,
two: x === 2 ? 1 : 0
}
})
})`,
`hey.then(({x, y}) => {
if (y) {
throw new Error(x || y)
}
return x
})`,
],
invalid: [
{
code: 'hey.then(x => {})',
errors: [{ message }],
},
{
code: 'hey.then(function() { })',
errors: [{ message }],
},
{
code: 'hey.then(function() { }).then(x)',
errors: [{ message }],
},
{
code: 'hey.then(function() { }).then(function() { })',
errors: [{ message }, { message }],
},
{
code: 'hey.then(function() { return; }).then(function() { })',
errors: [{ message }],
},
{
code: 'hey.then(function() { doSomethingWicked(); })',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { return x; } })',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { return x; } else { }})',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { } else { return x; }})',
errors: [{ message }],
},
{
code: 'hey.then(function() { if (x) { return you.then(function() { return x; }); } })',
errors: [{ message }],
},
{
code: 'hey.then( x => { x ? x.id : null })',
errors: [{ message }],
},
{
code: 'hey.then(function(x) { x ? x.id : null })',
errors: [{ message }],
},
{
code: `(function() {
return hey.then(x => {
anotherFunc({
nested: {
one: x === 1 ? 1 : 0,
two: x === 2 ? 1 : 0
}
})
})
})()`,
errors: [{ message }],
},
{
code: `
hey.then(({x, y}) => {
if (y) {
throw new Error(x || y)
}
})`,
errors: [{ message }],
},
{
code: `
hey.then(({x, y}) => {
if (y) {
return x
}
})`,
errors: [{ message }],
},
],
})