unicorn/no-this-assignment
Disallow assigning this to a variable
const self = this exists to reach the outer this from a nested function. An arrow function keeps the this from the surrounding code, so you don't need the extra variable.
Only a plain variable is reported. Destructuring (const { name } = this) and assigning to a property (cache.owner = this) are fine.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/no-this-assignment" = "error" # or "warn"Examples#
example.js1 problem
class Timer {
start() {
const self = thisDo not assign this to self;
setInterval(function () {
self.tick();
}, 1000);
}
}example.jsNo problems
class Timer {
start() {
setInterval(() => {
this.tick();
}, 1000);
}
}example.jsNo problems
class Timer {
start() {
const { interval } = this;
registry.current = this;
setInterval(tick, interval);
}
}