Skip to content

Commit 2dc3c8e

Browse files
committed
Refactor convert to unknown codefix
1 parent 4598943 commit 2dc3c8e

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/services/codefixes/addConvertToUnknownForNonOverlappingTypes.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,22 @@ namespace ts.codefix {
1717

1818
const asExpression = findAncestor<AsExpression>(token, isAsExpression)!;
1919
if (!!asExpression) {
20-
Debug.assert(!!asExpression, "Expected position to be owned by an as-expression.");
20+
const nodeBeingConverted = asExpression.getChildAt(0);
21+
const expressionBeingConverted = findAncestor<Expression>(nodeBeingConverted, isExpression)!;
22+
Debug.assert(!!expressionBeingConverted, "Expected position to be owned by an expression.");
2123

22-
const nodeBeingCast = asExpression.getChildAt(0);
23-
const expressionBeingCast = findAncestor<Expression>(nodeBeingCast, isExpression)!;
24-
Debug.assert(!!expressionBeingCast, "Expected position to be owned by an expression.");
25-
26-
const replacement = createAsExpression(expressionBeingCast, createKeywordTypeNode(SyntaxKind.UnknownKeyword));
27-
changeTracker.replaceNode(sourceFile, expressionBeingCast, replacement);
24+
const replacement = createAsExpression(expressionBeingConverted, createKeywordTypeNode(SyntaxKind.UnknownKeyword));
25+
changeTracker.replaceNode(sourceFile, expressionBeingConverted, replacement);
2826
}
2927

3028
const typeAssertion = findAncestor<TypeAssertion>(token, isTypeAssertion)!;
3129
if (!!typeAssertion) {
32-
Debug.assert(!!typeAssertion, "Expected position to be owned by a type assertion.");
33-
34-
const nodeBeingCast = typeAssertion.getLastToken();
35-
const expressionBeingCast = findAncestor<Expression>(nodeBeingCast, isExpression)!;
36-
Debug.assert(!!expressionBeingCast, "Expected position to be owned by an expression.");
30+
const nodeBeingConverted = typeAssertion.getLastToken();
31+
const expressionBeingConverted = findAncestor<Expression>(nodeBeingConverted, isExpression)!;
32+
Debug.assert(!!expressionBeingConverted, "Expected position to be owned by an expression.");
3733

38-
const replacement = createTypeAssertion(createKeywordTypeNode(SyntaxKind.UnknownKeyword), expressionBeingCast);
39-
changeTracker.replaceNode(sourceFile, expressionBeingCast, replacement);
34+
const replacement = createTypeAssertion(createKeywordTypeNode(SyntaxKind.UnknownKeyword), expressionBeingConverted);
35+
changeTracker.replaceNode(sourceFile, expressionBeingConverted, replacement);
4036
}
4137
}
4238
}

0 commit comments

Comments
 (0)