From 18b8625ef81f2a113f6b3fb4a9da0d770beb7af9 Mon Sep 17 00:00:00 2001 From: Masahiro Wakame Date: Tue, 19 Mar 2019 01:18:27 +0900 Subject: [PATCH] fix error on globalThis type extend (#30460) * Add test for extend globalThis * Fix compile aborting --- src/compiler/checker.ts | 2 +- tests/baselines/reference/extendGlobalThis.js | 24 ++++++++++++ .../reference/extendGlobalThis.symbols | 30 +++++++++++++++ .../reference/extendGlobalThis.types | 37 +++++++++++++++++++ tests/cases/compiler/extendGlobalThis.ts | 14 +++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/extendGlobalThis.js create mode 100644 tests/baselines/reference/extendGlobalThis.symbols create mode 100644 tests/baselines/reference/extendGlobalThis.types create mode 100644 tests/cases/compiler/extendGlobalThis.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 12dccd8bc64..8ef07866299 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -27980,8 +27980,8 @@ namespace ts { // The following checks only apply on a non-ambient instantiated module declaration. if (symbol.flags & SymbolFlags.ValueModule - && symbol.declarations.length > 1 && !inAmbientContext + && symbol.declarations.length > 1 && isInstantiatedModule(node, !!compilerOptions.preserveConstEnums || !!compilerOptions.isolatedModules)) { const firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); if (firstNonAmbientClassOrFunc) { diff --git a/tests/baselines/reference/extendGlobalThis.js b/tests/baselines/reference/extendGlobalThis.js new file mode 100644 index 00000000000..c8b73a0c39f --- /dev/null +++ b/tests/baselines/reference/extendGlobalThis.js @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/extendGlobalThis.ts] //// + +//// [extension.d.ts] +declare global { + namespace globalThis { + var test: string; + } +} + +export {} + +//// [index.ts] +import "./extention"; + +globalThis.tests = "a-b"; +console.log(globalThis.test.split("-")); + + +//// [index.js] +"use strict"; +exports.__esModule = true; +require("./extention"); +globalThis.tests = "a-b"; +console.log(globalThis.test.split("-")); diff --git a/tests/baselines/reference/extendGlobalThis.symbols b/tests/baselines/reference/extendGlobalThis.symbols new file mode 100644 index 00000000000..944d7b23715 --- /dev/null +++ b/tests/baselines/reference/extendGlobalThis.symbols @@ -0,0 +1,30 @@ +=== tests/cases/compiler/extension.d.ts === +declare global { +>global : Symbol(global, Decl(extension.d.ts, 0, 0)) + + namespace globalThis { +>globalThis : Symbol(globalThis) + + var test: string; +>test : Symbol(test, Decl(extension.d.ts, 2, 11)) + } +} + +export {} + +=== tests/cases/compiler/index.ts === +import "./extention"; + +globalThis.tests = "a-b"; +>globalThis : Symbol(globalThis) + +console.log(globalThis.test.split("-")); +>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) +>globalThis.test.split : Symbol(String.split, Decl(lib.es5.d.ts, --, --)) +>globalThis.test : Symbol(test, Decl(extension.d.ts, 2, 11)) +>globalThis : Symbol(globalThis) +>test : Symbol(test, Decl(extension.d.ts, 2, 11)) +>split : Symbol(String.split, Decl(lib.es5.d.ts, --, --)) + diff --git a/tests/baselines/reference/extendGlobalThis.types b/tests/baselines/reference/extendGlobalThis.types new file mode 100644 index 00000000000..88fe0462d65 --- /dev/null +++ b/tests/baselines/reference/extendGlobalThis.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/extension.d.ts === +declare global { +>global : typeof global + + namespace globalThis { +>globalThis : typeof globalThis + + var test: string; +>test : string + } +} + +export {} + +=== tests/cases/compiler/index.ts === +import "./extention"; + +globalThis.tests = "a-b"; +>globalThis.tests = "a-b" : "a-b" +>globalThis.tests : any +>globalThis : typeof globalThis +>tests : any +>"a-b" : "a-b" + +console.log(globalThis.test.split("-")); +>console.log(globalThis.test.split("-")) : void +>console.log : (message?: any, ...optionalParams: any[]) => void +>console : Console +>log : (message?: any, ...optionalParams: any[]) => void +>globalThis.test.split("-") : string[] +>globalThis.test.split : (separator: string | RegExp, limit?: number) => string[] +>globalThis.test : string +>globalThis : typeof globalThis +>test : string +>split : (separator: string | RegExp, limit?: number) => string[] +>"-" : "-" + diff --git a/tests/cases/compiler/extendGlobalThis.ts b/tests/cases/compiler/extendGlobalThis.ts new file mode 100644 index 00000000000..1ad9f0552b4 --- /dev/null +++ b/tests/cases/compiler/extendGlobalThis.ts @@ -0,0 +1,14 @@ +// @filename: extension.d.ts +declare global { + namespace globalThis { + var test: string; + } +} + +export {} + +// @filename: index.ts +import "./extention"; + +globalThis.tests = "a-b"; +console.log(globalThis.test.split("-"));