From 9f9825a4f062d9e6b61858acc8394a51653ef9ee Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Thu, 25 Feb 2021 15:21:41 -0800 Subject: [PATCH] Fix: checkAliasSymbol crash when checking for @deprecated It's possible that we shouldn't be creating symbol with no declarations from non-homomorphic mapped types, but for 4.2, the right fix is to make the @deprecated-check in checkAliasSymbol ensure that target.declarations is defined. --- src/compiler/checker.ts | 2 +- .../reference/importPropertyFromMappedType.js | 16 ++++++++++++++ .../importPropertyFromMappedType.symbols | 21 +++++++++++++++++++ .../importPropertyFromMappedType.types | 17 +++++++++++++++ .../compiler/importPropertyFromMappedType.ts | 11 ++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/importPropertyFromMappedType.js create mode 100644 tests/baselines/reference/importPropertyFromMappedType.symbols create mode 100644 tests/baselines/reference/importPropertyFromMappedType.types create mode 100644 tests/cases/compiler/importPropertyFromMappedType.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8f159f198fbb1..1ef57f9b843c9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -37124,7 +37124,7 @@ namespace ts { error(node, Diagnostics.Re_exporting_a_type_when_the_isolatedModules_flag_is_provided_requires_using_export_type); } - if (isImportSpecifier(node) && every(target.declarations, d => !!(getCombinedNodeFlags(d) & NodeFlags.Deprecated))) { + if (isImportSpecifier(node) && target.declarations?.every(d => !!(getCombinedNodeFlags(d) & NodeFlags.Deprecated))) { addDeprecatedSuggestion(node.name, target.declarations, symbol.escapedName as string); } } diff --git a/tests/baselines/reference/importPropertyFromMappedType.js b/tests/baselines/reference/importPropertyFromMappedType.js new file mode 100644 index 0000000000000..ec31897f0f20b --- /dev/null +++ b/tests/baselines/reference/importPropertyFromMappedType.js @@ -0,0 +1,16 @@ +//// [tests/cases/compiler/importPropertyFromMappedType.ts] //// + +//// [errors.d.ts] +export = createHttpError; +declare const createHttpError: createHttpError.NamedConstructors; +declare namespace createHttpError { + type NamedConstructors = { [P in 'NotFound']: unknown;} +} + +//// [main.ts] +import { NotFound } from './errors' + + +//// [main.js] +"use strict"; +exports.__esModule = true; diff --git a/tests/baselines/reference/importPropertyFromMappedType.symbols b/tests/baselines/reference/importPropertyFromMappedType.symbols new file mode 100644 index 0000000000000..2b847049346d3 --- /dev/null +++ b/tests/baselines/reference/importPropertyFromMappedType.symbols @@ -0,0 +1,21 @@ +=== tests/cases/compiler/errors.d.ts === +export = createHttpError; +>createHttpError : Symbol(createHttpError, Decl(errors.d.ts, 1, 13), Decl(errors.d.ts, 1, 65)) + +declare const createHttpError: createHttpError.NamedConstructors; +>createHttpError : Symbol(createHttpError, Decl(errors.d.ts, 1, 13), Decl(errors.d.ts, 1, 65)) +>createHttpError : Symbol(createHttpError, Decl(errors.d.ts, 1, 13), Decl(errors.d.ts, 1, 65)) +>NamedConstructors : Symbol(createHttpError.NamedConstructors, Decl(errors.d.ts, 2, 35)) + +declare namespace createHttpError { +>createHttpError : Symbol(createHttpError, Decl(errors.d.ts, 1, 13), Decl(errors.d.ts, 1, 65)) + + type NamedConstructors = { [P in 'NotFound']: unknown;} +>NamedConstructors : Symbol(NamedConstructors, Decl(errors.d.ts, 2, 35)) +>P : Symbol(P, Decl(errors.d.ts, 3, 33)) +} + +=== tests/cases/compiler/main.ts === +import { NotFound } from './errors' +>NotFound : Symbol(NotFound, Decl(main.ts, 0, 8)) + diff --git a/tests/baselines/reference/importPropertyFromMappedType.types b/tests/baselines/reference/importPropertyFromMappedType.types new file mode 100644 index 0000000000000..7cd7b0278aa92 --- /dev/null +++ b/tests/baselines/reference/importPropertyFromMappedType.types @@ -0,0 +1,17 @@ +=== tests/cases/compiler/errors.d.ts === +export = createHttpError; +>createHttpError : createHttpError.NamedConstructors + +declare const createHttpError: createHttpError.NamedConstructors; +>createHttpError : createHttpError.NamedConstructors +>createHttpError : any + +declare namespace createHttpError { + type NamedConstructors = { [P in 'NotFound']: unknown;} +>NamedConstructors : NamedConstructors +} + +=== tests/cases/compiler/main.ts === +import { NotFound } from './errors' +>NotFound : unknown + diff --git a/tests/cases/compiler/importPropertyFromMappedType.ts b/tests/cases/compiler/importPropertyFromMappedType.ts new file mode 100644 index 0000000000000..ad80aba2d0a50 --- /dev/null +++ b/tests/cases/compiler/importPropertyFromMappedType.ts @@ -0,0 +1,11 @@ +// # + +// @filename: errors.d.ts +export = createHttpError; +declare const createHttpError: createHttpError.NamedConstructors; +declare namespace createHttpError { + type NamedConstructors = { [P in 'NotFound']: unknown;} +} + +// @filename: main.ts +import { NotFound } from './errors'