1 |
3a515b92
|
cagy
|
"use strict";
|
2 |
|
|
|
3 |
|
|
exports.__esModule = true;
|
4 |
|
|
|
5 |
|
|
exports.default = function (_ref) {
|
6 |
|
|
var node = _ref.node,
|
7 |
|
|
parent = _ref.parent,
|
8 |
|
|
scope = _ref.scope,
|
9 |
|
|
id = _ref.id;
|
10 |
|
|
|
11 |
|
|
if (node.id) return;
|
12 |
|
|
|
13 |
|
|
if ((t.isObjectProperty(parent) || t.isObjectMethod(parent, { kind: "method" })) && (!parent.computed || t.isLiteral(parent.key))) {
|
14 |
|
|
id = parent.key;
|
15 |
|
|
} else if (t.isVariableDeclarator(parent)) {
|
16 |
|
|
id = parent.id;
|
17 |
|
|
|
18 |
|
|
if (t.isIdentifier(id)) {
|
19 |
|
|
var binding = scope.parent.getBinding(id.name);
|
20 |
|
|
if (binding && binding.constant && scope.getBinding(id.name) === binding) {
|
21 |
|
|
node.id = id;
|
22 |
|
|
node.id[t.NOT_LOCAL_BINDING] = true;
|
23 |
|
|
return;
|
24 |
|
|
}
|
25 |
|
|
}
|
26 |
|
|
} else if (t.isAssignmentExpression(parent)) {
|
27 |
|
|
id = parent.left;
|
28 |
|
|
} else if (!id) {
|
29 |
|
|
return;
|
30 |
|
|
}
|
31 |
|
|
|
32 |
|
|
var name = void 0;
|
33 |
|
|
if (id && t.isLiteral(id)) {
|
34 |
|
|
name = id.value;
|
35 |
|
|
} else if (id && t.isIdentifier(id)) {
|
36 |
|
|
name = id.name;
|
37 |
|
|
} else {
|
38 |
|
|
return;
|
39 |
|
|
}
|
40 |
|
|
|
41 |
|
|
name = t.toBindingIdentifierName(name);
|
42 |
|
|
id = t.identifier(name);
|
43 |
|
|
|
44 |
|
|
id[t.NOT_LOCAL_BINDING] = true;
|
45 |
|
|
|
46 |
|
|
var state = visit(node, name, scope);
|
47 |
|
|
return wrap(state, node, id, scope) || node;
|
48 |
|
|
};
|
49 |
|
|
|
50 |
|
|
var _babelHelperGetFunctionArity = require("babel-helper-get-function-arity");
|
51 |
|
|
|
52 |
|
|
var _babelHelperGetFunctionArity2 = _interopRequireDefault(_babelHelperGetFunctionArity);
|
53 |
|
|
|
54 |
|
|
var _babelTemplate = require("babel-template");
|
55 |
|
|
|
56 |
|
|
var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
|
57 |
|
|
|
58 |
|
|
var _babelTypes = require("babel-types");
|
59 |
|
|
|
60 |
|
|
var t = _interopRequireWildcard(_babelTypes);
|
61 |
|
|
|
62 |
|
|
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; } }
|
63 |
|
|
|
64 |
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
65 |
|
|
|
66 |
|
|
var buildPropertyMethodAssignmentWrapper = (0, _babelTemplate2.default)("\n (function (FUNCTION_KEY) {\n function FUNCTION_ID() {\n return FUNCTION_KEY.apply(this, arguments);\n }\n\n FUNCTION_ID.toString = function () {\n return FUNCTION_KEY.toString();\n }\n\n return FUNCTION_ID;\n })(FUNCTION)\n");
|
67 |
|
|
|
68 |
|
|
var buildGeneratorPropertyMethodAssignmentWrapper = (0, _babelTemplate2.default)("\n (function (FUNCTION_KEY) {\n function* FUNCTION_ID() {\n return yield* FUNCTION_KEY.apply(this, arguments);\n }\n\n FUNCTION_ID.toString = function () {\n return FUNCTION_KEY.toString();\n };\n\n return FUNCTION_ID;\n })(FUNCTION)\n");
|
69 |
|
|
|
70 |
|
|
var visitor = {
|
71 |
|
|
"ReferencedIdentifier|BindingIdentifier": function ReferencedIdentifierBindingIdentifier(path, state) {
|
72 |
|
|
if (path.node.name !== state.name) return;
|
73 |
|
|
|
74 |
|
|
var localDeclar = path.scope.getBindingIdentifier(state.name);
|
75 |
|
|
if (localDeclar !== state.outerDeclar) return;
|
76 |
|
|
|
77 |
|
|
state.selfReference = true;
|
78 |
|
|
path.stop();
|
79 |
|
|
}
|
80 |
|
|
};
|
81 |
|
|
|
82 |
|
|
function wrap(state, method, id, scope) {
|
83 |
|
|
if (state.selfReference) {
|
84 |
|
|
if (scope.hasBinding(id.name) && !scope.hasGlobal(id.name)) {
|
85 |
|
|
scope.rename(id.name);
|
86 |
|
|
} else {
|
87 |
|
|
if (!t.isFunction(method)) return;
|
88 |
|
|
|
89 |
|
|
var build = buildPropertyMethodAssignmentWrapper;
|
90 |
|
|
if (method.generator) build = buildGeneratorPropertyMethodAssignmentWrapper;
|
91 |
|
|
var _template = build({
|
92 |
|
|
FUNCTION: method,
|
93 |
|
|
FUNCTION_ID: id,
|
94 |
|
|
FUNCTION_KEY: scope.generateUidIdentifier(id.name)
|
95 |
|
|
}).expression;
|
96 |
|
|
_template.callee._skipModulesRemap = true;
|
97 |
|
|
|
98 |
|
|
var params = _template.callee.body.body[0].params;
|
99 |
|
|
for (var i = 0, len = (0, _babelHelperGetFunctionArity2.default)(method); i < len; i++) {
|
100 |
|
|
params.push(scope.generateUidIdentifier("x"));
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
return _template;
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
method.id = id;
|
108 |
|
|
scope.getProgramParent().references[id.name] = true;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
function visit(node, name, scope) {
|
112 |
|
|
var state = {
|
113 |
|
|
selfAssignment: false,
|
114 |
|
|
selfReference: false,
|
115 |
|
|
outerDeclar: scope.getBindingIdentifier(name),
|
116 |
|
|
references: [],
|
117 |
|
|
name: name
|
118 |
|
|
};
|
119 |
|
|
|
120 |
|
|
var binding = scope.getOwnBinding(name);
|
121 |
|
|
|
122 |
|
|
if (binding) {
|
123 |
|
|
if (binding.kind === "param") {
|
124 |
|
|
state.selfReference = true;
|
125 |
|
|
} else {}
|
126 |
|
|
} else if (state.outerDeclar || scope.hasGlobal(name)) {
|
127 |
|
|
scope.traverse(node, visitor, state);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
return state;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
module.exports = exports["default"];
|