Skip to content

Commit

Permalink
fix(cli/ast): Pass importsNotUsedAsValues to swc (denoland#9714)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Mar 7, 2021
1 parent 74584ee commit 33eea04
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 2 deletions.
46 changes: 44 additions & 2 deletions cli/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -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(),
);
Expand Down Expand Up @@ -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)),
);

Expand Down
4 changes: 4 additions & 0 deletions cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:https://",
"removeComments": true,
Expand Down Expand Up @@ -942,6 +944,7 @@ impl Graph {
"emitDecoratorMetadata": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": false,
"isolatedModules": true,
"jsx": "react",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions cli/tests/087_hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type SomeType = unknown;
console.log("Hello, world!");
4 changes: 4 additions & 0 deletions cli/tests/087_no_check_imports_not_used_as_values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { SomeType } from "./087_hello.ts";

const string: SomeType = "Hi!";
console.log(string);
2 changes: 2 additions & 0 deletions cli/tests/087_no_check_imports_not_used_as_values.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[WILDCARD]Hello, world!
Hi!
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/preserve_imports.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"importsNotUsedAsValues": "preserve"
}
}
1 change: 1 addition & 0 deletions cli/tsc_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 33eea04

Please sign in to comment.