|
| 1 | +import getIndentString from './utils/get-indent-string.js'; |
| 2 | + |
| 3 | +const MESSAGE_ID_ERROR = 'prefer-class-fields/error'; |
| 4 | +const MESSAGE_ID_SUGGESTION = 'prefer-class-fields/suggestion'; |
| 5 | +const messages = { |
| 6 | + [MESSAGE_ID_ERROR]: |
| 7 | + 'Prefer class field declaration over `this` assignment in constructor for static values.', |
| 8 | + [MESSAGE_ID_SUGGESTION]: |
| 9 | + 'Encountered same-named class field declaration and `this` assignment in constructor. Replace the class field declaration with the value from `this` assignment.', |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | +@param {import('eslint').Rule.Node} node |
| 14 | +@param {import('eslint').Rule.RuleContext['sourceCode']} sourceCode |
| 15 | +@param {import('eslint').Rule.RuleFixer} fixer |
| 16 | +*/ |
| 17 | +const removeFieldAssignment = (node, sourceCode, fixer) => { |
| 18 | + const {line} = sourceCode.getLoc(node).start; |
| 19 | + const nodeText = sourceCode.getText(node); |
| 20 | + const lineText = sourceCode.lines[line - 1]; |
| 21 | + const isOnlyNodeOnLine = lineText.trim() === nodeText; |
| 22 | + |
| 23 | + return isOnlyNodeOnLine |
| 24 | + ? fixer.removeRange([ |
| 25 | + sourceCode.getIndexFromLoc({line, column: 0}), |
| 26 | + sourceCode.getIndexFromLoc({line: line + 1, column: 0}), |
| 27 | + ]) |
| 28 | + : fixer.remove(node); |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | +@type {import('eslint').Rule.RuleModule['create']} |
| 33 | +*/ |
| 34 | +const create = context => { |
| 35 | + const {sourceCode} = context; |
| 36 | + |
| 37 | + return { |
| 38 | + ClassBody(classBody) { |
| 39 | + const constructor = classBody.body.find(node => |
| 40 | + node.kind === 'constructor' |
| 41 | + && !node.computed |
| 42 | + && !node.static |
| 43 | + && node.type === 'MethodDefinition' |
| 44 | + && node.value.type === 'FunctionExpression', |
| 45 | + ); |
| 46 | + |
| 47 | + if (!constructor) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + const node = constructor.value.body.body.find(node => node.type !== 'EmptyStatement'); |
| 52 | + |
| 53 | + if (!( |
| 54 | + node?.type === 'ExpressionStatement' |
| 55 | + && node.expression.type === 'AssignmentExpression' |
| 56 | + && node.expression.operator === '=' |
| 57 | + && node.expression.left.type === 'MemberExpression' |
| 58 | + && node.expression.left.object.type === 'ThisExpression' |
| 59 | + && !node.expression.left.computed |
| 60 | + && ['Identifier', 'PrivateIdentifier'].includes(node.expression.left.property.type) |
| 61 | + && node.expression.right.type === 'Literal' |
| 62 | + )) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const propertyName = node.expression.left.property.name; |
| 67 | + const propertyValue = node.expression.right.raw; |
| 68 | + const propertyType = node.expression.left.property.type; |
| 69 | + const existingProperty = classBody.body.find(node => |
| 70 | + node.type === 'PropertyDefinition' |
| 71 | + && !node.computed |
| 72 | + && !node.static |
| 73 | + && node.key.type === propertyType |
| 74 | + && node.key.name === propertyName, |
| 75 | + ); |
| 76 | + |
| 77 | + const problem = { |
| 78 | + node, |
| 79 | + messageId: MESSAGE_ID_ERROR, |
| 80 | + }; |
| 81 | + |
| 82 | + /** |
| 83 | + @param {import('eslint').Rule.RuleFixer} fixer |
| 84 | + */ |
| 85 | + function * fix(fixer) { |
| 86 | + yield removeFieldAssignment(node, sourceCode, fixer); |
| 87 | + |
| 88 | + if (existingProperty) { |
| 89 | + yield existingProperty.value |
| 90 | + ? fixer.replaceText(existingProperty.value, propertyValue) |
| 91 | + : fixer.insertTextAfter(existingProperty.key, ` = ${propertyValue}`); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const closingBrace = sourceCode.getLastToken(classBody); |
| 96 | + const indent = getIndentString(constructor, sourceCode); |
| 97 | + |
| 98 | + let text = `${indent}${propertyName} = ${propertyValue};\n`; |
| 99 | + |
| 100 | + const characterBefore = sourceCode.getText()[sourceCode.getRange(closingBrace)[0] - 1]; |
| 101 | + if (characterBefore !== '\n') { |
| 102 | + text = `\n${text}`; |
| 103 | + } |
| 104 | + |
| 105 | + const lastProperty = classBody.body.at(-1); |
| 106 | + if ( |
| 107 | + lastProperty.type === 'PropertyDefinition' |
| 108 | + && sourceCode.getLastToken(lastProperty).value !== ';' |
| 109 | + ) { |
| 110 | + text = `;${text}`; |
| 111 | + } |
| 112 | + |
| 113 | + yield fixer.insertTextBefore(closingBrace, text); |
| 114 | + } |
| 115 | + |
| 116 | + if (existingProperty?.value) { |
| 117 | + problem.suggest = [ |
| 118 | + { |
| 119 | + messageId: MESSAGE_ID_SUGGESTION, |
| 120 | + fix, |
| 121 | + }, |
| 122 | + ]; |
| 123 | + return problem; |
| 124 | + } |
| 125 | + |
| 126 | + problem.fix = fix; |
| 127 | + return problem; |
| 128 | + }, |
| 129 | + }; |
| 130 | +}; |
| 131 | + |
| 132 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 133 | +const config = { |
| 134 | + create, |
| 135 | + meta: { |
| 136 | + type: 'suggestion', |
| 137 | + docs: { |
| 138 | + description: 'Prefer class field declarations over `this` assignments in constructors.', |
| 139 | + recommended: true, |
| 140 | + }, |
| 141 | + fixable: 'code', |
| 142 | + hasSuggestions: true, |
| 143 | + messages, |
| 144 | + }, |
| 145 | +}; |
| 146 | + |
| 147 | +export default config; |
0 commit comments