From 00b655add1c83a3924f1e86b51689aa1ed8e5d05 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Thu, 17 Jan 2019 09:57:15 +1100 Subject: [PATCH] Add globalThis definition to runtime (#1534) --- js/globals_test.ts | 17 +++++++++++++++++ js/unit_tests.ts | 1 + tools/ts_library_builder/build_library.ts | 4 ++++ tools/ts_library_builder/test.ts | 13 +++++++------ 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 js/globals_test.ts diff --git a/js/globals_test.ts b/js/globals_test.ts new file mode 100644 index 0000000000..e40718f28b --- /dev/null +++ b/js/globals_test.ts @@ -0,0 +1,17 @@ +import { test, assert } from "./test_util.ts"; + +test(function globalThisExists() { + assert(globalThis != null); +}); + +test(function windowExists() { + assert(window != null); +}); + +test(function windowWindowExists() { + assert(window.window === window); +}); + +test(function globalThisEqualsWindow() { + assert(globalThis === window); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index e02d5e3037..35050fc846 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -18,6 +18,7 @@ import "./fetch_test.ts"; // import "./file_test.ts"; import "./files_test.ts"; import "./form_data_test.ts"; +import "./globals_test.ts"; import "./headers_test.ts"; import "./make_temp_dir_test.ts"; import "./metrics_test.ts"; diff --git a/tools/ts_library_builder/build_library.ts b/tools/ts_library_builder/build_library.ts index 00e9425407..0717de9874 100644 --- a/tools/ts_library_builder/build_library.ts +++ b/tools/ts_library_builder/build_library.ts @@ -156,6 +156,10 @@ export function mergeGlobal({ // Declare the global variable addVariableDeclaration(targetSourceFile, globalVarName, interfaceName, true); + // `globalThis` accesses the global scope and is defined here: + // https://github.com/tc39/proposal-global + addVariableDeclaration(targetSourceFile, "globalThis", interfaceName, true); + // Add self reference to the global variable addInterfaceProperty(interfaceDeclaration, globalVarName, interfaceName); diff --git a/tools/ts_library_builder/test.ts b/tools/ts_library_builder/test.ts index b5883433a3..ad0d7cfa14 100644 --- a/tools/ts_library_builder/test.ts +++ b/tools/ts_library_builder/test.ts @@ -140,24 +140,25 @@ test(function buildLibraryMerge() { assertEqual(targetSourceFile.getInterfaces().length, 1); const variableDeclarations = targetSourceFile.getVariableDeclarations(); assertEqual(variableDeclarations[0].getType().getText(), `FooBar`); - assertEqual(variableDeclarations[1].getType().getText(), `moduleC.Bar`); + assertEqual(variableDeclarations[1].getType().getText(), `FooBar`); + assertEqual(variableDeclarations[2].getType().getText(), `moduleC.Bar`); assertEqual( - variableDeclarations[2].getType().getText(), + variableDeclarations[3].getType().getText(), `typeof moduleC.qat` ); assertEqual( - variableDeclarations[3].getType().getText(), + variableDeclarations[4].getType().getText(), `typeof moduleE.process` ); assertEqual( - variableDeclarations[4].getType().getText(), + variableDeclarations[5].getType().getText(), `typeof moduleD.reprocess` ); assertEqual( - variableDeclarations[5].getType().getText(), + variableDeclarations[6].getType().getText(), `typeof moduleC.Bar` ); - assertEqual(variableDeclarations.length, 6); + assertEqual(variableDeclarations.length, 7); const typeAliases = targetSourceFile.getTypeAliases(); assertEqual(typeAliases[0].getName(), "Bar"); assertEqual(typeAliases[0].getType().getText(), "moduleC.Bar");