-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
default.js
140 lines (115 loc) 路 3.46 KB
/
default.js
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
/* eslint max-len: 0 */
import getFunctionArity from "babel-helper-get-function-arity";
import callDelegate from "babel-helper-call-delegate";
import template from "babel-template";
import * as t from "babel-types";
let buildDefaultParam = template(`
let VARIABLE_NAME =
ARGUMENTS.length > ARGUMENT_KEY && ARGUMENTS[ARGUMENT_KEY] !== undefined ?
ARGUMENTS[ARGUMENT_KEY]
:
DEFAULT_VALUE;
`);
let buildCutOff = template(`
let $0 = $1[$2];
`);
function hasDefaults(node) {
for (let param of (node.params: Array<Object>)) {
if (!t.isIdentifier(param)) return true;
}
return false;
}
function isSafeBinding(scope, node) {
if (!scope.hasOwnBinding(node.name)) return true;
const { kind } = scope.getOwnBinding(node.name);
return kind === "param" || kind === "local";
}
let iifeVisitor = {
ReferencedIdentifier(path, state) {
const { scope, node } = path;
if (node.name === "eval" || !isSafeBinding(scope, node)) {
state.iife = true;
path.stop();
}
},
Scope(path) {
// different bindings
path.skip();
}
};
export let visitor = {
Function(path) {
let { node, scope } = path;
if (!hasDefaults(node)) return;
// ensure it's a block, useful for arrow functions
path.ensureBlock();
let state = {
iife: false,
scope: scope
};
let body = [];
//
let argsIdentifier = t.identifier("arguments");
argsIdentifier._shadowedFunctionLiteral = path;
// push a default parameter definition
function pushDefNode(left, right, i) {
const defNode = buildDefaultParam({
VARIABLE_NAME: left,
DEFAULT_VALUE: right,
ARGUMENT_KEY: t.numericLiteral(i),
ARGUMENTS: argsIdentifier
});
defNode._blockHoist = node.params.length - i;
body.push(defNode);
}
//
let lastNonDefaultParam = getFunctionArity(node);
//
let params = path.get("params");
for (let i = 0; i < params.length; i++) {
let param = params[i];
if (!param.isAssignmentPattern()) {
if (!state.iife && !param.isIdentifier()) {
param.traverse(iifeVisitor, state);
}
continue;
}
let left = param.get("left");
let right = param.get("right");
//
if (i >= lastNonDefaultParam || left.isPattern()) {
let placeholder = scope.generateUidIdentifier("x");
placeholder._isDefaultPlaceholder = true;
node.params[i] = placeholder;
} else {
node.params[i] = left.node;
}
//
if (!state.iife) {
if (right.isIdentifier() && !isSafeBinding(scope, right.node)) {
// the right hand side references a parameter
state.iife = true;
} else {
right.traverse(iifeVisitor, state);
}
}
pushDefNode(left.node, right.node, i);
}
// add declarations for trailing parameters
for (let i = lastNonDefaultParam + 1; i < node.params.length; i++) {
let param = node.params[i];
if (param._isDefaultPlaceholder) continue;
let declar = buildCutOff(param, argsIdentifier, t.numericLiteral(i));
declar._blockHoist = node.params.length - i;
body.push(declar);
}
// we need to cut off all trailing parameters
node.params = node.params.slice(0, lastNonDefaultParam);
if (state.iife) {
body.push(callDelegate(path, scope));
path.set("body", t.blockStatement(body));
} else {
path.get("body").unshiftContainer("body", body);
}
}
};