-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mod.rs
142 lines (129 loc) · 3.49 KB
/
mod.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
use ast::*;
use crate::util::ExprFactory;
use swc_common::{Fold, FoldWith, Visit, VisitWith, DUMMY_SP};
#[cfg(test)]
mod tests;
/// 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() -> impl Fold<Expr> {
Arrow
}
#[derive(Debug, Clone, Copy)]
struct Arrow;
impl Fold<Expr> for Arrow {
fn fold(&mut self, e: Expr) -> Expr {
let e = e.fold_children(self);
match e {
Expr::Arrow(ArrowExpr {
span,
params,
body,
async_token,
generator_token,
}) => {
let used_this = contains_this_expr(&body);
let fn_expr = Expr::Fn(FnExpr {
ident: None,
function: Function {
span,
params,
async_token,
generator_token,
body: match body {
BlockStmtOrExpr::BlockStmt(block) => block,
BlockStmtOrExpr::Expr(expr) => BlockStmt {
span: DUMMY_SP,
stmts: vec![Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(expr),
})],
},
},
},
});
if !used_this {
return fn_expr;
}
Expr::Call(CallExpr {
span,
callee: Expr::Member(MemberExpr {
span,
obj: ExprOrSuper::Expr(box fn_expr),
prop: box quote_ident!("bind").into(),
computed: false,
})
.as_callee(),
args: vec![ThisExpr { span: DUMMY_SP }.as_arg()],
})
}
_ => e,
}
}
}
fn contains_this_expr(body: &BlockStmtOrExpr) -> bool {
struct Visitor {
found: bool,
}
impl Visit<ThisExpr> for Visitor {
fn visit(&mut self, _: &ThisExpr) {
self.found = true;
}
}
impl Visit<FnExpr> for Visitor {
/// Don't recurse into fn
fn visit(&mut self, _: &FnExpr) {}
}
impl Visit<FnDecl> for Visitor {
/// Don't recurse into fn
fn visit(&mut self, _: &FnDecl) {}
}
let mut visitor = Visitor { found: false };
body.visit_with(&mut visitor);
visitor.found
}