From 06c6af30ffa3159c76df14fb9d3d4beb9e4fe668 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sun, 27 Mar 2022 12:31:31 +0300 Subject: [PATCH] fix(48445): show errors on type-only import/export specifiers in JavaScript files --- src/compiler/program.ts | 7 +++++++ .../reference/exportSpecifiers_js.errors.txt | 9 +++++++++ tests/baselines/reference/exportSpecifiers_js.symbols | 7 +++++++ tests/baselines/reference/exportSpecifiers_js.types | 8 ++++++++ .../reference/importSpecifiers_js.errors.txt | 11 +++++++++++ tests/baselines/reference/importSpecifiers_js.symbols | 8 ++++++++ tests/baselines/reference/importSpecifiers_js.types | 7 +++++++ .../externalModules/typeOnly/exportSpecifiers_js.ts | 7 +++++++ .../externalModules/typeOnly/importSpecifiers_js.ts | 9 +++++++++ 9 files changed, 73 insertions(+) create mode 100644 tests/baselines/reference/exportSpecifiers_js.errors.txt create mode 100644 tests/baselines/reference/exportSpecifiers_js.symbols create mode 100644 tests/baselines/reference/exportSpecifiers_js.types create mode 100644 tests/baselines/reference/importSpecifiers_js.errors.txt create mode 100644 tests/baselines/reference/importSpecifiers_js.symbols create mode 100644 tests/baselines/reference/importSpecifiers_js.types create mode 100644 tests/cases/conformance/externalModules/typeOnly/exportSpecifiers_js.ts create mode 100644 tests/cases/conformance/externalModules/typeOnly/importSpecifiers_js.ts diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 1fddfb58cc178..a17654e981fdc 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2273,6 +2273,13 @@ namespace ts { return "skip"; } break; + case SyntaxKind.ImportSpecifier: + case SyntaxKind.ExportSpecifier: + if ((node as ImportOrExportSpecifier).isTypeOnly) { + diagnostics.push(createDiagnosticForNode(node, Diagnostics._0_declarations_can_only_be_used_in_TypeScript_files, isImportSpecifier(node) ? "import...type" : "export...type")); + return "skip"; + } + break; case SyntaxKind.ImportEqualsDeclaration: diagnostics.push(createDiagnosticForNode(node, Diagnostics.import_can_only_be_used_in_TypeScript_files)); return "skip"; diff --git a/tests/baselines/reference/exportSpecifiers_js.errors.txt b/tests/baselines/reference/exportSpecifiers_js.errors.txt new file mode 100644 index 0000000000000..7d8fdd6fe4a41 --- /dev/null +++ b/tests/baselines/reference/exportSpecifiers_js.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/externalModules/typeOnly/a.js(2,10): error TS8006: 'export...type' declarations can only be used in TypeScript files. + + +==== tests/cases/conformance/externalModules/typeOnly/a.js (1 errors) ==== + const foo = 0; + export { type foo }; + ~~~~~~~~ +!!! error TS8006: 'export...type' declarations can only be used in TypeScript files. + \ No newline at end of file diff --git a/tests/baselines/reference/exportSpecifiers_js.symbols b/tests/baselines/reference/exportSpecifiers_js.symbols new file mode 100644 index 0000000000000..0652aca8a1cd7 --- /dev/null +++ b/tests/baselines/reference/exportSpecifiers_js.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.js === +const foo = 0; +>foo : Symbol(foo, Decl(a.js, 0, 5)) + +export { type foo }; +>foo : Symbol(foo, Decl(a.js, 1, 8)) + diff --git a/tests/baselines/reference/exportSpecifiers_js.types b/tests/baselines/reference/exportSpecifiers_js.types new file mode 100644 index 0000000000000..0637788c0599f --- /dev/null +++ b/tests/baselines/reference/exportSpecifiers_js.types @@ -0,0 +1,8 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.js === +const foo = 0; +>foo : 0 +>0 : 0 + +export { type foo }; +>foo : 0 + diff --git a/tests/baselines/reference/importSpecifiers_js.errors.txt b/tests/baselines/reference/importSpecifiers_js.errors.txt new file mode 100644 index 0000000000000..98e07951a2d0b --- /dev/null +++ b/tests/baselines/reference/importSpecifiers_js.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/externalModules/typeOnly/a.js(1,10): error TS8006: 'import...type' declarations can only be used in TypeScript files. + + +==== tests/cases/conformance/externalModules/typeOnly/a.ts (0 errors) ==== + export interface A {} + +==== tests/cases/conformance/externalModules/typeOnly/a.js (1 errors) ==== + import { type A } from "./a"; + ~~~~~~ +!!! error TS8006: 'import...type' declarations can only be used in TypeScript files. + \ No newline at end of file diff --git a/tests/baselines/reference/importSpecifiers_js.symbols b/tests/baselines/reference/importSpecifiers_js.symbols new file mode 100644 index 0000000000000..c486fa6bb7a07 --- /dev/null +++ b/tests/baselines/reference/importSpecifiers_js.symbols @@ -0,0 +1,8 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export interface A {} +>A : Symbol(A, Decl(a.ts, 0, 0)) + +=== tests/cases/conformance/externalModules/typeOnly/a.js === +import { type A } from "./a"; +>A : Symbol(A, Decl(a.js, 0, 8)) + diff --git a/tests/baselines/reference/importSpecifiers_js.types b/tests/baselines/reference/importSpecifiers_js.types new file mode 100644 index 0000000000000..b668b5e5fe256 --- /dev/null +++ b/tests/baselines/reference/importSpecifiers_js.types @@ -0,0 +1,7 @@ +=== tests/cases/conformance/externalModules/typeOnly/a.ts === +export interface A {} +No type information for this code. +No type information for this code.=== tests/cases/conformance/externalModules/typeOnly/a.js === +import { type A } from "./a"; +>A : any + diff --git a/tests/cases/conformance/externalModules/typeOnly/exportSpecifiers_js.ts b/tests/cases/conformance/externalModules/typeOnly/exportSpecifiers_js.ts new file mode 100644 index 0000000000000..b8280ae66a55c --- /dev/null +++ b/tests/cases/conformance/externalModules/typeOnly/exportSpecifiers_js.ts @@ -0,0 +1,7 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @Filename: ./a.js +const foo = 0; +export { type foo }; diff --git a/tests/cases/conformance/externalModules/typeOnly/importSpecifiers_js.ts b/tests/cases/conformance/externalModules/typeOnly/importSpecifiers_js.ts new file mode 100644 index 0000000000000..e2c2d8411f25d --- /dev/null +++ b/tests/cases/conformance/externalModules/typeOnly/importSpecifiers_js.ts @@ -0,0 +1,9 @@ +// @allowJs: true +// @checkJs: true +// @noEmit: true + +// @Filename: ./a.ts +export interface A {} + +// @Filename: ./a.js +import { type A } from "./a";