fix(47415): don't show addConvertToUnknownForNonOverlappingTypes QF in JS (#47424)

This commit is contained in:
Oleksandr T 2022-01-13 21:17:36 +02:00 committed by GitHub
parent 82377825d7
commit 42ea5ec3d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -5,19 +5,29 @@ namespace ts.codefix {
registerCodeFix({
errorCodes,
getCodeActions: function getCodeActionsToAddConvertToUnknownForNonOverlappingTypes(context) {
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
const assertion = getAssertion(context.sourceFile, context.span.start);
if (assertion === undefined) return undefined;
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, assertion));
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)),
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const assertion = getAssertion(diag.file, diag.start);
if (assertion) {
makeChange(changes, diag.file, assertion);
}
}),
});
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, pos: number) {
const token = getTokenAtPosition(sourceFile, pos);
const assertion = Debug.checkDefined(findAncestor(token, (n): n is AsExpression | TypeAssertion => isAsExpression(n) || isTypeAssertionExpression(n)), "Expected to find an assertion expression");
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, assertion: AsExpression | TypeAssertion) {
const replacement = isAsExpression(assertion)
? factory.createAsExpression(assertion.expression, factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword))
: factory.createTypeAssertion(factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword), assertion.expression);
changeTracker.replaceNode(sourceFile, assertion.expression, replacement);
}
function getAssertion(sourceFile: SourceFile, pos: number): AsExpression | TypeAssertion | undefined {
if (isInJSFile(sourceFile)) return undefined;
return findAncestor(getTokenAtPosition(sourceFile, pos), (n): n is AsExpression | TypeAssertion => isAsExpression(n) || isTypeAssertionExpression(n));
}
}

View File

@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />
// @checkJs: true
// @allowJs: true
// @filename: a.js
////let x = /** @type {string} */ (100);
verify.not.codeFixAvailable(ts.Diagnostics.Add_unknown_conversion_for_non_overlapping_types.message);