0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

fix(cli/ast): Pass importsNotUsedAsValues to swc (#9714)

Fixes #9709
This commit is contained in:
Nayeem Rahman 2021-03-07 20:40:11 +00:00 committed by GitHub
parent 74584eef04
commit 33eea0400d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 2 deletions

View file

@ -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)),
);

View file

@ -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",

2
cli/tests/087_hello.ts Normal file
View file

@ -0,0 +1,2 @@
export type SomeType = unknown;
console.log("Hello, world!");

View file

@ -0,0 +1,4 @@
import { SomeType } from "./087_hello.ts";
const string: SomeType = "Hi!";
console.log(string);

View file

@ -0,0 +1,2 @@
[WILDCARD]Hello, world!
Hi!

View file

@ -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",

View file

@ -0,0 +1,5 @@
{
"compilerOptions": {
"importsNotUsedAsValues": "preserve"
}
}

View file

@ -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,