Skip to content

Commit

Permalink
fix(eslint-plugin): replace auto-fix of class literal property style …
Browse files Browse the repository at this point in the history
…rule with suggestion (#7054)

* Complete sentence documenting turning on parserOptions.project

* fix: replace auto-fix with suggestion

* test: adapt tests to expect suggestion rather than output from auto-fix

* fix: add return type

* Revert "Complete sentence documenting turning on parserOptions.project"

This reverts commit a05601f.

* Replace text with suggestion

* Replace both suggestion messages

* Formatting

* yarn format

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
Melandra and JoshuaKGoldberg committed Jul 9, 2023
1 parent 4437d18 commit a8c824a
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 109 deletions.
70 changes: 43 additions & 27 deletions packages/eslint-plugin/src/rules/class-literal-property-style.ts
@@ -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,12 @@ 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: 'Replace the literals with readonly fields.',
preferGetterStyle: 'Literals should be exposed using getters.',
preferGetterStyleSuggestion: 'Replace the literals with getters.',
},
schema: [{ enum: ['fields', 'getters'] }],
},
Expand Down Expand Up @@ -80,18 +86,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 +121,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

0 comments on commit a8c824a

Please sign in to comment.