From 75ca64120f7029eecd586b80f4b46cadc83576c5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 2 Jan 2020 16:30:46 -0800 Subject: [PATCH] Fix error on `interface`s in `.js` files (#35969) * Use the correct error message. * Accepted baselines. --- src/compiler/program.ts | 4 +- ...etJavaScriptSyntacticDiagnostics6.baseline | 4 +- .../jsDeclarationsInterfaces.errors.txt | 104 +++++++++--------- ...sFileCompilationInterfaceSyntax.errors.txt | 4 +- 4 files changed, 59 insertions(+), 57 deletions(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 891763ddbbd..3956301f3da 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1837,7 +1837,9 @@ namespace ts { } break; case SyntaxKind.InterfaceDeclaration: - diagnostics.push(createDiagnosticForNode(node, Diagnostics.Interface_declaration_cannot_have_implements_clause)); + const interfaceKeyword = tokenToString(SyntaxKind.InterfaceKeyword); + Debug.assertDefined(interfaceKeyword); + diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, interfaceKeyword)); return; case SyntaxKind.ModuleDeclaration: const moduleKeyword = node.flags & NodeFlags.Namespace ? tokenToString(SyntaxKind.NamespaceKeyword) : tokenToString(SyntaxKind.ModuleKeyword); diff --git a/tests/baselines/reference/getJavaScriptSyntacticDiagnostics6.baseline b/tests/baselines/reference/getJavaScriptSyntacticDiagnostics6.baseline index a129f7ca8c5..61985450110 100644 --- a/tests/baselines/reference/getJavaScriptSyntacticDiagnostics6.baseline +++ b/tests/baselines/reference/getJavaScriptSyntacticDiagnostics6.baseline @@ -1,8 +1,8 @@ Syntactic Diagnostics for file '/tests/cases/fourslash/getJavaScriptSyntacticDiagnostics6.ts': -/tests/cases/fourslash/a.js(1,11): error TS1176: Interface declaration cannot have 'implements' clause. +/tests/cases/fourslash/a.js(1,11): error TS8006: 'interface' declarations can only be used in TypeScript files. ==== /tests/cases/fourslash/a.js (1 errors) ==== interface I { } ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. \ No newline at end of file +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. \ No newline at end of file diff --git a/tests/baselines/reference/jsDeclarationsInterfaces.errors.txt b/tests/baselines/reference/jsDeclarationsInterfaces.errors.txt index 50efca43598..2c6cdb8c67c 100644 --- a/tests/baselines/reference/jsDeclarationsInterfaces.errors.txt +++ b/tests/baselines/reference/jsDeclarationsInterfaces.errors.txt @@ -1,29 +1,29 @@ -tests/cases/conformance/jsdoc/declarations/index.js(4,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(6,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(10,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(31,11): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(35,11): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(39,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(43,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(45,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(49,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(53,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(57,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(61,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(65,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(67,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(71,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(75,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(80,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(84,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(87,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(91,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(95,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(100,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(105,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(107,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(111,18): error TS1176: Interface declaration cannot have 'implements' clause. -tests/cases/conformance/jsdoc/declarations/index.js(115,18): error TS1176: Interface declaration cannot have 'implements' clause. +tests/cases/conformance/jsdoc/declarations/index.js(4,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(6,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(10,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(31,11): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(35,11): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(39,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(43,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(45,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(49,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(53,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(57,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(61,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(65,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(67,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(71,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(75,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(80,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(84,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(87,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(91,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(95,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(100,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(105,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(107,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(111,18): error TS8006: 'interface' declarations can only be used in TypeScript files. +tests/cases/conformance/jsdoc/declarations/index.js(115,18): error TS8006: 'interface' declarations can only be used in TypeScript files. ==== tests/cases/conformance/jsdoc/declarations/index.js (26 errors) ==== @@ -32,17 +32,17 @@ tests/cases/conformance/jsdoc/declarations/index.js(115,18): error TS1176: Inter export interface A {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export interface B { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. cat: string; } export interface C { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. field: T & U; optionalField?: T; readonly readonlyField: T & U; @@ -65,135 +65,135 @@ tests/cases/conformance/jsdoc/declarations/index.js(115,18): error TS1176: Inter interface G {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export { G }; interface HH {} ~~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export { HH as H }; export interface I {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export { I as II }; export { J as JJ }; export interface J {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export interface K extends I,J { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. x: string; } export interface L extends K { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. y: string; } export interface M { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. field: T; } export interface N extends M { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. other: U; } export interface O { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: string; } export interface P extends O {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export interface Q extends O { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: "ok"; } export interface R extends O { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: number]: "ok"; } export interface S extends O { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: "ok"; [idx: number]: never; } export interface T { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: number]: string; } export interface U extends T {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export interface V extends T { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: string; } export interface W extends T { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: number]: "ok"; } export interface X extends T { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: string; [idx: number]: "ok"; } export interface Y { ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: {x: number}; [idx: number]: {x: number, y: number}; } export interface Z extends Y {} ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. export interface AA extends Y { ~~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: {x: number, y: number}; } export interface BB extends Y { ~~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: number]: {x: 0, y: 0}; } export interface CC extends Y { ~~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. [idx: string]: {x: number, y: number}; [idx: number]: {x: 0, y: 0}; } diff --git a/tests/baselines/reference/jsFileCompilationInterfaceSyntax.errors.txt b/tests/baselines/reference/jsFileCompilationInterfaceSyntax.errors.txt index 0f2923f1094..9024811d3f2 100644 --- a/tests/baselines/reference/jsFileCompilationInterfaceSyntax.errors.txt +++ b/tests/baselines/reference/jsFileCompilationInterfaceSyntax.errors.txt @@ -1,6 +1,6 @@ error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. -tests/cases/compiler/a.js(1,11): error TS1176: Interface declaration cannot have 'implements' clause. +tests/cases/compiler/a.js(1,11): error TS8006: 'interface' declarations can only be used in TypeScript files. !!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file. @@ -8,4 +8,4 @@ tests/cases/compiler/a.js(1,11): error TS1176: Interface declaration cannot have ==== tests/cases/compiler/a.js (1 errors) ==== interface I { } ~ -!!! error TS1176: Interface declaration cannot have 'implements' clause. \ No newline at end of file +!!! error TS8006: 'interface' declarations can only be used in TypeScript files. \ No newline at end of file