Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/transform_typescript): should consider A as a type in export { type A } #7196

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions crates/swc_ecma_transforms_typescript/src/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ pub fn strip_with_config(config: Config, top_level_mark: Mark) -> impl Fold + Vi
is_type_only_export: Default::default(),
decl_names: Default::default(),
in_var_pat: Default::default(),
keys: Default::default()
keys: Default::default(),
exported_variable_to_is_type: Default::default(),
}),
inline_enum(ts_enum_lit, ts_enum_config)
)
Expand Down Expand Up @@ -236,6 +237,7 @@ where
decl_names: Default::default(),
in_var_pat: Default::default(),
keys: Default::default(),
exported_variable_to_is_type: Default::default(),
}),
inline_enum(ts_enum_lit, ts_enum_config)
)
Expand Down Expand Up @@ -292,6 +294,10 @@ where
in_var_pat: bool,

keys: Vec<VarDeclarator>,

/// Key is the `orig` in ExportNamedSpecifier
/// Value is `true` if the Key is a type
exported_variable_to_is_type: AHashMap<Id, bool>,
}

impl<C> Strip<C>
Expand Down Expand Up @@ -1675,6 +1681,24 @@ where
self.is_type_only_export = old;
}

fn visit_export_named_specifier(&mut self, n: &ExportNamedSpecifier) {
let is_type = self.is_type_only_export || n.is_type_only;
if let ModuleExportName::Ident(local_ident) = &n.orig {
// If the stored exported `Id` is a value, we just leave it here.
// If the stored exported `Id` is both a type and value, we consider it's a
// value. See https://github.com/denoland/deno/issues/8978
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was easy but not easy as I thought when I met denoland/deno#8978.

let is_type_to_the_existed = self
.exported_variable_to_is_type
.entry(local_ident.to_id())
.or_insert(is_type);

if *is_type_to_the_existed && !is_type {
*is_type_to_the_existed = false
}
}
n.visit_children_with(self)
}

fn visit_prop_name(&mut self, n: &PropName) {
if let PropName::Computed(e) = n {
e.visit_with(self)
Expand Down Expand Up @@ -2057,8 +2081,18 @@ where

import.specifiers.retain(|s| match *s {
ImportSpecifier::Named(ImportNamedSpecifier {
ref is_type_only, ..
}) if *is_type_only => false,
ref is_type_only,
ref local,
..
}) if *is_type_only
|| self
.exported_variable_to_is_type
.get(&local.to_id())
.copied()
.unwrap_or_default() =>
{
false
}

ImportSpecifier::Default(ImportDefaultSpecifier { ref local, .. })
| ImportSpecifier::Named(ImportNamedSpecifier { ref local, .. })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { A } from "./a.ts";
export { type A };