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

Skip to content
Merged
70 changes: 43 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,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
Loading