|
| 1 | +import { |
| 2 | + getParenthesizedRange, |
| 3 | + isSameReference, |
| 4 | + unwrapTypeScriptExpression, |
| 5 | +} from './utils/index.js'; |
| 6 | + |
| 7 | +const MESSAGE_ID_REMOVE_OPTIONAL = 'consistent-optional-chaining/remove-optional'; |
| 8 | +const MESSAGE_ID_USE_OPTIONAL = 'consistent-optional-chaining/use-optional'; |
| 9 | +const MESSAGE_ID_SUGGEST_REMOVE_OPTIONAL = 'consistent-optional-chaining/suggest-remove-optional'; |
| 10 | +const MESSAGE_ID_SUGGEST_USE_OPTIONAL = 'consistent-optional-chaining/suggest-use-optional'; |
| 11 | + |
| 12 | +const messages = { |
| 13 | + [MESSAGE_ID_REMOVE_OPTIONAL]: 'Remove unnecessary optional chaining.', |
| 14 | + [MESSAGE_ID_USE_OPTIONAL]: 'Use optional chaining consistently.', |
| 15 | + [MESSAGE_ID_SUGGEST_REMOVE_OPTIONAL]: 'Remove optional chaining.', |
| 16 | + [MESSAGE_ID_SUGGEST_USE_OPTIONAL]: 'Use optional chaining.', |
| 17 | +}; |
| 18 | +const supportedBaseTypes = new Set([ |
| 19 | + 'Identifier', |
| 20 | + 'ThisExpression', |
| 21 | + 'Super', |
| 22 | +]); |
| 23 | + |
| 24 | +function unwrapExpression(node) { |
| 25 | + let previousNode; |
| 26 | + |
| 27 | + while (node !== previousNode) { |
| 28 | + previousNode = node; |
| 29 | + node = unwrapTypeScriptExpression(node); |
| 30 | + |
| 31 | + if (node.type === 'ChainExpression') { |
| 32 | + node = node.expression; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return node; |
| 37 | +} |
| 38 | + |
| 39 | +function getMemberExpression(node) { |
| 40 | + node = unwrapExpression(node); |
| 41 | + |
| 42 | + return node.type === 'MemberExpression' ? node : undefined; |
| 43 | +} |
| 44 | + |
| 45 | +function getMemberBase(memberExpression) { |
| 46 | + return unwrapExpression(memberExpression.object); |
| 47 | +} |
| 48 | + |
| 49 | +function isSupportedMemberBase(node) { |
| 50 | + node = unwrapExpression(node); |
| 51 | + |
| 52 | + if (supportedBaseTypes.has(node.type)) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + if (node.type === 'MemberExpression') { |
| 57 | + return isSupportedMemberBase(node.object); |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | +} |
| 62 | + |
| 63 | +function getMemberAccessOperatorRange(memberExpression, context) { |
| 64 | + const {sourceCode} = context; |
| 65 | + const [, start] = getParenthesizedRange(memberExpression.object, context); |
| 66 | + const end = memberExpression.computed |
| 67 | + ? sourceCode.getRange(sourceCode.getTokenBefore(memberExpression.property, token => token.value === '['))[1] |
| 68 | + : sourceCode.getRange(memberExpression.property)[0]; |
| 69 | + |
| 70 | + return [start, end]; |
| 71 | +} |
| 72 | + |
| 73 | +function hasCommentInRange(sourceCode, [start, end]) { |
| 74 | + return sourceCode.getAllComments().some(comment => { |
| 75 | + const [commentStart, commentEnd] = sourceCode.getRange(comment); |
| 76 | + |
| 77 | + return commentStart >= start && commentEnd <= end; |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +function canReplaceMemberAccessOperator(memberExpression, context) { |
| 82 | + const range = getMemberAccessOperatorRange(memberExpression, context); |
| 83 | + |
| 84 | + return !hasCommentInRange(context.sourceCode, range); |
| 85 | +} |
| 86 | + |
| 87 | +function replaceMemberAccessOperator({memberExpression, context, fixer, replacement}) { |
| 88 | + const range = getMemberAccessOperatorRange(memberExpression, context); |
| 89 | + return fixer.replaceTextRange(range, replacement); |
| 90 | +} |
| 91 | + |
| 92 | +function removeOptionalChaining(memberExpression, context, fixer) { |
| 93 | + return replaceMemberAccessOperator({ |
| 94 | + memberExpression, |
| 95 | + context, |
| 96 | + fixer, |
| 97 | + replacement: memberExpression.computed ? '[' : '.', |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +function addOptionalChaining(memberExpression, context, fixer) { |
| 102 | + return replaceMemberAccessOperator({ |
| 103 | + memberExpression, |
| 104 | + context, |
| 105 | + fixer, |
| 106 | + replacement: memberExpression.computed ? '?.[' : '?.', |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +function getProblem(logicalExpression, context) { |
| 111 | + const {left, operator, right} = logicalExpression; |
| 112 | + const leftMemberExpression = getMemberExpression(left); |
| 113 | + const rightMemberExpression = getMemberExpression(right); |
| 114 | + |
| 115 | + if ( |
| 116 | + !leftMemberExpression |
| 117 | + || !rightMemberExpression |
| 118 | + ) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const leftMemberBase = getMemberBase(leftMemberExpression); |
| 123 | + const rightMemberBase = getMemberBase(rightMemberExpression); |
| 124 | + |
| 125 | + if ( |
| 126 | + !isSupportedMemberBase(leftMemberBase) |
| 127 | + || !isSupportedMemberBase(rightMemberBase) |
| 128 | + || !isSameReference(leftMemberBase, rightMemberBase) |
| 129 | + ) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + if (operator === '&&' && rightMemberExpression.optional) { |
| 134 | + return { |
| 135 | + node: rightMemberExpression, |
| 136 | + messageId: MESSAGE_ID_REMOVE_OPTIONAL, |
| 137 | + ...(canReplaceMemberAccessOperator(rightMemberExpression, context) && { |
| 138 | + suggest: [ |
| 139 | + { |
| 140 | + messageId: MESSAGE_ID_SUGGEST_REMOVE_OPTIONAL, |
| 141 | + fix: fixer => removeOptionalChaining(rightMemberExpression, context, fixer), |
| 142 | + }, |
| 143 | + ], |
| 144 | + }), |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + if (operator !== '||' || leftMemberExpression.optional === rightMemberExpression.optional) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + if (rightMemberExpression.optional) { |
| 153 | + return { |
| 154 | + node: rightMemberExpression, |
| 155 | + messageId: MESSAGE_ID_REMOVE_OPTIONAL, |
| 156 | + ...(canReplaceMemberAccessOperator(rightMemberExpression, context) && { |
| 157 | + suggest: [ |
| 158 | + { |
| 159 | + messageId: MESSAGE_ID_SUGGEST_REMOVE_OPTIONAL, |
| 160 | + fix: fixer => removeOptionalChaining(rightMemberExpression, context, fixer), |
| 161 | + }, |
| 162 | + ], |
| 163 | + }), |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + return { |
| 168 | + node: rightMemberExpression, |
| 169 | + messageId: MESSAGE_ID_USE_OPTIONAL, |
| 170 | + ...(canReplaceMemberAccessOperator(rightMemberExpression, context) && { |
| 171 | + suggest: [ |
| 172 | + { |
| 173 | + messageId: MESSAGE_ID_SUGGEST_USE_OPTIONAL, |
| 174 | + fix: fixer => addOptionalChaining(rightMemberExpression, context, fixer), |
| 175 | + }, |
| 176 | + ], |
| 177 | + }), |
| 178 | + }; |
| 179 | +} |
| 180 | + |
| 181 | +/** @param {import('eslint').Rule.RuleContext} context */ |
| 182 | +const create = context => { |
| 183 | + context.on('LogicalExpression', logicalExpression => getProblem(logicalExpression, context)); |
| 184 | +}; |
| 185 | + |
| 186 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 187 | +const config = { |
| 188 | + create, |
| 189 | + meta: { |
| 190 | + type: 'suggestion', |
| 191 | + docs: { |
| 192 | + description: 'Enforce consistent optional chaining for same-base member access.', |
| 193 | + recommended: 'unopinionated', |
| 194 | + }, |
| 195 | + hasSuggestions: true, |
| 196 | + messages, |
| 197 | + languages: [ |
| 198 | + 'js/js', |
| 199 | + ], |
| 200 | + }, |
| 201 | +}; |
| 202 | + |
| 203 | +export default config; |
0 commit comments