UNPKG

1.6 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const t = require("babel-types");
4function isString(node) {
5 return t.isLiteral(node) && typeof node.value === 'string';
6}
7function buildBinaryExpression(left, right) {
8 return t.binaryExpression('+', left, right);
9}
10function 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 // tslint:disable-next-line:no-multi-spaces
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 // filter out empty string literals
27 nodes = nodes.filter((n) => !t.isLiteral(n, { value: '' }));
28 // since `+` is left-to-right associative
29 // ensure the first node is a string if first/second isn't
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}
44exports.templateLiterals = templateLiterals;
45//# sourceMappingURL=plugins.js.map
\No newline at end of file