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

Appearance settings

fix(eslint-plugin): [class-literal-property-style] allow getter when same key setter exists #8277

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 5 commits into from
Feb 2, 2024
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
24 changes: 22 additions & 2 deletions 24 packages/eslint-plugin/src/rules/class-literal-property-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -66,6 +66,12 @@ export default createRule<Options, MessageIds>({
},
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 {
Expand All @@ -89,14 +95,28 @@ export default createRule<Options, MessageIds>({
return;
}

const name = getMethodName(node);

if (node.parent.type === AST_NODE_TYPES.ClassBody) {
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved
const hasDuplicateKeySetter = node.parent.body.some(element => {
return (
element.type === AST_NODE_TYPES.MethodDefinition &&
element.kind === 'set' &&
getMethodName(element) === name
);
});
if (hasDuplicateKeySetter) {
return;
}
}

context.report({
node: node.key,
messageId: 'preferFieldStyle',
suggest: [
{
messageId: 'preferFieldStyleSuggestion',
fix(fixer): TSESLint.RuleFix {
const sourceCode = getSourceCode(context);
const name = sourceCode.getText(node.key);

let text = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,48 @@ abstract class Mx {
\`;
}
`,
`
class Mx {
set p1(val) {}
get p1() {
return '';
}
}
`,
`
let p1 = 'p1';
class Mx {
set [p1](val) {}
get [p1]() {
return '';
}
}
`,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
`
let p1 = 'p1';
class Mx {
set [/* before set */ p1 /* after set */](val) {}
get [/* before get */ p1 /* after get */]() {
return '';
}
}
`,
`
class Mx {
set ['foo'](val) {}
get foo() {
return '';
}
set bar(val) {}
get ['bar']() {
return '';
}
set ['baz'](val) {}
get baz() {
return '';
}
}
`,
{
code: `
class Mx {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.