Skip to content

Commit

Permalink
Update to TypeScript 3.8
Browse files Browse the repository at this point in the history
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 denoland#3937
Resolves denoland#3301
Resolves denoland#3391
  • Loading branch information
kitsonk committed Feb 24, 2020
1 parent 4e1abb4 commit 74dfc02
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 25 deletions.
2 changes: 0 additions & 2 deletions cli/js/compiler_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/lib_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ const [errors, program] = await Deno.compile(

console.log(errors);
console.log(Object.keys(program));

export {};
2 changes: 2 additions & 0 deletions cli/tests/lib_runtime_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ const [errors, program] = await Deno.compile(

console.log(errors);
console.log(Object.keys(program));

export {};
2 changes: 2 additions & 0 deletions cli/tests/lock_write_fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ const runCode = (await runProc.status()).code;
console.log(`run code: ${runCode}`);

Deno.removeSync("./lock_write_fetch.json");

export {};
1 change: 1 addition & 0 deletions cli/tests/subdir/tla.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = await Promise.resolve("Hello");
1 change: 1 addition & 0 deletions cli/tests/top_level_await.ts
Original file line number Diff line number Diff line change
@@ -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 {};
2 changes: 2 additions & 0 deletions cli/tests/top_level_for_await.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ async function* asyncGenerator(): AsyncIterableIterator<number> {
for await (const num of asyncGenerator()) {
console.log(num);
}

export {};
38 changes: 18 additions & 20 deletions deno_typescript/compiler_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class Host {
*/
readFile(_fileName) {
unreachable();
return undefined;
}

useCaseSensitiveFileNames() {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -246,7 +234,6 @@ class Host {
_shouldCreateNewSourceFile
) {
unreachable();
return undefined;
}

/**
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions deno_typescript/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion deno_typescript/typescript
Submodule typescript updated 2676 files
1 change: 0 additions & 1 deletion std/encoding/yaml/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion third_party
Submodule third_party updated 1146 files

0 comments on commit 74dfc02

Please sign in to comment.