Skip to content
Permalink
Browse files

Update: Adds default 'that' for consistent-this (fixes #4833)

  • Loading branch information...
alberto committed Jan 6, 2016
1 parent 3c6d818 commit 15696956f756261e1a6d8ba06cf990b27b50b40c
Showing with 26 additions and 25 deletions.
  1. +23 −23 docs/rules/consistent-this.md
  2. +1 −1 lib/rules/consistent-this.js
  3. +2 −1 tests/lib/rules/consistent-this.js
@@ -3,14 +3,14 @@
It is often necessary to capture the current execution context in order to make it available subsequently. A prominent example of this are jQuery callbacks:

```js
var self = this;
var that = this;
jQuery('li').click(function (event) {
// here, "this" is the HTMLElement where the click event occurred
self.setFoo(42);
that.setFoo(42);
});
```

There are many commonly used aliases for `this` such as `self`, `that` or `me`. It is desirable to ensure that whichever alias the team agrees upon is used consistently throughout the application.
There are many commonly used aliases for `this` such as `that`, `self` or `me`. It is desirable to ensure that whichever alias the team agrees upon is used consistently throughout the application.

## Rule Details

@@ -21,67 +21,67 @@ This rule designates a variable as the chosen alias for `this`. It then enforces

### Options

This rule takes one option, a string, which is the designated `this` variable.
This rule takes one option, a string, which is the designated `this` variable. The default is `that`.

#### Usage

You can set the rule configuration like this:

```json
"consistent-this": [2, "self"]
"consistent-this": [2, "that"]
```

The following patterns are considered problems:

```js
/*eslint consistent-this: [2, "self"]*/
/*eslint consistent-this: [2, "that"]*/
var self = 42; /*error Designated alias 'self' is not assigned to 'this'.*/
var that = 42; /*error Designated alias 'that' is not assigned to 'this'.*/
var that = this; /*error Unexpected alias 'that' for 'this'.*/
var self = this; /*error Unexpected alias 'self' for 'this'.*/
self = 42; /*error Designated alias 'self' is not assigned to 'this'.*/
that = 42; /*error Designated alias 'that' is not assigned to 'this'.*/
that = this; /*error Unexpected alias 'that' for 'this'.*/
self = this; /*error Unexpected alias 'self' for 'this'.*/
```

The following patterns are not considered problems:

```js
/*eslint consistent-this: [2, "self"]*/
/*eslint consistent-this: [2, "that"]*/
var self = this;
var that = this;
var that = 42;
var self = 42;
var that;
var self;
self = this;
that = this;
foo.bar = this;
```

A declaration of an alias does not need to assign `this` in the declaration, but it must perform an appropriate assignment in the same scope as the declaration. The following patterns are also considered okay:

```js
/*eslint consistent-this: [2, "self"]*/
/*eslint consistent-this: [2, "that"]*/
var self;
self = this;
var that;
that = this;
var foo, self;
var foo, that;
foo = 42;
self = this;
that = this;
```

But the following pattern is considered a warning:

```js
/*eslint consistent-this: [2, "self"]*/
/*eslint consistent-this: [2, "that"]*/
var self; /*error Designated alias 'self' is not assigned to 'this'.*/
var that; /*error Designated alias 'that' is not assigned to 'this'.*/
function f() {
self = this;
that = this;
}
```

@@ -11,7 +11,7 @@
//------------------------------------------------------------------------------

module.exports = function(context) {
var alias = context.options[0];
var alias = context.options[0] || "that";

/**
* Reports that a variable declarator or assignment expression is assigning
@@ -39,6 +39,7 @@ function destructuringTest(code) {
var ruleTester = new RuleTester();
ruleTester.run("consistent-this", rule, {
valid: [
{ code: "var foo = 42, that = this" },
{ code: "var foo = 42, self = this", options: ["self"] },
{ code: "var self = 42", options: ["that"] },
{ code: "var self", options: ["that"] },
@@ -53,7 +54,7 @@ ruleTester.run("consistent-this", rule, {
destructuringTest("[foo, bar] = this")
],
invalid: [
{ code: "var context = this", options: ["that"], errors: [{ message: "Unexpected alias 'context' for 'this'.", type: "VariableDeclarator"}] },
{ code: "var context = this", errors: [{ message: "Unexpected alias 'context' for 'this'.", type: "VariableDeclarator"}] },
{ code: "var that = this", options: ["self"], errors: [{ message: "Unexpected alias 'that' for 'this'.", type: "VariableDeclarator"}] },
{ code: "var foo = 42, self = this", options: ["that"], errors: [{ message: "Unexpected alias 'self' for 'this'.", type: "VariableDeclarator"}] },
{ code: "var self = 42", options: ["self"], errors: [{ message: "Designated alias 'self' is not assigned to 'this'.", type: "VariableDeclarator"}] },

0 comments on commit 1569695

Please sign in to comment.
You can’t perform that action at this time.