2 * @fileoverview Test for no-underscore-dangle rule
3 * @author Matt DuVall <http://www.mattduvall.com>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/no-underscore-dangle"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("no-underscore-dangle", rule
, {
24 "function foo_bar() {}",
26 "console.log(__filename); console.log(__dirname);",
27 "var _ = require('underscore');",
29 "function foo(_bar) {}",
30 "function foo(bar_) {}",
31 "(function _foo() {})",
32 { code
: "function foo(_bar) {}", options
: [{}] },
33 { code
: "function foo( _bar = 0) {}", parserOptions
: { ecmaVersion
: 6 } },
34 { code
: "const foo = { onClick(_bar) { } }", parserOptions
: { ecmaVersion
: 6 } },
35 { code
: "const foo = { onClick(_bar = 0) { } }", parserOptions
: { ecmaVersion
: 6 } },
36 { code
: "const foo = (_bar) => {}", parserOptions
: { ecmaVersion
: 6 } },
37 { code
: "const foo = (_bar = 0) => {}", parserOptions
: { ecmaVersion
: 6 } },
38 { code
: "function foo( ..._bar) {}", parserOptions
: { ecmaVersion
: 6 } },
39 { code
: "const foo = (..._bar) => {}", parserOptions
: { ecmaVersion
: 6 } },
40 { code
: "const foo = { onClick(..._bar) { } }", parserOptions
: { ecmaVersion
: 6 } },
41 { code
: "export default function() {}", parserOptions
: { ecmaVersion
: 6, sourceType
: "module" } },
42 { code
: "var _foo = 1", options
: [{ allow
: ["_foo"] }] },
43 { code
: "var __proto__ = 1;", options
: [{ allow
: ["__proto__"] }] },
44 { code
: "foo._bar;", options
: [{ allow
: ["_bar"] }] },
45 { code
: "function _foo() {}", options
: [{ allow
: ["_foo"] }] },
46 { code
: "this._bar;", options
: [{ allowAfterThis
: true }] },
47 { code
: "class foo { constructor() { super._bar; } }", options
: [{ allowAfterSuper
: true }], parserOptions
: { ecmaVersion
: 6 } },
48 { code
: "class foo { _onClick() { } }", parserOptions
: { ecmaVersion
: 6 } },
49 { code
: "class foo { onClick_() { } }", parserOptions
: { ecmaVersion
: 6 } },
50 { code
: "const o = { _onClick() { } }", parserOptions
: { ecmaVersion
: 6 } },
51 { code
: "const o = { onClick_() { } }", parserOptions
: { ecmaVersion
: 6 } },
52 { code
: "const o = { _onClick() { } }", options
: [{ allow
: ["_onClick"], enforceInMethodNames
: true }], parserOptions
: { ecmaVersion
: 6 } },
53 { code
: "const o = { _foo: 'bar' }", parserOptions
: { ecmaVersion
: 6 } },
54 { code
: "const o = { foo_: 'bar' }", parserOptions
: { ecmaVersion
: 6 } },
55 { code
: "this.constructor._bar", options
: [{ allowAfterThisConstructor
: true }] },
56 { code
: "const foo = { onClick(bar) { } }", parserOptions
: { ecmaVersion
: 6 } },
57 { code
: "const foo = (bar) => {}", parserOptions
: { ecmaVersion
: 6 } },
58 { code
: "function foo(_bar) {}", options
: [{ allowFunctionParams
: true }] },
59 { code
: "function foo( _bar = 0) {}", options
: [{ allowFunctionParams
: true }], parserOptions
: { ecmaVersion
: 6 } },
60 { code
: "const foo = { onClick(_bar) { } }", options
: [{ allowFunctionParams
: true }], parserOptions
: { ecmaVersion
: 6 } },
61 { code
: "const foo = (_bar) => {}", options
: [{ allowFunctionParams
: true }], parserOptions
: { ecmaVersion
: 6 } },
62 { code
: "function foo(bar) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
63 { code
: "const foo = { onClick(bar) { } }", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
64 { code
: "const foo = (bar) => {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
65 { code
: "function foo(_bar) {}", options
: [{ allowFunctionParams
: false, allow
: ["_bar"] }] },
66 { code
: "const foo = { onClick(_bar) { } }", options
: [{ allowFunctionParams
: false, allow
: ["_bar"] }], parserOptions
: { ecmaVersion
: 6 } },
67 { code
: "const foo = (_bar) => {}", options
: [{ allowFunctionParams
: false, allow
: ["_bar"] }], parserOptions
: { ecmaVersion
: 6 } },
68 { code
: "function foo([_bar]) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
69 { code
: "function foo([_bar] = []) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
70 { code
: "function foo( { _bar }) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
71 { code
: "function foo( { _bar = 0 } = {}) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 } },
72 { code
: "function foo(...[_bar]) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 2016 } },
73 { code
: "const [_foo] = arr", parserOptions
: { ecmaVersion
: 6 } },
74 { code
: "const [_foo] = arr", options
: [{}], parserOptions
: { ecmaVersion
: 6 } },
75 { code
: "const [_foo] = arr", options
: [{ allowInArrayDestructuring
: true }], parserOptions
: { ecmaVersion
: 6 } },
76 { code
: "const [foo, ...rest] = [1, 2, 3]", options
: [{ allowInArrayDestructuring
: false }], parserOptions
: { ecmaVersion
: 2022 } },
77 { code
: "const [foo, _bar] = [1, 2, 3]", options
: [{ allowInArrayDestructuring
: false, allow
: ["_bar"] }], parserOptions
: { ecmaVersion
: 2022 } },
78 { code
: "const { _foo } = obj", parserOptions
: { ecmaVersion
: 6 } },
79 { code
: "const { _foo } = obj", options
: [{}], parserOptions
: { ecmaVersion
: 6 } },
80 { code
: "const { _foo } = obj", options
: [{ allowInObjectDestructuring
: true }], parserOptions
: { ecmaVersion
: 6 } },
81 { code
: "const { foo, bar: _bar } = { foo: 1, bar: 2 }", options
: [{ allowInObjectDestructuring
: false, allow
: ["_bar"] }], parserOptions
: { ecmaVersion
: 2022 } },
82 { code
: "const { foo, _bar } = { foo: 1, _bar: 2 }", options
: [{ allowInObjectDestructuring
: false, allow
: ["_bar"] }], parserOptions
: { ecmaVersion
: 2022 } },
83 { code
: "const { foo, _bar: bar } = { foo: 1, _bar: 2 }", options
: [{ allowInObjectDestructuring
: false }], parserOptions
: { ecmaVersion
: 2022 } },
84 { code
: "class foo { _field; }", parserOptions
: { ecmaVersion
: 2022 } },
85 { code
: "class foo { _field; }", options
: [{ enforceInClassFields
: false }], parserOptions
: { ecmaVersion
: 2022 } },
86 { code
: "class foo { #_field; }", parserOptions
: { ecmaVersion
: 2022 } },
87 { code
: "class foo { #_field; }", options
: [{ enforceInClassFields
: false }], parserOptions
: { ecmaVersion
: 2022 } },
88 { code
: "class foo { _field; }", options
: [{}], parserOptions
: { ecmaVersion
: 2022 } }
91 { code
: "var _foo = 1", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" }, type
: "VariableDeclarator" }] },
92 { code
: "var foo_ = 1", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "foo_" }, type
: "VariableDeclarator" }] },
93 { code
: "function _foo() {}", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" }, type
: "FunctionDeclaration" }] },
94 { code
: "function foo_() {}", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "foo_" }, type
: "FunctionDeclaration" }] },
95 { code
: "var __proto__ = 1;", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "__proto__" }, type
: "VariableDeclarator" }] },
96 { code
: "foo._bar;", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "MemberExpression" }] },
97 { code
: "this._prop;", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_prop" }, type
: "MemberExpression" }] },
98 { code
: "class foo { constructor() { super._prop; } }", parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_prop" }, type
: "MemberExpression" }] },
99 { code
: "class foo { constructor() { this._prop; } }", options
: [{ allowAfterSuper
: true }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_prop" }, type
: "MemberExpression" }] },
100 { code
: "class foo { _onClick() { } }", options
: [{ enforceInMethodNames
: true }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_onClick" }, type
: "MethodDefinition" }] },
101 { code
: "class foo { onClick_() { } }", options
: [{ enforceInMethodNames
: true }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "onClick_" }, type
: "MethodDefinition" }] },
102 { code
: "const o = { _onClick() { } }", options
: [{ enforceInMethodNames
: true }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_onClick" }, type
: "Property" }] },
103 { code
: "const o = { onClick_() { } }", options
: [{ enforceInMethodNames
: true }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "onClick_" }, type
: "Property" }] },
104 { code
: "this.constructor._bar", errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "MemberExpression" }] },
105 { code
: "function foo(_bar) {}", options
: [{ allowFunctionParams
: false }], errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "Identifier" }] },
106 { code
: "(function foo(_bar) {})", options
: [{ allowFunctionParams
: false }], errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "Identifier" }] },
107 { code
: "function foo(bar, _foo) {}", options
: [{ allowFunctionParams
: false }], errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" }, type
: "Identifier" }] },
108 { code
: "const foo = { onClick(_bar) { } }", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "Identifier" }] },
109 { code
: "const foo = (_bar) => {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "Identifier" }] },
110 { code
: "function foo(_bar = 0) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "AssignmentPattern" }] },
111 { code
: "const foo = { onClick(_bar = 0) { } }", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "AssignmentPattern" }] },
112 { code
: "const foo = (_bar = 0) => {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "AssignmentPattern" }] },
113 { code
: "function foo(..._bar) {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "RestElement" }] },
114 { code
: "const foo = { onClick(..._bar) { } }", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "RestElement" }] },
115 { code
: "const foo = (..._bar) => {}", options
: [{ allowFunctionParams
: false }], parserOptions
: { ecmaVersion
: 6 }, errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" }, type
: "RestElement" }] },
117 code
: "const [foo, _bar] = [1, 2]",
118 options
: [{ allowInArrayDestructuring
: false }],
119 parserOptions
: { ecmaVersion
: 2022 },
120 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" } }]
122 code
: "const [_foo = 1] = arr",
123 options
: [{ allowInArrayDestructuring
: false }],
124 parserOptions
: { ecmaVersion
: 2022 },
125 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" } }]
127 code
: "const [foo, ..._rest] = [1, 2, 3]",
128 options
: [{ allowInArrayDestructuring
: false }],
129 parserOptions
: { ecmaVersion
: 2022 },
130 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_rest" } }]
132 code
: "const [foo, [bar_, baz]] = [1, [2, 3]]",
133 options
: [{ allowInArrayDestructuring
: false }],
134 parserOptions
: { ecmaVersion
: 2022 },
135 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "bar_" } }]
137 code
: "const { _foo, bar } = { _foo: 1, bar: 2 }",
138 options
: [{ allowInObjectDestructuring
: false }],
139 parserOptions
: { ecmaVersion
: 2022 },
140 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" } }]
142 code
: "const { _foo = 1 } = obj",
143 options
: [{ allowInObjectDestructuring
: false }],
144 parserOptions
: { ecmaVersion
: 2022 },
145 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" } }]
147 code
: "const { bar: _foo = 1 } = obj",
148 options
: [{ allowInObjectDestructuring
: false }],
149 parserOptions
: { ecmaVersion
: 2022 },
150 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" } }]
152 code
: "const { foo: _foo, bar } = { foo: 1, bar: 2 }",
153 options
: [{ allowInObjectDestructuring
: false }],
154 parserOptions
: { ecmaVersion
: 2022 },
155 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_foo" } }]
157 code
: "const { foo, ..._rest} = { foo: 1, bar: 2, baz: 3 }",
158 options
: [{ allowInObjectDestructuring
: false }],
159 parserOptions
: { ecmaVersion
: 2022 },
160 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_rest" } }]
162 code
: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
163 options
: [{ allowInArrayDestructuring
: false, allowInObjectDestructuring
: false }],
164 parserOptions
: { ecmaVersion
: 2022 },
166 { messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" } },
167 { messageId
: "unexpectedUnderscore", data
: { identifier
: "_a" } }
170 code
: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
171 options
: [{ allowInArrayDestructuring
: true, allowInObjectDestructuring
: false }],
172 parserOptions
: { ecmaVersion
: 2022 },
173 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_a" } }]
175 code
: "const [{ foo: [_bar, _, { bar: _baz }] }] = [{ foo: [1, 2, { bar: 'a' }] }]",
176 options
: [{ allowInArrayDestructuring
: false, allowInObjectDestructuring
: false }],
177 parserOptions
: { ecmaVersion
: 2022 },
179 { messageId
: "unexpectedUnderscore", data
: { identifier
: "_bar" } },
180 { messageId
: "unexpectedUnderscore", data
: { identifier
: "_baz" } }
183 code
: "const { foo, bar: { baz, _qux } } = { foo: 1, bar: { baz: 3, _qux: 4 } }",
184 options
: [{ allowInObjectDestructuring
: false }],
185 parserOptions
: { ecmaVersion
: 2022 },
186 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_qux" } }]
188 code
: "class foo { #_bar() {} }",
189 options
: [{ enforceInMethodNames
: true }],
190 parserOptions
: { ecmaVersion
: 2022 },
191 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "#_bar" } }]
193 code
: "class foo { #bar_() {} }",
194 options
: [{ enforceInMethodNames
: true }],
195 parserOptions
: { ecmaVersion
: 2022 },
196 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "#bar_" } }]
199 code
: "class foo { _field; }",
200 options
: [{ enforceInClassFields
: true }],
201 parserOptions
: { ecmaVersion
: 2022 },
202 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "_field" } }]
205 code
: "class foo { #_field; }",
206 options
: [{ enforceInClassFields
: true }],
207 parserOptions
: { ecmaVersion
: 2022 },
208 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "#_field" } }]
211 code
: "class foo { field_; }",
212 options
: [{ enforceInClassFields
: true }],
213 parserOptions
: { ecmaVersion
: 2022 },
214 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "field_" } }]
217 code
: "class foo { #field_; }",
218 options
: [{ enforceInClassFields
: true }],
219 parserOptions
: { ecmaVersion
: 2022 },
220 errors
: [{ messageId
: "unexpectedUnderscore", data
: { identifier
: "#field_" } }]