]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-underscore-dangle.js
first commit
[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 { code: "export default function() {}", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
30 { code: "var _foo = 1", options: [{ allow: ["_foo"] }] },
31 { code: "var __proto__ = 1;", options: [{ allow: ["__proto__"] }] },
32 { code: "foo._bar;", options: [{ allow: ["_bar"] }] },
33 { code: "function _foo() {}", options: [{ allow: ["_foo"] }] },
34 { code: "this._bar;", options: [{ allowAfterThis: true }] },
35 { code: "class foo { constructor() { super._bar; } }", options: [{ allowAfterSuper: true }], parserOptions: { ecmaVersion: 6 } },
36 { code: "class foo { _onClick() { } }", parserOptions: { ecmaVersion: 6 } },
37 { code: "class foo { onClick_() { } }", parserOptions: { ecmaVersion: 6 } },
38 { code: "const o = { _onClick() { } }", parserOptions: { ecmaVersion: 6 } },
39 { code: "const o = { onClick_() { } }", parserOptions: { ecmaVersion: 6 } },
40 { code: "const o = { _onClick() { } }", options: [{ allow: ["_onClick"], enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 } },
41 { code: "const o = { _foo: 'bar' }", parserOptions: { ecmaVersion: 6 } },
42 { code: "const o = { foo_: 'bar' }", parserOptions: { ecmaVersion: 6 } },
43 { code: "this.constructor._bar", options: [{ allowAfterThisConstructor: true }] }
44 ],
45 invalid: [
46 { code: "var _foo = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "VariableDeclarator" }] },
47 { code: "var foo_ = 1", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "VariableDeclarator" }] },
48 { code: "function _foo() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_foo" }, type: "FunctionDeclaration" }] },
49 { code: "function foo_() {}", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "foo_" }, type: "FunctionDeclaration" }] },
50 { code: "var __proto__ = 1;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "__proto__" }, type: "VariableDeclarator" }] },
51 { code: "foo._bar;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
52 { code: "this._prop;", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
53 { code: "class foo { constructor() { super._prop; } }", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
54 { code: "class foo { constructor() { this._prop; } }", options: [{ allowAfterSuper: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_prop" }, type: "MemberExpression" }] },
55 { code: "class foo { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "MethodDefinition" }] },
56 { code: "class foo { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "MethodDefinition" }] },
57 { code: "const o = { _onClick() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_onClick" }, type: "Property" }] },
58 { code: "const o = { onClick_() { } }", options: [{ enforceInMethodNames: true }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "onClick_" }, type: "Property" }] },
59 { code: "this.constructor._bar", errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] },
60 { code: "foo.constructor._bar", options: [{ allowAfterThisConstructor: true }], errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "MemberExpression" }] }
61 ]
62 });