From 8581a592bbaf5ddc89dfde423a7c9239d470a185 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 6 Apr 2021 18:46:22 +0300 Subject: [PATCH] fix(43347): fix crash occurred when export type to existing commonJs imported name (#43369) --- src/compiler/checker.ts | 7 ++-- ...monJsExportTypeDeclarationError.errors.txt | 31 +++++++++++++++++ .../commonJsExportTypeDeclarationError.js | 33 ++++++++++++++++++ ...commonJsExportTypeDeclarationError.symbols | 32 +++++++++++++++++ .../commonJsExportTypeDeclarationError.types | 34 +++++++++++++++++++ .../commonJsExportTypeDeclarationError.ts | 22 ++++++++++++ 6 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/commonJsExportTypeDeclarationError.errors.txt create mode 100644 tests/baselines/reference/commonJsExportTypeDeclarationError.js create mode 100644 tests/baselines/reference/commonJsExportTypeDeclarationError.symbols create mode 100644 tests/baselines/reference/commonJsExportTypeDeclarationError.types create mode 100644 tests/cases/compiler/commonJsExportTypeDeclarationError.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 273e6fc0e06..81673d7ac5f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -33805,11 +33805,14 @@ namespace ts { case SyntaxKind.SourceFile: return DeclarationSpaces.ExportType | DeclarationSpaces.ExportValue | DeclarationSpaces.ExportNamespace; case SyntaxKind.ExportAssignment: + case SyntaxKind.BinaryExpression: + const node = d as ExportAssignment | BinaryExpression; + const expression = isExportAssignment(node) ? node.expression : node.right; // Export assigned entity name expressions act as aliases and should fall through, otherwise they export values - if (!isEntityNameExpression((d as ExportAssignment).expression)) { + if (!isEntityNameExpression(expression)) { return DeclarationSpaces.ExportValue; } - d = (d as ExportAssignment).expression; + d = expression; // The below options all declare an Alias, which is allowed to merge with other values within the importing module. // falls through diff --git a/tests/baselines/reference/commonJsExportTypeDeclarationError.errors.txt b/tests/baselines/reference/commonJsExportTypeDeclarationError.errors.txt new file mode 100644 index 00000000000..f20ee3d36db --- /dev/null +++ b/tests/baselines/reference/commonJsExportTypeDeclarationError.errors.txt @@ -0,0 +1,31 @@ +tests/cases/compiler/types1.ts(2,17): error TS1110: Type expected. +tests/cases/compiler/types1.ts(3,1): error TS1005: '=' expected. +tests/cases/compiler/types2.ts(2,19): error TS1110: Type expected. +tests/cases/compiler/types3.ts(2,13): error TS2456: Type alias 'test' circularly references itself. + + +==== tests/cases/compiler/test.js (0 errors) ==== + module.exports = { + message: "" + } + +==== tests/cases/compiler/types1.ts (2 errors) ==== + import test from "./test"; + export type test + +!!! error TS1110: Type expected. + + +!!! error TS1005: '=' expected. +==== tests/cases/compiler/types2.ts (1 errors) ==== + import test from "./test"; + export type test = + +!!! error TS1110: Type expected. + +==== tests/cases/compiler/types3.ts (1 errors) ==== + import test from "./test"; + export type test = test; + ~~~~ +!!! error TS2456: Type alias 'test' circularly references itself. + \ No newline at end of file diff --git a/tests/baselines/reference/commonJsExportTypeDeclarationError.js b/tests/baselines/reference/commonJsExportTypeDeclarationError.js new file mode 100644 index 00000000000..61208330358 --- /dev/null +++ b/tests/baselines/reference/commonJsExportTypeDeclarationError.js @@ -0,0 +1,33 @@ +//// [tests/cases/compiler/commonJsExportTypeDeclarationError.ts] //// + +//// [test.js] +module.exports = { + message: "" +} + +//// [types1.ts] +import test from "./test"; +export type test + +//// [types2.ts] +import test from "./test"; +export type test = + +//// [types3.ts] +import test from "./test"; +export type test = test; + + +//// [test.js] +module.exports = { + message: "" +}; +//// [types1.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//// [types2.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//// [types3.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/tests/baselines/reference/commonJsExportTypeDeclarationError.symbols b/tests/baselines/reference/commonJsExportTypeDeclarationError.symbols new file mode 100644 index 00000000000..9fe5082da58 --- /dev/null +++ b/tests/baselines/reference/commonJsExportTypeDeclarationError.symbols @@ -0,0 +1,32 @@ +=== tests/cases/compiler/test.js === +module.exports = { +>module.exports : Symbol(module.exports, Decl(test.js, 0, 0)) +>module : Symbol(export=, Decl(test.js, 0, 0)) +>exports : Symbol(export=, Decl(test.js, 0, 0)) + + message: "" +>message : Symbol(message, Decl(test.js, 0, 18)) +} + +=== tests/cases/compiler/types1.ts === +import test from "./test"; +>test : Symbol(test, Decl(types1.ts, 0, 6), Decl(types1.ts, 0, 26)) + +export type test +>test : Symbol(test, Decl(types1.ts, 0, 26)) + +=== tests/cases/compiler/types2.ts === +import test from "./test"; +>test : Symbol(test, Decl(types2.ts, 0, 6), Decl(types2.ts, 0, 26)) + +export type test = +>test : Symbol(test, Decl(types2.ts, 0, 26)) + +=== tests/cases/compiler/types3.ts === +import test from "./test"; +>test : Symbol(test, Decl(types3.ts, 0, 6), Decl(types3.ts, 0, 26)) + +export type test = test; +>test : Symbol(test, Decl(types3.ts, 0, 26)) +>test : Symbol(test, Decl(types3.ts, 0, 26)) + diff --git a/tests/baselines/reference/commonJsExportTypeDeclarationError.types b/tests/baselines/reference/commonJsExportTypeDeclarationError.types new file mode 100644 index 00000000000..30295b22ba7 --- /dev/null +++ b/tests/baselines/reference/commonJsExportTypeDeclarationError.types @@ -0,0 +1,34 @@ +=== tests/cases/compiler/test.js === +module.exports = { +>module.exports = { message: ""} : { message: string; } +>module.exports : { message: string; } +>module : { exports: { message: string; }; } +>exports : { message: string; } +>{ message: ""} : { message: string; } + + message: "" +>message : string +>"" : "" +} + +=== tests/cases/compiler/types1.ts === +import test from "./test"; +>test : { message: string; } + +export type test +>test : any + +=== tests/cases/compiler/types2.ts === +import test from "./test"; +>test : { message: string; } + +export type test = +>test : any + +=== tests/cases/compiler/types3.ts === +import test from "./test"; +>test : { message: string; } + +export type test = test; +>test : any + diff --git a/tests/cases/compiler/commonJsExportTypeDeclarationError.ts b/tests/cases/compiler/commonJsExportTypeDeclarationError.ts new file mode 100644 index 00000000000..4de3177256e --- /dev/null +++ b/tests/cases/compiler/commonJsExportTypeDeclarationError.ts @@ -0,0 +1,22 @@ +// @esModuleInterop: true +// @checkJs: true +// @module: commonjs +// @target: es5 +// @outDir: ./out + +// @Filename: ./test.js +module.exports = { + message: "" +} + +// @Filename: ./types1.ts +import test from "./test"; +export type test + +// @Filename: ./types2.ts +import test from "./test"; +export type test = + +// @Filename: ./types3.ts +import test from "./test"; +export type test = test;