From 97011267e0b7e77deea7a20ea00a9eb028f621d1 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Sat, 20 Jan 2024 16:57:06 +0900 Subject: [PATCH 1/3] fix(eslint-plugin): [class-literal-property-style] allow getter when same key setter exists --- .../src/rules/class-literal-property-style.ts | 18 +++++++++++++++--- .../rules/class-literal-property-style.test.ts | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index ec5df76bb61a..764e7cfa1c11 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -89,6 +89,21 @@ export default createRule({ return; } + const sourceCode = getSourceCode(context); + const name = sourceCode.getText(node.key); + + if (node.parent.type === AST_NODE_TYPES.ClassBody) { + const hasDuplicateKeySetter = node.parent.body.some( + element => + element.type === AST_NODE_TYPES.MethodDefinition && + element.kind === 'set' && + sourceCode.getText(element.key) === name, + ); + if (hasDuplicateKeySetter) { + return; + } + } + context.report({ node: node.key, messageId: 'preferFieldStyle', @@ -96,9 +111,6 @@ export default createRule({ { messageId: 'preferFieldStyleSuggestion', fix(fixer): TSESLint.RuleFix { - const sourceCode = getSourceCode(context); - const name = sourceCode.getText(node.key); - let text = ''; text += printNodeModifiers(node, 'readonly'); diff --git a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts index 6eeda2a62fb3..0efba2526f8c 100644 --- a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts +++ b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts @@ -84,6 +84,23 @@ abstract class Mx { \`; } `, + ` + class Mx { + set p1(val) {} + get p1() { + return ''; + } + } + `, + ` + let p1 = 'p1'; + class Mx { + set [p1](val) {} + get [p1]() { + return ''; + } + } + `, { code: ` class Mx { From b511b8a64a6c341d3a6fa29a1c8254dd3eede958 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Mon, 29 Jan 2024 22:27:47 +0900 Subject: [PATCH 2/3] apply review --- .../src/rules/class-literal-property-style.ts | 22 +++++++++++++------ .../class-literal-property-style.test.ts | 16 ++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index 764e7cfa1c11..d2212e8e751a 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -2,7 +2,7 @@ import type { TSESLint, TSESTree } from '@typescript-eslint/utils'; import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import { getSourceCode } from '@typescript-eslint/utils/eslint-utils'; -import { createRule } from '../util'; +import { createRule, getStaticStringValue } from '../util'; type Options = ['fields' | 'getters']; type MessageIds = @@ -66,6 +66,12 @@ export default createRule({ }, defaultOptions: ['fields'], create(context, [style]) { + const sourceCode = getSourceCode(context); + + function getMethodName(node: TSESTree.MethodDefinition): string { + return getStaticStringValue(node.key) ?? sourceCode.getText(node.key); + } + return { ...(style === 'fields' && { MethodDefinition(node): void { @@ -89,16 +95,16 @@ export default createRule({ return; } - const sourceCode = getSourceCode(context); - const name = sourceCode.getText(node.key); + const name = getMethodName(node); if (node.parent.type === AST_NODE_TYPES.ClassBody) { - const hasDuplicateKeySetter = node.parent.body.some( - element => + const hasDuplicateKeySetter = node.parent.body.some(element => { + return ( element.type === AST_NODE_TYPES.MethodDefinition && element.kind === 'set' && - sourceCode.getText(element.key) === name, - ); + getMethodName(element) === name + ); + }); if (hasDuplicateKeySetter) { return; } @@ -111,6 +117,8 @@ export default createRule({ { messageId: 'preferFieldStyleSuggestion', fix(fixer): TSESLint.RuleFix { + const name = sourceCode.getText(node.key); + let text = ''; text += printNodeModifiers(node, 'readonly'); diff --git a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts index 0efba2526f8c..f7221590920a 100644 --- a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts +++ b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts @@ -101,6 +101,22 @@ abstract class Mx { } } `, + ` + class Mx { + set ['foo'](val) {} + get foo() { + return ''; + } + set bar(val) {} + get ['bar']() { + return ''; + } + set ['baz'](val) {} + get baz() { + return ''; + } + } + `, { code: ` class Mx { From dbf2f6704b07d0f3e709514f2034aed0003a19ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Thu, 1 Feb 2024 11:52:59 -0500 Subject: [PATCH 3/3] Update packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts --- .../tests/rules/class-literal-property-style.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts index f7221590920a..1959b1cdaa8d 100644 --- a/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts +++ b/packages/eslint-plugin/tests/rules/class-literal-property-style.test.ts @@ -101,6 +101,15 @@ abstract class Mx { } } `, + ` + let p1 = 'p1'; + class Mx { + set [/* before set */ p1 /* after set */](val) {} + get [/* before get */ p1 /* after get */]() { + return ''; + } + } + `, ` class Mx { set ['foo'](val) {}