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(ecma/transform/typescript): remove import used in interface and type signature #3893

Merged
merged 2 commits into from
Mar 7, 2022
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
60 changes: 20 additions & 40 deletions crates/swc_ecma_transforms_typescript/src/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ struct Scope {

#[derive(Debug, Default)]
struct DeclInfo {
/// interface / type alias
has_type: bool,
/// Var, Fn, Class
has_concrete: bool,
/// In `import foo = bar.baz`, `foo`'s dependency is `bar`. This means that
Expand All @@ -343,8 +341,6 @@ where

if concrete {
entry.has_concrete = true
} else {
entry.has_type = true;
}
}

Expand Down Expand Up @@ -1540,17 +1536,13 @@ where

fn visit_ident(&mut self, n: &Ident) {
let entry = self.scope.referenced_idents.entry(n.to_id()).or_default();
if self.is_type_only_export {
entry.has_type = true;
} else {
if !self.is_type_only_export {
entry.has_concrete = true;
}
if let Some(i) = &entry.maybe_dependency {
let id = i.to_id();
if let Some(entry) = self.scope.referenced_idents.get_mut(&id) {
if self.is_type_only_export {
entry.has_type = true;
} else {
if !self.is_type_only_export {
entry.has_concrete = true;
}
}
Expand All @@ -1559,22 +1551,19 @@ where
}

fn visit_import_decl(&mut self, n: &ImportDecl) {
macro_rules! store {
($i:expr) => {{
self.scope
.referenced_idents
.entry(($i.sym.clone(), $i.span.ctxt()))
.or_default();
if n.type_only {
self.scope.decls.entry($i.to_id()).or_default().has_type = true;
}
}};
}
for s in &n.specifiers {
match *s {
ImportSpecifier::Default(ref import) => store!(import.local),
ImportSpecifier::Named(ref import) => store!(import.local),
ImportSpecifier::Namespace(ref import) => store!(import.local),
match s {
ImportSpecifier::Default(ImportDefaultSpecifier { local, .. })
| ImportSpecifier::Named(ImportNamedSpecifier { local, .. })
| ImportSpecifier::Namespace(ImportStarAsSpecifier { local, .. }) => {
self.scope
.referenced_idents
.entry((local.sym.clone(), local.span.ctxt()))
.or_default();
if n.type_only {
self.scope.decls.entry(local.to_id()).or_default();
}
}
}
}
}
Expand Down Expand Up @@ -1608,21 +1597,12 @@ where
self.non_top_level = old;
}

fn visit_ts_entity_name(&mut self, name: &TsEntityName) {
match *name {
TsEntityName::Ident(ref i) => {
let entry = self.scope.referenced_idents.entry(i.to_id()).or_default();
entry.has_type = true;
if let Some(i) = &entry.maybe_dependency {
let id = i.to_id();
if let Some(entry) = self.scope.referenced_idents.get_mut(&id) {
entry.has_type = true;
}
}
}
TsEntityName::TsQualifiedName(ref q) => q.left.visit_with(self),
}
}
fn visit_ts_entity_name(&mut self, _: &TsEntityName) {}

// these may contain expr
fn visit_ts_expr_with_type_args(&mut self, _: &TsExprWithTypeArgs) {}

fn visit_ts_type_element(&mut self, _: &TsTypeElement) {}

fn visit_class(&mut self, c: &Class) {
c.decorators.visit_with(self);
Expand Down
17 changes: 15 additions & 2 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use swc_common::{chain, pass::Optional, Mark};
use swc_ecma_parser::{Syntax, TsConfig};
use swc_ecma_transforms_base::resolver::resolver_with_mark;
use swc_ecma_transforms_base::resolver::ts_resolver;
use swc_ecma_transforms_compat::{
es2015::{block_scoping, destructuring, parameters},
es2017::async_to_generator,
Expand Down Expand Up @@ -33,7 +33,7 @@ fn tr_config(
decorators(decorators_config.unwrap_or_default()),
has_decorators,
),
resolver_with_mark(mark),
ts_resolver(mark),
strip_with_config(config, mark),
)
}
Expand Down Expand Up @@ -4288,6 +4288,19 @@ to!(
"
);

to!(
issue_3827,
"
import { foo } from './foo'

type A = {
get [foo](): number
}
",
"
"
);

to!(
issue_1122_2,
"
Expand Down