Skip to content

Commit ecb88f5

Browse files
committed
Fix comments on pull request
1 parent 2dc3c8e commit ecb88f5

10 files changed

+29
-48
lines changed

src/services/codefixes/addConvertToUnknownForNonOverlappingTypes.ts

+5-20
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,10 @@ namespace ts.codefix {
1414

1515
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
1616
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);
3722
}
3823
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|0 as string|]
3+
////0 as string
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `0 as unknown as string`
7+
newFileContent: `0 as unknown as string`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|0 * (4 + 3) / 100 as string|]
3+
////0 * (4 + 3) / 100 as string
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `0 * (4 + 3) / 100 as unknown as string`
7+
newFileContent: `0 * (4 + 3) / 100 as unknown as string`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|["words"] as string|]
3+
////["words"] as string
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `["words"] as unknown as string`
7+
newFileContent: `["words"] as unknown as string`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|"words" as object|]
3+
////"words" as object
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `"words" as unknown as object`
7+
newFileContent: `"words" as unknown as object`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|<string>0|]
3+
////<string>0
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `<string><unknown>0`
7+
newFileContent: `<string><unknown>0`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|<string>0 * (4 + 3) / 100|]
3+
////<string>0 * (4 + 3) / 100
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `<string><unknown>0 * (4 + 3) / 100`
7+
newFileContent: `<string><unknown>0 * (4 + 3) / 100`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|<string>["words"]|]
3+
////<string>["words"]
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `<string><unknown>["words"]`
7+
newFileContent: `<string><unknown>["words"]`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// <reference path='fourslash.ts' />
22

3-
////[|<object>"words"|]
3+
////<object>"words"
44

55
verify.codeFix({
66
description: "Add 'unknown' conversion for non-overlapping types",
7-
newRangeContent: `<object><unknown>"words"`
7+
newFileContent: `<object><unknown>"words"`
88
});
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
/// <reference path='fourslash.ts' />
22

3-
////class C {
4-
//// const s1 = 1 as string;
5-
//// const o1 = s + " word" as object;
3+
////const s1 = 1 as string;
4+
////const o1 = s + " word" as object;
65
////
7-
//// const s2 = <string>2;
8-
//// const o2 = <object>s2;
9-
////}
6+
////const s2 = <string>2;
7+
////const o2 = <object>s2;
108

119
verify.codeFixAll({
1210
fixId: "addConvertToUnknownForNonOverlappingTypes",
1311
fixAllDescription: "Add 'unknown' to all conversions of non-overlapping types",
1412
newFileContent:
15-
`class C {
16-
const s1 = 1 as unknown as string;
17-
const o1 = s + " word" as unknown as object;
13+
`const s1 = 1 as unknown as string;
14+
const o1 = s + " word" as unknown as object;
1815
19-
const s2 = <string><unknown>2;
20-
const o2 = <object>s2;
21-
}`
16+
const s2 = <string><unknown>2;
17+
const o2 = <object><unknown>s2;`
2218
});

0 commit comments

Comments
 (0)