From da9db319eff8705289e959c126f563362f3a5674 Mon Sep 17 00:00:00 2001 From: Noel Varanda Date: Mon, 15 May 2017 22:59:43 +0100 Subject: [PATCH 1/2] Fix #15540: Throw error when importing @types Fix issue: #15540 - Modify checker; external imports to account for imported modules containing '@types/'. - Add diagnostic message. - Add test case --- src/compiler/checker.ts | 5 +++++ src/compiler/diagnosticMessages.json | 4 ++++ .../reference/importDeclTypes.errors.txt | 14 ++++++++++++++ tests/baselines/reference/importDeclTypes.js | 15 +++++++++++++++ tests/cases/compiler/importDeclTypes.ts | 9 +++++++++ 5 files changed, 47 insertions(+) create mode 100644 tests/baselines/reference/importDeclTypes.errors.txt create mode 100644 tests/baselines/reference/importDeclTypes.js create mode 100644 tests/cases/compiler/importDeclTypes.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d6f56a69b9ad5..cdc9642751ff0 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1642,6 +1642,11 @@ namespace ts { return; } + if (moduleReference.substr(0, 7) === "@types/") { + const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1; + error(errorNode, diag, moduleReference.substr(7), moduleReference); + } + const ambientModule = tryFindAmbientModule(moduleName, /*withAugmentations*/ true); if (ambientModule) { return ambientModule; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4aee7a37a8878..be7228bc26b5f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2135,6 +2135,10 @@ "category": "Error", "code": 2710 }, + "Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": { + "category": "Error", + "code": 2711 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/importDeclTypes.errors.txt b/tests/baselines/reference/importDeclTypes.errors.txt new file mode 100644 index 0000000000000..75546059897b2 --- /dev/null +++ b/tests/baselines/reference/importDeclTypes.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/a.ts(1,21): error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. + + +==== /node_modules/@types/foo-bar/index.d.ts (0 errors) ==== + export interface Foo { + bar: string; + } + + // This should error +==== tests/cases/compiler/a.ts (1 errors) ==== + import { Foo } from "@types/foo-bar"; + ~~~~~~~~~~~~~~~~ +!!! error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. + \ No newline at end of file diff --git a/tests/baselines/reference/importDeclTypes.js b/tests/baselines/reference/importDeclTypes.js new file mode 100644 index 0000000000000..640dd4a19b46b --- /dev/null +++ b/tests/baselines/reference/importDeclTypes.js @@ -0,0 +1,15 @@ +//// [tests/cases/compiler/importDeclTypes.ts] //// + +//// [index.d.ts] +export interface Foo { + bar: string; +} + +// This should error +//// [a.ts] +import { Foo } from "@types/foo-bar"; + + +//// [a.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/cases/compiler/importDeclTypes.ts b/tests/cases/compiler/importDeclTypes.ts new file mode 100644 index 0000000000000..01fcd0e7b6fc1 --- /dev/null +++ b/tests/cases/compiler/importDeclTypes.ts @@ -0,0 +1,9 @@ + +// @filename: /node_modules/@types/foo-bar/index.d.ts +export interface Foo { + bar: string; +} + +// This should error +// @filename: a.ts +import { Foo } from "@types/foo-bar"; From a686d613272d5998f923a72ca68ecce461f431f6 Mon Sep 17 00:00:00 2001 From: Noel Varanda Date: Tue, 16 May 2017 19:25:43 +0100 Subject: [PATCH 2/2] FIX-15540: Review changes - Replace `substr` with `startsWith` - move diagnostics message to more relevant place - Add `removePrefix` helper function --- src/compiler/checker.ts | 5 +++-- src/compiler/core.ts | 5 +++++ src/compiler/diagnosticMessages.json | 8 ++++---- tests/baselines/reference/importDeclTypes.errors.txt | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index cdc9642751ff0..e68beecaa0024 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1642,9 +1642,10 @@ namespace ts { return; } - if (moduleReference.substr(0, 7) === "@types/") { + if (startsWith(moduleReference, "@types/")) { const diag = Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1; - error(errorNode, diag, moduleReference.substr(7), moduleReference); + const withoutAtTypePrefix = removePrefix(moduleReference, "@types/"); + error(errorNode, diag, withoutAtTypePrefix, moduleReference); } const ambientModule = tryFindAmbientModule(moduleName, /*withAugmentations*/ true); diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 5e4afce98c8a8..a9ccab05f1b94 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1762,6 +1762,11 @@ namespace ts { return str.lastIndexOf(prefix, 0) === 0; } + /* @internal */ + export function removePrefix(str: string, prefix: string): string { + return startsWith(str, prefix) ? str.substr(prefix.length) : str; + } + /* @internal */ export function endsWith(str: string, suffix: string): boolean { const expectedPos = str.length - suffix.length; diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index be7228bc26b5f..b414abed5eab6 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2135,10 +2135,6 @@ "category": "Error", "code": 2710 }, - "Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": { - "category": "Error", - "code": 2711 - }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", @@ -3041,6 +3037,10 @@ "category": "Message", "code": 6136 }, + "Cannot import type declaration files. Consider importing '{0}' instead of '{1}'.": { + "category": "Error", + "code": 6137 + }, "Property '{0}' is declared but never used.": { "category": "Error", "code": 6138 diff --git a/tests/baselines/reference/importDeclTypes.errors.txt b/tests/baselines/reference/importDeclTypes.errors.txt index 75546059897b2..dfb8fcb6f485b 100644 --- a/tests/baselines/reference/importDeclTypes.errors.txt +++ b/tests/baselines/reference/importDeclTypes.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/a.ts(1,21): error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. +tests/cases/compiler/a.ts(1,21): error TS6137: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. ==== /node_modules/@types/foo-bar/index.d.ts (0 errors) ==== @@ -10,5 +10,5 @@ tests/cases/compiler/a.ts(1,21): error TS2711: Cannot import type declaration fi ==== tests/cases/compiler/a.ts (1 errors) ==== import { Foo } from "@types/foo-bar"; ~~~~~~~~~~~~~~~~ -!!! error TS2711: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. +!!! error TS6137: Cannot import type declaration files. Consider importing 'foo-bar' instead of '@types/foo-bar'. \ No newline at end of file