Skip to content
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

fix(eslint-plugin): replace auto-fix of class literal property style rule with suggestion #7054

2 changes: 1 addition & 1 deletion docs/linting/Typed_Linting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ You may see new rules reporting errors based on type information!
The `parserOptions.project` option can be turned on with either:

- `true`: to always use `tsconfig.json`s nearest to source files
- `string | string[]`: any number of glob paths to match TSConfig files relative to the
- `string | string[]`: any number of glob paths to match TSConfig files relative to `parserOptions.tsconfigRootDir`, or the CWD if that is not provided

For example, if you use a specific `tsconfig.eslint.json` for linting, you'd specify:

Expand Down
71 changes: 44 additions & 27 deletions packages/eslint-plugin/src/rules/class-literal-property-style.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { TSESTree } from '@typescript-eslint/utils';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';

type Options = ['fields' | 'getters'];
type MessageIds = 'preferFieldStyle' | 'preferGetterStyle';
type MessageIds =
| 'preferFieldStyle'
| 'preferFieldStyleSuggestion'
| 'preferGetterStyle'
| 'preferGetterStyleSuggestion';

interface NodeWithModifiers {
accessibility?: TSESTree.Accessibility;
Expand Down Expand Up @@ -45,10 +49,13 @@ export default util.createRule<Options, MessageIds>({
'Enforce that literals on classes are exposed in a consistent style',
recommended: 'strict',
},
fixable: 'code',
hasSuggestions: true,
messages: {
preferFieldStyle: 'Literals should be exposed using readonly fields.',
preferFieldStyleSuggestion:
'Try replacing the literals with readonly fields.',
preferGetterStyle: 'Literals should be exposed using getters.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure what messaging to use here, I'm open for other suggestions 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah these can be tricky. Searching around other rules that have hasSuggestions: true, we normally go with language like "Do X with Y". Here I think that'd be:

- 'Try replacing the literals with readonly fields.'
+ 'Replace the literals with readonly fields.'

What do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds good, I've updated both suggestion messages.

preferGetterStyleSuggestion: 'Try replacing the literals with getters.',
},
schema: [{ enum: ['fields', 'getters'] }],
},
Expand Down Expand Up @@ -80,18 +87,23 @@ export default util.createRule<Options, MessageIds>({
context.report({
node: node.key,
messageId: 'preferFieldStyle',
fix(fixer) {
const sourceCode = context.getSourceCode();
const name = sourceCode.getText(node.key);

let text = '';

text += printNodeModifiers(node, 'readonly');
text += node.computed ? `[${name}]` : name;
text += ` = ${sourceCode.getText(argument)};`;

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

let text = '';

text += printNodeModifiers(node, 'readonly');
text += node.computed ? `[${name}]` : name;
text += ` = ${sourceCode.getText(argument)};`;

return fixer.replaceText(node, text);
},
},
],
});
},
}),
Expand All @@ -110,18 +122,23 @@ export default util.createRule<Options, MessageIds>({
context.report({
node: node.key,
messageId: 'preferGetterStyle',
fix(fixer) {
const sourceCode = context.getSourceCode();
const name = sourceCode.getText(node.key);

let text = '';

text += printNodeModifiers(node, 'get');
text += node.computed ? `[${name}]` : name;
text += `() { return ${sourceCode.getText(value)}; }`;

return fixer.replaceText(node, text);
},
suggest: [
{
messageId: 'preferGetterStyleSuggestion',
fix(fixer): TSESLint.RuleFix {
const sourceCode = context.getSourceCode();
const name = sourceCode.getText(node.key);

let text = '';

text += printNodeModifiers(node, 'get');
text += node.computed ? `[${name}]` : name;
text += `() { return ${sourceCode.getText(value)}; }`;

return fixer.replaceText(node, text);
},
},
],
});
},
}),
Expand Down