Skip to content

Commit

Permalink
Use JsWord instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
padmaia committed Dec 8, 2021
1 parent 80eb556 commit 250e812
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
19 changes: 10 additions & 9 deletions packages/next-swc/crates/core/src/shake_exports.rs
@@ -1,12 +1,13 @@
use serde::Deserialize;
use swc_atoms::js_word;
use swc_atoms::JsWord;
use swc_ecmascript::ast::*;
use swc_ecmascript::transforms::optimization::simplify::dce::{dce, Config as DCEConfig};
use swc_ecmascript::visit::{Fold, FoldWith};

#[derive(Clone, Debug, Deserialize)]
pub struct Config {
pub ignore: Vec<String>,
pub ignore: Vec<JsWord>,
}

pub fn shake_exports(config: Config) -> impl Fold {
Expand All @@ -18,7 +19,7 @@ pub fn shake_exports(config: Config) -> impl Fold {

#[derive(Debug, Default)]
struct ExportShaker {
ignore: Vec<String>,
ignore: Vec<JsWord>,
remove_export: bool,
}

Expand All @@ -45,12 +46,12 @@ impl Fold for ExportShaker {
fn fold_export_decl(&mut self, mut decl: ExportDecl) -> ExportDecl {
match &mut decl.decl {
Decl::Fn(fn_decl) => {
if !self.ignore.contains(&fn_decl.ident.sym.to_string()) {
if !self.ignore.contains(&fn_decl.ident.sym) {
self.remove_export = true;
}
}
Decl::Class(class_decl) => {
if !self.ignore.contains(&class_decl.ident.sym.to_string()) {
if !self.ignore.contains(&class_decl.ident.sym) {
self.remove_export = true;
}
}
Expand All @@ -60,7 +61,7 @@ impl Fold for ExportShaker {
.iter()
.filter_map(|var_decl| {
if let Pat::Ident(BindingIdent { id, .. }) = &var_decl.name {
if self.ignore.contains(&id.sym.to_string()) {
if self.ignore.contains(&id.sym) {
return Some(var_decl.to_owned());
}
}
Expand All @@ -83,10 +84,10 @@ impl Fold for ExportShaker {
.filter_map(|spec| {
if let ExportSpecifier::Named(named_spec) = spec {
if let Some(ident) = &named_spec.exported {
if self.ignore.contains(&ident.sym.to_string()) {
if self.ignore.contains(&ident.sym) {
return Some(ExportSpecifier::Named(named_spec));
}
} else if self.ignore.contains(&named_spec.orig.sym.to_string()) {
} else if self.ignore.contains(&named_spec.orig.sym) {
return Some(ExportSpecifier::Named(named_spec));
}
}
Expand All @@ -100,14 +101,14 @@ impl Fold for ExportShaker {
}

fn fold_export_default_decl(&mut self, decl: ExportDefaultDecl) -> ExportDefaultDecl {
if !self.ignore.contains(&js_word!("default").to_string()) {
if !self.ignore.contains(&js_word!("default")) {
self.remove_export = true
}
decl
}

fn fold_export_default_expr(&mut self, expr: ExportDefaultExpr) -> ExportDefaultExpr {
if !self.ignore.contains(&js_word!("default").to_string()) {
if !self.ignore.contains(&js_word!("default")) {
self.remove_export = true
}
expr
Expand Down
12 changes: 6 additions & 6 deletions packages/next-swc/crates/core/tests/fixture.rs
Expand Up @@ -173,11 +173,11 @@ fn shake_exports_fixture(input: PathBuf) {
&|_tr| {
shake_exports(ShakeExportsConfig {
ignore: vec![
String::from("keep"),
String::from("keep1"),
String::from("keep2"),
String::from("keep3"),
String::from("keep4"),
String::from("keep").into(),
String::from("keep1").into(),
String::from("keep2").into(),
String::from("keep3").into(),
String::from("keep4").into(),
],
})
},
Expand All @@ -193,7 +193,7 @@ fn shake_exports_fixture_default(input: PathBuf) {
syntax(),
&|_tr| {
shake_exports(ShakeExportsConfig {
ignore: vec![String::from("default")],
ignore: vec![String::from("default").into()],
})
},
&input,
Expand Down

0 comments on commit 250e812

Please sign in to comment.