Add codefix for 'convert to unknown' diagnostic

Codefix for diagnostic error 2352: "Conversion of type '{0}' to type '{1}' may be
a mistake because neither type sufficiently overlaps with the other. If this was
intentional, convert the expression to 'unknown' first."

Add codefix for both AsExpressions and TypeAssertions
This commit is contained in:
Ryan Clarke
2018-11-01 06:11:55 -04:00
parent c780186a92
commit 4598943ed3
12 changed files with 137 additions and 0 deletions

View File

@@ -4724,5 +4724,13 @@
"Generate types for all packages without types": {
"category": "Message",
"code": 95068
},
"Add 'unknown' conversion for non-overlapping types": {
"category": "Message",
"code": 95069
},
"Add 'unknown' to all conversions of non-overlapping types": {
"category": "Message",
"code": 95070
}
}

View File

@@ -0,0 +1,42 @@
/* @internal */
namespace ts.codefix {
const fixId = "addConvertToUnknownForNonOverlappingTypes";
const errorCodes = [Diagnostics.Conversion_of_type_0_to_type_1_may_be_a_mistake_because_neither_type_sufficiently_overlaps_with_the_other_If_this_was_intentional_convert_the_expression_to_unknown_first.code];
registerCodeFix({
errorCodes,
getCodeActions: (context) => {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
return [createCodeFixAction(fixId, changes, Diagnostics.Add_unknown_conversion_for_non_overlapping_types, fixId, Diagnostics.Add_unknown_to_all_conversions_of_non_overlapping_types)];
},
fixIds: [fixId],
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => makeChange(changes, diag.file, diag.start)),
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos);
const asExpression = findAncestor<AsExpression>(token, isAsExpression)!;
if (!!asExpression) {
Debug.assert(!!asExpression, "Expected position to be owned by an as-expression.");
const nodeBeingCast = asExpression.getChildAt(0);
const expressionBeingCast = findAncestor<Expression>(nodeBeingCast, isExpression)!;
Debug.assert(!!expressionBeingCast, "Expected position to be owned by an expression.");
const replacement = createAsExpression(expressionBeingCast, createKeywordTypeNode(SyntaxKind.UnknownKeyword));
changeTracker.replaceNode(sourceFile, expressionBeingCast, replacement);
}
const typeAssertion = findAncestor<TypeAssertion>(token, isTypeAssertion)!;
if (!!typeAssertion) {
Debug.assert(!!typeAssertion, "Expected position to be owned by a type assertion.");
const nodeBeingCast = typeAssertion.getLastToken();
const expressionBeingCast = findAncestor<Expression>(nodeBeingCast, isExpression)!;
Debug.assert(!!expressionBeingCast, "Expected position to be owned by an expression.");
const replacement = createTypeAssertion(createKeywordTypeNode(SyntaxKind.UnknownKeyword), expressionBeingCast);
changeTracker.replaceNode(sourceFile, expressionBeingCast, replacement);
}
}
}

View File

@@ -43,6 +43,7 @@
"textChanges.ts",
"codeFixProvider.ts",
"refactorProvider.ts",
"codefixes/addConvertToUnknownForNonOverlappingTypes.ts",
"codefixes/addMissingInvocationForDecorator.ts",
"codefixes/annotateWithTypeFromJSDoc.ts",
"codefixes/convertFunctionToEs6Class.ts",