| 1 | import type Printer from "../printer"; |
| 2 | import type * as t from "@babel/types"; |
| 3 | |
| 4 | export function JSXAttribute(this: Printer, node: t.JSXAttribute) { |
| 5 | this.print(node.name, node); |
| 6 | if (node.value) { |
| 7 | this.token("="); |
| 8 | this.print(node.value, node); |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | export function JSXIdentifier(this: Printer, node: t.JSXIdentifier) { |
| 13 | this.word(node.name); |
| 14 | } |
| 15 | |
| 16 | export function JSXNamespacedName(this: Printer, node: t.JSXNamespacedName) { |
| 17 | this.print(node.namespace, node); |
| 18 | this.token(":"); |
| 19 | this.print(node.name, node); |
| 20 | } |
| 21 | |
| 22 | export function JSXMemberExpression( |
| 23 | this: Printer, |
| 24 | node: t.JSXMemberExpression, |
| 25 | ) { |
| 26 | this.print(node.object, node); |
| 27 | this.token("."); |
| 28 | this.print(node.property, node); |
| 29 | } |
| 30 | |
| 31 | export function JSXSpreadAttribute(this: Printer, node: t.JSXSpreadAttribute) { |
| 32 | this.token("{"); |
| 33 | this.token("..."); |
| 34 | this.print(node.argument, node); |
| 35 | this.token("}"); |
| 36 | } |
| 37 | |
| 38 | export function JSXExpressionContainer( |
| 39 | this: Printer, |
| 40 | node: t.JSXExpressionContainer, |
| 41 | ) { |
| 42 | this.token("{"); |
| 43 | this.print(node.expression, node); |
| 44 | this.token("}"); |
| 45 | } |
| 46 | |
| 47 | export function JSXSpreadChild(this: Printer, node: t.JSXSpreadChild) { |
| 48 | this.token("{"); |
| 49 | this.token("..."); |
| 50 | this.print(node.expression, node); |
| 51 | this.token("}"); |
| 52 | } |
| 53 | |
| 54 | export function JSXText(this: Printer, node: t.JSXText) { |
| 55 | const raw = this.getPossibleRaw(node); |
| 56 | |
| 57 | if (raw !== undefined) { |
| 58 | this.token(raw, true); |
| 59 | } else { |
| 60 | this.token(node.value, true); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | export function JSXElement(this: Printer, node: t.JSXElement) { |
| 65 | const open = node.openingElement; |
| 66 | this.print(open, node); |
| 67 | if (open.selfClosing) return; |
| 68 | |
| 69 | this.indent(); |
| 70 | for (const child of node.children) { |
| 71 | this.print(child, node); |
| 72 | } |
| 73 | this.dedent(); |
| 74 | |
| 75 | this.print(node.closingElement, node); |
| 76 | } |
| 77 | |
| 78 | function spaceSeparator(this: Printer) { |
| 79 | this.space(); |
| 80 | } |
| 81 | |
| 82 | export function JSXOpeningElement(this: Printer, node: t.JSXOpeningElement) { |
| 83 | this.token("<"); |
| 84 | this.print(node.name, node); |
| 85 | this.print(node.typeParameters, node); // TS |
| 86 | if (node.attributes.length > 0) { |
| 87 | this.space(); |
| 88 | this.printJoin(node.attributes, node, { separator: spaceSeparator }); |
| 89 | } |
| 90 | if (node.selfClosing) { |
| 91 | this.space(); |
| 92 | this.token("/>"); |
| 93 | } else { |
| 94 | this.token(">"); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | export function JSXClosingElement(this: Printer, node: t.JSXClosingElement) { |
| 99 | this.token("</"); |
| 100 | this.print(node.name, node); |
| 101 | this.token(">"); |
| 102 | } |
| 103 | |
| 104 | export function JSXEmptyExpression(this: Printer) { |
| 105 | // This node is empty, so forcefully print its inner comments. |
| 106 | this.printInnerComments(); |
| 107 | } |
| 108 | |
| 109 | export function JSXFragment(this: Printer, node: t.JSXFragment) { |
| 110 | this.print(node.openingFragment, node); |
| 111 | |
| 112 | this.indent(); |
| 113 | for (const child of node.children) { |
| 114 | this.print(child, node); |
| 115 | } |
| 116 | this.dedent(); |
| 117 | |
| 118 | this.print(node.closingFragment, node); |
| 119 | } |
| 120 | |
| 121 | export function JSXOpeningFragment(this: Printer) { |
| 122 | this.token("<"); |
| 123 | this.token(">"); |
| 124 | } |
| 125 | |
| 126 | export function JSXClosingFragment(this: Printer) { |
| 127 | this.token("</"); |
| 128 | this.token(">"); |
| 129 | } |