| 1 | import type Printer from "../printer"; |
| 2 | import type * as t from "@babel/types"; |
| 3 | |
| 4 | export function TaggedTemplateExpression( |
| 5 | this: Printer, |
| 6 | node: t.TaggedTemplateExpression, |
| 7 | ) { |
| 8 | this.print(node.tag, node); |
| 9 | this.print(node.typeParameters, node); // TS |
| 10 | this.print(node.quasi, node); |
| 11 | } |
| 12 | |
| 13 | export function TemplateElement( |
| 14 | this: Printer, |
| 15 | node: t.TemplateElement, |
| 16 | parent: t.TemplateLiteral, |
| 17 | ) { |
| 18 | const isFirst = parent.quasis[0] === node; |
| 19 | const isLast = parent.quasis[parent.quasis.length - 1] === node; |
| 20 | |
| 21 | const value = (isFirst ? "`" : "}") + node.value.raw + (isLast ? "`" : "${"); |
| 22 | |
| 23 | this.token(value, true); |
| 24 | } |
| 25 | |
| 26 | export function TemplateLiteral(this: Printer, node: t.TemplateLiteral) { |
| 27 | const quasis = node.quasis; |
| 28 | |
| 29 | for (let i = 0; i < quasis.length; i++) { |
| 30 | this.print(quasis[i], node); |
| 31 | |
| 32 | if (i + 1 < quasis.length) { |
| 33 | this.print(node.expressions[i], node); |
| 34 | } |
| 35 | } |
| 36 | } |