unicorn/no-this-assignment
eslint-plugin-unicorn
No autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-this-assignment": "error",
},
},
];const foo = this;
setTimeout(function () {
foo.bar();
}, 1000);const foo = this;
class Bar {
method() {
foo.baz();
}
}
new Bar().method();setTimeout(() => {
this.bar();
}, 1000);setTimeout(function () {
this.bar();
}.bind(this), 1000);class Bar {
constructor(fooInstance) {
this.fooInstance = fooInstance;
}
method() {
this.fooInstance.baz();
}
}
new Bar(this).method();Examples from the ESLint documentation.
unicorn/no-this-assignment
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/no-this-assignment": "error",
},
},
];unicorn/no-this-assignment
unicorn plugin · pedantic
{
"rules": {
"unicorn/no-this-assignment": "error"
}
}Biome does not have this rule.
Disallow the use of objects as default parameters.
Disallow comparing undefined using typeof.
Prefer Array#toSorted() over Array#sort().
Disallow using 1 as the depth argument of Array#flat().
Prefer Array#toReversed() over Array#reverse().
Disallow using .length or Infinity as the deleteCount or skipCount argument of Array#{splice,toSpliced}().