no-invalid-this
eslint
No autofix
eslint.config.js
export default [
{
rules: {
"no-invalid-this": "error",
},
},
];Disallow use of this in contexts where the value of this is undefined
"use strict";
(function() {
this.a = 0;
baz(() => this);
})();
function foo() {
this.a = 0;
baz(() => this);
}
const bar = function() {
this.a = 0;
baz(() => this);
};
foo(function() {
this.a = 0;
baz(() => this);
});
const obj = {
aaa: function() {
return function foo() {
// There is in a method `aaa`, but `foo` is not a method.
this.a = 0;
baz(() => this);
};
}
};
// …"use strict";
this.a = 0;
baz(() => this);
function Foo() {
// OK, this is in a legacy style constructor.
this.a = 0;
baz(() => this);
}
class Bar {
constructor() {
// OK, this is in a constructor.
this.a = 0;
baz(() => this);
}
}
const obj = {
foo: function foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
}
};
const obj1 = {
foo() {
// OK, this is in a method (this function is on object literal).
this.a = 0;
}
};
// …Examples from the ESLint documentation.
no-invalid-this
eslint
export default [
{
rules: {
"no-invalid-this": "error",
},
},
];Oxlint does not have this rule.
nursery/noInvalidThis
nursery · since 2.6.0
{
"linter": {
"rules": {
"nursery": {
"noInvalidThis": "error"
}
}
}
}Disallow this keywords outside of classes or class-like objects
Enforce a maximum depth that blocks can be nested
Enforce a maximum number of lines per file
Enforce a maximum number of parameters in function definitions
Disallow shorthand type conversions
Enforce or disallow capitalization of the first letter of a comment