Skip to content

Commit 3da85df

Browse files
Clean up error messages for using TypeScript syntax in JavaScr… (microsoft#35254)
* Fix up quotation marks in error messages in JavaScript files. * Accepted baselines. * Typescript -> TypeScript * Accepted baselines. * Migrate syntactic diagnostics tests to baselining tests. * Accepted baselines. * Update diagnosticMessages.json * Removed markers. * Add ability to baseline both semantic and syntactic diagnostics. * Fix up broken diagnostics when using a server LS. * Accepted baselines. * Lints. * Fake up sourcefile objects in the tsserver session client instead. * Fewer allocations.
1 parent 3e32946 commit 3da85df

File tree

79 files changed

+547
-387
lines changed

Some content is hidden

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

79 files changed

+547
-387
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ namespace ts {
15391539
}
15401540

15411541
if (element.questionToken) {
1542-
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element.questionToken, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
1542+
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element.questionToken, Diagnostics.The_0_modifier_can_only_be_used_in_TypeScript_files, "?"));
15431543
}
15441544
if (!isDoubleQuotedString(element.name)) {
15451545
errors.push(createDiagnosticForNodeInSourceFile(sourceFile, element.name, Diagnostics.String_literal_with_double_quotes_expected));

src/compiler/diagnosticMessages.json

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4543,59 +4543,51 @@
45434543
"category": "Error",
45444544
"code": 8001
45454545
},
4546-
"'import ... =' can only be used in a .ts file.": {
4546+
"'import ... =' can only be used in TypeScript files.": {
45474547
"category": "Error",
45484548
"code": 8002
45494549
},
4550-
"'export=' can only be used in a .ts file.": {
4550+
"'export =' can only be used in TypeScript files.": {
45514551
"category": "Error",
45524552
"code": 8003
45534553
},
4554-
"'type parameter declarations' can only be used in a .ts file.": {
4554+
"Type parameter declarations can only be used in TypeScript files.": {
45554555
"category": "Error",
45564556
"code": 8004
45574557
},
4558-
"'implements clauses' can only be used in a .ts file.": {
4558+
"'implements' clauses can only be used in TypeScript files.": {
45594559
"category": "Error",
45604560
"code": 8005
45614561
},
4562-
"'interface declarations' can only be used in a .ts file.": {
4562+
"'{0}' declarations can only be used in TypeScript files.": {
45634563
"category": "Error",
45644564
"code": 8006
45654565
},
4566-
"'module declarations' can only be used in a .ts file.": {
4567-
"category": "Error",
4568-
"code": 8007
4569-
},
4570-
"'type aliases' can only be used in a .ts file.": {
4566+
"Type aliases can only be used in TypeScript files.": {
45714567
"category": "Error",
45724568
"code": 8008
45734569
},
4574-
"'{0}' can only be used in a .ts file.": {
4570+
"The '{0}' modifier can only be used in TypeScript files.": {
45754571
"category": "Error",
45764572
"code": 8009
45774573
},
4578-
"'types' can only be used in a .ts file.": {
4574+
"Type annotations can only be used in TypeScript files.": {
45794575
"category": "Error",
45804576
"code": 8010
45814577
},
4582-
"'type arguments' can only be used in a .ts file.": {
4578+
"Type arguments can only be used in TypeScript files.": {
45834579
"category": "Error",
45844580
"code": 8011
45854581
},
4586-
"'parameter modifiers' can only be used in a .ts file.": {
4582+
"Parameter modifiers can only be used in TypeScript files.": {
45874583
"category": "Error",
45884584
"code": 8012
45894585
},
4590-
"'non-null assertions' can only be used in a .ts file.": {
4586+
"Non-null assertions can only be used in TypeScript files.": {
45914587
"category": "Error",
45924588
"code": 8013
45934589
},
4594-
"'enum declarations' can only be used in a .ts file.": {
4595-
"category": "Error",
4596-
"code": 8015
4597-
},
4598-
"'type assertion expressions' can only be used in a .ts file.": {
4590+
"Type assertion expressions can only be used in TypeScript files.": {
45994591
"category": "Error",
46004592
"code": 8016
46014593
},

src/compiler/program.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,7 +1797,7 @@ namespace ts {
17971797
case SyntaxKind.PropertyDeclaration:
17981798
case SyntaxKind.MethodDeclaration:
17991799
if ((<ParameterDeclaration | PropertyDeclaration | MethodDeclaration>parent).questionToken === node) {
1800-
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_can_only_be_used_in_a_ts_file, "?"));
1800+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.The_0_modifier_can_only_be_used_in_TypeScript_files, "?"));
18011801
return;
18021802
}
18031803
// falls through
@@ -1811,45 +1811,48 @@ namespace ts {
18111811
case SyntaxKind.VariableDeclaration:
18121812
// type annotation
18131813
if ((<FunctionLikeDeclaration | VariableDeclaration | ParameterDeclaration | PropertyDeclaration>parent).type === node) {
1814-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.types_can_only_be_used_in_a_ts_file));
1814+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Type_annotations_can_only_be_used_in_TypeScript_files));
18151815
return;
18161816
}
18171817
}
18181818

18191819
switch (node.kind) {
18201820
case SyntaxKind.ImportEqualsDeclaration:
1821-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.import_can_only_be_used_in_a_ts_file));
1821+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.import_can_only_be_used_in_TypeScript_files));
18221822
return;
18231823
case SyntaxKind.ExportAssignment:
18241824
if ((<ExportAssignment>node).isExportEquals) {
1825-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.export_can_only_be_used_in_a_ts_file));
1825+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.export_can_only_be_used_in_TypeScript_files));
18261826
return;
18271827
}
18281828
break;
18291829
case SyntaxKind.HeritageClause:
18301830
const heritageClause = <HeritageClause>node;
18311831
if (heritageClause.token === SyntaxKind.ImplementsKeyword) {
1832-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.implements_clauses_can_only_be_used_in_a_ts_file));
1832+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.implements_clauses_can_only_be_used_in_TypeScript_files));
18331833
return;
18341834
}
18351835
break;
18361836
case SyntaxKind.InterfaceDeclaration:
1837-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.interface_declarations_can_only_be_used_in_a_ts_file));
1837+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Interface_declaration_cannot_have_implements_clause));
18381838
return;
18391839
case SyntaxKind.ModuleDeclaration:
1840-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.module_declarations_can_only_be_used_in_a_ts_file));
1840+
const moduleKeyword = node.flags & NodeFlags.Namespace ? tokenToString(SyntaxKind.NamespaceKeyword) : tokenToString(SyntaxKind.ModuleKeyword);
1841+
Debug.assertDefined(moduleKeyword);
1842+
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, moduleKeyword));
18411843
return;
18421844
case SyntaxKind.TypeAliasDeclaration:
1843-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.type_aliases_can_only_be_used_in_a_ts_file));
1845+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Type_aliases_can_only_be_used_in_TypeScript_files));
18441846
return;
18451847
case SyntaxKind.EnumDeclaration:
1846-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.enum_declarations_can_only_be_used_in_a_ts_file));
1848+
const enumKeyword = Debug.assertDefined(tokenToString(SyntaxKind.EnumKeyword));
1849+
diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, enumKeyword));
18471850
return;
18481851
case SyntaxKind.NonNullExpression:
1849-
diagnostics.push(createDiagnosticForNode(node, Diagnostics.non_null_assertions_can_only_be_used_in_a_ts_file));
1852+
diagnostics.push(createDiagnosticForNode(node, Diagnostics.Non_null_assertions_can_only_be_used_in_TypeScript_files));
18501853
return;
18511854
case SyntaxKind.AsExpression:
1852-
diagnostics.push(createDiagnosticForNode((node as AsExpression).type, Diagnostics.type_assertion_expressions_can_only_be_used_in_a_ts_file));
1855+
diagnostics.push(createDiagnosticForNode((node as AsExpression).type, Diagnostics.Type_assertion_expressions_can_only_be_used_in_TypeScript_files));
18531856
return;
18541857
case SyntaxKind.TypeAssertionExpression:
18551858
Debug.fail(); // Won't parse these in a JS file anyway, as they are interpreted as JSX.
@@ -1878,7 +1881,7 @@ namespace ts {
18781881
case SyntaxKind.ArrowFunction:
18791882
// Check type parameters
18801883
if (nodes === (<DeclarationWithTypeParameterChildren>parent).typeParameters) {
1881-
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_parameter_declarations_can_only_be_used_in_a_ts_file));
1884+
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.Type_parameter_declarations_can_only_be_used_in_TypeScript_files));
18821885
return;
18831886
}
18841887
// falls through
@@ -1894,7 +1897,7 @@ namespace ts {
18941897
if (nodes === (<PropertyDeclaration>parent).modifiers) {
18951898
for (const modifier of <NodeArray<Modifier>>nodes) {
18961899
if (modifier.kind !== SyntaxKind.StaticKeyword) {
1897-
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind)));
1900+
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics.The_0_modifier_can_only_be_used_in_TypeScript_files, tokenToString(modifier.kind)));
18981901
}
18991902
}
19001903
return;
@@ -1903,7 +1906,7 @@ namespace ts {
19031906
case SyntaxKind.Parameter:
19041907
// Check modifiers of parameter declaration
19051908
if (nodes === (<ParameterDeclaration>parent).modifiers) {
1906-
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.parameter_modifiers_can_only_be_used_in_a_ts_file));
1909+
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.Parameter_modifiers_can_only_be_used_in_TypeScript_files));
19071910
return;
19081911
}
19091912
break;
@@ -1915,7 +1918,7 @@ namespace ts {
19151918
case SyntaxKind.TaggedTemplateExpression:
19161919
// Check type arguments
19171920
if (nodes === (<NodeWithTypeArguments>parent).typeArguments) {
1918-
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.type_arguments_can_only_be_used_in_a_ts_file));
1921+
diagnostics.push(createDiagnosticForNodeArray(nodes, Diagnostics.Type_arguments_can_only_be_used_in_TypeScript_files));
19191922
return;
19201923
}
19211924
break;
@@ -1941,7 +1944,7 @@ namespace ts {
19411944
case SyntaxKind.ReadonlyKeyword:
19421945
case SyntaxKind.DeclareKeyword:
19431946
case SyntaxKind.AbstractKeyword:
1944-
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics._0_can_only_be_used_in_a_ts_file, tokenToString(modifier.kind)));
1947+
diagnostics.push(createDiagnosticForNode(modifier, Diagnostics.The_0_modifier_can_only_be_used_in_TypeScript_files, tokenToString(modifier.kind)));
19451948
break;
19461949

19471950
// These are all legal modifiers.

src/harness/client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,14 @@ namespace ts.server {
374374
private getDiagnostics(file: string, command: CommandNames): DiagnosticWithLocation[] {
375375
const request = this.processRequest<protocol.SyntacticDiagnosticsSyncRequest | protocol.SemanticDiagnosticsSyncRequest | protocol.SuggestionDiagnosticsSyncRequest>(command, { file, includeLinePosition: true });
376376
const response = this.processResponse<protocol.SyntacticDiagnosticsSyncResponse | protocol.SemanticDiagnosticsSyncResponse | protocol.SuggestionDiagnosticsSyncResponse>(request);
377+
const sourceText = getSnapshotText(this.host.getScriptSnapshot(file)!);
378+
const fakeSourceFile = { fileName: file, text: sourceText } as SourceFile; // Warning! This is a huge lie!
377379

378380
return (<protocol.DiagnosticWithLinePosition[]>response.body).map((entry): DiagnosticWithLocation => {
379381
const category = firstDefined(Object.keys(DiagnosticCategory), id =>
380382
isString(id) && entry.category === id.toLowerCase() ? (<any>DiagnosticCategory)[id] : undefined);
381383
return {
382-
file: undefined!, // TODO: GH#18217
384+
file: fakeSourceFile,
383385
start: entry.start,
384386
length: entry.length,
385387
messageText: entry.message,
@@ -518,14 +520,14 @@ namespace ts.server {
518520
return notImplemented();
519521
}
520522

521-
getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems {
523+
getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems | undefined {
522524
const args: protocol.SignatureHelpRequestArgs = this.createFileLocationRequestArgs(fileName, position);
523525

524526
const request = this.processRequest<protocol.SignatureHelpRequest>(CommandNames.SignatureHelp, args);
525527
const response = this.processResponse<protocol.SignatureHelpResponse>(request);
526528

527529
if (!response.body) {
528-
return undefined!; // TODO: GH#18217
530+
return undefined;
529531
}
530532

531533
const { items, applicableSpan: encodedApplicableSpan, selectedItemIndex, argumentIndex, argumentCount } = response.body;

0 commit comments

Comments
 (0)