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 { 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 }] }
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" }] }