]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-underscore-dangle.js
import 8.23.1 source
[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: "class foo { _field; }", parserOptions: { ecmaVersion: 2022 } },
74 { code: "class foo { _field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
75 { code: "class foo { #_field; }", parserOptions: { ecmaVersion: 2022 } },
76 { code: "class foo { #_field; }", options: [{ enforceInClassFields: false }], parserOptions: { ecmaVersion: 2022 } },
77 { code: "class foo { _field; }", options: [{}], parserOptions: { ecmaVersion: 2022 } }
78 ],
79 invalid: [
80 { code: "var _foo = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "VariableDeclarator" }] },
81 { code: "var foo_ = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "VariableDeclarator" }] },
82 { code: "function _foo() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "FunctionDeclaration" }] },
83 { code: "function foo_() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "FunctionDeclaration" }] },
84 { code: "var __proto__ = 1;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "__proto__" }, type: "VariableDeclarator" }] },
85 { code: "foo._bar;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
86 { code: "this._prop;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
87 { code: "class foo { constructor() { super._prop; } }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
88 { code: "class foo { constructor() { this._prop; } }", options: [{ allowAfterSuper: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
89 { code: "class foo { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "MethodDefinition" }] },
90 { code: "class foo { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "MethodDefinition" }] },
91 { code: "const o = { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "Property" }] },
92 { code: "const o = { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "Property" }] },
93 { code: "this.constructor._bar", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
94 { code: "function foo(_bar) {}", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
95 { code: "(function foo(_bar) {})", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
96 { code: "function foo(bar, _foo) {}", options: [{ allowFunctionParams: false }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "Identifier" }] },
97 { code: "const foo = { onClick(_bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
98 { code: "const foo = (_bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
99 { code: "function foo(_bar = 0) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
100 { code: "const foo = { onClick(_bar = 0) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
101 { code: "const foo = (_bar = 0) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "AssignmentPattern" }] },
102 { code: "function foo(..._bar) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
103 { code: "const foo = { onClick(..._bar) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
104 { code: "const foo = (..._bar) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "RestElement" }] },
105 {
106 code: "class foo { #_bar() {} }",
107 options: [{ enforceInMethodNames: true }],
108 parserOptions: { ecmaVersion: 2022 },
109 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_bar" } }]
110 }, {
111 code: "class foo { #bar_() {} }",
112 options: [{ enforceInMethodNames: true }],
113 parserOptions: { ecmaVersion: 2022 },
114 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#bar_" } }]
115 },
116 {
117 code: "class foo { _field; }",
118 options: [{ enforceInClassFields: true }],
119 parserOptions: { ecmaVersion: 2022 },
120 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_field" } }]
121 },
122 {
123 code: "class foo { #_field; }",
124 options: [{ enforceInClassFields: true }],
125 parserOptions: { ecmaVersion: 2022 },
126 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#_field" } }]
127 },
128 {
129 code: "class foo { field_; }",
130 options: [{ enforceInClassFields: true }],
131 parserOptions: { ecmaVersion: 2022 },
132 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "field_" } }]
133 },
134 {
135 code: "class foo { #field_; }",
136 options: [{ enforceInClassFields: true }],
137 parserOptions: { ecmaVersion: 2022 },
138 errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "#field_" } }]
139 }
140 ]
141 });