-
Notifications
You must be signed in to change notification settings - Fork 3
/
inferTypeSpec.ts
152 lines (114 loc) · 7.11 KB
/
inferTypeSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as ts from 'typescript';
import { compileSource, findChild, getKindName, getTypeStringFor, hasDeclaredType } from "../src";
describe('type inference', () => {
it('getTypeStringFor', () => {
const code1 = `
const a=1
function f(): {a: number, b: string[]} {
return {a:1, b: []}
}
function k(){return []}
var h :Array<String> = ['asd']
const b = a+2
const c = 1+4
const d = f()
const typedConst: Array<Date> = []
class CCC{ /* move-class /home/sg */
}
const numberVariable = 1
const stringVariable = '1'
`
const { program, fileName, tsconfigPath } = compileSource(code1)
const sourceFile = program.getSourceFile(fileName)
if (!sourceFile) {
return fail()
}
let a = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'a')
if (!a) {
return fail()
}
const at = program.getTypeChecker().getTypeAtLocation(a)
expect(at.symbol).toBeUndefined()
expect(hasDeclaredType(a, program)).toBe(false)
getKindName(a.kind)
let typedConst = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'typedConst')
if (!typedConst) {
return fail()
}
const typedConstType = program.getTypeChecker().getTypeAtLocation(typedConst)
expect(typedConstType.symbol.escapedName.toString()).toBe('Array')
let c = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'c')
if (!c) {
return fail()
}
expect(getTypeStringFor(c, program)).toBe('number')
let b = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'b')
if (!b) {
return fail()
}
expect(getTypeStringFor(c, program)).toBe('number')
let d = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'd')
if (!d) {
return fail()
}
expect(getTypeStringFor(d, program).replace(/\s+/g, '')).toBe('{a:number;b:string[];}')
let f = findChild(sourceFile, c => c.kind == ts.SyntaxKind.FunctionDeclaration && ((c as ts.FunctionDeclaration).name as ts.Identifier).escapedText == 'f')
if (!f) {
return fail()
}
expect(getTypeStringFor(f, program).replace(/\s+/g, '')).toBe('()=>{a:number;b:string[];}')
let h = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'h')
if (!h) {
return fail()
}
expect(hasDeclaredType(h, program)).toBe(true)
let k = findChild(sourceFile, c => c.kind == ts.SyntaxKind.FunctionDeclaration && ((c as ts.FunctionDeclaration).name as ts.Identifier).escapedText == 'k') as ts.FunctionDeclaration
if (!k) {
return fail()
}
let ccc = findChild(sourceFile, c => c.kind == ts.SyntaxKind.ClassDeclaration && ((c as ts.ClassDeclaration).name as ts.Identifier).escapedText == 'CCC')
if (!ccc) {
return fail()
}
let numberVariable = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'numberVariable')
if (!numberVariable) {
return fail()
}
expect(hasDeclaredType(numberVariable, program)).toBe(false)
expect(getTypeStringFor(numberVariable, program).replace(/\s+/g, '')).toBe('number')
let stringVariable = findChild(sourceFile, c => c.kind == ts.SyntaxKind.VariableDeclaration && ((c as ts.VariableDeclaration).name as ts.Identifier).escapedText == 'stringVariable')
if (!stringVariable) {
return fail()
}
expect(hasDeclaredType(stringVariable, program)).toBe(false)
expect(getTypeStringFor(stringVariable, program).replace(/\s+/g, '')).toBe('string')
})
})
// const TypeFormatFlags = ts.TypeFormatFlags
// const NodeBuilderFlags= ts.NodeBuilderFlags
// let flags = 0|TypeFormatFlags.UseAliasDefinedOutsideCurrentScope | TypeFormatFlags.MultilineObjectLiterals | TypeFormatFlags.WriteTypeArgumentsOfSignature | TypeFormatFlags.OmitParameterModifiers;
// const type = getTypeFor(f, program)
// console.log(program.getTypeChecker().typeToString(type, f, flags))
// // console.log(program.getTypeChecker().typeToString(type, f, 0|TypeFormatFlags.WriteTypeArgumentsOfSignature))
// // console.log(program.getTypeChecker().typeToString(type, f, 0|ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType))
// // console.log(program.getTypeChecker().typeToString(type, f, NodeBuilderFlags.MultilineObjectLiterals | TypeFormatFlags.WriteClassExpressionAsTypeLiteral | NodeBuilderFlags.UseTypeOfFunction | NodeBuilderFlags.UseStructuralFallback | NodeBuilderFlags.AllowEmptyTuple))
// console.log(program.getTypeChecker().typeToString(type,undefined, TypeFormatFlags.NoTruncation | TypeFormatFlags.WriteArrayAsGenericType | TypeFormatFlags.UseStructuralFallback | TypeFormatFlags.WriteTypeArgumentsOfSignature |
// TypeFormatFlags. UseFullyQualifiedType | TypeFormatFlags.SuppressAnyReturnType | TypeFormatFlags.MultilineObjectLiterals | TypeFormatFlags.WriteClassExpressionAsTypeLiteral |
// TypeFormatFlags.OmitParameterModifiers | TypeFormatFlags.UseAliasDefinedOutsideCurrentScope | TypeFormatFlags.AllowUniqueESSymbolType | TypeFormatFlags.InTypeAlias,))
// console.log(program.getTypeChecker().typeToString(type, f, 0))
// console.log(program.getTypeChecker().typeToString(type, f, -1|TypeFormatFlags.WriteArrowStyleSignature))
// console.log(program.getTypeChecker().typeToString(type, f, -1&TypeFormatFlags.WriteArrowStyleSignature))
// // console.log(program.getTypeChecker().typeToString(type, f, TypeFormatFlags.OmitParameterModifiers&TypeFormatFlags.WriteArrowStyleSignature))
// // console.log(program.getTypeChecker().typeToString(type, f, 0|ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType)) console.log(program.getTypeChecker().typeToString(type, f, 0|ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType)) console.log(program.getTypeChecker().typeToString(type, f, 0|ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType))
// Object.keys(typeFormatFlagsToMap()).forEach(k=>{
// console.log(program.getTypeChecker().typeToString(type, f, typeFormatFlagsToMap()[k]))
// // const v = syntaxKindToMap()[k]
// // const type = getTypeFor(f, program)
// // console.log(k, '\t\t\t\t\t' , program.getTypeChecker().typeToString(type, f, v))
// })
// // debugger;
// // program.getTypeChecker().typet
// // const s = ts.createMethodSignature(k.typeParameters, k.parameters, k.type, k.name, k.questionToken)
// // const ss = program.getTypeChecker().signatureToSignatureDeclaration(getTypeFor(k, program).getConstructSignatures()[0], ts.SyntaxKind.FunctionDeclaration)
// // expect(getTypeStringFor(k, program).replace(/\s+/g, '')).toBe('()=>string[]')
// // const changes = getFileTextChanges(k)