Skip to content

Commit 6385c66

Browse files
author
Andy
authored
Simplify diagnostics fourslash tests (#22245)
1 parent c487a9d commit 6385c66

26 files changed

+100
-218
lines changed

src/harness/fourslash.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,22 +1239,23 @@ Actual: ${stringify(fullActual)}`);
12391239
return this.languageService.findReferences(this.activeFile.fileName, this.currentCaretPosition);
12401240
}
12411241

1242-
public getSyntacticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
1242+
public getSyntacticDiagnostics(expected: ReadonlyArray<FourSlashInterface.Diagnostic>) {
12431243
const diagnostics = this.languageService.getSyntacticDiagnostics(this.activeFile.fileName);
1244-
this.testDiagnostics(expected, diagnostics);
1244+
this.testDiagnostics(expected, diagnostics, "error");
12451245
}
12461246

1247-
public getSemanticDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>) {
1247+
public getSemanticDiagnostics(expected: ReadonlyArray<FourSlashInterface.Diagnostic>) {
12481248
const diagnostics = this.languageService.getSemanticDiagnostics(this.activeFile.fileName);
1249-
this.testDiagnostics(expected, diagnostics);
1249+
this.testDiagnostics(expected, diagnostics, "error");
12501250
}
12511251

1252-
public getSuggestionDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>): void {
1253-
this.testDiagnostics(expected, this.languageService.getSuggestionDiagnostics(this.activeFile.fileName));
1252+
public getSuggestionDiagnostics(expected: ReadonlyArray<FourSlashInterface.Diagnostic>): void {
1253+
this.testDiagnostics(expected, this.languageService.getSuggestionDiagnostics(this.activeFile.fileName), "suggestion");
12541254
}
12551255

1256-
private testDiagnostics(expected: ReadonlyArray<ts.RealizedDiagnostic>, diagnostics: ReadonlyArray<ts.Diagnostic>) {
1257-
assert.deepEqual(ts.realizeDiagnostics(diagnostics, ts.newLineCharacter), expected);
1256+
private testDiagnostics(expected: ReadonlyArray<FourSlashInterface.Diagnostic>, diagnostics: ReadonlyArray<ts.Diagnostic>, category: string) {
1257+
assert.deepEqual(ts.realizeDiagnostics(diagnostics, ts.newLineCharacter), expected.map<ts.RealizedDiagnostic>(e => (
1258+
{ message: e.message, category, code: e.code, ...ts.createTextSpanFromRange(e.range || this.getRanges()[0]) })));
12581259
}
12591260

12601261
public verifyQuickInfoAt(markerName: string, expectedText: string, expectedDocumentation?: string) {
@@ -4676,4 +4677,10 @@ namespace FourSlashInterface {
46764677
source?: string;
46774678
description: string;
46784679
}
4680+
4681+
export interface Diagnostic {
4682+
message: string;
4683+
range?: FourSlash.Range;
4684+
code: number;
4685+
}
46794686
}

tests/cases/fourslash/fourslash.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ declare namespace FourSlashInterface {
347347
start: number;
348348
length: number;
349349
}, displayParts: ts.SymbolDisplayPart[], documentation: ts.SymbolDisplayPart[], tags: ts.JSDocTagInfo[]): void;
350-
getSyntacticDiagnostics(expected: ReadonlyArray<RealizedDiagnostic>): void;
351-
getSemanticDiagnostics(expected: ReadonlyArray<RealizedDiagnostic>): void;
352-
getSuggestionDiagnostics(expected: ReadonlyArray<RealizedDiagnostic>): void;
350+
getSyntacticDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
351+
getSemanticDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
352+
getSuggestionDiagnostics(expected: ReadonlyArray<Diagnostic>): void;
353353
ProjectInfo(expected: string[]): void;
354354
allRangesAppearInImplementationList(markerName: string): void;
355355
}
@@ -521,11 +521,10 @@ declare namespace FourSlashInterface {
521521
text: string;
522522
range: Range;
523523
}
524-
interface RealizedDiagnostic {
524+
interface Diagnostic {
525525
message: string;
526-
start: number;
527-
length: number;
528-
category: string;
526+
/** @default `test.ranges()[0]` */
527+
range?: Range;
529528
code: number;
530529
}
531530
}

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics1.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// import a = b;
5+
////[|import a = b;|]
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'import ... =' can only be used in a .ts file.",
10-
start: 0,
11-
length: 13,
12-
category: "error",
139
code: 8002
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics10.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// function F<T>() { }
5+
////function F<[|T|]>() { }
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'type parameter declarations' can only be used in a .ts file.",
10-
start: 11,
11-
length: 1,
12-
category: "error",
139
code: 8004
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics11.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// function F(): number { }
5+
////function F(): [|number|] { }
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'types' can only be used in a .ts file.",
10-
start: 14,
11-
length: 6,
12-
category: "error",
139
code: 8010
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics12.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// declare var v;
5+
////[|declare|] var v;
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'declare' can only be used in a .ts file.",
10-
start: 0,
11-
length: 7,
12-
category: "error",
139
code: 8009
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics13.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// var v: () => number;
5+
////var v: [|() => number|];
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'types' can only be used in a .ts file.",
10-
start: 7,
11-
length: 12,
12-
category: "error",
139
code: 8010
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics14.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// Foo<number>();
5+
////Foo<[|number|]>();
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'type arguments' can only be used in a .ts file.",
10-
start: 4,
11-
length: 6,
12-
category: "error",
139
code: 8011
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics15.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// function F(public p) { }
5+
////function F([|public|] p) { }
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'parameter modifiers' can only be used in a .ts file.",
10-
start: 11,
11-
length: 6,
12-
category: "error",
139
code: 8012
14-
}
15-
]);
10+
}]);

tests/cases/fourslash/getJavaScriptSyntacticDiagnostics16.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
// @allowJs: true
44
// @Filename: a.js
5-
//// function F(p?) { }
5+
////function F(p[|?|]) { }
66

7-
verify.getSyntacticDiagnostics([
8-
{
7+
verify.getSyntacticDiagnostics([{
98
message: "'?' can only be used in a .ts file.",
10-
start: 12,
11-
length: 1,
12-
category: "error",
139
code: 8009
14-
}
15-
]);
10+
}]);

0 commit comments

Comments
 (0)