Skip to content

Commit 5a72da7

Browse files
author
Benjamin Lichtman
committed
Only perform async refactor if it won't delete code
1 parent ddedfd4 commit 5a72da7

File tree

6 files changed

+176
-125
lines changed

6 files changed

+176
-125
lines changed

src/services/codefixes/convertToAsyncFunction.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
namespace ts.codefix {
33
const fixId = "convertToAsyncFunction";
44
const errorCodes = [Diagnostics.This_may_be_converted_to_an_async_function.code];
5+
let codeActionSucceeded = true;
56
registerCodeFix({
67
errorCodes,
78
getCodeActions(context: CodeFixContext) {
9+
codeActionSucceeded = true;
810
const changes = textChanges.ChangeTracker.with(context, (t) => convertToAsyncFunction(t, context.sourceFile, context.span.start, context.program.getTypeChecker(), context));
9-
return [createCodeFixAction(fixId, changes, Diagnostics.Convert_to_async_function, fixId, Diagnostics.Convert_all_to_async_functions)];
11+
return codeActionSucceeded ? [createCodeFixAction(fixId, changes, Diagnostics.Convert_to_async_function, fixId, Diagnostics.Convert_all_to_async_functions)] : [];
1012
},
1113
fixIds: [fixId],
1214
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, err) => convertToAsyncFunction(changes, err.file, err.start, context.program.getTypeChecker(), context)),
@@ -387,6 +389,10 @@ namespace ts.codefix {
387389
const hasArgName = argName && argName.identifier.text.length > 0;
388390
const shouldReturn = transformer.setOfExpressionsToReturn.get(getNodeId(parent).toString());
389391
switch (func.kind) {
392+
case SyntaxKind.NullKeyword:
393+
case SyntaxKind.UndefinedKeyword:
394+
// do not produce a transformed statement for a null or undefined argument
395+
break;
390396
case SyntaxKind.Identifier:
391397
if (!hasArgName) break;
392398

@@ -443,6 +449,9 @@ namespace ts.codefix {
443449
return createNodeArray([createReturn(getSynthesizedDeepClone(funcBody) as Expression)]);
444450
}
445451
}
452+
default:
453+
// We've found a transformation body we don't know how to handle, so the refactoring should no-op to avoid deleting code.
454+
codeActionSucceeded = false;
446455
break;
447456
}
448457
return createNodeArray([]);

0 commit comments

Comments
 (0)