forked from mysticatea/eslint-plugin-node
-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
handle-callback-err.js
155 lines (152 loc) · 5.82 KB
/
handle-callback-err.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* @author Jamund Ferguson
* See LICENSE file in root directory for full license.
*/
"use strict"
const RuleTester = require("#test-helpers").RuleTester
const rule = require("../../../lib/rules/handle-callback-err")
const ruleTester = new RuleTester()
const EXPECTED_DECL_ERROR = {
messageId: "expected",
type: "FunctionDeclaration",
}
const EXPECTED_FUNC_ERROR = {
messageId: "expected",
type: "FunctionExpression",
}
ruleTester.run("handle-callback-err", rule, {
valid: [
"function test(error) {}",
"function test(err) {console.log(err);}",
"function test(err, data) {if(err){ data = 'ERROR';}}",
"var test = function(err) {console.log(err);};",
"var test = function(err) {if(err){/* do nothing */}};",
"var test = function(err) {if(!err){doSomethingHere();}else{};}",
"var test = function(err, data) {if(!err) { good(); } else { bad(); }}",
"try { } catch(err) {}",
"getData(function(err, data) {if (err) {}getMoreDataWith(data, function(err, moreData) {if (err) {}getEvenMoreDataWith(moreData, function(err, allOfTheThings) {if (err) {}});});});",
"var test = function(err) {if(! err){doSomethingHere();}};",
"function test(err, data) {if (data) {doSomething(function(err) {console.error(err);});} else if (err) {console.log(err);}}",
"function handler(err, data) {if (data) {doSomethingWith(data);} else if (err) {console.log(err);}}",
"function handler(err) {logThisAction(function(err) {if (err) {}}); console.log(err);}",
"function userHandler(err) {process.nextTick(function() {if (err) {}})}",
"function help() { function userHandler(err) {function tester() { err; process.nextTick(function() { err; }); } } }",
"function help(done) { var err = new Error('error'); done(); }",
{
code: "var test = err => err;",
},
{
code: "var test = err => !err;",
},
{
code: "var test = err => err.message;",
},
{
code: "var test = function(error) {if(error){/* do nothing */}};",
options: ["error"],
},
{
code: "var test = (error) => {if(error){/* do nothing */}};",
options: ["error"],
},
{
code: "var test = function(error) {if(! error){doSomethingHere();}};",
options: ["error"],
},
{
code: "var test = function(err) { console.log(err); };",
options: ["^(err|error)$"],
},
{
code: "var test = function(error) { console.log(error); };",
options: ["^(err|error)$"],
},
{
code: "var test = function(anyError) { console.log(anyError); };",
options: ["^.+Error$"],
},
{
code: "var test = function(any_error) { console.log(anyError); };",
options: ["^.+Error$"],
},
{
code: "var test = function(any_error) { console.log(any_error); };",
options: ["^.+(e|E)rror$"],
},
],
invalid: [
{ code: "function test(err) {}", errors: [EXPECTED_DECL_ERROR] },
{
code: "function test(err, data) {}",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "function test(err) {errorLookingWord();}",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "function test(err) {try{} catch(err) {}}",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "function test(err, callback) { foo(function(err, callback) {}); }",
errors: [EXPECTED_DECL_ERROR, EXPECTED_FUNC_ERROR],
},
{
code: "var test = (err) => {};",
errors: [{ messageId: "expected" }],
},
{
code: "var test = function(err) {};",
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "var test = function test(err, data) {};",
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "var test = function test(err) {/* if(err){} */};",
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "function test(err) {doSomethingHere(function(err){console.log(err);})}",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "function test(error) {}",
options: ["error"],
errors: [EXPECTED_DECL_ERROR],
},
{
code: "getData(function(err, data) {getMoreDataWith(data, function(err, moreData) {if (err) {}getEvenMoreDataWith(moreData, function(err, allOfTheThings) {if (err) {}});}); });",
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "getData(function(err, data) {getMoreDataWith(data, function(err, moreData) {getEvenMoreDataWith(moreData, function(err, allOfTheThings) {if (err) {}});}); });",
errors: [EXPECTED_FUNC_ERROR, EXPECTED_FUNC_ERROR],
},
{
code: "function userHandler(err) {logThisAction(function(err) {if (err) { console.log(err); } })}",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "function help() { function userHandler(err) {function tester(err) { err; process.nextTick(function() { err; }); } } }",
errors: [EXPECTED_DECL_ERROR],
},
{
code: "var test = function(anyError) { console.log(otherError); };",
options: ["^.+Error$"],
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "var test = function(anyError) { };",
options: ["^.+Error$"],
errors: [EXPECTED_FUNC_ERROR],
},
{
code: "var test = function(err) { console.log(error); };",
options: ["^(err|error)$"],
errors: [EXPECTED_FUNC_ERROR],
},
],
})