refactor: Cleanup (#2749)

This commit is contained in:
Donny/강동윤 2021-11-15 15:24:25 +09:00 committed by GitHub
parent 2cd565bfc0
commit 2462b9941f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 28 additions and 29 deletions

View File

@ -149,7 +149,7 @@ impl<I: Tokens> Capturing<I> {
}
/// Take captured tokens
pub fn take(&mut self) -> Vec<TokenAndSpan> {
mem::replace(&mut *self.captured.borrow_mut(), Default::default())
mem::take(&mut *self.captured.borrow_mut())
}
}

View File

@ -1,5 +1,5 @@
use smallvec::SmallVec;
use std::mem::{replace, take};
use std::mem::take;
use swc_atoms::js_word;
use swc_common::{collections::AHashMap, util::map::Map, Mark, Spanned, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
@ -642,7 +642,7 @@ impl BlockScoping {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: replace(&mut self.vars, Default::default()),
decls: take(&mut self.vars),
}))),
);
}

View File

@ -1,6 +1,6 @@
use super::leap::{CatchEntry, Entry, FinallyEntry, LeapManager, TryEntry};
use smallvec::SmallVec;
use std::mem::replace;
use std::mem::take;
use swc_atoms::JsWord;
use swc_common::{
util::{map::Map, move_map::MoveMap},
@ -93,7 +93,7 @@ impl CaseHandler<'_> {
// let mut last_loc_value = 0;
Some(ArrayLit {
span: DUMMY_SP,
elems: replace(&mut self.try_entries, Default::default())
elems: take(&mut self.try_entries)
.into_iter()
.map(|entry: TryEntry| {
// let this_loc_value = entry.first_loc;
@ -850,7 +850,7 @@ impl CaseHandler<'_> {
let mut already_ended = false;
let stmts = {
let stmts = replace(&mut self.listing, vec![]);
let stmts = take(&mut self.listing);
let mut v = InvalidToLit { map: &self.marked };
stmts.fold_with(&mut v)

View File

@ -243,7 +243,7 @@ impl Spread {
macro_rules! make_arr {
() => {
let elems = mem::replace(&mut tmp_arr, vec![]);
let elems = mem::take(&mut tmp_arr);
match first_arr {
Some(_) => {
if !elems.is_empty() {

View File

@ -175,7 +175,7 @@ impl VisitMut for TemplateLiteral {
if last && !args.is_empty() {
obj = if self.c.ignore_to_primitive {
let args = mem::replace(&mut args, vec![]);
let args = mem::take(&mut args);
for arg in args {
obj = Box::new(Expr::Bin(BinExpr {
span: span.with_hi(expr_span.hi() + BytePos(1)),
@ -198,7 +198,7 @@ impl VisitMut for TemplateLiteral {
computed: false,
}))),
args: mem::replace(&mut args, vec![])
args: mem::take(&mut args)
.into_iter()
.map(|expr| ExprOrSpread { expr, spread: None })
.collect(),
@ -209,7 +209,7 @@ impl VisitMut for TemplateLiteral {
} else {
if !args.is_empty() {
obj = if self.c.ignore_to_primitive {
let args = mem::replace(&mut args, vec![]);
let args = mem::take(&mut args);
let len = args.len();
for arg in args {
// for `${asd}a`
@ -240,7 +240,7 @@ impl VisitMut for TemplateLiteral {
computed: false,
}))),
args: mem::replace(&mut args, vec![])
args: mem::take(&mut args)
.into_iter()
.map(|expr| ExprOrSpread { expr, spread: None })
.collect(),

View File

@ -93,7 +93,7 @@ impl OptChaining {
span: DUMMY_SP,
declare: false,
kind: VarDeclKind::Var,
decls: mem::replace(&mut self.vars_with_init, vec![]),
decls: mem::take(&mut self.vars_with_init),
}))));
}
new.push(stmt);
@ -159,7 +159,7 @@ impl OptChaining {
span: DUMMY_SP,
declare: false,
kind: VarDeclKind::Var,
decls: mem::replace(&mut self.vars_without_init, vec![]),
decls: mem::take(&mut self.vars_without_init),
}))),
);
}

View File

@ -114,7 +114,7 @@ macro_rules! take_vars {
Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: mem::replace(&mut self.vars, vec![]),
decls: mem::take(&mut self.vars),
declare: false,
})),

View File

@ -1,7 +1,7 @@
use self::metadata::{Metadata, ParamMetadata};
use super::{contains_decorator, DecoratorFinder};
use smallvec::SmallVec;
use std::mem::{replace, take};
use std::mem::take;
use swc_common::{collections::AHashMap, util::move_map::MoveMap, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_transforms_base::helper;
@ -213,7 +213,7 @@ impl Fold for Legacy {
Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: replace(&mut self.uninitialized_vars, Default::default()),
decls: take(&mut self.uninitialized_vars),
declare: false,
})),
);
@ -255,7 +255,7 @@ impl Legacy {
buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
decls: replace(&mut self.initialized_vars, Default::default()),
decls: take(&mut self.initialized_vars),
declare: false,
}))));
}

View File

@ -733,7 +733,7 @@ where
args.push(
ObjectLit {
span: DUMMY_SP,
props: mem::replace(&mut cur_obj_props, vec![]),
props: mem::take(&mut cur_obj_props),
}
.as_arg(),
)

View File

@ -6,7 +6,6 @@ pub use self::{
pure_annotations::pure_annotations,
refresh::{options::RefreshOptions, refresh},
};
use std::mem;
use swc_common::{chain, comments::Comments, sync::Lrc, Mark, SourceMap};
use swc_ecma_visit::Fold;
@ -41,7 +40,7 @@ where
{
let Options { development, .. } = options;
let refresh_options = mem::replace(&mut options.refresh, None);
let refresh_options = options.refresh.take();
chain!(
jsx_src(development, cm.clone()),

View File

@ -300,7 +300,7 @@ impl<C: Comments> Refresh<C> {
fn gen_hook_register(&self, func: Expr, hook_fn: &mut FnWithHook) -> Expr {
let mut args = vec![func];
let mut sign = Vec::new();
let hooks = mem::replace(&mut hook_fn.hook, Vec::new());
let hooks = mem::take(&mut hook_fn.hook);
let mut custom_hook = Vec::new();
for hook in hooks {
@ -592,7 +592,7 @@ impl<C: Comments> Fold for Refresh<C> {
let orig_bindings = self.scope_binding.len();
self.scope_binding.extend(current_scope.into_iter());
let orig_hook = mem::replace(&mut self.curr_hook_fn, Vec::new());
let orig_hook = mem::take(&mut self.curr_hook_fn);
let mut n = n.fold_children_with(self);
let curr_hook = mem::replace(&mut self.curr_hook_fn, orig_hook);
self.scope_binding.truncate(orig_bindings);
@ -769,7 +769,7 @@ impl<C: Comments> Fold for Refresh<C> {
items.push(ModuleItem::Stmt(self.gen_hook_handle(&self.curr_hook_fn)));
}
let curr_hook_fn = mem::replace(&mut self.curr_hook_fn, Vec::new());
let curr_hook_fn = mem::take(&mut self.curr_hook_fn);
let (mut handle_map, ignore) = hook_to_handle_map(curr_hook_fn);
for mut item in module_items {

View File

@ -6,7 +6,7 @@ use std::{
env,
fs::{self, create_dir_all, read_to_string, OpenOptions},
io::{self, Write},
mem::{replace, take},
mem::take,
path::Path,
process::Command,
rc::Rc,
@ -17,6 +17,7 @@ use swc_common::{
comments::SingleThreadedComments,
errors::{Handler, HANDLER},
sync::Lrc,
util::take::Take,
FileName, SourceMap, DUMMY_SP,
};
use swc_ecma_ast::{Pat, *};
@ -594,8 +595,7 @@ impl VisitMut for Normalizer {
match node {
PatOrExpr::Pat(pat) => match &mut **pat {
Pat::Expr(e) => {
let e = replace(e, Box::new(Expr::Invalid(Invalid { span: DUMMY_SP })));
*node = PatOrExpr::Expr(e);
*node = PatOrExpr::Expr(e.take());
}
_ => {}
},

View File

@ -1,6 +1,6 @@
use crate::{prepend_stmts, ExprFactory};
use std::{iter, mem::replace};
use swc_common::DUMMY_SP;
use std::iter;
use swc_common::{util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_visit::{noop_fold_type, noop_visit_mut_type, Fold, FoldWith, VisitMut, VisitMutWith};
@ -139,7 +139,7 @@ impl VisitMut for ExprInjector<'_> {
.unwrap_or_else(|| private_ident!("_temp")),
);
self.injected = true;
let e = replace(expr, Expr::Invalid(Invalid { span: DUMMY_SP }));
let e = expr.take();
*expr = Expr::Seq(SeqExpr {
span: DUMMY_SP,