1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const t = require("babel-types");
|
4 | function isString(node) {
|
5 | return t.isLiteral(node) && typeof node.value === 'string';
|
6 | }
|
7 | function buildBinaryExpression(left, right) {
|
8 | return t.binaryExpression('+', left, right);
|
9 | }
|
10 | function templateLiterals(path, state) {
|
11 | let nodes = [];
|
12 | const expressions = path.get('expressions');
|
13 | for (const elem of (path.node.quasis)) {
|
14 | nodes.push(t.stringLiteral(elem.value.cooked));
|
15 | const expr = expressions.shift();
|
16 | if (expr) {
|
17 |
|
18 | if (state.opts.spec && !expr.isBaseType('string') && !expr.isBaseType('number')) {
|
19 | nodes.push(t.callExpression(t.identifier('String'), [expr.node]));
|
20 | }
|
21 | else {
|
22 | nodes.push(expr.node);
|
23 | }
|
24 | }
|
25 | }
|
26 |
|
27 | nodes = nodes.filter((n) => !t.isLiteral(n, { value: '' }));
|
28 |
|
29 |
|
30 | if (!isString(nodes[0]) && !isString(nodes[1])) {
|
31 | nodes.unshift(t.stringLiteral(''));
|
32 | }
|
33 | if (nodes.length > 1) {
|
34 | let root = buildBinaryExpression(nodes.shift(), nodes.shift());
|
35 | for (const node of nodes) {
|
36 | root = buildBinaryExpression(root, node);
|
37 | }
|
38 | path.replaceWith(root);
|
39 | }
|
40 | else {
|
41 | path.replaceWith(nodes[0]);
|
42 | }
|
43 | }
|
44 | exports.templateLiterals = templateLiterals;
|
45 |
|
\ | No newline at end of file |