Skip to content

Commit

Permalink
Update: allowAfterThis option in no-underscore-dangle (fixes #3435)
Browse files Browse the repository at this point in the history
  • Loading branch information
just-boris committed Nov 27, 2015
1 parent 3d712c3 commit c21e51f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
16 changes: 15 additions & 1 deletion docs/rules/no-underscore-dangle.md
Expand Up @@ -24,6 +24,14 @@ This rule aims to eliminate the use of dangling underscores in identifiers.

Array of variable names that are permitted to be used with underscore. If provided, it must be an `Array`.

#### allowAfterThis

```json
"no-underscore-dangle": [2, { "allowAfterThis": true }]
```

This option allows usage of dangled variables as members of `this`.

The following patterns are considered problems:

```js
Expand Down Expand Up @@ -53,7 +61,13 @@ var foo_;
foo._bar();
```

```js
/*eslint no-underscore-dangle: [2, { "allowAfterThis": true }]*/

var a = this.foo_;
this._bar();
```

## When Not To Use It

If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.

11 changes: 9 additions & 2 deletions lib/rules/no-underscore-dangle.js
Expand Up @@ -11,7 +11,9 @@

module.exports = function(context) {

var ALLOWED_VARIABLES = context.options[0] && context.options[0].allow ? context.options[0].allow : [];
var options = context.options[0] || {};
var ALLOWED_VARIABLES = options.allow ? options.allow : [];
var allowAfterThis = typeof options.allowAfterThis !== "undefined" ? options.allowAfterThis : false;

//-------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -100,9 +102,11 @@ module.exports = function(context) {
* @private
*/
function checkForTrailingUnderscoreInMemberExpression(node) {
var identifier = node.property.name;
var identifier = node.property.name,
isMemberOfThis = node.object.type === "ThisExpression";

if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
!(isMemberOfThis && allowAfterThis) &&
!isSpecialCaseIdentifierForMemberExpression(identifier) && !isAllowed(identifier)) {
context.report(node, "Unexpected dangling \"_\" in \"" + identifier + "\".");
}
Expand All @@ -129,6 +133,9 @@ module.exports.schema = [
"items": {
"type": "string"
}
},
"allowAfterThis": {
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/no-underscore-dangle.js
Expand Up @@ -29,14 +29,16 @@ ruleTester.run("no-underscore-dangle", rule, {
{ code: "var _foo = 1", options: [{ allow: ["_foo"] }]},
{ code: "var __proto__ = 1;", options: [{ allow: ["__proto__"] }]},
{ code: "foo._bar;", options: [{ allow: ["_bar"] }]},
{ code: "function _foo() {}", options: [{ allow: ["_foo"] }]}
{ code: "function _foo() {}", options: [{ allow: ["_foo"] }]},
{ code: "this._bar;", options: [{allowAfterThis: true}]}
],
invalid: [
{ code: "var _foo = 1", errors: [{ message: "Unexpected dangling \"_\" in \"_foo\".", type: "VariableDeclarator"}] },
{ code: "var foo_ = 1", errors: [{ message: "Unexpected dangling \"_\" in \"foo_\".", type: "VariableDeclarator"}] },
{ code: "function _foo() {}", errors: [{ message: "Unexpected dangling \"_\" in \"_foo\".", type: "FunctionDeclaration"}] },
{ code: "function foo_() {}", errors: [{ message: "Unexpected dangling \"_\" in \"foo_\".", type: "FunctionDeclaration"}] },
{ code: "var __proto__ = 1;", errors: [{ message: "Unexpected dangling \"_\" in \"__proto__\".", type: "VariableDeclarator"}] },
{ code: "foo._bar;", errors: [{ message: "Unexpected dangling \"_\" in \"_bar\".", type: "MemberExpression"}] }
{ code: "foo._bar;", errors: [{ message: "Unexpected dangling \"_\" in \"_bar\".", type: "MemberExpression"}] },
{ code: "this._prop;", errors: [{ message: "Unexpected dangling \"_\" in \"_prop\".", type: "MemberExpression"}] }
]
});

0 comments on commit c21e51f

Please sign in to comment.