|
2 | 2 | namespace ts.codefix {
|
3 | 3 | const fixId = "convertToAsyncFunction";
|
4 | 4 | const errorCodes = [Diagnostics.This_may_be_converted_to_an_async_function.code];
|
| 5 | + let codeActionSucceeded = true; |
5 | 6 | registerCodeFix({
|
6 | 7 | errorCodes,
|
7 | 8 | getCodeActions(context: CodeFixContext) {
|
| 9 | + codeActionSucceeded = true; |
8 | 10 | 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)] : []; |
10 | 12 | },
|
11 | 13 | fixIds: [fixId],
|
12 | 14 | getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, err) => convertToAsyncFunction(changes, err.file, err.start, context.program.getTypeChecker(), context)),
|
@@ -387,6 +389,10 @@ namespace ts.codefix {
|
387 | 389 | const hasArgName = argName && argName.identifier.text.length > 0;
|
388 | 390 | const shouldReturn = transformer.setOfExpressionsToReturn.get(getNodeId(parent).toString());
|
389 | 391 | 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; |
390 | 396 | case SyntaxKind.Identifier:
|
391 | 397 | if (!hasArgName) break;
|
392 | 398 |
|
@@ -443,6 +449,9 @@ namespace ts.codefix {
|
443 | 449 | return createNodeArray([createReturn(getSynthesizedDeepClone(funcBody) as Expression)]);
|
444 | 450 | }
|
445 | 451 | }
|
| 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; |
446 | 455 | break;
|
447 | 456 | }
|
448 | 457 | return createNodeArray([]);
|
|
0 commit comments