diff options
Diffstat (limited to 'cli')
21 files changed, 131 insertions, 12 deletions
diff --git a/cli/config_file.rs b/cli/config_file.rs index 363a215eb..5cd76eaca 100644 --- a/cli/config_file.rs +++ b/cli/config_file.rs @@ -104,6 +104,7 @@ pub const IGNORED_COMPILER_OPTIONS: &[&str] = &[ "paths", "preserveConstEnums", "reactNamespace", + "resolveJsonModule", "rootDir", "rootDirs", "skipLibCheck", diff --git a/cli/emit.rs b/cli/emit.rs index c9a90abb7..3aa2c4794 100644 --- a/cli/emit.rs +++ b/cli/emit.rs @@ -153,6 +153,7 @@ pub(crate) fn get_ts_config( "isolatedModules": true, "lib": lib, "module": "esnext", + "resolveJsonModule": true, "strict": true, "target": "esnext", "tsBuildInfoFile": "deno:///.tsbuildinfo", @@ -186,6 +187,7 @@ pub(crate) fn get_ts_config( "jsx": "react", "jsxFactory": "React.createElement", "jsxFragmentFactory": "React.Fragment", + "resolveJsonModule": true, })), ConfigType::RuntimeEmit { tsc_emit } => { let mut ts_config = TsConfig::new(json!({ @@ -403,14 +405,15 @@ pub(crate) fn check_and_maybe_emit( log::debug!("module missing, skipping emit for {}", specifier); continue; }; - // Sometimes if `tsc` sees a CommonJS file it will _helpfully_ output it - // to ESM, which we don't really want to do unless someone has enabled - // check_js. - if !check_js - && matches!( - media_type, - MediaType::JavaScript | MediaType::Cjs | MediaType::Mjs - ) + // Sometimes if `tsc` sees a CommonJS file or a JSON module, it will + // _helpfully_ output it, which we don't really want to do unless + // someone has enabled check_js. + if matches!(media_type, MediaType::Json) + || (!check_js + && matches!( + media_type, + MediaType::JavaScript | MediaType::Cjs | MediaType::Mjs + )) { log::debug!("skipping emit for {}", specifier); continue; @@ -429,7 +432,10 @@ pub(crate) fn check_and_maybe_emit( MediaType::Dts | MediaType::Dcts | MediaType::Dmts => { cache.set(CacheType::Declaration, &specifier, emit.data)?; } - _ => unreachable!(), + _ => unreachable!( + "unexpected media_type {} {}", + emit.media_type, specifier + ), } } } diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index aa465cf48..e9e446918 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -569,6 +569,7 @@ impl Inner { "lib": ["deno.ns", "deno.window"], "module": "esnext", "noEmit": true, + "resolveJsonModule": true, "strict": true, "target": "esnext", "useDefineForClassFields": true, diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 1a0f61e71..d82f8d017 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -31,6 +31,7 @@ use deno_core::url::Url; use deno_core::CompiledWasmModuleStore; use deno_core::ModuleSource; use deno_core::ModuleSpecifier; +use deno_core::ModuleType; use deno_core::SharedArrayBufferStore; use deno_graph::create_graph; use deno_graph::Dependency; @@ -68,6 +69,7 @@ pub struct ProcState(Arc<Inner>); enum ModuleEntry { Module { code: String, + media_type: MediaType, dependencies: BTreeMap<String, Dependency>, }, Error(ModuleGraphError), @@ -584,6 +586,7 @@ impl ProcState { | MediaType::Unknown | MediaType::Cjs | MediaType::Mjs + | MediaType::Json ) { module.maybe_source().unwrap_or("").to_string() // The emit may also be missing when a declaration file is in the @@ -602,7 +605,11 @@ impl ProcState { module.maybe_dependencies().cloned().unwrap_or_default(); graph_data.modules.insert( specifier.clone(), - ModuleEntry::Module { code, dependencies }, + ModuleEntry::Module { + code, + dependencies, + media_type: *media_type, + }, ); if let Some(dependencies) = module.maybe_dependencies() { for dep in dependencies.values() { @@ -724,10 +731,16 @@ impl ProcState { _ => &specifier, }; match graph_data.modules.get(found_specifier) { - Some(ModuleEntry::Module { code, .. }) => Ok(ModuleSource { + Some(ModuleEntry::Module { + code, media_type, .. + }) => Ok(ModuleSource { code: code.clone(), module_url_specified: specifier.to_string(), module_url_found: found_specifier.to_string(), + module_type: match media_type { + MediaType::Json => ModuleType::Json, + _ => ModuleType::JavaScript, + }, }), _ => Err(anyhow!( "Loading unprepared module: {}", diff --git a/cli/standalone.rs b/cli/standalone.rs index 47b0e2aa8..d7fbb3106 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -164,6 +164,7 @@ impl ModuleLoader for EmbeddedModuleLoader { Ok(deno_core::ModuleSource { code, + module_type: deno_core::ModuleType::JavaScript, module_url_specified: module_specifier.to_string(), module_url_found: module_specifier.to_string(), }) diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index b6929b58a..c04e4731c 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -2407,3 +2407,36 @@ fn issue12807() { .unwrap(); assert!(status.success()); } + +itest!(import_assertions_static_import { + args: "run --allow-read import_assertions/static_import.ts", + output: "import_assertions/static_import.out", +}); + +itest!(import_assertions_static_export { + args: "run --allow-read import_assertions/static_export.ts", + output: "import_assertions/static_export.out", +}); + +itest!(import_assertions_static_error { + args: "run --allow-read import_assertions/static_error.ts", + output: "import_assertions/static_error.out", + exit_code: 1, +}); + +itest!(import_assertions_dynamic_import { + args: "run --allow-read import_assertions/dynamic_import.ts", + output: "import_assertions/dynamic_import.out", +}); + +itest!(import_assertions_dynamic_error { + args: "run --allow-read import_assertions/dynamic_error.ts", + output: "import_assertions/dynamic_error.out", + exit_code: 1, +}); + +itest!(import_assertions_type_check { + args: "run --allow-read import_assertions/type_check.ts", + output: "import_assertions/type_check.out", + exit_code: 1, +}); diff --git a/cli/tests/testdata/import_assertions/data.json b/cli/tests/testdata/import_assertions/data.json new file mode 100644 index 000000000..37b3ee1e0 --- /dev/null +++ b/cli/tests/testdata/import_assertions/data.json @@ -0,0 +1,6 @@ +{ + "a": "b", + "c": { + "d": 10 + } +} diff --git a/cli/tests/testdata/import_assertions/dynamic_error.out b/cli/tests/testdata/import_assertions/dynamic_error.out new file mode 100644 index 000000000..1ad40889b --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_error.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: Uncaught (in promise) TypeError: Expected a "JavaScript" module but loaded a "JSON" module. +const data = await import("./data.json"); + ^ + at async [WILDCARD]dynamic_error.ts:1:14 diff --git a/cli/tests/testdata/import_assertions/dynamic_error.ts b/cli/tests/testdata/import_assertions/dynamic_error.ts new file mode 100644 index 000000000..2d9c6757f --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_error.ts @@ -0,0 +1,3 @@ +const data = await import("./data.json"); + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/dynamic_import.out b/cli/tests/testdata/import_assertions/dynamic_import.out new file mode 100644 index 000000000..3280e0f53 --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_import.out @@ -0,0 +1,2 @@ +[WILDCARD] +Module { default: { a: "b", c: { d: 10 } } } diff --git a/cli/tests/testdata/import_assertions/dynamic_import.ts b/cli/tests/testdata/import_assertions/dynamic_import.ts new file mode 100644 index 000000000..d6983852c --- /dev/null +++ b/cli/tests/testdata/import_assertions/dynamic_import.ts @@ -0,0 +1,3 @@ +const data = await import("./data.json", { assert: { type: "json" } }); + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_error.out b/cli/tests/testdata/import_assertions/static_error.out new file mode 100644 index 000000000..8524079de --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_error.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: An unsupported media type was attempted to be imported as a module. + Specifier: [WILDCARD]data.json + MediaType: Json + at [WILDCARD]static_error.ts:1:18 diff --git a/cli/tests/testdata/import_assertions/static_error.ts b/cli/tests/testdata/import_assertions/static_error.ts new file mode 100644 index 000000000..0bc3a93f8 --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_error.ts @@ -0,0 +1,3 @@ +import data from "./data.json"; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_export.out b/cli/tests/testdata/import_assertions/static_export.out new file mode 100644 index 000000000..42fbc066c --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_export.out @@ -0,0 +1,2 @@ +[WILDCARD] +{ a: "b", c: { d: 10 } } diff --git a/cli/tests/testdata/import_assertions/static_export.ts b/cli/tests/testdata/import_assertions/static_export.ts new file mode 100644 index 000000000..ac3ee694f --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_export.ts @@ -0,0 +1,3 @@ +import data from "./static_reexport.ts"; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_import.out b/cli/tests/testdata/import_assertions/static_import.out new file mode 100644 index 000000000..42fbc066c --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_import.out @@ -0,0 +1,2 @@ +[WILDCARD] +{ a: "b", c: { d: 10 } } diff --git a/cli/tests/testdata/import_assertions/static_import.ts b/cli/tests/testdata/import_assertions/static_import.ts new file mode 100644 index 000000000..180ab75f2 --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_import.ts @@ -0,0 +1,3 @@ +import data from "./data.json" assert { type: "json" }; + +console.log(data); diff --git a/cli/tests/testdata/import_assertions/static_reexport.ts b/cli/tests/testdata/import_assertions/static_reexport.ts new file mode 100644 index 000000000..81af428be --- /dev/null +++ b/cli/tests/testdata/import_assertions/static_reexport.ts @@ -0,0 +1 @@ +export { default } from "./data.json" assert { type: "json" }; diff --git a/cli/tests/testdata/import_assertions/type_check.out b/cli/tests/testdata/import_assertions/type_check.out new file mode 100644 index 000000000..8e1387456 --- /dev/null +++ b/cli/tests/testdata/import_assertions/type_check.out @@ -0,0 +1,5 @@ +[WILDCARD] +error: TS2339 [ERROR]: Property 'foo' does not exist on type '{ a: string; c: { d: number; }; }'. +console.log(data.foo); + ~~~ + at [WILDCARD]type_check.ts:3:18 diff --git a/cli/tests/testdata/import_assertions/type_check.ts b/cli/tests/testdata/import_assertions/type_check.ts new file mode 100644 index 000000000..19adb3eae --- /dev/null +++ b/cli/tests/testdata/import_assertions/type_check.ts @@ -0,0 +1,3 @@ +import data from "./data.json" assert { type: "json" }; + +console.log(data.foo); diff --git a/cli/tsc.rs b/cli/tsc.rs index bc18efee0..f76fb18fb 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -352,6 +352,24 @@ struct LoadArgs { specifier: String, } +fn as_ts_script_kind(media_type: &MediaType) -> i32 { + match media_type { + MediaType::JavaScript => 1, + MediaType::Jsx => 2, + MediaType::Mjs => 1, + MediaType::Cjs => 1, + MediaType::TypeScript => 3, + MediaType::Mts => 3, + MediaType::Cts => 3, + MediaType::Dts => 3, + MediaType::Dmts => 3, + MediaType::Dcts => 3, + MediaType::Tsx => 4, + MediaType::Json => 6, + _ => 0, + } +} + fn op_load(state: &mut State, args: Value) -> Result<Value, AnyError> { let v: LoadArgs = serde_json::from_value(args) .context("Invalid request from JavaScript for \"op_load\".")?; @@ -395,7 +413,7 @@ fn op_load(state: &mut State, args: Value) -> Result<Value, AnyError> { }; Ok( - json!({ "data": data, "hash": hash, "scriptKind": media_type.as_ts_script_kind() }), + json!({ "data": data, "hash": hash, "scriptKind": as_ts_script_kind(&media_type) }), ) } |