forked from eslint-community/eslint-plugin-n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-callback-literal.js
119 lines (113 loc) · 3.05 KB
/
no-callback-literal.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
/**
* @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/no-callback-literal")
const tsParser = require("@typescript-eslint/parser")
const ruleTester = new RuleTester()
ruleTester.run("no-callback-literal", rule, {
valid: [
// random stuff
"horse()",
"sort(null)",
'require("zyx")',
'require("zyx", data)',
// callback()
"callback()",
"callback(undefined)",
"callback(null)",
"callback(x)",
'callback(new Error("error"))',
"callback(friendly, data)",
"callback(undefined, data)",
"callback(null, data)",
"callback(x, data)",
'callback(new Error("error"), data)',
"callback(x = obj, data)",
"callback((1, a), data)",
"callback(a || b, data)",
"callback(a ? b : c, data)",
"callback(a ? 1 : c, data)",
"callback(a ? b : 1, data)",
// cb()
"cb()",
"cb(undefined)",
"cb(null)",
'cb(undefined, "super")',
'cb(null, "super")',
// https://github.com/eslint-community/eslint-plugin-n/issues/162
{
code: "cb(e as Error)",
languageOptions: { parser: tsParser },
},
],
invalid: [
// callback
{
code: 'callback(false, "snork")',
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
{
code: 'callback("help")',
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
{
code: 'callback("help", data)',
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
// cb
{
code: "cb(false)",
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
{
code: 'cb("help")',
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
{
code: 'cb("help", data)',
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
{
code: "callback((a, 1), data)",
errors: [
{
message:
"Unexpected literal in error position of callback.",
},
],
},
],
})