default.js
4.61 KB
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
"use strict";
exports.__esModule = true;
exports.visitor = undefined;
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _babelHelperGetFunctionArity = require("babel-helper-get-function-arity");
var _babelHelperGetFunctionArity2 = _interopRequireDefault(_babelHelperGetFunctionArity);
var _babelHelperCallDelegate = require("babel-helper-call-delegate");
var _babelHelperCallDelegate2 = _interopRequireDefault(_babelHelperCallDelegate);
var _babelTemplate = require("babel-template");
var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
var _babelTypes = require("babel-types");
var t = _interopRequireWildcard(_babelTypes);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var buildDefaultParam = (0, _babelTemplate2.default)("\n let VARIABLE_NAME =\n ARGUMENTS.length > ARGUMENT_KEY && ARGUMENTS[ARGUMENT_KEY] !== undefined ?\n ARGUMENTS[ARGUMENT_KEY]\n :\n DEFAULT_VALUE;\n");
var buildCutOff = (0, _babelTemplate2.default)("\n let $0 = $1[$2];\n");
function hasDefaults(node) {
for (var _iterator = node.params, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var param = _ref;
if (!t.isIdentifier(param)) return true;
}
return false;
}
function isSafeBinding(scope, node) {
if (!scope.hasOwnBinding(node.name)) return true;
var _scope$getOwnBinding = scope.getOwnBinding(node.name),
kind = _scope$getOwnBinding.kind;
return kind === "param" || kind === "local";
}
var iifeVisitor = {
ReferencedIdentifier: function ReferencedIdentifier(path, state) {
var scope = path.scope,
node = path.node;
if (node.name === "eval" || !isSafeBinding(scope, node)) {
state.iife = true;
path.stop();
}
},
Scope: function Scope(path) {
path.skip();
}
};
var visitor = exports.visitor = {
Function: function Function(path) {
var node = path.node,
scope = path.scope;
if (!hasDefaults(node)) return;
path.ensureBlock();
var state = {
iife: false,
scope: scope
};
var body = [];
var argsIdentifier = t.identifier("arguments");
argsIdentifier._shadowedFunctionLiteral = path;
function pushDefNode(left, right, i) {
var defNode = buildDefaultParam({
VARIABLE_NAME: left,
DEFAULT_VALUE: right,
ARGUMENT_KEY: t.numericLiteral(i),
ARGUMENTS: argsIdentifier
});
defNode._blockHoist = node.params.length - i;
body.push(defNode);
}
var lastNonDefaultParam = (0, _babelHelperGetFunctionArity2.default)(node);
var params = path.get("params");
for (var i = 0; i < params.length; i++) {
var param = params[i];
if (!param.isAssignmentPattern()) {
if (!state.iife && !param.isIdentifier()) {
param.traverse(iifeVisitor, state);
}
continue;
}
var left = param.get("left");
var right = param.get("right");
if (i >= lastNonDefaultParam || left.isPattern()) {
var 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)) {
state.iife = true;
} else {
right.traverse(iifeVisitor, state);
}
}
pushDefNode(left.node, right.node, i);
}
for (var _i2 = lastNonDefaultParam + 1; _i2 < node.params.length; _i2++) {
var _param = node.params[_i2];
if (_param._isDefaultPlaceholder) continue;
var declar = buildCutOff(_param, argsIdentifier, t.numericLiteral(_i2));
declar._blockHoist = node.params.length - _i2;
body.push(declar);
}
node.params = node.params.slice(0, lastNonDefaultParam);
if (state.iife) {
body.push((0, _babelHelperCallDelegate2.default)(path, scope));
path.set("body", t.blockStatement(body));
} else {
path.get("body").unshiftContainer("body", body);
}
}
};