Skip to content

Commit 7371ec1

Browse files
committed
Add elaboration & quickfix for async-able arrow function
1 parent 38eccba commit 7371ec1

File tree

8 files changed

+179
-0
lines changed

8 files changed

+179
-0
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13929,6 +13929,17 @@ namespace ts {
1392913929
Diagnostics.The_expected_type_comes_from_the_return_type_of_this_signature,
1393013930
));
1393113931
}
13932+
if ((getFunctionFlags(node) & FunctionFlags.Async) === 0
13933+
// exclude cases where source itself is promisy - this way we don't make a suggestion when relating
13934+
// an IPromise and a Promise that are slightly different
13935+
&& !getTypeOfPropertyOfType(sourceReturn, "then" as __String)
13936+
&& checkTypeRelatedTo(createPromiseType(sourceReturn), targetReturn, relation, /*errorNode*/ undefined)
13937+
) {
13938+
addRelatedInfo(resultObj.errors[resultObj.errors.length - 1], createDiagnosticForNode(
13939+
node,
13940+
Diagnostics.Did_you_mean_to_mark_this_function_as_async
13941+
));
13942+
}
1393213943
return true;
1393313944
}
1393413945
}

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5436,6 +5436,14 @@
54365436
"category": "Message",
54375437
"code": 95099
54385438
},
5439+
"Add 'async'.": {
5440+
"category": "Message",
5441+
"code": 95100
5442+
},
5443+
"Fix all functions possibly missing 'async'.": {
5444+
"category": "Message",
5445+
"code": 95101
5446+
},
54395447

54405448
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
54415449
"category": "Error",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
type ContextualTrackChangesFunction = (cb: (changeTracker: textChanges.ChangeTracker) => void) => FileTextChanges[];
4+
const fixId = "addMissingAsync";
5+
const errorCodes = [
6+
Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code,
7+
Diagnostics.Type_0_is_not_assignable_to_type_1.code,
8+
Diagnostics.Type_0_is_not_comparable_to_type_1.code
9+
];
10+
11+
registerCodeFix({
12+
fixIds: [fixId],
13+
errorCodes,
14+
getCodeActions: context => {
15+
const { sourceFile, errorCode, cancellationToken, program, span } = context;
16+
const diagnostic = find(program.getDiagnosticsProducingTypeChecker().getDiagnostics(sourceFile, cancellationToken), getIsMatchingAsyncError(span, errorCode));
17+
const directSpan = diagnostic && diagnostic.relatedInformation && find(diagnostic.relatedInformation, r => r.code === Diagnostics.Did_you_mean_to_mark_this_function_as_async.code) as TextSpan | undefined;
18+
19+
const decl = getFixableErrorSpanDeclaration(sourceFile, directSpan);
20+
if (!decl) {
21+
return;
22+
}
23+
24+
const trackChanges: ContextualTrackChangesFunction = cb => textChanges.ChangeTracker.with(context, cb);
25+
return [getFix(context, decl, trackChanges)];
26+
},
27+
getAllCodeActions: context => {
28+
const { sourceFile } = context;
29+
const fixedDeclarations = createMap<true>();
30+
return codeFixAll(context, errorCodes, (t, diagnostic) => {
31+
const span = diagnostic.relatedInformation && find(diagnostic.relatedInformation, r => r.code === Diagnostics.Did_you_mean_to_mark_this_function_as_async.code) as TextSpan | undefined;
32+
const decl = getFixableErrorSpanDeclaration(sourceFile, span);
33+
if (!decl) {
34+
return;
35+
}
36+
const trackChanges: ContextualTrackChangesFunction = cb => (cb(t), []);
37+
return getFix(context, decl, trackChanges, fixedDeclarations);
38+
});
39+
},
40+
});
41+
42+
type FixableDeclaration = ArrowFunction | FunctionDeclaration | FunctionExpression | MethodDeclaration;
43+
function getFix(context: CodeFixContext | CodeFixAllContext, decl: FixableDeclaration, trackChanges: ContextualTrackChangesFunction, fixedDeclarations?: Map<true>) {
44+
const changes = trackChanges(t => makeChange(t, context.sourceFile, decl, fixedDeclarations));
45+
return createCodeFixAction(fixId, changes, Diagnostics.Add_async, fixId, Diagnostics.Fix_all_functions_possibly_missing_async);
46+
}
47+
48+
function makeChange(changeTracker: textChanges.ChangeTracker, sourceFile: SourceFile, insertionSite: FixableDeclaration, fixedDeclarations?: Map<true>) {
49+
if (fixedDeclarations) {
50+
if (fixedDeclarations.has(getNodeId(insertionSite).toString())) {
51+
return;
52+
}
53+
}
54+
fixedDeclarations?.set(getNodeId(insertionSite).toString(), true);
55+
const cloneWithModifier = getSynthesizedDeepClone(insertionSite, /*includeTrivia*/ true);
56+
cloneWithModifier.modifiers = createNodeArray(createModifiersFromModifierFlags(getModifierFlags(insertionSite) | ModifierFlags.Async));
57+
cloneWithModifier.modifierFlagsCache = 0;
58+
changeTracker.replaceNode(
59+
sourceFile,
60+
insertionSite,
61+
cloneWithModifier);
62+
}
63+
64+
function getFixableErrorSpanDeclaration(sourceFile: SourceFile, span: TextSpan | undefined): FixableDeclaration | undefined {
65+
if (!span) return undefined;
66+
const token = getTokenAtPosition(sourceFile, span.start);
67+
// Checker has already done work to determine that async might be possible, and has attached
68+
// related info to the node, so start by finding the signature that exactly matches up
69+
// with the diagnostic range.
70+
const decl = findAncestor(token, node => {
71+
if (node.getStart(sourceFile) < span.start || node.getEnd() > textSpanEnd(span)) {
72+
return "quit";
73+
}
74+
return (isArrowFunction(node) || isMethodDeclaration(node) || isFunctionExpression(node) || isFunctionDeclaration(node)) && textSpansEqual(span, createTextSpanFromNode(node, sourceFile));
75+
}) as FixableDeclaration | undefined;
76+
77+
return decl;
78+
}
79+
80+
function getIsMatchingAsyncError(span: TextSpan, errorCode: number) {
81+
return ({ start, length, relatedInformation, code }: Diagnostic) =>
82+
isNumber(start) && isNumber(length) && textSpansEqual({ start, length }, span) &&
83+
code === errorCode &&
84+
!!relatedInformation &&
85+
some(relatedInformation, related => related.code === Diagnostics.Did_you_mean_to_mark_this_function_as_async.code);
86+
}
87+
}

src/services/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"refactorProvider.ts",
4949
"codefixes/addConvertToUnknownForNonOverlappingTypes.ts",
5050
"codefixes/addEmptyExportDeclaration.ts",
51+
"codefixes/addMissingAsync.ts",
5152
"codefixes/addMissingAwait.ts",
5253
"codefixes/addMissingConst.ts",
5354
"codefixes/addMissingDeclareProperty.ts",

tests/baselines/reference/errorOnUnionVsObjectShouldDeeplyDisambiguate.errors.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,51 @@ tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts(27,16): err
3737
~~~~~~~
3838
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
3939
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:3:8: The expected type comes from the return type of this signature.
40+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:18:10: Did you mean to mark this function as 'async'?
4041
c: () => "hello",
4142
~~~~~~~
4243
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
4344
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:4:8: The expected type comes from the return type of this signature.
45+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:19:10: Did you mean to mark this function as 'async'?
4446
d: () => "hello",
4547
~~~~~~~
4648
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
4749
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:5:8: The expected type comes from the return type of this signature.
50+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:20:10: Did you mean to mark this function as 'async'?
4851
e: () => "hello",
4952
~~~~~~~
5053
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5154
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:6:8: The expected type comes from the return type of this signature.
55+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:21:10: Did you mean to mark this function as 'async'?
5256
f: () => "hello",
5357
~~~~~~~
5458
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5559
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:7:8: The expected type comes from the return type of this signature.
60+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:22:10: Did you mean to mark this function as 'async'?
5661
g: () => "hello",
5762
~~~~~~~
5863
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5964
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:8:8: The expected type comes from the return type of this signature.
65+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:23:10: Did you mean to mark this function as 'async'?
6066
h: () => "hello",
6167
~~~~~~~
6268
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
6369
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:9:8: The expected type comes from the return type of this signature.
70+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:24:10: Did you mean to mark this function as 'async'?
6471
i: () => "hello",
6572
~~~~~~~
6673
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
6774
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:10:8: The expected type comes from the return type of this signature.
75+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:25:10: Did you mean to mark this function as 'async'?
6876
j: () => "hello",
6977
~~~~~~~
7078
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
7179
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:11:8: The expected type comes from the return type of this signature.
80+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:26:10: Did you mean to mark this function as 'async'?
7281
k: () => 123
7382
~~~
7483
!!! error TS2322: Type 'number' is not assignable to type 'Promise<number>'.
7584
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:12:8: The expected type comes from the return type of this signature.
85+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate.ts:27:10: Did you mean to mark this function as 'async'?
7686
}
7787
}

tests/baselines/reference/errorOnUnionVsObjectShouldDeeplyDisambiguate2.errors.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,51 @@ tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts(27,14): er
3737
~~~~~~~
3838
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
3939
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:3:6: The expected type comes from the return type of this signature.
40+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:18:8: Did you mean to mark this function as 'async'?
4041
c: () => "hello",
4142
~~~~~~~
4243
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
4344
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:4:6: The expected type comes from the return type of this signature.
45+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:19:8: Did you mean to mark this function as 'async'?
4446
d: () => "hello",
4547
~~~~~~~
4648
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
4749
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:5:6: The expected type comes from the return type of this signature.
50+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:20:8: Did you mean to mark this function as 'async'?
4851
e: () => "hello",
4952
~~~~~~~
5053
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5154
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:6:6: The expected type comes from the return type of this signature.
55+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:21:8: Did you mean to mark this function as 'async'?
5256
f: () => "hello",
5357
~~~~~~~
5458
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5559
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:7:6: The expected type comes from the return type of this signature.
60+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:22:8: Did you mean to mark this function as 'async'?
5661
g: () => "hello",
5762
~~~~~~~
5863
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
5964
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:8:6: The expected type comes from the return type of this signature.
65+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:23:8: Did you mean to mark this function as 'async'?
6066
h: () => "hello",
6167
~~~~~~~
6268
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
6369
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:9:6: The expected type comes from the return type of this signature.
70+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:24:8: Did you mean to mark this function as 'async'?
6471
i: () => "hello",
6572
~~~~~~~
6673
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
6774
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:10:6: The expected type comes from the return type of this signature.
75+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:25:8: Did you mean to mark this function as 'async'?
6876
j: () => "hello",
6977
~~~~~~~
7078
!!! error TS2322: Type 'string' is not assignable to type 'Promise<string>'.
7179
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:11:6: The expected type comes from the return type of this signature.
80+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:26:8: Did you mean to mark this function as 'async'?
7281
k: () => 123
7382
~~~
7483
!!! error TS2322: Type 'number' is not assignable to type 'Promise<number>'.
7584
!!! related TS6502 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:12:6: The expected type comes from the return type of this signature.
85+
!!! related TS1356 tests/cases/compiler/errorOnUnionVsObjectShouldDeeplyDisambiguate2.ts:27:8: Did you mean to mark this function as 'async'?
7686
}
7787
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
////interface Stuff {
3+
//// b: () => Promise<string>;
4+
////}
5+
////
6+
////function foo(): Stuff | Date {
7+
//// return {
8+
//// b: () => "hello",
9+
//// }
10+
////}
11+
12+
verify.codeFix({
13+
description: ts.Diagnostics.Add_async.message,
14+
index: 0,
15+
newFileContent:
16+
`interface Stuff {
17+
b: () => Promise<string>;
18+
}
19+
20+
function foo(): Stuff | Date {
21+
return {
22+
b: async () => "hello",
23+
}
24+
}`
25+
});
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
////interface Stuff {
3+
//// b: () => Promise<string>;
4+
////}
5+
////
6+
////function foo(): Stuff | Date {
7+
//// return {
8+
//// b: _ => "hello",
9+
//// }
10+
////}
11+
12+
verify.codeFix({
13+
description: ts.Diagnostics.Add_async.message,
14+
index: 0,
15+
newFileContent:
16+
`interface Stuff {
17+
b: () => Promise<string>;
18+
}
19+
20+
function foo(): Stuff | Date {
21+
return {
22+
b: async (_) => "hello",
23+
}
24+
}`
25+
});
26+

0 commit comments

Comments
 (0)