swc_ecma_compat_es2015/arrow.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
use std::mem;
use swc_common::{util::take::Take, Mark, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{
function::{init_this, FnEnvHoister},
prepend_stmt,
};
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, InjectVars, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;
/// Compile ES2015 arrow functions to ES5
///
///# Example
///
///## In
/// ```js
/// var a = () => {};
/// var a = (b) => b;ß
///
/// const double = [1,2,3].map((num) => num * 2);
/// console.log(double); // [2,4,6]
///
/// var bob = {
/// _name: "Bob",
/// _friends: ["Sally", "Tom"],
/// printFriends() {
/// this._friends.forEach(f =>
/// console.log(this._name + " knows " + f));
/// }
/// };
/// console.log(bob.printFriends());
/// ```
///
///## Out
///```js
/// var a = function () {};
/// var a = function (b) {
/// return b;
/// };
///
/// const double = [1, 2, 3].map(function (num) {
/// return num * 2;
/// });
/// console.log(double); // [2,4,6]
///
/// var bob = {
/// _name: "Bob",
/// _friends: ["Sally", "Tom"],
/// printFriends() {
/// var _this = this;
///
/// this._friends.forEach(function (f) {
/// return console.log(_this._name + " knows " + f);
/// });
/// }
/// };
/// console.log(bob.printFriends());
/// ```
pub fn arrow(unresolved_mark: Mark) -> impl Pass + VisitMut + InjectVars {
visit_mut_pass(Arrow {
in_subclass: false,
hoister: FnEnvHoister::new(SyntaxContext::empty().apply_mark(unresolved_mark)),
})
}
#[derive(Default)]
struct Arrow {
in_subclass: bool,
hoister: FnEnvHoister,
}
#[swc_trace]
impl VisitMut for Arrow {
noop_visit_mut_type!(fail);
fn visit_mut_class(&mut self, c: &mut Class) {
let old = self.in_subclass;
if c.super_class.is_some() {
self.in_subclass = true;
}
c.visit_mut_children_with(self);
self.in_subclass = old;
}
fn visit_mut_constructor(&mut self, c: &mut Constructor) {
c.params.visit_mut_children_with(self);
if let Some(BlockStmt { span: _, stmts, .. }) = &mut c.body {
let old_rep = self.hoister.take();
stmts.visit_mut_children_with(self);
if self.in_subclass {
let (decl, this_id) =
mem::replace(&mut self.hoister, old_rep).to_stmt_in_subclass();
if let Some(stmt) = decl {
if let Some(this_id) = this_id {
init_this(stmts, &this_id)
}
prepend_stmt(stmts, stmt);
}
} else {
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let Some(stmt) = decl {
prepend_stmt(stmts, stmt);
}
}
}
}
fn visit_mut_expr(&mut self, expr: &mut Expr) {
match expr {
Expr::Arrow(ArrowExpr {
span,
params,
body,
is_async,
is_generator,
..
}) => {
params.visit_mut_with(self);
params.visit_mut_with(&mut self.hoister);
let params: Vec<Param> = params
.take()
.into_iter()
.map(|pat| Param {
span: DUMMY_SP,
decorators: Default::default(),
pat,
})
.collect();
body.visit_mut_with(self);
body.visit_mut_with(&mut self.hoister);
let fn_expr = Function {
decorators: Vec::new(),
span: *span,
params,
is_async: *is_async,
is_generator: *is_generator,
body: Some(match &mut **body {
BlockStmtOrExpr::BlockStmt(block) => block.take(),
BlockStmtOrExpr::Expr(expr) => BlockStmt {
span: DUMMY_SP,
stmts: vec![Stmt::Return(ReturnStmt {
// this is needed so
// () => /* 123 */ 1 would become
// function { return /*123 */ 123 }
span: DUMMY_SP,
arg: Some(expr.take()),
})],
..Default::default()
},
}),
..Default::default()
}
.into();
*expr = fn_expr;
}
_ => {
expr.visit_mut_children_with(self);
}
}
}
fn visit_mut_function(&mut self, f: &mut Function) {
let old_rep = self.hoister.take();
f.visit_mut_children_with(self);
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let (Some(body), Some(stmt)) = (&mut f.body, decl) {
prepend_stmt(&mut body.stmts, stmt);
}
}
fn visit_mut_getter_prop(&mut self, f: &mut GetterProp) {
f.key.visit_mut_with(self);
if let Some(body) = &mut f.body {
let old_rep = self.hoister.take();
body.visit_mut_with(self);
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let Some(stmt) = decl {
prepend_stmt(&mut body.stmts, stmt);
}
}
}
fn visit_mut_module_items(&mut self, stmts: &mut Vec<ModuleItem>) {
stmts.visit_mut_children_with(self);
let decl = self.hoister.take().to_stmt();
if let Some(stmt) = decl {
prepend_stmt(stmts, stmt.into());
}
}
fn visit_mut_script(&mut self, script: &mut Script) {
script.visit_mut_children_with(self);
let decl = self.hoister.take().to_stmt();
if let Some(stmt) = decl {
prepend_stmt(&mut script.body, stmt);
}
}
fn visit_mut_setter_prop(&mut self, f: &mut SetterProp) {
f.key.visit_mut_with(self);
f.param.visit_mut_with(self);
if let Some(body) = &mut f.body {
let old_rep = self.hoister.take();
body.visit_mut_with(self);
let decl = mem::replace(&mut self.hoister, old_rep).to_stmt();
if let Some(stmt) = decl {
prepend_stmt(&mut body.stmts, stmt);
}
}
}
}
impl InjectVars for Arrow {
fn take_vars(&mut self) -> Vec<VarDeclarator> {
self.hoister.take().to_decl()
}
}