fix(linter/eslint/class-methods-use-this): count this in parameter defaults (#27756)

Methods such as `t(errors = this.errors) { return errors; }` and fields such as `f = (x = this.y) => x` were reported despite using `this` in their defaults. Visit parameters with the existing `ThisFinder` to match ESLint while preserving nested function boundaries.

Addresses report 2 (`eslint/class-methods-use-this`) in #27746.
This commit is contained in:
camc314 committed 2026-10-15 20:27:29 +00:00
1 parent d85923f968
commit 3c44398772
2 files changed
+47 -5

No files matched your search

@@ -200,12 +200,15 @@ impl Rule for ClassMethodsUseThis {
return;
}
accessor.value.as_ref().and_then(|value| match value {
Expression::ArrowFunctionExpression(arrow_function) => {
Some((FunctionLikeBody::Arrow(&arrow_function.body), &accessor.key))
}
Expression::ArrowFunctionExpression(arrow_function) => Some((
FunctionLikeBody::Arrow(&arrow_function.body),
&accessor.key,
&arrow_function.params,
)),
Expression::FunctionExpression(function_expression) => Some((
FunctionLikeBody::Function(function_expression.body.as_deref()?),
&accessor.key,
&function_expression.params,
)),
_ => None,
})
@@ -224,7 +227,11 @@ impl Rule for ClassMethodsUseThis {
return;
}
let Some(function_body) = method_definition.value.body.as_ref() else { return };
Some((FunctionLikeBody::Function(function_body), &method_definition.key))
Some((
FunctionLikeBody::Function(function_body),
&method_definition.key,
&method_definition.value.params,
))
}
AstKind::PropertyDefinition(property_definition) => {
if property_definition.r#static
@@ -243,17 +250,19 @@ impl Rule for ClassMethodsUseThis {
Expression::ArrowFunctionExpression(arrow_function) => Some((
FunctionLikeBody::Arrow(&arrow_function.body),
&property_definition.key,
&arrow_function.params,
)),
Expression::FunctionExpression(function_expression) => Some((
FunctionLikeBody::Function(function_expression.body.as_deref()?),
&property_definition.key,
&function_expression.params,
)),
_ => None,
})
}
_ => return,
};
let Some((function_body, name)) = function_pair else { return };
let Some((function_body, name, params)) = function_pair else { return };
if let Some(name_str) = name.name()
&& self.except_methods.iter().any(|method| {
name_str == method.name.as_str() && method.private == name.is_private_identifier()
@@ -262,6 +271,7 @@ impl Rule for ClassMethodsUseThis {
return;
}
let mut finder = ThisFinder::new();
finder.visit_formal_parameters(params);
match function_body {
FunctionLikeBody::Function(body) => finder.visit_function_body(body),
FunctionLikeBody::Arrow(body) => finder.visit_arrow_function_body(body),
@@ -535,6 +545,14 @@ fn test() {
"class Foo implements Bar { property = () => {} }",
Some(serde_json::json!([{ "enforceForClassFields": false }])),
),
("export class A { t(errors = this.errors) { return errors; } }", None),
("class A { f = (x = this.y) => x; }", None),
("class A { f = function(x = this.y) { return x; }; }", None),
("class A { accessor f = (x = this.y) => x; }", None),
("class A { accessor f = function(x = this.y) { return x; }; }", None),
("class A extends B { f(x = super.y) { return x; } }", None),
("class A { f({ x = this.y } = {}) { return x; } }", None),
("class A { f(x = () => this.y) { return x; } }", None),
];
let fail = vec![
@@ -634,6 +652,9 @@ fn test() {
"class Foo implements Bar { protected property = () => {}; }",
Some(serde_json::json!([{ "ignoreClassesWithImplements": "public-fields" }])),
),
("class A { f(x = 1) { return x; } }", None),
("class A { f(x = function() { return this.y; }) { return x; } }", None),
("class A { f = (x = function() { return this.y; }) => x; }", None),
];
Tester::new(ClassMethodsUseThis::NAME, ClassMethodsUseThis::PLUGIN, pass, fail)
@@ -421,3 +421,24 @@ source: crates/oxc_linter/src/tester.rs
· ────────
╰────
help: Consider converting method `property` to a static method.
⚠ eslint(class-methods-use-this): Expected method `f` to have this.
╭─[class_methods_use_this.tsx:1:11]
1 │ class A { f(x = 1) { return x; } }
· ─
╰────
help: Consider converting method `f` to a static method.
⚠ eslint(class-methods-use-this): Expected method `f` to have this.
╭─[class_methods_use_this.tsx:1:11]
1 │ class A { f(x = function() { return this.y; }) { return x; } }
· ─
╰────
help: Consider converting method `f` to a static method.
⚠ eslint(class-methods-use-this): Expected method `f` to have this.
╭─[class_methods_use_this.tsx:1:11]
1 │ class A { f = (x = function() { return this.y; }) => x; }
· ─
╰────
help: Consider converting method `f` to a static method.