|
| 1 | +/* @internal */ |
| 2 | +namespace ts.refactor.convertToOptionalChainExpression { |
| 3 | + const refactorName = "Convert to optional chain expression"; |
| 4 | + const convertToOptionalChainExpressionMessage = getLocaleSpecificMessage(Diagnostics.Convert_to_optional_chain_expression); |
| 5 | + |
| 6 | + registerRefactor(refactorName, { getAvailableActions, getEditsForAction }); |
| 7 | + |
| 8 | + function getAvailableActions(context: RefactorContext): readonly ApplicableRefactorInfo[] { |
| 9 | + const info = getInfo(context, context.triggerReason === "invoked"); |
| 10 | + if (!info) return emptyArray; |
| 11 | + |
| 12 | + if (!info.error) { |
| 13 | + return [{ |
| 14 | + name: refactorName, |
| 15 | + description: convertToOptionalChainExpressionMessage, |
| 16 | + actions: [{ |
| 17 | + name: refactorName, |
| 18 | + description: convertToOptionalChainExpressionMessage |
| 19 | + }] |
| 20 | + }]; |
| 21 | + } |
| 22 | + |
| 23 | + if (context.preferences.provideRefactorNotApplicableReason) { |
| 24 | + return [{ |
| 25 | + name: refactorName, |
| 26 | + description: convertToOptionalChainExpressionMessage, |
| 27 | + actions: [{ |
| 28 | + name: refactorName, |
| 29 | + description: convertToOptionalChainExpressionMessage, |
| 30 | + notApplicableReason: info.error |
| 31 | + }] |
| 32 | + }]; |
| 33 | + } |
| 34 | + return emptyArray; |
| 35 | + } |
| 36 | + |
| 37 | + function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { |
| 38 | + const info = getInfo(context); |
| 39 | + if (!info || !info.info) return undefined; |
| 40 | + const edits = textChanges.ChangeTracker.with(context, t => |
| 41 | + doChange(context.file, context.program.getTypeChecker(), t, Debug.checkDefined(info.info, "context must have info"), actionName) |
| 42 | + ); |
| 43 | + return { edits, renameFilename: undefined, renameLocation: undefined }; |
| 44 | + } |
| 45 | + |
| 46 | + type InfoOrError = { |
| 47 | + info: Info, |
| 48 | + error?: never; |
| 49 | + } | { |
| 50 | + info?: never, |
| 51 | + error: string; |
| 52 | + }; |
| 53 | + |
| 54 | + interface Info { |
| 55 | + finalExpression: PropertyAccessExpression | CallExpression, |
| 56 | + occurrences: (PropertyAccessExpression | Identifier)[], |
| 57 | + expression: ValidExpression, |
| 58 | + }; |
| 59 | + |
| 60 | + type ValidExpressionOrStatement = ValidExpression | ValidStatement; |
| 61 | + |
| 62 | + /** |
| 63 | + * Types for which a "Convert to optional chain refactor" are offered. |
| 64 | + */ |
| 65 | + type ValidExpression = BinaryExpression | ConditionalExpression; |
| 66 | + |
| 67 | + /** |
| 68 | + * Types of statements which are likely to include a valid expression for extraction. |
| 69 | + */ |
| 70 | + type ValidStatement = ExpressionStatement | ReturnStatement | VariableStatement; |
| 71 | + |
| 72 | + function isValidExpression(node: Node): node is ValidExpression { |
| 73 | + return isBinaryExpression(node) || isConditionalExpression(node); |
| 74 | + } |
| 75 | + |
| 76 | + function isValidStatement(node: Node): node is ValidStatement { |
| 77 | + return isExpressionStatement(node) || isReturnStatement(node) || isVariableStatement(node); |
| 78 | + } |
| 79 | + |
| 80 | + function isValidExpressionOrStatement(node: Node): node is ValidExpressionOrStatement { |
| 81 | + return isValidExpression(node) || isValidStatement(node); |
| 82 | + } |
| 83 | + |
| 84 | + function getInfo(context: RefactorContext, considerEmptySpans = true): InfoOrError | undefined { |
| 85 | + const { file, program } = context; |
| 86 | + const span = getRefactorContextSpan(context); |
| 87 | + |
| 88 | + const forEmptySpan = span.length === 0; |
| 89 | + if (forEmptySpan && !considerEmptySpans) return undefined; |
| 90 | + |
| 91 | + // selecting fo[|o && foo.ba|]r should be valid, so adjust span to fit start and end tokens |
| 92 | + const startToken = getTokenAtPosition(file, span.start); |
| 93 | + const endToken = findTokenOnLeftOfPosition(file, span.start + span.length); |
| 94 | + const adjustedSpan = createTextSpanFromBounds(startToken.pos, endToken && endToken.end >= startToken.pos ? endToken.getEnd() : startToken.getEnd()); |
| 95 | + |
| 96 | + const parent = forEmptySpan ? getValidParentNodeOfEmptySpan(startToken) : getValidParentNodeContainingSpan(startToken, adjustedSpan); |
| 97 | + const expression = parent && isValidExpressionOrStatement(parent) ? getExpression(parent) : undefined; |
| 98 | + if (!expression) return { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_convertible_access_expression) }; |
| 99 | + |
| 100 | + const checker = program.getTypeChecker(); |
| 101 | + return isConditionalExpression(expression) ? getConditionalInfo(expression, checker) : getBinaryInfo(expression); |
| 102 | + } |
| 103 | + |
| 104 | + function getConditionalInfo(expression: ConditionalExpression, checker: TypeChecker): InfoOrError | undefined { |
| 105 | + const condition = expression.condition; |
| 106 | + const finalExpression = getFinalExpressionInChain(expression.whenTrue); |
| 107 | + |
| 108 | + if (!finalExpression || checker.isNullableType(checker.getTypeAtLocation(finalExpression))) { |
| 109 | + return { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_convertible_access_expression) }; |
| 110 | + }; |
| 111 | + |
| 112 | + if ((isPropertyAccessExpression(condition) || isIdentifier(condition)) |
| 113 | + && getMatchingStart(condition, finalExpression.expression)) { |
| 114 | + return { info: { finalExpression, occurrences: [condition], expression } }; |
| 115 | + } |
| 116 | + else if (isBinaryExpression(condition)) { |
| 117 | + const occurrences = getOccurrencesInExpression(finalExpression.expression, condition); |
| 118 | + return occurrences ? { info: { finalExpression, occurrences, expression } } : |
| 119 | + { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_matching_access_expressions) }; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + function getBinaryInfo(expression: BinaryExpression): InfoOrError | undefined { |
| 124 | + if (expression.operatorToken.kind !== SyntaxKind.AmpersandAmpersandToken) { |
| 125 | + return { error: getLocaleSpecificMessage(Diagnostics.Can_only_convert_logical_AND_access_chains) }; |
| 126 | + }; |
| 127 | + const finalExpression = getFinalExpressionInChain(expression.right); |
| 128 | + |
| 129 | + if (!finalExpression) return { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_convertible_access_expression) }; |
| 130 | + |
| 131 | + const occurrences = getOccurrencesInExpression(finalExpression.expression, expression.left); |
| 132 | + return occurrences ? { info: { finalExpression, occurrences, expression } } : |
| 133 | + { error: getLocaleSpecificMessage(Diagnostics.Could_not_find_matching_access_expressions) }; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Gets a list of property accesses that appear in matchTo and occur in sequence in expression. |
| 138 | + */ |
| 139 | + function getOccurrencesInExpression(matchTo: Expression, expression: Expression): (PropertyAccessExpression | Identifier)[] | undefined { |
| 140 | + const occurrences: (PropertyAccessExpression | Identifier)[] = []; |
| 141 | + while (isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.AmpersandAmpersandToken) { |
| 142 | + const match = getMatchingStart(skipParentheses(matchTo), skipParentheses(expression.right)); |
| 143 | + if (!match) { |
| 144 | + break; |
| 145 | + } |
| 146 | + occurrences.push(match); |
| 147 | + matchTo = match; |
| 148 | + expression = expression.left; |
| 149 | + } |
| 150 | + const finalMatch = getMatchingStart(matchTo, expression); |
| 151 | + if (finalMatch) { |
| 152 | + occurrences.push(finalMatch); |
| 153 | + } |
| 154 | + return occurrences.length > 0 ? occurrences: undefined; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns subchain if chain begins with subchain syntactically. |
| 159 | + */ |
| 160 | + function getMatchingStart(chain: Expression, subchain: Expression): PropertyAccessExpression | Identifier | undefined { |
| 161 | + return (isIdentifier(subchain) || isPropertyAccessExpression(subchain)) && |
| 162 | + chainStartsWith(chain, subchain) ? subchain : undefined; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Returns true if chain begins with subchain syntactically. |
| 167 | + */ |
| 168 | + function chainStartsWith(chain: Node, subchain: Node): boolean { |
| 169 | + // skip until we find a matching identifier. |
| 170 | + while (isCallExpression(chain) || isPropertyAccessExpression(chain)) { |
| 171 | + const subchainName = isPropertyAccessExpression(subchain) ? subchain.name.getText() : subchain.getText(); |
| 172 | + if (isPropertyAccessExpression(chain) && chain.name.getText() === subchainName) break; |
| 173 | + chain = chain.expression; |
| 174 | + } |
| 175 | + // check that the chains match at each access. Call chains in subchain are not valid. |
| 176 | + while (isPropertyAccessExpression(chain) && isPropertyAccessExpression(subchain)) { |
| 177 | + if (chain.name.getText() !== subchain.name.getText()) return false; |
| 178 | + chain = chain.expression; |
| 179 | + subchain = subchain.expression; |
| 180 | + } |
| 181 | + // check if we have reached a final identifier. |
| 182 | + return isIdentifier(chain) && isIdentifier(subchain) && chain.getText() === subchain.getText(); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Find the least ancestor of the input node that is a valid type for extraction and contains the input span. |
| 187 | + */ |
| 188 | + function getValidParentNodeContainingSpan(node: Node, span: TextSpan): ValidExpressionOrStatement | undefined { |
| 189 | + while (node.parent) { |
| 190 | + if (isValidExpressionOrStatement(node) && span.length !== 0 && node.end >= span.start + span.length) { |
| 191 | + return node; |
| 192 | + } |
| 193 | + node = node.parent; |
| 194 | + } |
| 195 | + return undefined; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Finds an ancestor of the input node that is a valid type for extraction, skipping subexpressions. |
| 200 | + */ |
| 201 | + function getValidParentNodeOfEmptySpan(node: Node): ValidExpressionOrStatement | undefined { |
| 202 | + while (node.parent) { |
| 203 | + if (isValidExpressionOrStatement(node) && !isValidExpressionOrStatement(node.parent)) { |
| 204 | + return node; |
| 205 | + } |
| 206 | + node = node.parent; |
| 207 | + } |
| 208 | + return undefined; |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Gets an expression of valid extraction type from a valid statement or expression. |
| 213 | + */ |
| 214 | + function getExpression(node: ValidExpressionOrStatement): ValidExpression | undefined { |
| 215 | + if (isValidExpression(node)) { |
| 216 | + return node; |
| 217 | + } |
| 218 | + if (isVariableStatement(node)) { |
| 219 | + const variable = getSingleVariableOfVariableStatement(node); |
| 220 | + const initializer = variable?.initializer; |
| 221 | + return initializer && isValidExpression(initializer) ? initializer : undefined; |
| 222 | + } |
| 223 | + return node.expression && isValidExpression(node.expression) ? node.expression : undefined; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Gets a property access expression which may be nested inside of a binary expression. The final |
| 228 | + * expression in an && chain will occur as the right child of the parent binary expression, unless |
| 229 | + * it is followed by a different binary operator. |
| 230 | + * @param node the right child of a binary expression or a call expression. |
| 231 | + */ |
| 232 | + function getFinalExpressionInChain(node: Expression): CallExpression | PropertyAccessExpression | undefined { |
| 233 | + // foo && |foo.bar === 1|; - here the right child of the && binary expression is another binary expression. |
| 234 | + // the rightmost member of the && chain should be the leftmost child of that expression. |
| 235 | + node = skipParentheses(node); |
| 236 | + if (isBinaryExpression(node)) { |
| 237 | + return getFinalExpressionInChain(node.left); |
| 238 | + } |
| 239 | + // foo && |foo.bar()()| - nested calls are treated like further accesses. |
| 240 | + else if ((isPropertyAccessExpression(node) || isCallExpression(node)) && !isOptionalChain(node)) { |
| 241 | + return node; |
| 242 | + } |
| 243 | + return undefined; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Creates an access chain from toConvert with '?.' accesses at expressions appearing in occurrences. |
| 248 | + */ |
| 249 | + function convertOccurrences(checker: TypeChecker, toConvert: Expression, occurrences: (PropertyAccessExpression | Identifier)[]): Expression { |
| 250 | + if (isPropertyAccessExpression(toConvert) || isCallExpression(toConvert)) { |
| 251 | + const chain = convertOccurrences(checker, toConvert.expression, occurrences); |
| 252 | + const lastOccurrence = occurrences.length > 0 ? occurrences[occurrences.length - 1] : undefined; |
| 253 | + const isOccurrence = lastOccurrence?.getText() === toConvert.expression.getText(); |
| 254 | + if (isOccurrence) occurrences.pop(); |
| 255 | + if (isCallExpression(toConvert)) { |
| 256 | + return isOccurrence ? |
| 257 | + factory.createCallChain(chain, factory.createToken(SyntaxKind.QuestionDotToken), toConvert.typeArguments, toConvert.arguments) : |
| 258 | + factory.createCallChain(chain, toConvert.questionDotToken, toConvert.typeArguments, toConvert.arguments); |
| 259 | + } |
| 260 | + else if (isPropertyAccessExpression(toConvert)) { |
| 261 | + return isOccurrence ? |
| 262 | + factory.createPropertyAccessChain(chain, factory.createToken(SyntaxKind.QuestionDotToken), toConvert.name) : |
| 263 | + factory.createPropertyAccessChain(chain, toConvert.questionDotToken, toConvert.name); |
| 264 | + } |
| 265 | + } |
| 266 | + return toConvert; |
| 267 | + } |
| 268 | + |
| 269 | + function doChange(sourceFile: SourceFile, checker: TypeChecker, changes: textChanges.ChangeTracker, info: Info, _actionName: string): void { |
| 270 | + const { finalExpression, occurrences, expression } = info; |
| 271 | + const firstOccurrence = occurrences[occurrences.length - 1]; |
| 272 | + const convertedChain = convertOccurrences(checker, finalExpression, occurrences); |
| 273 | + if (convertedChain && (isPropertyAccessExpression(convertedChain) || isCallExpression(convertedChain))) { |
| 274 | + if (isBinaryExpression(expression)) { |
| 275 | + changes.replaceNodeRange(sourceFile, firstOccurrence, finalExpression, convertedChain); |
| 276 | + } |
| 277 | + else if (isConditionalExpression(expression)) { |
| 278 | + changes.replaceNode(sourceFile, expression, |
| 279 | + factory.createBinaryExpression(convertedChain, factory.createToken(SyntaxKind.QuestionQuestionToken), expression.whenFalse) |
| 280 | + ); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments