|
| 1 | +import { |
| 2 | + AST_NODE_TYPES, |
| 3 | + TSESTree, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
| 5 | +import * as util from '../util'; |
| 6 | + |
| 7 | +type Options = ['fields' | 'getters']; |
| 8 | +type MessageIds = 'preferFieldStyle' | 'preferGetterStyle'; |
| 9 | + |
| 10 | +interface NodeWithModifiers { |
| 11 | + accessibility?: TSESTree.Accessibility; |
| 12 | + static: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +const printNodeModifiers = ( |
| 16 | + node: NodeWithModifiers, |
| 17 | + final: 'readonly' | 'get', |
| 18 | +): string => |
| 19 | + `${node.accessibility ?? ''}${ |
| 20 | + node.static ? ' static' : '' |
| 21 | + } ${final} `.trimLeft(); |
| 22 | + |
| 23 | +const isSupportedLiteral = ( |
| 24 | + node: TSESTree.Node, |
| 25 | +): node is TSESTree.LiteralExpression => { |
| 26 | + if ( |
| 27 | + node.type === AST_NODE_TYPES.Literal || |
| 28 | + node.type === AST_NODE_TYPES.BigIntLiteral |
| 29 | + ) { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + if ( |
| 34 | + node.type === AST_NODE_TYPES.TaggedTemplateExpression || |
| 35 | + node.type === AST_NODE_TYPES.TemplateLiteral |
| 36 | + ) { |
| 37 | + return ('quasi' in node ? node.quasi.quasis : node.quasis).length === 1; |
| 38 | + } |
| 39 | + |
| 40 | + return false; |
| 41 | +}; |
| 42 | + |
| 43 | +export default util.createRule<Options, MessageIds>({ |
| 44 | + name: 'class-literal-property-style', |
| 45 | + meta: { |
| 46 | + type: 'problem', |
| 47 | + docs: { |
| 48 | + description: |
| 49 | + 'Ensures that literals on classes are exposed in a consistent style', |
| 50 | + category: 'Best Practices', |
| 51 | + recommended: false, |
| 52 | + }, |
| 53 | + fixable: 'code', |
| 54 | + messages: { |
| 55 | + preferFieldStyle: 'Literals should be exposed using readonly fields.', |
| 56 | + preferGetterStyle: 'Literals should be exposed using getters.', |
| 57 | + }, |
| 58 | + schema: [{ enum: ['fields', 'getters'] }], |
| 59 | + }, |
| 60 | + defaultOptions: ['fields'], |
| 61 | + create(context, [style]) { |
| 62 | + if (style === 'fields') { |
| 63 | + return { |
| 64 | + MethodDefinition(node: TSESTree.MethodDefinition): void { |
| 65 | + if ( |
| 66 | + node.kind !== 'get' || |
| 67 | + !node.value.body || |
| 68 | + !node.value.body.body.length |
| 69 | + ) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const [statement] = node.value.body.body; |
| 74 | + |
| 75 | + if (statement.type !== AST_NODE_TYPES.ReturnStatement) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const { argument } = statement; |
| 80 | + |
| 81 | + if (!argument || !isSupportedLiteral(argument)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + context.report({ |
| 86 | + node: node.key, |
| 87 | + messageId: 'preferFieldStyle', |
| 88 | + fix(fixer) { |
| 89 | + const sourceCode = context.getSourceCode(); |
| 90 | + const name = sourceCode.getText(node.key); |
| 91 | + |
| 92 | + let text = ''; |
| 93 | + |
| 94 | + text += printNodeModifiers(node, 'readonly'); |
| 95 | + text += node.computed ? `[${name}]` : name; |
| 96 | + text += ` = ${sourceCode.getText(argument)};`; |
| 97 | + |
| 98 | + return fixer.replaceText(node, text); |
| 99 | + }, |
| 100 | + }); |
| 101 | + }, |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + return { |
| 106 | + ClassProperty(node: TSESTree.ClassProperty): void { |
| 107 | + if (!node.readonly || node.declare) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const { value } = node; |
| 112 | + |
| 113 | + if (!value || !isSupportedLiteral(value)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + context.report({ |
| 118 | + node: node.key, |
| 119 | + messageId: 'preferGetterStyle', |
| 120 | + fix(fixer) { |
| 121 | + const sourceCode = context.getSourceCode(); |
| 122 | + const name = sourceCode.getText(node.key); |
| 123 | + |
| 124 | + let text = ''; |
| 125 | + |
| 126 | + text += printNodeModifiers(node, 'get'); |
| 127 | + text += node.computed ? `[${name}]` : name; |
| 128 | + text += `() { return ${sourceCode.getText(value)}; }`; |
| 129 | + |
| 130 | + return fixer.replaceText(node, text); |
| 131 | + }, |
| 132 | + }); |
| 133 | + }, |
| 134 | + }; |
| 135 | + }, |
| 136 | +}); |
0 commit comments