diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/ast.rs | 46 | ||||
-rw-r--r-- | cli/module_graph.rs | 4 | ||||
-rw-r--r-- | cli/tests/087_hello.ts | 2 | ||||
-rw-r--r-- | cli/tests/087_no_check_imports_not_used_as_values.ts | 4 | ||||
-rw-r--r-- | cli/tests/087_no_check_imports_not_used_as_values.ts.out | 2 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 5 | ||||
-rw-r--r-- | cli/tests/preserve_imports.tsconfig.json | 5 | ||||
-rw-r--r-- | cli/tsc_config.rs | 1 |
8 files changed, 67 insertions, 2 deletions
diff --git a/cli/ast.rs b/cli/ast.rs index a8bc5adb4..e22b242d7 100644 --- a/cli/ast.rs +++ b/cli/ast.rs @@ -182,6 +182,13 @@ pub fn get_syntax(media_type: &MediaType) -> Syntax { } } +#[derive(Debug, Clone)] +pub enum ImportsNotUsedAsValues { + Remove, + Preserve, + Error, +} + /// Options which can be adjusted when transpiling a module. #[derive(Debug, Clone)] pub struct EmitOptions { @@ -191,6 +198,10 @@ pub struct EmitOptions { /// When emitting a legacy decorator, also emit experimental decorator meta /// data. Defaults to `false`. pub emit_metadata: bool, + /// What to do with import statements that only import types i.e. whether to + /// remove them (`Remove`), keep them as side-effect imports (`Preserve`) + /// or error (`Error`). Defaults to `Remove`. + pub imports_not_used_as_values: ImportsNotUsedAsValues, /// Should the source map be inlined in the emitted code file, or provided /// as a separate file. Defaults to `true`. pub inline_source_map: bool, @@ -209,6 +220,7 @@ impl Default for EmitOptions { EmitOptions { check_js: false, emit_metadata: false, + imports_not_used_as_values: ImportsNotUsedAsValues::Remove, inline_source_map: true, jsx_factory: "React.createElement".into(), jsx_fragment_factory: "React.Fragment".into(), @@ -221,9 +233,16 @@ impl From<tsc_config::TsConfig> for EmitOptions { fn from(config: tsc_config::TsConfig) -> Self { let options: tsc_config::EmitConfigOptions = serde_json::from_value(config.0).unwrap(); + let imports_not_used_as_values = + match options.imports_not_used_as_values.as_str() { + "preserve" => ImportsNotUsedAsValues::Preserve, + "error" => ImportsNotUsedAsValues::Error, + _ => ImportsNotUsedAsValues::Remove, + }; EmitOptions { check_js: options.check_js, emit_metadata: options.emit_decorator_metadata, + imports_not_used_as_values, inline_source_map: options.inline_source_map, jsx_factory: options.jsx_factory, jsx_fragment_factory: options.jsx_fragment_factory, @@ -232,6 +251,25 @@ impl From<tsc_config::TsConfig> for EmitOptions { } } +fn strip_config_from_emit_options( + options: &EmitOptions, +) -> typescript::strip::Config { + let mut config = typescript::strip::Config::default(); + config.import_not_used_as_values = match options.imports_not_used_as_values { + ImportsNotUsedAsValues::Remove => { + typescript::strip::ImportNotUsedAsValues::Remove + } + ImportsNotUsedAsValues::Preserve => { + typescript::strip::ImportNotUsedAsValues::Preserve + } + // `Error` only affects the type-checking stage. Fall back to `Remove` here. + ImportsNotUsedAsValues::Error => { + typescript::strip::ImportNotUsedAsValues::Remove + } + }; + config +} + /// A logical structure to hold the value of a parsed module for further /// processing. #[derive(Clone)] @@ -299,7 +337,9 @@ impl ParsedModule { emit_metadata: options.emit_metadata }), helpers::inject_helpers(), - typescript::strip(), + typescript::strip::strip_with_config(strip_config_from_emit_options( + options + )), fixer(Some(&self.comments)), hygiene(), ); @@ -513,7 +553,9 @@ pub fn transpile_module( emit_metadata: emit_options.emit_metadata }), helpers::inject_helpers(), - typescript::strip(), + typescript::strip::strip_with_config(strip_config_from_emit_options( + emit_options + )), fixer(Some(&parsed_module.comments)), ); diff --git a/cli/module_graph.rs b/cli/module_graph.rs index 8a3c140ba..ad6d01589 100644 --- a/cli/module_graph.rs +++ b/cli/module_graph.rs @@ -773,6 +773,7 @@ impl Graph { let mut ts_config = TsConfig::new(json!({ "checkJs": false, "emitDecoratorMetadata": false, + "importsNotUsedAsValues": "remove", "inlineSourceMap": true, "jsx": "react", "jsxFactory": "React.createElement", @@ -814,6 +815,7 @@ impl Graph { // TODO(@kitsonk) consider enabling this by default // see: https://github.com/denoland/deno/issues/7732 "emitDecoratorMetadata": false, + "importsNotUsedAsValues": "remove", "inlineSourceMap": true, "outDir": "deno://", "removeComments": true, @@ -942,6 +944,7 @@ impl Graph { "emitDecoratorMetadata": false, "esModuleInterop": true, "experimentalDecorators": true, + "importsNotUsedAsValues": "remove", "inlineSourceMap": false, "isolatedModules": true, "jsx": "react", @@ -1589,6 +1592,7 @@ impl Graph { let mut ts_config = TsConfig::new(json!({ "checkJs": false, "emitDecoratorMetadata": false, + "importsNotUsedAsValues": "remove", "inlineSourceMap": true, "jsx": "react", "jsxFactory": "React.createElement", diff --git a/cli/tests/087_hello.ts b/cli/tests/087_hello.ts new file mode 100644 index 000000000..1a9d8f114 --- /dev/null +++ b/cli/tests/087_hello.ts @@ -0,0 +1,2 @@ +export type SomeType = unknown; +console.log("Hello, world!"); diff --git a/cli/tests/087_no_check_imports_not_used_as_values.ts b/cli/tests/087_no_check_imports_not_used_as_values.ts new file mode 100644 index 000000000..64a13f9b1 --- /dev/null +++ b/cli/tests/087_no_check_imports_not_used_as_values.ts @@ -0,0 +1,4 @@ +import { SomeType } from "./087_hello.ts"; + +const string: SomeType = "Hi!"; +console.log(string); diff --git a/cli/tests/087_no_check_imports_not_used_as_values.ts.out b/cli/tests/087_no_check_imports_not_used_as_values.ts.out new file mode 100644 index 000000000..f744c4183 --- /dev/null +++ b/cli/tests/087_no_check_imports_not_used_as_values.ts.out @@ -0,0 +1,2 @@ +[WILDCARD]Hello, world! +Hi! diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index de5cc6aae..a09b331d8 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2837,6 +2837,11 @@ console.log("finish"); output: "086_dynamic_import_already_rejected.ts.out", }); + itest!(_087_no_check_imports_not_used_as_values { + args: "run --config preserve_imports.tsconfig.json --no-check 087_no_check_imports_not_used_as_values.ts", + output: "087_no_check_imports_not_used_as_values.ts.out", + }); + itest!(js_import_detect { args: "run --quiet --reload js_import_detect.ts", output: "js_import_detect.ts.out", diff --git a/cli/tests/preserve_imports.tsconfig.json b/cli/tests/preserve_imports.tsconfig.json new file mode 100644 index 000000000..9b19291aa --- /dev/null +++ b/cli/tests/preserve_imports.tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "importsNotUsedAsValues": "preserve" + } +} diff --git a/cli/tsc_config.rs b/cli/tsc_config.rs index 9d380f967..54ed34bf1 100644 --- a/cli/tsc_config.rs +++ b/cli/tsc_config.rs @@ -23,6 +23,7 @@ use std::str::FromStr; pub struct EmitConfigOptions { pub check_js: bool, pub emit_decorator_metadata: bool, + pub imports_not_used_as_values: String, pub inline_source_map: bool, pub jsx: String, pub jsx_factory: String, |