From 74dfc0228ce28edb901b3b4b88e99570d02b5a71 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Mon, 24 Feb 2020 13:11:47 +1100 Subject: [PATCH] Update to TypeScript 3.8 In regards to top-level-await, TypeScript will throw if the input does not look like a module (as top-level-await is not supported in scripts). TypeScript recommends adding `export {};` to the file, which is the current idomatic way of identifying a module as a module that otherwise has no imports or exports. Resolves #3937 Resolves #3301 Resolves #3391 --- cli/js/compiler_util.ts | 2 -- cli/tests/integration_tests.rs | 46 ++++++++++++++++++++++++++++++ cli/tests/lib_ref.ts | 2 ++ cli/tests/lib_runtime_api.ts | 2 ++ cli/tests/lock_write_fetch.ts | 2 ++ cli/tests/subdir/tla.ts | 1 + cli/tests/top_level_await.ts | 1 + cli/tests/top_level_for_await.ts | 2 ++ deno_typescript/compiler_main.js | 38 ++++++++++++------------ deno_typescript/lib.rs | 2 ++ deno_typescript/typescript | 2 +- std/encoding/yaml/loader/loader.ts | 1 - third_party | 2 +- 13 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 cli/tests/subdir/tla.ts diff --git a/cli/js/compiler_util.ts b/cli/js/compiler_util.ts index a28e2d109d6de..4f517f088bb42 100644 --- a/cli/js/compiler_util.ts +++ b/cli/js/compiler_util.ts @@ -289,8 +289,6 @@ export const ignoredDiagnostics = [ // TS1103: 'for-await-of' statement is only allowed within an async function // or async generator. 1103, - // TS1308: 'await' expression is only allowed within an async function. - 1308, // TS2691: An import path cannot end with a '.ts' extension. Consider // importing 'bad-module' instead. 2691, diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 5429170c71405..3a856c1eb3eb2 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -326,6 +326,52 @@ fn bundle_single_module() { assert_eq!(output.stderr, b""); } + +#[test] +fn bundle_tla() { + use tempfile::TempDir; + + // First we have to generate a bundle of some module that has exports. + let tla_import = + util::root_path().join("cli/tests/subdir/tla.ts"); + assert!(tla_import.is_file()); + let t = TempDir::new().expect("tempdir fail"); + let bundle = t.path().join("tla.bundle.js"); + let mut deno = util::deno_cmd() + .current_dir(util::root_path()) + .arg("bundle") + .arg(tla_import) + .arg(&bundle) + .spawn() + .expect("failed to spawn script"); + let status = deno.wait().expect("failed to wait for the child process"); + assert!(status.success()); + assert!(bundle.is_file()); + + // Now we try to use that bundle from another module. + let test = t.path().join("test.js"); + std::fs::write( + &test, + " + import { foo } from \"./tla.bundle.js\"; + console.log(foo); ", + ) + .expect("error writing file"); + + let output = util::deno_cmd() + .current_dir(util::root_path()) + .arg("run") + .arg(&test) + .output() + .expect("failed to spawn script"); + // check the output of the test.ts program. + assert!(std::str::from_utf8(&output.stdout) + .unwrap() + .trim() + .ends_with("Hello")); + assert_eq!(output.stderr, b""); +} + // TODO(#2933): Rewrite this test in rust. #[test] fn repl_test() { diff --git a/cli/tests/lib_ref.ts b/cli/tests/lib_ref.ts index 9fc84ea3e7cf8..27f71e26265c6 100644 --- a/cli/tests/lib_ref.ts +++ b/cli/tests/lib_ref.ts @@ -11,3 +11,5 @@ const [errors, program] = await Deno.compile( console.log(errors); console.log(Object.keys(program)); + +export {}; diff --git a/cli/tests/lib_runtime_api.ts b/cli/tests/lib_runtime_api.ts index 848b523a10af5..24c8863315e5a 100644 --- a/cli/tests/lib_runtime_api.ts +++ b/cli/tests/lib_runtime_api.ts @@ -10,3 +10,5 @@ const [errors, program] = await Deno.compile( console.log(errors); console.log(Object.keys(program)); + +export {}; diff --git a/cli/tests/lock_write_fetch.ts b/cli/tests/lock_write_fetch.ts index 7d597724dec95..b92da1d4e9a5d 100644 --- a/cli/tests/lock_write_fetch.ts +++ b/cli/tests/lock_write_fetch.ts @@ -42,3 +42,5 @@ const runCode = (await runProc.status()).code; console.log(`run code: ${runCode}`); Deno.removeSync("./lock_write_fetch.json"); + +export {}; diff --git a/cli/tests/subdir/tla.ts b/cli/tests/subdir/tla.ts new file mode 100644 index 0000000000000..713dbfca0f8e3 --- /dev/null +++ b/cli/tests/subdir/tla.ts @@ -0,0 +1 @@ +export const foo = await Promise.resolve("Hello"); diff --git a/cli/tests/top_level_await.ts b/cli/tests/top_level_await.ts index 65de253eae40d..7099f9feefe86 100644 --- a/cli/tests/top_level_await.ts +++ b/cli/tests/top_level_await.ts @@ -1,3 +1,4 @@ const buf: Uint8Array = await Deno.readFile("hello.txt"); const n: number = await Deno.stdout.write(buf); console.log(`\n\nwrite ${n}`); +export {}; diff --git a/cli/tests/top_level_for_await.ts b/cli/tests/top_level_for_await.ts index 9179322d78f4b..fe72333e4aa6f 100644 --- a/cli/tests/top_level_for_await.ts +++ b/cli/tests/top_level_for_await.ts @@ -8,3 +8,5 @@ async function* asyncGenerator(): AsyncIterableIterator { for await (const num of asyncGenerator()) { console.log(num); } + +export {}; diff --git a/deno_typescript/compiler_main.js b/deno_typescript/compiler_main.js index 013d6e157d1f1..6f2da5b57c5f3 100644 --- a/deno_typescript/compiler_main.js +++ b/deno_typescript/compiler_main.js @@ -133,7 +133,6 @@ class Host { */ readFile(_fileName) { unreachable(); - return undefined; } useCaseSensitiveFileNames() { @@ -180,33 +179,22 @@ class Host { } // This looks up any modules that have been mapped to internal names - if (moduleMap.has(fileName)) { - fileName = moduleMap.get(fileName); - } + const moduleUrl = moduleMap.has(fileName) + ? moduleMap.get(fileName) + : fileName; - const { sourceCode, moduleName } = dispatch("loadModule", { - moduleUrl: fileName, + const { sourceCode } = dispatch("loadModule", { + moduleUrl, languageVersion, shouldCreateNewSourceFile }); - // If we match the external specifier regex, we will then create an internal - // specifier and then use that when creating the source file - let internalModuleName = moduleName; - const result = externalSpecifierRegEx.exec(moduleName); - if (result) { - const [, specifier] = result; - const internalSpecifier = `$deno$${specifier}`; - moduleMap.set(internalSpecifier, moduleName); - internalModuleName = internalSpecifier; - } - const sourceFile = ts.createSourceFile( - internalModuleName, + fileName, sourceCode, languageVersion ); - sourceFile.moduleName = internalModuleName; + sourceFile.moduleName = fileName; return sourceFile; } @@ -246,7 +234,6 @@ class Host { _shouldCreateNewSourceFile ) { unreachable(); - return undefined; } /** @@ -279,6 +266,17 @@ class Host { /** @type {ts.ResolvedModule[]} */ const r = resolvedNames.map(resolvedFileName => { const extension = getExtension(resolvedFileName); + if (!moduleMap.has(resolvedFileName)) { + // If we match the external specifier regex, we will then create an internal + // specifier and then use that when creating the source file + const result = externalSpecifierRegEx.exec(resolvedFileName); + if (result) { + const [, specifier] = result; + const internalSpecifier = `$deno$${specifier}`; + moduleMap.set(internalSpecifier, resolvedFileName); + resolvedFileName = internalSpecifier; + } + } return { resolvedFileName, extension }; }); return r; diff --git a/deno_typescript/lib.rs b/deno_typescript/lib.rs index c825277e37058..ad2df46d7a184 100644 --- a/deno_typescript/lib.rs +++ b/deno_typescript/lib.rs @@ -290,6 +290,8 @@ pub fn get_asset(name: &str) -> Option<&'static str> { "lib.es2019.object.d.ts" => inc!("lib.es2019.object.d.ts"), "lib.es2019.string.d.ts" => inc!("lib.es2019.string.d.ts"), "lib.es2019.symbol.d.ts" => inc!("lib.es2019.symbol.d.ts"), + "lib.es2020.bigint.d.ts" => inc!("lib.es2020.bigint.d.ts"), + "lib.es2020.promise.d.ts" => inc!("lib.es2020.promise.d.ts"), "lib.es2020.string.d.ts" => inc!("lib.es2020.string.d.ts"), "lib.es2020.symbol.wellknown.d.ts" => { inc!("lib.es2020.symbol.wellknown.d.ts") diff --git a/deno_typescript/typescript b/deno_typescript/typescript index 7cf6c70d90b60..af614ccea19e8 160000 --- a/deno_typescript/typescript +++ b/deno_typescript/typescript @@ -1 +1 @@ -Subproject commit 7cf6c70d90b60e962db417d80290288eb786b5fd +Subproject commit af614ccea19e844142c8e6b0fdd70ccfdfcfa0db diff --git a/std/encoding/yaml/loader/loader.ts b/std/encoding/yaml/loader/loader.ts index 7db72a01dc52b..1ab4fc7f5b5ea 100644 --- a/std/encoding/yaml/loader/loader.ts +++ b/std/encoding/yaml/loader/loader.ts @@ -3,7 +3,6 @@ // Copyright 2011-2015 by Vitaly Puzrin. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -/* eslint-disable no-conditional-assignment */ /* eslint-disable max-len */ import { YAMLError } from "../error.ts"; diff --git a/third_party b/third_party index 9ab7948049d96..b1103a02e8112 160000 --- a/third_party +++ b/third_party @@ -1 +1 @@ -Subproject commit 9ab7948049d96bdd3af67323f758b81a9e4cbc27 +Subproject commit b1103a02e8112a20126c84d2d4751ed1302c8ade