@@ -14,25 +14,10 @@ namespace ts.codefix {
14
14
15
15
function makeChange ( changeTracker : textChanges . ChangeTracker , sourceFile : SourceFile , pos : number ) {
16
16
const token = getTokenAtPosition ( sourceFile , pos ) ;
17
-
18
- const asExpression = findAncestor < AsExpression > ( token , isAsExpression ) ! ;
19
- if ( ! ! asExpression ) {
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." ) ;
23
-
24
- const replacement = createAsExpression ( expressionBeingConverted , createKeywordTypeNode ( SyntaxKind . UnknownKeyword ) ) ;
25
- changeTracker . replaceNode ( sourceFile , expressionBeingConverted , replacement ) ;
26
- }
27
-
28
- const typeAssertion = findAncestor < TypeAssertion > ( token , isTypeAssertion ) ! ;
29
- if ( ! ! typeAssertion ) {
30
- const nodeBeingConverted = typeAssertion . getLastToken ( ) ;
31
- const expressionBeingConverted = findAncestor < Expression > ( nodeBeingConverted , isExpression ) ! ;
32
- Debug . assert ( ! ! expressionBeingConverted , "Expected position to be owned by an expression." ) ;
33
-
34
- const replacement = createTypeAssertion ( createKeywordTypeNode ( SyntaxKind . UnknownKeyword ) , expressionBeingConverted ) ;
35
- changeTracker . replaceNode ( sourceFile , expressionBeingConverted , replacement ) ;
36
- }
17
+ const assertion = Debug . assertDefined ( findAncestor ( token , ( n ) : n is AsExpression | TypeAssertion => isAsExpression ( n ) || isTypeAssertion ( n ) ) ) ;
18
+ const replacement = isAsExpression ( assertion )
19
+ ? createAsExpression ( assertion . expression , createKeywordTypeNode ( SyntaxKind . UnknownKeyword ) )
20
+ : createTypeAssertion ( createKeywordTypeNode ( SyntaxKind . UnknownKeyword ) , assertion . expression ) ;
21
+ changeTracker . replaceNode ( sourceFile , assertion . expression , replacement ) ;
37
22
}
38
23
}
0 commit comments