/
no-underscore-dangle.js
223 lines (217 loc) · 17.2 KB
/
no-underscore-dangle.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @fileoverview Test for no-underscore-dangle rule
* @author Matt DuVall <http://www.mattduvall.com>
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-underscore-dangle"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-underscore-dangle", rule, {
valid: [
"var foo_bar = 1;",
"function foo_bar() {}",
"foo.bar.__proto__;",
"console.log(__filename); console.log(__dirname);",
"var _ = require('underscore');",
"var a = b._;",
"function foo(_bar) {}",
"function foo(bar_) {}",
"(function _foo() {})",
{ code: "function foo(_bar) {}", options: [{}] },
{ code: "function foo( _bar = 0) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(_bar) { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(_bar = 0) { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (_bar) => {}", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (_bar = 0) => {}", parserOptions: { ecmaVersion: 6 } },
{ code: "function foo( ..._bar) {}", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (..._bar) => {}", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(..._bar) { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "export default function() {}", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
{ code: "var _foo = 1", options: [{ allow: ["_foo"] }] },
{ code: "var __proto__ = 1;", options: [{ allow: ["__proto__"] }] },
{ code: "foo._bar;", options: [{ allow: ["_bar"] }] },
{ code: "function _foo() {}", options: [{ allow: ["_foo"] }] },
{ code: "this._bar;", options: [{ allowAfterThis: true }] },
{ code: "class foo { constructor() { super._bar; } }", options: [{ allowAfterSuper: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "class foo { _onClick() { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "class foo { onClick_() { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const o = { _onClick() { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const o = { onClick_() { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const o = { _onClick() { } }", options: [{ allow: ["_onClick"], enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "const o = { _foo: 'bar' }", parserOptions: { ecmaVersion: 6 } },
{ code: "const o = { foo_: 'bar' }", parserOptions: { ecmaVersion: 6 } },
{ code: "this.constructor._bar", options: [{ allowAfterThisConstructor: true }] },
{ code: "const foo = { onClick(bar) { } }", parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (bar) => {}", parserOptions: { ecmaVersion: 6 } },
{ code: "function foo(_bar) {}", options: [{ allowFunctionParams: true }] },
{ code: "function foo( _bar = 0) {}", options: [{ allowFunctionParams: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(_bar) { } }", options: [{ allowFunctionParams: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (_bar) => {}", options: [{ allowFunctionParams: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo(bar) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = { onClick(bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo(_bar) {}", options: [{ allowFunctionParams: false, allow: ["_bar"] }] },
{ code: "const foo = { onClick(_bar) { } }", options: [{ allowFunctionParams: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 6 } },
{ code: "const foo = (_bar) => {}", options: [{ allowFunctionParams: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo([_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo([_bar] = []) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo( { _bar }) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo( { _bar = 0 } = {}) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo(...[_bar]) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 2016 } },
{ code: "const [_foo] = arr", parserOptions: { ecmaVersion: 6 } },
{ code: "const [_foo] = arr", options: [{}], parserOptions: { ecmaVersion: 6 } },
{ code: "const [_foo] = arr", options: [{ allowInArrayDestructuring: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "const [foo, ...rest] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const [foo, _bar] = [1, 2, 3]", options: [{ allowInArrayDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { _foo } = obj", parserOptions: { ecmaVersion: 6 } },
{ code: "const { _foo } = obj", options: [{}], parserOptions: { ecmaVersion: 6 } },
{ code: "const { _foo } = obj", options: [{ allowInObjectDestructuring: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "const { foo, bar: _bar } = { foo: 1, bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { foo, _bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false, allow: ["_bar"] }], parserOptions: { ecmaVersion: 2022 } },
{ code: "const { foo, _bar: bar } = { foo: 1, _bar: 2 }", options: [{ allowInObjectDestructuring: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { _field; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { _field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { #_field; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { #_field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
{ code: "class foo { _field; }", options: [{}], parserOptions: { ecmaVersion: 2022 } }
],
invalid: [
{ code: "var _foo = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "VariableDeclarator" }] },
{ code: "var foo_ = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "VariableDeclarator" }] },
{ code: "function _foo() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "FunctionDeclaration" }] },
{ code: "function foo_() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "FunctionDeclaration" }] },
{ code: "var __proto__ = 1;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "__proto__" }, type: "VariableDeclarator" }] },
{ code: "foo._bar;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
{ code: "this._prop;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
{ code: "class foo { constructor() { super._prop; } }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
{ code: "class foo { constructor() { this._prop; } }", options: [{ allowAfterSuper: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
{ code: "class foo { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "MethodDefinition" }] },
{ code: "class foo { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "MethodDefinition" }] },
{ code: "const o = { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "Property" }] },
{ code: "const o = { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "Property" }] },
{ code: "this.constructor._bar", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
{ code: "function foo(_bar) {}", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "(function foo(_bar) {})", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "function foo(bar, _foo) {}", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "Identifier" }] },
{ code: "const foo = { onClick(_bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "const foo = (_bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "function foo(_bar = 0) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
{ code: "const foo = { onClick(_bar = 0) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
{ code: "const foo = (_bar = 0) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
{ code: "function foo(..._bar) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
{ code: "const foo = { onClick(..._bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
{ code: "const foo = (..._bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
{
code: "const [foo, _bar] = [1, 2]",
options: [{ allowInArrayDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } }]
}, {
code: "const [_foo = 1] = arr",
options: [{ allowInArrayDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
}, {
code: "const [foo, ..._rest] = [1, 2, 3]",
options: [{ allowInArrayDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]
}, {
code: "const [foo, [bar_, baz]] = [1, [2, 3]]",
options: [{ allowInArrayDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "bar_" } }]
}, {
code: "const { _foo, bar } = { _foo: 1, bar: 2 }",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
}, {
code: "const { _foo = 1 } = obj",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
}, {
code: "const { bar: _foo = 1 } = obj",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
}, {
code: "const { foo: _foo, bar } = { foo: 1, bar: 2 }",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
}, {
code: "const { foo, ..._rest} = { foo: 1, bar: 2, baz: 3 }",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]
}, {
code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }
]
}, {
code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
options: [{ allowInArrayDestructuring: true, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_a" } }]
}, {
code: "const [{ foo: [_bar, _, { bar: _baz }] }] = [{ foo: [1, 2, { bar: 'a' }] }]",
options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [
{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
{ messageId: "unexpectedUnderscore", data: { identifier: "_baz" } }
]
}, {
code: "const { foo, bar: { baz, _qux } } = { foo: 1, bar: { baz: 3, _qux: 4 } }",
options: [{ allowInObjectDestructuring: false }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_qux" } }]
}, {
code: "class foo { #_bar() {} }",
options: [{ enforceInMethodNames: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_bar" } }]
}, {
code: "class foo { #bar_() {} }",
options: [{ enforceInMethodNames: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#bar_" } }]
},
{
code: "class foo { _field; }",
options: [{ enforceInClassFields: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_field" } }]
},
{
code: "class foo { #_field; }",
options: [{ enforceInClassFields: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_field" } }]
},
{
code: "class foo { field_; }",
options: [{ enforceInClassFields: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "field_" } }]
},
{
code: "class foo { #field_; }",
options: [{ enforceInClassFields: true }],
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#field_" } }]
}
]
});