8000 fix(eslint-plugin): [class-methods-use-this] check `accessor` methods with a function initializer by ronami · Pull Request #10796 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [class-methods-use-this] check accessor methods with a function initializer #10796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import TabItem from '@theme/TabItem';
>
> See **https://typescript-eslint.io/rules/class-methods-use-this** for documentation.

It adds support for ignoring `override` methods and/or methods on classes that implement an interface.
It adds support for ignoring `override` methods and/or methods on classes that implement an interface. It also supports auto-accessor properties.

## Options

Expand Down
32 changes: 28 additions & 4 deletions packages/eslint-plugin/src/rules/class-methods-use-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,20 @@ export default createRule<Options, MessageIds>({
}
| {
class: TSESTree.ClassDeclaration | TSESTree.ClassExpression;
member: TSESTree.MethodDefinition | TSESTree.PropertyDefinition;
member:
| TSESTree.AccessorProperty
| TSESTree.MethodDefinition
| TSESTree.PropertyDefinition;
parent: Stack | undefined;
usesThis: boolean;
};
let stack: Stack | undefined;

function pushContext(
member?: TSESTree.MethodDefinition | TSESTree.PropertyDefinition,
member?:
| TSESTree.AccessorProperty
| TSESTree.MethodDefinition
| TSESTree.PropertyDefinition,
): void {
if (member?.parent.type === AST_NODE_TYPES.ClassBody) {
stack = {
Expand All @@ -135,7 +141,8 @@ export default createRule<Options, MessageIds>({
): void {
if (
node.parent.type === AST_NODE_TYPES.MethodDefinition ||
node.parent.type === AST_NODE_TYPES.PropertyDefinition
node.parent.type === AST_NODE_TYPES.PropertyDefinition ||
node.parent.type === AST_NODE_TYPES.AccessorProperty
) {
pushContext(node.parent);
} else {
Expand Down Expand Up @@ -172,7 +179,8 @@ export default createRule<Options, MessageIds>({
node.static ||
(node.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor') ||
(node.type === AST_NODE_TYPES.PropertyDefinition &&
((node.type === AST_NODE_TYPES.PropertyDefinition ||
node.type === AST_NODE_TYPES.AccessorProperty) &&
!enforceForClassFields)
) {
return false;
Expand Down Expand Up @@ -242,6 +250,16 @@ export default createRule<Options, MessageIds>({
},
...(enforceForClassFields
? {
'AccessorProperty > ArrowFunctionExpression.value'(
node: TSESTree.ArrowFunctionExpression,
): void {
enterFunction(node);
},
'AccessorProperty > ArrowFunctionExpression.value:exit'(
node: TSESTree.ArrowFunctionExpression,
): void {
exitFunction(node);
},
'PropertyDefinition > ArrowFunctionExpression.value'(
node: TSESTree.ArrowFunctionExpression,
): void {
Expand All @@ -258,6 +276,12 @@ export default createRule<Options, MessageIds>({
/*
* Class field value are implicit functions.
*/
'AccessorProperty:exit'(): void {
popContext();
},
'AccessorProperty > *.key:exit'(): void {
pushContext();
},
'PropertyDefinition:exit'(): void {
popContext();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,45 @@ class Foo {
},
{
code: `
class Foo {
accessor method = () => {};
}
`,
errors: [
{
messageId: 'missingThis',
},
],
options: [{}],
},
{
code: `
class Foo {
private accessor method = () => {};
}
`,
errors: [
{
messageId: 'missingThis',
},
],
options: [{}],
},
{
code: `
class Foo {
protected accessor method = () => {};
}
`,
errors: [
{
messageId: 'missingThis',
},
],
options: [{}],
},
{
code: `
class Foo {
#method() {}
}
Expand Down Expand Up @@ -577,6 +616,14 @@ class Foo implements Bar {
},
{
code: `
class Foo implements Bar {
accessor method = () => {};
}
`,
options: [{ ignoreClassesThatImplementAnInterface: true }],
},
{
code: `
class Foo implements Bar {
get getter() {}
}
Expand Down Expand Up @@ -617,6 +664,14 @@ class Foo {
},
{
code: `
class Foo {
override accessor method = () => {};
}
`,
options: [{ ignoreOverrideMethods: true }],
},
{
code: `
class Foo {
override get getter(): number {}
}
Expand Down Expand Up @@ -901,5 +956,23 @@ class Foo implements Bar {
},
],
},
{
code: `
class Foo {
accessor method = () => {
this;
};
}
`,
},
{
code: `
class Foo {
accessor method = function () {
this;
};
}
`,
},
],
});
Loading
0