Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(eslint-plugin): [class-methods-use-this] checkaccessor 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/class-methods-use-this.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand Up@@ -617,6 +664,14 @@ class Foo {
},
{
code: `
class Foo {
override accessor method = () => {};
}
`,
options: [{ ignoreOverrideMethods: true }],
},
{
code: `
class Foo {
override get getter(): number {}
}
Expand DownExpand Up@@ -901,5 +956,23 @@ class Foo implements Bar {
},
],
},
{
code: `
class Foo {
accessor method = () => {
this;
};
}
`,
},
{
code: `
class Foo {
accessor method = function () {
this;
};
}
`,
},
],
});
Loading

[8]ページ先頭

©2009-2025 Movatter.jp