Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8c824a

Browse files
fix(eslint-plugin): replace auto-fix of class literal property style rule with suggestion (typescript-eslint#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 <[email protected]>
1 parent 4437d18 commit a8c824a

File tree

2 files changed

+205
-109
lines changed

2 files changed

+205
-109
lines changed

packages/eslint-plugin/src/rules/class-literal-property-style.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import type { TSESTree } from '@typescript-eslint/utils';
1+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
22
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
33

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

66
type Options = ['fields' | 'getters'];
7-
type MessageIds = 'preferFieldStyle' | 'preferGetterStyle';
7+
type MessageIds =
8+
| 'preferFieldStyle'
9+
| 'preferFieldStyleSuggestion'
10+
| 'preferGetterStyle'
11+
| 'preferGetterStyleSuggestion';
812

913
interface NodeWithModifiers {
1014
accessibility?: TSESTree.Accessibility;
@@ -45,10 +49,12 @@ export default util.createRule<Options, MessageIds>({
4549
'Enforce that literals on classes are exposed in a consistent style',
4650
recommended: 'strict',
4751
},
48-
fixable: 'code',
52+
hasSuggestions: true,
4953
messages: {
5054
preferFieldStyle: 'Literals should be exposed using readonly fields.',
55+
preferFieldStyleSuggestion: 'Replace the literals with readonly fields.',
5156
preferGetterStyle: 'Literals should be exposed using getters.',
57+
preferGetterStyleSuggestion: 'Replace the literals with getters.',
5258
},
5359
schema: [{ enum: ['fields', 'getters'] }],
5460
},
@@ -80,18 +86,23 @@ export default util.createRule<Options, MessageIds>({
8086
context.report({
8187
node: node.key,
8288
messageId: 'preferFieldStyle',
83-
fix(fixer) {
84-
const sourceCode = context.getSourceCode();
85-
const name = sourceCode.getText(node.key);
86-
87-
let text = '';
88-
89-
text += printNodeModifiers(node, 'readonly');
90-
text += node.computed ? `[${name}]` : name;
91-
text += ` = ${sourceCode.getText(argument)};`;
92-
93-
return fixer.replaceText(node, text);
94-
},
89+
suggest: [
90+
{
91+
messageId: 'preferFieldStyleSuggestion',
92+
fix(fixer): TSESLint.RuleFix {
93+
const sourceCode = context.getSourceCode();
94+
const name = sourceCode.getText(node.key);
95+
96+
let text = '';
97+
98+
text += printNodeModifiers(node, 'readonly');
99+
text += node.computed ? `[${name}]` : name;
100+
text += ` = ${sourceCode.getText(argument)};`;
101+
102+
return fixer.replaceText(node, text);
103+
},
104+
},
105+
],
95106
});
96107
},
97108
}),
@@ -110,18 +121,23 @@ export default util.createRule<Options, MessageIds>({
110121
context.report({
111122
node: node.key,
112123
messageId: 'preferGetterStyle',
113-
fix(fixer) {
114-
const sourceCode = context.getSourceCode();
115-
const name = sourceCode.getText(node.key);
116-
117-
let text = '';
118-
119-
text += printNodeModifiers(node, 'get');
120-
text += node.computed ? `[${name}]` : name;
121-
text += `() { return ${sourceCode.getText(value)}; }`;
122-
123-
return fixer.replaceText(node, text);
124-
},
124+
suggest: [
125+
{
126+
messageId: 'preferGetterStyleSuggestion',
127+
fix(fixer): TSESLint.RuleFix {
128+
const sourceCode = context.getSourceCode();
129+
const name = sourceCode.getText(node.key);
130+
131+
let text = '';
132+
133+
text += printNodeModifiers(node, 'get');
134+
text += node.computed ? `[${name}]` : name;
135+
text += `() { return ${sourceCode.getText(value)}; }`;
136+
137+
return fixer.replaceText(node, text);
138+
},
139+
},
140+
],
125141
});
126142
},
127143
}),

0 commit comments

Comments
 (0)