Skip to content

Commit 052a3d9

Browse files
committed
Infer void from expr statement usage, not calls
This makes inferences a lot better.
1 parent ff38a1b commit 052a3d9

7 files changed

+26
-7
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ namespace ts.codefix {
504504
}
505505

506506
switch (node.parent.kind) {
507+
case SyntaxKind.ExpressionStatement:
508+
addCandidateType(usage, checker.getVoidType());
509+
break;
507510
case SyntaxKind.PostfixUnaryExpression:
508511
usage.isNumber = true;
509512
break;
@@ -871,12 +874,11 @@ namespace ts.codefix {
871874
}
872875

873876
if (usage.calls) {
874-
callSignatures.push(getSignatureFromCalls(usage.calls, checker.getVoidType()));
877+
callSignatures.push(getSignatureFromCalls(usage.calls, checker.getAnyType()));
875878
}
876879

877880
if (usage.constructs) {
878-
// TODO: fallback return should maybe be {}?
879-
constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getVoidType()));
881+
constructSignatures.push(getSignatureFromCalls(usage.constructs, checker.getAnyType()));
880882
}
881883

882884
if (usage.stringIndex) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function inferAny( [| app |] ) {
5+
//// const result = app.use('hi')
6+
//// return result
7+
//// }
8+
9+
verify.rangeAfterCodeFix("app: { use: (arg0: string) => any; }");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function inferVoid( [| app |] ) {
5+
//// app.use('hi')
6+
//// }
7+
8+
verify.rangeAfterCodeFix("app: { use: (arg0: string) => void; }");

tests/cases/fourslash/codeFixInferFromUsageCommentAfterParameter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ verify.codeFix({
1616
index: 0,
1717
newFileContent:
1818
`/**
19-
* @param {(arg0: any) => void} callback
19+
* @param {(arg0: any) => any} callback
2020
*/
2121
function coll(callback /*, name1, name2, ... */) {
2222
return callback(this);

tests/cases/fourslash/codeFixInferFromUsageJSXElement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
//// }
3131

3232

33-
verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => void; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);
33+
verify.rangeAfterCodeFix("props: { isLoading: any; update: (arg0: any) => any; }",/*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, 0);

tests/cases/fourslash/codeFixInferFromUsagePropertyAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
//// return x.y.z
1313
////}
1414

15-
verify.rangeAfterCodeFix("a: { b: { c: any; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
15+
verify.rangeAfterCodeFix("a: { b: { c: void; }; }, m: { n: () => number; }, x: { y: { z: number[]; }; }", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);

tests/cases/fourslash/codeFixInferFromUsagePropertyAccessJS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ verify.codeFix({
2121
index: 0,
2222
newFileContent:
2323
`/**
24-
* @param {{ b: { c: any; }; }} a
24+
* @param {{ b: { c: void; }; }} a
2525
* @param {{ n: () => number; }} m
2626
* @param {{ y: { z: number[]; }; }} x
2727
*/

0 commit comments

Comments
 (0)