1 | var parse = require('babylon').parse;
|
2 | var traverse = require('babel-traverse').default;
|
3 | var generate = require('babel-generator').default;
|
4 | var t = require('babel-types');
|
5 |
|
6 | module.exports = function (parsed, options) {
|
7 | var ast = parse('(' + parsed + ')', {
|
8 | plugins: [
|
9 | 'objectRestSpread',
|
10 | 'functionBind'
|
11 | ]
|
12 | });
|
13 |
|
14 | traverse(ast, {
|
15 | enter: function (path) {
|
16 | if (
|
17 | !path.scope.parent
|
18 | && t.isObjectProperty(path)
|
19 | && t.isFunctionExpression(path.node.value)
|
20 | ) {
|
21 | var original = path.parent.properties[0].value.value;
|
22 | var funcName = options.funcName;
|
23 |
|
24 | path.parentPath.replaceWith(
|
25 | t.sequenceExpression([
|
26 | t.assignmentExpression(
|
27 | '=',
|
28 | t.identifier(funcName),
|
29 | path.node.value
|
30 | ),
|
31 | t.assignmentExpression(
|
32 | '=',
|
33 | t.memberExpression(
|
34 | t.identifier(funcName),
|
35 | t.identifier('original')
|
36 | ),
|
37 | t.stringLiteral(original)
|
38 | ),
|
39 | t.identifier(funcName)
|
40 | ])
|
41 | );
|
42 | }
|
43 | }
|
44 | });
|
45 |
|
46 | return generate(ast, {}, parsed).code;
|
47 | };
|