From 783533d2e354ad73356d7517b26293e48c10fc17 Mon Sep 17 00:00:00 2001 From: Kenta Moriuchi Date: Tue, 30 Apr 2024 05:43:05 +0900 Subject: [PATCH] FUTURE: remove import assertions support for JavaScript (#23541) Ref #17944, https://github.com/swc-project/swc/issues/8893 TypeScript removes the `assert` keywords in the transpile, so this PR only works for JavaScript files --- cli/args/mod.rs | 2 +- cli/lsp/config.rs | 3 ++- cli/main.rs | 11 ++++++++++- tests/integration/run_tests.rs | 10 ---------- .../future/import_assertions/__test__.jsonc | 16 ++++++++++++++++ tests/specs/future/import_assertions/error.out | 4 ++++ tests/specs/future/import_assertions/main.js | 2 ++ tests/specs/future/import_assertions/main.json | 3 +++ tests/specs/future/import_assertions/success.out | 1 + tests/testdata/npm/import_json/main.js | 2 +- tests/testdata/run/deno_futures_env.ts | 3 --- tests/testdata/subdir/mod7.js | 2 +- tests/testdata/subdir/mod8.js | 2 +- tools/napi/generate_symbols_lists.js | 2 +- 14 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 tests/specs/future/import_assertions/__test__.jsonc create mode 100644 tests/specs/future/import_assertions/error.out create mode 100644 tests/specs/future/import_assertions/main.js create mode 100644 tests/specs/future/import_assertions/main.json create mode 100644 tests/specs/future/import_assertions/success.out delete mode 100644 tests/testdata/run/deno_futures_env.ts diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 1d8b06f49d..b77a8afdbf 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -110,7 +110,7 @@ pub static DENO_DISABLE_PEDANTIC_NODE_WARNINGS: Lazy = Lazy::new(|| { .is_some() }); -static DENO_FUTURE: Lazy = +pub static DENO_FUTURE: Lazy = Lazy::new(|| std::env::var("DENO_FUTURE").ok().is_some()); pub fn jsr_url() -> &'static Url { diff --git a/cli/lsp/config.rs b/cli/lsp/config.rs index 5ddc41cb22..e5703a21a4 100644 --- a/cli/lsp/config.rs +++ b/cli/lsp/config.rs @@ -4,6 +4,7 @@ use super::logging::lsp_log; use crate::args::ConfigFile; use crate::args::FmtOptions; use crate::args::LintOptions; +use crate::args::DENO_FUTURE; use crate::cache::FastInsecureHasher; use crate::file_fetcher::FileFetcher; use crate::lsp::logging::lsp_warn; @@ -1324,7 +1325,7 @@ impl ConfigData { .as_ref() .map(|c| c.has_unstable("byonm")) .unwrap_or(false) - || (std::env::var("DENO_FUTURE").is_ok() + || (*DENO_FUTURE && package_json.is_some() && config_file .as_ref() diff --git a/cli/main.rs b/cli/main.rs index 142ae017ce..3b103e7807 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -29,6 +29,7 @@ mod worker; use crate::args::flags_from_vec; use crate::args::DenoSubcommand; use crate::args::Flags; +use crate::args::DENO_FUTURE; use crate::util::display; use crate::util::v8::get_v8_flags_from_env; use crate::util::v8::init_v8_flags; @@ -389,7 +390,15 @@ fn resolve_flags_and_init( // Using same default as VSCode: // https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214 DenoSubcommand::Lsp => vec!["--max-old-space-size=3072".to_string()], - _ => vec![], + _ => { + if *DENO_FUTURE { + // deno_ast removes TypeScript `assert` keywords, so this flag only affects JavaScript + // TODO(petamoriken): Need to check TypeScript `assert` keywords in deno_ast + vec!["--no-harmony-import-assertions".to_string()] + } else { + vec![] + } + } }; init_v8_flags(&default_v8_flags, &flags.v8_flags, get_v8_flags_from_env()); diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index e28251b23d..e92fd36276 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -1694,16 +1694,6 @@ fn type_directives_js_main() { assert_not_contains!(output.combined_output(), "type_reference.d.ts"); } -#[test] -fn test_deno_futures_env() { - let context = TestContextBuilder::new().add_future_env_vars().build(); - let output = context - .new_command() - .args("run --quiet --reload run/deno_futures_env.ts") - .run(); - output.assert_exit_code(0); -} - itest!(type_directives_redirect { args: "run --reload --check run/type_directives_redirect.ts", output: "run/type_directives_redirect.ts.out", diff --git a/tests/specs/future/import_assertions/__test__.jsonc b/tests/specs/future/import_assertions/__test__.jsonc new file mode 100644 index 0000000000..a1e759c75e --- /dev/null +++ b/tests/specs/future/import_assertions/__test__.jsonc @@ -0,0 +1,16 @@ +{ + "steps": [ + { + "args": "run main.js", + "output": "error.out", + "exitCode": 1, + "envs": { + "DENO_FUTURE": "1" + } + }, + { + "args": "run main.js", + "output": "success.out" + } + ] +} diff --git a/tests/specs/future/import_assertions/error.out b/tests/specs/future/import_assertions/error.out new file mode 100644 index 0000000000..cef663f35f --- /dev/null +++ b/tests/specs/future/import_assertions/error.out @@ -0,0 +1,4 @@ +error: Uncaught SyntaxError: Unexpected identifier 'assert' +import foo from "./main.json" assert { type: "json" }; + ^ + at (file:///[WILDCARD]/main.js:[WILDCARD]) diff --git a/tests/specs/future/import_assertions/main.js b/tests/specs/future/import_assertions/main.js new file mode 100644 index 0000000000..9b4c4d0360 --- /dev/null +++ b/tests/specs/future/import_assertions/main.js @@ -0,0 +1,2 @@ +import foo from "./main.json" assert { type: "json" }; +console.log(foo); diff --git a/tests/specs/future/import_assertions/main.json b/tests/specs/future/import_assertions/main.json new file mode 100644 index 0000000000..abdd5202bd --- /dev/null +++ b/tests/specs/future/import_assertions/main.json @@ -0,0 +1,3 @@ +{ + "foo": "foo" +} diff --git a/tests/specs/future/import_assertions/success.out b/tests/specs/future/import_assertions/success.out new file mode 100644 index 0000000000..70ec274d9a --- /dev/null +++ b/tests/specs/future/import_assertions/success.out @@ -0,0 +1 @@ +{ foo: "foo" } diff --git a/tests/testdata/npm/import_json/main.js b/tests/testdata/npm/import_json/main.js index b752bdef8b..ac6cee9a83 100644 --- a/tests/testdata/npm/import_json/main.js +++ b/tests/testdata/npm/import_json/main.js @@ -1,4 +1,4 @@ -import json from "npm:@denotest/binary-package@1/package.json" assert { +import json from "npm:@denotest/binary-package@1/package.json" with { type: "json", }; console.log(json); diff --git a/tests/testdata/run/deno_futures_env.ts b/tests/testdata/run/deno_futures_env.ts deleted file mode 100644 index 21f76e3675..0000000000 --- a/tests/testdata/run/deno_futures_env.ts +++ /dev/null @@ -1,3 +0,0 @@ -if (typeof window !== "undefined") { - throw new Error("Window global available"); -} diff --git a/tests/testdata/subdir/mod7.js b/tests/testdata/subdir/mod7.js index 2bd4b5eb78..e71ced92e9 100644 --- a/tests/testdata/subdir/mod7.js +++ b/tests/testdata/subdir/mod7.js @@ -1,3 +1,3 @@ -import json1 from "./json_1.json" assert { type: "json" }; +import json1 from "./json_1.json" with { type: "json" }; console.log(json1); diff --git a/tests/testdata/subdir/mod8.js b/tests/testdata/subdir/mod8.js index 5bf7a49a88..ed41d992c0 100644 --- a/tests/testdata/subdir/mod8.js +++ b/tests/testdata/subdir/mod8.js @@ -1,3 +1,3 @@ -import json3 from "./json_3.json" assert { type: "json" }; +import json3 from "./json_3.json" with { type: "json" }; console.log(json3); diff --git a/tools/napi/generate_symbols_lists.js b/tools/napi/generate_symbols_lists.js index 96636ffd7d..11cf1c434a 100755 --- a/tools/napi/generate_symbols_lists.js +++ b/tools/napi/generate_symbols_lists.js @@ -1,7 +1,7 @@ #!/usr/bin/env -S deno run --allow-read --allow-write // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -import exports from "../../cli/napi/sym/symbol_exports.json" assert { +import exports from "../../cli/napi/sym/symbol_exports.json" with { type: "json", };