index.js
3.34 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
"use strict";
exports.__esModule = true;
var _symbol = require("babel-runtime/core-js/symbol");
var _symbol2 = _interopRequireDefault(_symbol);
exports.default = function (code, opts) {
var stack = void 0;
try {
throw new Error();
} catch (error) {
if (error.stack) {
stack = error.stack.split("\n").slice(1).join("\n");
}
}
opts = (0, _assign2.default)({
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
preserveComments: false
}, opts);
var _getAst = function getAst() {
var ast = void 0;
try {
ast = babylon.parse(code, opts);
ast = _babelTraverse2.default.removeProperties(ast, { preserveComments: opts.preserveComments });
_babelTraverse2.default.cheap(ast, function (node) {
node[FROM_TEMPLATE] = true;
});
} catch (err) {
err.stack = err.stack + "from\n" + stack;
throw err;
}
_getAst = function getAst() {
return ast;
};
return ast;
};
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return useTemplate(_getAst(), args);
};
};
var _cloneDeep = require("lodash/cloneDeep");
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
var _assign = require("lodash/assign");
var _assign2 = _interopRequireDefault(_assign);
var _has = require("lodash/has");
var _has2 = _interopRequireDefault(_has);
var _babelTraverse = require("babel-traverse");
var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
var _babylon = require("babylon");
var babylon = _interopRequireWildcard(_babylon);
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 FROM_TEMPLATE = "_fromTemplate";
var TEMPLATE_SKIP = (0, _symbol2.default)();
function useTemplate(ast, nodes) {
ast = (0, _cloneDeep2.default)(ast);
var _ast = ast,
program = _ast.program;
if (nodes.length) {
(0, _babelTraverse2.default)(ast, templateVisitor, null, nodes);
}
if (program.body.length > 1) {
return program.body;
} else {
return program.body[0];
}
}
var templateVisitor = {
noScope: true,
enter: function enter(path, args) {
var node = path.node;
if (node[TEMPLATE_SKIP]) return path.skip();
if (t.isExpressionStatement(node)) {
node = node.expression;
}
var replacement = void 0;
if (t.isIdentifier(node) && node[FROM_TEMPLATE]) {
if ((0, _has2.default)(args[0], node.name)) {
replacement = args[0][node.name];
} else if (node.name[0] === "$") {
var i = +node.name.slice(1);
if (args[i]) replacement = args[i];
}
}
if (replacement === null) {
path.remove();
}
if (replacement) {
replacement[TEMPLATE_SKIP] = true;
path.replaceInline(replacement);
}
},
exit: function exit(_ref) {
var node = _ref.node;
if (!node.loc) _babelTraverse2.default.clearNode(node);
}
};
module.exports = exports["default"];