Skip to content

Commit 330e51f

Browse files
committed
Add test + reshuffle/rename new code
1 parent 1703ae0 commit 330e51f

File tree

6 files changed

+77
-40
lines changed

6 files changed

+77
-40
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -899,59 +899,42 @@ namespace ts.codefix {
899899
if (usage.isNumberOrString) {
900900
types.push(checker.getUnionType([checker.getStringType(), checker.getNumberType()]));
901901
}
902-
903-
types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t)));
904-
types.push(...inferNamedTypesFromProperties(usage));
905-
906902
if (usage.numberIndex) {
907903
types.push(checker.createArrayType(unifyFromUsage(usage.numberIndex)));
908904
}
909-
const structural = inferStructuralType(usage);
910-
if (structural) {
911-
types.push(structural);
905+
if (usage.properties && usage.properties.size
906+
|| usage.calls && usage.calls.length
907+
|| usage.constructs && usage.constructs.length
908+
|| usage.stringIndex) {
909+
types.push(inferStructuralType(usage));
912910
}
911+
912+
types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t)));
913+
types.push(...inferNamedTypesFromProperties(usage));
914+
913915
return types;
914916
}
915917

916918
function inferStructuralType(usage: Usage) {
917-
if (usage.properties && usage.properties.size
918-
|| usage.calls && usage.calls.length
919-
|| usage.constructs && usage.constructs.length
920-
|| usage.stringIndex) {
921-
const members = createUnderscoreEscapedMap<Symbol>();
922-
const callSignatures: Signature[] = [];
923-
const constructSignatures: Signature[] = [];
924-
let stringIndexInfo: IndexInfo | undefined;
925-
926-
if (usage.properties) {
927-
usage.properties.forEach((u, name) => {
928-
const symbol = checker.createSymbol(SymbolFlags.Property, name);
929-
symbol.type = unifyFromUsage(u);
930-
members.set(name, symbol);
931-
});
932-
}
933-
934-
if (usage.calls) {
935-
callSignatures.push(getSignatureFromCalls(usage.calls));
936-
}
937-
938-
if (usage.constructs) {
939-
constructSignatures.push(getSignatureFromCalls(usage.constructs));
940-
}
941-
942-
if (usage.stringIndex) {
943-
stringIndexInfo = checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false);
944-
}
945-
946-
return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217
919+
const members = createUnderscoreEscapedMap<Symbol>();
920+
if (usage.properties) {
921+
usage.properties.forEach((u, name) => {
922+
const symbol = checker.createSymbol(SymbolFlags.Property, name);
923+
symbol.type = unifyFromUsage(u);
924+
members.set(name, symbol);
925+
});
947926
}
927+
const callSignatures: Signature[] = usage.calls ? [getSignatureFromCalls(usage.calls)] : [];
928+
const constructSignatures: Signature[] = usage.constructs ? [getSignatureFromCalls(usage.constructs)] : [];
929+
const stringIndexInfo = usage.stringIndex && checker.createIndexInfo(unifyFromUsage(usage.stringIndex), /*isReadonly*/ false);
930+
return checker.createAnonymousType(/*symbol*/ undefined!, members, callSignatures, constructSignatures, stringIndexInfo, /*numberIndexInfo*/ undefined); // TODO: GH#18217
948931
}
949932

950933
function inferNamedTypesFromProperties(usage: Usage): Type[] {
951934
if (!usage.properties || !usage.properties.size) return [];
952-
const matches = builtins.filter(t => allPropertiesAreAssignableToUsage(t, usage));
953-
if (0 < matches.length && matches.length < 3) {
954-
return matches.map(m => inferInstantiationFromUsage(m, usage));
935+
const types = builtins.filter(t => allPropertiesAreAssignableToUsage(t, usage));
936+
if (0 < types.length && types.length < 3) {
937+
return types.map(t => inferInstantiationFromUsage(t, usage));
955938
}
956939
return [];
957940
}
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 foo([|a, m |]) {
5+
//// return a + m
6+
//// }
7+
8+
verify.rangeAfterCodeFix("a: any, m: any", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
9+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function foo([|p, a, b, c, d, e |]) {
5+
//// var x: string = a.pop()
6+
//// b.reverse()
7+
//// var rr: boolean[] = c.reverse()
8+
//// d.some(t => t > 1); // can't infer from callbacks right now
9+
//// var y = e.concat(12); // can't infer from overloaded functions right now
10+
//// return p.push(12)
11+
//// }
12+
13+
verify.rangeAfterCodeFix("p: number[], a: string[], b: any[], c: boolean[], d: any[], e: number[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
14+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function foo([|a, m |]) {
5+
//// a = 'hi'
6+
//// m = 1
7+
//// }
8+
9+
verify.rangeAfterCodeFix("a: string, m: number", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
10+
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 foo([|p |]) {
5+
//// return p.then((x: string[]) => x[0])
6+
//// }
7+
8+
verify.rangeAfterCodeFix("p: Promise<string[]>", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
9+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: true
4+
//// function foo([|p, a, b, c, d |]) {
5+
//// var x
6+
//// p.charAt(x)
7+
//// a.charAt(0)
8+
//// b.concat('hi')
9+
//// }
10+
11+
verify.rangeAfterCodeFix("p: string, a: string, b: string | string[]", /*includeWhiteSpace*/ undefined, /*errorCode*/ undefined, /*index*/0);
12+

0 commit comments

Comments
 (0)