Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
8606a13
commit 62a2f92
Showing
7 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Disallow assigning `this` to a variable | ||
|
||
`this` should be used directly. If you want a reference to `this` from a higher scope, consider using [arrow function expression](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) or [`Function#bind()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind). | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = this; | ||
|
||
setTimeout(function () { | ||
foo.bar(); | ||
}, 1000); | ||
``` | ||
|
||
```js | ||
const foo = this; | ||
|
||
class Bar { | ||
method() { | ||
foo.baz(); | ||
} | ||
} | ||
|
||
new Bar().method(); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
setTimeout(() => { | ||
this.bar(); | ||
}, 1000); | ||
``` | ||
|
||
```js | ||
setTimeout(function () { | ||
this.bar(); | ||
}.bind(this), 1000); | ||
``` | ||
|
||
```js | ||
class Bar { | ||
constructor(fooInstance) { | ||
this.fooInstance = fooInstance; | ||
} | ||
method() { | ||
this.fooInstance.baz(); | ||
} | ||
} | ||
|
||
new Bar(this).method(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const MESSAGE_ID = 'no-this-assignment'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Do not assign `this` to `{{name}}`.' | ||
}; | ||
|
||
const variableDeclaratorSelector = [ | ||
'VariableDeclarator', | ||
'[init.type="ThisExpression"]', | ||
'[id.type="Identifier"]' | ||
].join(''); | ||
|
||
const assignmentExpressionSelector = [ | ||
'AssignmentExpression', | ||
'[right.type="ThisExpression"]', | ||
'[left.type="Identifier"]' | ||
].join(''); | ||
|
||
const selector = `:matches(${variableDeclaratorSelector}, ${assignmentExpressionSelector})`; | ||
|
||
const create = context => ({ | ||
[selector](node) { | ||
const variable = node.type === 'AssignmentExpression' ? node.left : node.id; | ||
context.report({ | ||
node, | ||
data: {name: variable.name}, | ||
messageId: MESSAGE_ID | ||
}); | ||
} | ||
}); | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
messages | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {outdent} from 'outdent'; | ||
import {test} from './utils/test.js'; | ||
|
||
test.visualize({ | ||
valid: [ | ||
'const {property} = this;', | ||
'const property = this.property;', | ||
'const [element] = this;', | ||
'const element = this[0];', | ||
'([element] = this);', | ||
'element = this[0];', | ||
'property = this.property;', | ||
'const [element] = [this];', | ||
'([element] = [this]);', | ||
'const {property} = {property: this};', | ||
'({property} = {property: this});', | ||
'const self = true && this;', | ||
'const self = false || this;', | ||
'const self = false ?? this;', | ||
'foo.bar = this;', | ||
'function foo(a = this) {}', | ||
'function foo({a = this}) {}', | ||
'function foo([a = this]) {}' | ||
], | ||
invalid: [ | ||
'const foo = this;', | ||
'let foo;foo = this;', | ||
'var foo = bar, baz = this;' | ||
] | ||
}); | ||
|
||
test.babel({ | ||
valid: [ | ||
outdent` | ||
class A { | ||
foo = this; | ||
} | ||
`, | ||
outdent` | ||
class A { | ||
static foo = this; | ||
} | ||
` | ||
], | ||
invalid: [] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Snapshot report for `test/no-this-assignment.js` | ||
|
||
The actual snapshot is saved in `no-this-assignment.js.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Invalid #1 | ||
1 | const foo = this; | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | const foo = this;␊ | ||
| ^^^^^^^^^^ Do not assign `this` to `foo`.␊ | ||
` | ||
|
||
## Invalid #2 | ||
1 | let foo;foo = this; | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | let foo;foo = this;␊ | ||
| ^^^^^^^^^^ Do not assign `this` to `foo`.␊ | ||
` | ||
|
||
## Invalid #3 | ||
1 | var foo = bar, baz = this; | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | var foo = bar, baz = this;␊ | ||
| ^^^^^^^^^^ Do not assign `this` to `baz`.␊ | ||
` |
Binary file not shown.