Skip to content

Commit dd0254a

Browse files
authored
Enable type baselines (#606)
1 parent 710ddfc commit dd0254a

File tree

19,292 files changed

+1488846
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

19,292 files changed

+1488846
-15
lines changed

internal/checker/printer.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ func (p *Printer) printTypeEx(t *Type, precedence ast.TypePrecedence) {
133133
}
134134

135135
func (p *Printer) printType(t *Type) {
136+
if p.sb.Len() > 1_000_000 {
137+
p.print("...")
138+
return
139+
}
140+
136141
if t.alias != nil && (p.flags&TypeFormatFlagsInTypeAlias == 0 || p.depth > 0) {
137142
p.printName(t.alias.symbol)
138143
p.printTypeArguments(t.alias.typeArguments)
@@ -249,8 +254,10 @@ func (p *Printer) printStringMappingType(t *Type) {
249254
}
250255

251256
func (p *Printer) printEnumLiteral(t *Type) {
252-
p.printName(p.c.getParentOfSymbol(t.symbol))
253-
p.print(".")
257+
if parent := p.c.getParentOfSymbol(t.symbol); parent != nil {
258+
p.printName(parent)
259+
p.print(".")
260+
}
254261
p.printName(t.symbol)
255262
}
256263

internal/testutil/baseline/baseline.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
type Options struct {
16-
Subfolder string
17-
IsSubmodule bool
16+
Subfolder string
17+
IsSubmodule bool
18+
DiffFixupOld func(string) string
1819
}
1920

2021
const (
@@ -25,19 +26,22 @@ const (
2526
func Run(t *testing.T, fileName string, actual string, opts Options) {
2627
if opts.IsSubmodule {
2728
opts.Subfolder = filepath.Join(submoduleFolder, opts.Subfolder)
28-
diff := getBaselineDiff(t, actual, fileName)
29+
diff := getBaselineDiff(t, actual, fileName, opts.DiffFixupOld)
2930
diffFileName := fileName + ".diff"
3031
writeComparison(t, diff, diffFileName, false, opts)
3132
}
3233
writeComparison(t, actual, fileName, false, opts)
3334
}
3435

35-
func getBaselineDiff(t *testing.T, actual string, fileName string) string {
36+
func getBaselineDiff(t *testing.T, actual string, fileName string, fixupOld func(string) string) string {
3637
expected := NoContent
3738
refFileName := submoduleReferencePath(fileName, "" /*subfolder*/)
3839
if content, err := os.ReadFile(refFileName); err == nil {
3940
expected = string(content)
4041
}
42+
if fixupOld != nil {
43+
expected = fixupOld(expected)
44+
}
4145
if actual == expected {
4246
return NoContent
4347
}

internal/testutil/tsbaseline/type_symbol_baseline.go

+48-9
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,56 @@ func DoTypeAndSymbolBaseline(
5252

5353
fullWalker := newTypeWriterWalker(program, hasErrorBaseline)
5454

55-
if !opts.IsSubmodule {
56-
t.Run("type", func(t *testing.T) {
57-
defer testutil.RecoverAndFail(t, "Panic on creating type baseline for test "+header)
58-
checkBaselines(t, baselinePath, allFiles, fullWalker, header, opts, false /*isSymbolBaseline*/)
59-
})
60-
}
55+
t.Run("type", func(t *testing.T) {
56+
defer testutil.RecoverAndFail(t, "Panic on creating type baseline for test "+header)
57+
58+
// !!! Remove once the type baselines print node reuse lines
59+
typesOpts := opts
60+
typesOpts.DiffFixupOld = func(s string) string {
61+
var sb strings.Builder
62+
sb.Grow(len(s))
63+
64+
for line := range strings.SplitSeq(s, "\n") {
65+
if isTypeBaselineNodeReuseLine(line) {
66+
continue
67+
}
68+
sb.WriteString(line)
69+
sb.WriteString("\n")
70+
}
71+
72+
return sb.String()[:sb.Len()-1]
73+
}
74+
75+
checkBaselines(t, baselinePath, allFiles, fullWalker, header, typesOpts, false /*isSymbolBaseline*/)
76+
})
6177
t.Run("symbol", func(t *testing.T) {
6278
defer testutil.RecoverAndFail(t, "Panic on creating symbol baseline for test "+header)
6379
checkBaselines(t, baselinePath, allFiles, fullWalker, header, opts, true /*isSymbolBaseline*/)
6480
})
6581
}
6682

83+
func isTypeBaselineNodeReuseLine(line string) bool {
84+
line, ok := strings.CutPrefix(line, ">")
85+
if !ok {
86+
return false
87+
}
88+
line = strings.TrimLeft(line[1:], " ")
89+
line, ok = strings.CutPrefix(line, ":")
90+
if !ok {
91+
return false
92+
}
93+
94+
for _, c := range line {
95+
switch c {
96+
case ' ', '^', '\r':
97+
// Okay
98+
default:
99+
return false
100+
}
101+
}
102+
return true
103+
}
104+
67105
func checkBaselines(
68106
t *testing.T,
69107
baselinePath string,
@@ -308,7 +346,7 @@ func (walker *typeWriterWalker) writeTypeOrSymbol(node *ast.Node, isSymbolWalk b
308346
!ast.IsMetaProperty(node.Parent) &&
309347
!isImportStatementName(node) &&
310348
!isExportStatementName(node) &&
311-
!isIntrinsicJsxTag(node) {
349+
!isIntrinsicJsxTag(node, walker.currentSourceFile) {
312350
typeString = t.AsIntrinsicType().IntrinsicName()
313351
} else {
314352
// !!! TODO: full type printing and underline when we have node builder
@@ -400,12 +438,13 @@ func isExportStatementName(node *ast.Node) bool {
400438
return false
401439
}
402440

403-
func isIntrinsicJsxTag(node *ast.Node) bool {
441+
func isIntrinsicJsxTag(node *ast.Node, sourceFile *ast.SourceFile) bool {
404442
if !(ast.IsJsxOpeningElement(node.Parent) || ast.IsJsxClosingElement(node.Parent) || ast.IsJsxSelfClosingElement(node.Parent)) {
405443
return false
406444
}
407445
if node.Parent.TagName() != node {
408446
return false
409447
}
410-
return checker.IsIntrinsicJsxName(node.Text())
448+
text := scanner.GetSourceTextOfNodeFromSourceFile(sourceFile, node, false /*includeTrivia*/)
449+
return checker.IsIntrinsicJsxName(text)
411450
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/2dArrays.ts] ////
2+
3+
=== 2dArrays.ts ===
4+
class Cell {
5+
>Cell : Cell
6+
}
7+
8+
class Ship {
9+
>Ship : Ship
10+
11+
isSunk: boolean;
12+
>isSunk : boolean
13+
}
14+
15+
class Board {
16+
>Board : Board
17+
18+
ships: Ship[];
19+
>ships : Ship[]
20+
21+
cells: Cell[];
22+
>cells : Cell[]
23+
24+
private allShipsSunk() {
25+
>allShipsSunk : () => boolean
26+
27+
return this.ships.every(function (val) { return val.isSunk; });
28+
>this.ships.every(function (val) { return val.isSunk; }) : boolean
29+
>this.ships.every : { <S extends Ship>(predicate: (value: Ship, index: number, array: Ship[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Ship, index: number, array: Ship[]) => unknown, thisArg?: any): boolean; }
30+
>this.ships : Ship[]
31+
>this : this
32+
>ships : Ship[]
33+
>every : { <S extends Ship>(predicate: (value: Ship, index: number, array: Ship[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Ship, index: number, array: Ship[]) => unknown, thisArg?: any): boolean; }
34+
>function (val) { return val.isSunk; } : (val: Ship) => boolean
35+
>val : Ship
36+
>val.isSunk : boolean
37+
>val : Ship
38+
>isSunk : boolean
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [tests/cases/compiler/ArrowFunctionExpression1.ts] ////
2+
3+
=== ArrowFunctionExpression1.ts ===
4+
var v = (public x: string) => { };
5+
>v : (x: string) => void
6+
>(public x: string) => { } : (x: string) => void
7+
>x : string
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/ClassDeclaration10.ts] ////
2+
3+
=== ClassDeclaration10.ts ===
4+
class C {
5+
>C : C
6+
7+
constructor();
8+
foo();
9+
>foo : () => any
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/ClassDeclaration11.ts] ////
2+
3+
=== ClassDeclaration11.ts ===
4+
class C {
5+
>C : C
6+
7+
constructor();
8+
foo() { }
9+
>foo : () => void
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/ClassDeclaration13.ts] ////
2+
3+
=== ClassDeclaration13.ts ===
4+
class C {
5+
>C : C
6+
7+
foo();
8+
>foo : () => any
9+
10+
bar() { }
11+
>bar : () => void
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/ClassDeclaration14.ts] ////
2+
3+
=== ClassDeclaration14.ts ===
4+
class C {
5+
>C : C
6+
7+
foo();
8+
>foo : () => any
9+
10+
constructor();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/compiler/ClassDeclaration15.ts] ////
2+
3+
=== ClassDeclaration15.ts ===
4+
class C {
5+
>C : C
6+
7+
foo();
8+
>foo : () => any
9+
10+
constructor() { }
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/ClassDeclaration21.ts] ////
2+
3+
=== ClassDeclaration21.ts ===
4+
class C {
5+
>C : C
6+
7+
0();
8+
>0 : () => any
9+
10+
1() { }
11+
>1 : () => void
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/ClassDeclaration22.ts] ////
2+
3+
=== ClassDeclaration22.ts ===
4+
class C {
5+
>C : C
6+
7+
"foo"();
8+
>"foo" : () => any
9+
10+
"bar"() { }
11+
>"bar" : () => void
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [tests/cases/compiler/ClassDeclaration24.ts] ////
2+
3+
=== ClassDeclaration24.ts ===
4+
class any {
5+
>any : any
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/ClassDeclaration25.ts] ////
2+
3+
=== ClassDeclaration25.ts ===
4+
interface IList<T> {
5+
>IList : IList<T>
6+
>T : T
7+
8+
data(): T;
9+
>data : () => T
10+
11+
next(): string;
12+
>next : () => string
13+
}
14+
class List<U> implements IList<U> {
15+
>List : List<U>
16+
>U : U
17+
18+
data(): U;
19+
>data : () => U
20+
21+
next(): string;
22+
>next : () => string
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--- old.ClassDeclaration25.types
2+
+++ new.ClassDeclaration25.types
3+
@@= skipped -1, +1 lines =@@
4+
5+
=== ClassDeclaration25.ts ===
6+
interface IList<T> {
7+
+>IList : IList<T>
8+
+>T : T
9+
+
10+
data(): T;
11+
>data : () => T
12+
13+
@@= skipped -8, +11 lines =@@
14+
}
15+
class List<U> implements IList<U> {
16+
>List : List<U>
17+
+>U : U
18+
19+
data(): U;
20+
>data : () => U
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/ClassDeclaration26.ts] ////
2+
3+
=== ClassDeclaration26.ts ===
4+
class C {
5+
>C : C
6+
7+
public const var export foo = 10;
8+
>var : any
9+
>foo : number
10+
>10 : 10
11+
12+
var constructor() { }
13+
>constructor : any
14+
>() { } : () => void
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [tests/cases/compiler/ClassDeclaration8.ts] ////
2+
3+
=== ClassDeclaration8.ts ===
4+
class C {
5+
>C : C
6+
7+
constructor();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/compiler/ClassDeclaration9.ts] ////
2+
3+
=== ClassDeclaration9.ts ===
4+
class C {
5+
>C : C
6+
7+
foo();
8+
>foo : () => any
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts] ////
2+
3+
=== ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts ===
4+
class AtomicNumbers {
5+
>AtomicNumbers : AtomicNumbers
6+
7+
static const H = 1;
8+
>H : number
9+
>1 : 1
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts] ////
2+
3+
=== ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts ===
4+
class C {
5+
>C : C
6+
7+
const
8+
>const : any
9+
10+
x = 10;
11+
>x : number
12+
>10 : 10
13+
}

0 commit comments

Comments
 (0)