]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-underscore-dangle.js
bump version to 8.41.0-3
[pve-eslint.git] / eslint / tests / lib / rules / no-underscore-dangle.js
1 /**
2 * @fileoverview Test for no-underscore-dangle rule
3 * @author Matt DuVall <http://www.mattduvall.com>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-underscore-dangle"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-underscore-dangle", rule, {
22 valid: [
23 "var foo_bar = 1;",
24 "function foo_bar() {}",
25 "foo.bar.__proto__;",
26 "console.log(__filename); console.log(__dirname);",
27 "var _ = require('underscore');",
28 "var a = b._;",
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 } }
89 ],
90 invalid: [
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" }] },
116 {
117 code: "const [foo, _bar] = [1, 2]",
118 options: [{ allowInArrayDestructuring: false }],
119 parserOptions: { ecmaVersion: 2022 },
120 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" } }]
121 }, {
122 code: "const [_foo = 1] = arr",
123 options: [{ allowInArrayDestructuring: false }],
124 parserOptions: { ecmaVersion: 2022 },
125 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
126 }, {
127 code: "const [foo, ..._rest] = [1, 2, 3]",
128 options: [{ allowInArrayDestructuring: false }],
129 parserOptions: { ecmaVersion: 2022 },
130 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_rest" } }]
131 }, {
132 code: "const [foo, [bar_, baz]] = [1, [2, 3]]",
133 options: [{ allowInArrayDestructuring: false }],
134 parserOptions: { ecmaVersion: 2022 },
135 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "bar_" } }]
136 }, {
137 code: "const { _foo, bar } = { _foo: 1, bar: 2 }",
138 options: [{ allowInObjectDestructuring: false }],
139 parserOptions: { ecmaVersion: 2022 },
140 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
141 }, {
142 code: "const { _foo = 1 } = obj",
143 options: [{ allowInObjectDestructuring: false }],
144 parserOptions: { ecmaVersion: 2022 },
145 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
146 }, {
147 code: "const { bar: _foo = 1 } = obj",
148 options: [{ allowInObjectDestructuring: false }],
149 parserOptions: { ecmaVersion: 2022 },
150 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" } }]
151 }, {
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" } }]
156 }, {
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" } }]
161 }, {
162 code: "const { foo: [_bar, { a: _a, b } ] } = { foo: [1, { a: 'a', b: 'b' }] }",
163 options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
164 parserOptions: { ecmaVersion: 2022 },
165 errors: [
166 { messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
167 { messageId: "unexpectedUnderscore", data: { identifier: "_a" } }
168 ]
169 }, {
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" } }]
174 }, {
175 code: "const [{ foo: [_bar, _, { bar: _baz }] }] = [{ foo: [1, 2, { bar: 'a' }] }]",
176 options: [{ allowInArrayDestructuring: false, allowInObjectDestructuring: false }],
177 parserOptions: { ecmaVersion: 2022 },
178 errors: [
179 { messageId: "unexpectedUnderscore", data: { identifier: "_bar" } },
180 { messageId: "unexpectedUnderscore", data: { identifier: "_baz" } }
181 ]
182 }, {
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" } }]
187 }, {
188 code: "class foo { #_bar() {} }",
189 options: [{ enforceInMethodNames: true }],
190 parserOptions: { ecmaVersion: 2022 },
191 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_bar" } }]
192 }, {
193 code: "class foo { #bar_() {} }",
194 options: [{ enforceInMethodNames: true }],
195 parserOptions: { ecmaVersion: 2022 },
196 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#bar_" } }]
197 },
198 {
199 code: "class foo { _field; }",
200 options: [{ enforceInClassFields: true }],
201 parserOptions: { ecmaVersion: 2022 },
202 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_field" } }]
203 },
204 {
205 code: "class foo { #_field; }",
206 options: [{ enforceInClassFields: true }],
207 parserOptions: { ecmaVersion: 2022 },
208 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_field" } }]
209 },
210 {
211 code: "class foo { field_; }",
212 options: [{ enforceInClassFields: true }],
213 parserOptions: { ecmaVersion: 2022 },
214 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "field_" } }]
215 },
216 {
217 code: "class foo { #field_; }",
218 options: [{ enforceInClassFields: true }],
219 parserOptions: { ecmaVersion: 2022 },
220 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#field_" } }]
221 }
222 ]
223 });