Skip to content

Commit d58b1f6

Browse files
authored
mark error type in baselines very not-aggressively (microsoft#26346)
1 parent 56bac29 commit d58b1f6

File tree

84 files changed

+320
-270
lines changed

Some content is hidden

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

84 files changed

+320
-270
lines changed

src/harness/harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ namespace Harness {
14881488
!errors || (errors.length === 0) ? null : getErrorBaseline(inputFiles, errors, pretty));
14891489
}
14901490

1491-
export function doTypeAndSymbolBaseline(baselinePath: string, program: ts.Program, allFiles: {unitName: string, content: string}[], opts?: Baseline.BaselineOptions, multifile?: boolean, skipTypeBaselines?: boolean, skipSymbolBaselines?: boolean) {
1491+
export function doTypeAndSymbolBaseline(baselinePath: string, program: ts.Program, allFiles: {unitName: string, content: string}[], opts?: Baseline.BaselineOptions, multifile?: boolean, skipTypeBaselines?: boolean, skipSymbolBaselines?: boolean, hasErrorBaseline?: boolean) {
14921492
// The full walker simulates the types that you would get from doing a full
14931493
// compile. The pull walker simulates the types you get when you just do
14941494
// a type query for a random node (like how the LS would do it). Most of the
@@ -1504,7 +1504,7 @@ namespace Harness {
15041504
// These types are equivalent, but depend on what order the compiler observed
15051505
// certain parts of the program.
15061506

1507-
const fullWalker = new TypeWriterWalker(program, /*fullTypeCheck*/ true);
1507+
const fullWalker = new TypeWriterWalker(program, /*fullTypeCheck*/ true, !!hasErrorBaseline);
15081508

15091509
// Produce baselines. The first gives the types for all expressions.
15101510
// The second gives symbols for all identifiers.

src/harness/typeWriter.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class TypeWriterWalker {
2525

2626
private checker: ts.TypeChecker;
2727

28-
constructor(private program: ts.Program, fullTypeCheck: boolean) {
28+
constructor(private program: ts.Program, fullTypeCheck: boolean, private hadErrorBaseline: boolean) {
2929
// Consider getting both the diagnostics checker and the non-diagnostics checker to verify
3030
// they are consistent.
3131
this.checker = fullTypeCheck
@@ -69,6 +69,26 @@ class TypeWriterWalker {
6969
}
7070
}
7171

72+
private isImportStatementName(node: ts.Node) {
73+
if (ts.isImportSpecifier(node.parent) && (node.parent.name === node || node.parent.propertyName === node)) return true;
74+
if (ts.isImportClause(node.parent) && node.parent.name === node) return true;
75+
if (ts.isImportEqualsDeclaration(node.parent) && node.parent.name === node) return true;
76+
return false;
77+
}
78+
79+
private isExportStatementName(node: ts.Node) {
80+
if (ts.isExportAssignment(node.parent) && node.parent.expression === node) return true;
81+
if (ts.isExportSpecifier(node.parent) && (node.parent.name === node || node.parent.propertyName === node)) return true;
82+
return false;
83+
}
84+
85+
private isIntrinsicJsxTag(node: ts.Node) {
86+
const p = node.parent;
87+
if (!(ts.isJsxOpeningElement(p) || ts.isJsxClosingElement(p) || ts.isJsxSelfClosingElement(p))) return false;
88+
if (p.tagName !== node) return false;
89+
return ts.isIntrinsicJsxName(node.getText());
90+
}
91+
7292
private writeTypeOrSymbol(node: ts.Node, isSymbolWalk: boolean): TypeWriterResult | undefined {
7393
const actualPos = ts.skipTrivia(this.currentSourceFile.text, node.pos);
7494
const lineAndCharacter = this.currentSourceFile.getLineAndCharacterOfPosition(actualPos);
@@ -85,7 +105,31 @@ class TypeWriterWalker {
85105
// let type = this.checker.getTypeAtLocation(node);
86106
let type = ts.isExpressionWithTypeArgumentsInClassExtendsClause(node.parent) ? this.checker.getTypeAtLocation(node.parent) : undefined;
87107
if (!type || type.flags & ts.TypeFlags.Any) type = this.checker.getTypeAtLocation(node);
88-
const typeString = this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
108+
const typeString =
109+
// Distinguish `errorType`s from `any`s; but only if the file has no errors.
110+
// Additionally,
111+
// * the LHS of a qualified name
112+
// * a binding pattern name
113+
// * labels
114+
// * the "global" in "declare global"
115+
// * the "target" in "new.target"
116+
// * names in import statements
117+
// * type-only names in export statements
118+
// * and intrinsic jsx tag names
119+
// return `error`s via `getTypeAtLocation`
120+
// But this is generally expected, so we don't call those out, either
121+
(!this.hadErrorBaseline &&
122+
type.flags & ts.TypeFlags.Any &&
123+
!ts.isBindingElement(node.parent) &&
124+
!ts.isPropertyAccessOrQualifiedName(node.parent) &&
125+
!ts.isLabelName(node) &&
126+
!(ts.isModuleDeclaration(node.parent) && ts.isGlobalScopeAugmentation(node.parent)) &&
127+
!ts.isMetaProperty(node.parent) &&
128+
!this.isImportStatementName(node) &&
129+
!this.isExportStatementName(node) &&
130+
!this.isIntrinsicJsxTag(node)) ?
131+
(type as ts.IntrinsicType).intrinsicName :
132+
this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.AllowUniqueESSymbolType);
89133
return {
90134
line: lineAndCharacter.line,
91135
syntaxKind: node.kind,

src/testRunner/compilerRunner.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ class CompilerTest {
253253
Harness.Compiler.doTypeAndSymbolBaseline(
254254
this.justName,
255255
this.result.program!,
256-
this.toBeCompiled.concat(this.otherFiles).filter(file => !!this.result.program!.getSourceFile(file.unitName)));
256+
this.toBeCompiled.concat(this.otherFiles).filter(file => !!this.result.program!.getSourceFile(file.unitName)),
257+
/*opts*/ undefined,
258+
/*multifile*/ undefined,
259+
/*skipTypeBaselines*/ undefined,
260+
/*skipSymbolBaselines*/ undefined,
261+
!!ts.length(this.result.diagnostics)
262+
);
257263
}
258264

259265
private makeUnitName(name: string, root: string) {

tests/baselines/reference/acceptableAlias1.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module M {
66
}
77
export import X = N;
88
>X : any
9-
>N : any
9+
>N : error
1010
}
1111

1212
import r = M.X;

tests/baselines/reference/aliasInaccessibleModule.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ module M {
66
}
77
export import X = N;
88
>X : any
9-
>N : any
9+
>N : error
1010
}

tests/baselines/reference/checkJsFiles_skipDiagnostics.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ var x = 0;
66

77
/// @ts-ignore
88
x();
9-
>x() : any
9+
>x() : error
1010
>x : number
1111

1212
/// @ts-ignore
1313
x();
14-
>x() : any
14+
>x() : error
1515
>x : number
1616

1717
/// @ts-ignore
1818
x(
19-
>x( 2, 3) : any
19+
>x( 2, 3) : error
2020
>x : number
2121

2222
2,
@@ -34,13 +34,13 @@ x(
3434
// @another
3535

3636
x();
37-
>x() : any
37+
>x() : error
3838
>x : number
3939

4040

4141

4242
// @ts-ignore: no call signature
4343
x();
44-
>x() : any
44+
>x() : error
4545
>x : number
4646

tests/baselines/reference/declarationEmitNameConflictsWithAlias.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export module C { export interface I { } }
33
export import v = C;
44
>v : any
5-
>C : any
5+
>C : error
66

77
export module M {
88
>M : typeof M

tests/baselines/reference/doubleUnderscoreReactNamespace.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ declare var __foot: any;
1111
>__foot : any
1212

1313
const thing = <__foot />;
14-
>thing : any
15-
><__foot /> : any
14+
>thing : error
15+
><__foot /> : error
1616
>__foot : any
1717

1818
export {}

tests/baselines/reference/duplicateVarAndImport.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ var a;
77
module M { }
88
import a = M;
99
>a : any
10-
>M : any
10+
>M : error
1111

tests/baselines/reference/es3-jsx-preserve.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const React: any = null;
44
>null : null
55

66
const elem = <div></div>;
7-
>elem : any
8-
><div></div> : any
7+
>elem : error
8+
><div></div> : error
99
>div : any
1010
>div : any
1111

0 commit comments

Comments
 (0)