mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
fix(47415): don't show addConvertToUnknownForNonOverlappingTypes QF in JS (#47424)
This commit is contained in:
parent
82377825d7
commit
42ea5ec3d8
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user