|
| 1 | +/** @import { Handlers } from '../types' */ |
| 2 | + |
| 3 | +import { handle, indent, dedent, newline } from '../handlers'; |
| 4 | + |
| 5 | +/** @type {Handlers} */ |
| 6 | +export default { |
| 7 | + JSXElement(node, state) { |
| 8 | + handle(node.openingElement, state); |
| 9 | + |
| 10 | + if (node.children.length > 0) { |
| 11 | + state.commands.push(indent); |
| 12 | + } |
| 13 | + for (const child of node.children) { |
| 14 | + handle(child, state); |
| 15 | + if (child !== node.children.at(-1)) { |
| 16 | + state.commands.push(newline) |
| 17 | + } |
| 18 | + } |
| 19 | + if (node.children.length > 0) { |
| 20 | + state.commands.push(dedent); |
| 21 | + state.commands.push(newline); |
| 22 | + } |
| 23 | + |
| 24 | + if (node.closingElement) { |
| 25 | + handle(node.closingElement, state); |
| 26 | + } |
| 27 | + }, |
| 28 | + JSXOpeningElement(node, state) { |
| 29 | + state.commands.push('<'); |
| 30 | + |
| 31 | + handle(node.name, state); |
| 32 | + |
| 33 | + for (const attribute of node.attributes) { |
| 34 | + state.commands.push(' '); |
| 35 | + handle(attribute, state); |
| 36 | + } |
| 37 | + |
| 38 | + if (node.selfClosing) { |
| 39 | + state.commands.push(' /'); |
| 40 | + } |
| 41 | + |
| 42 | + state.commands.push('>'); |
| 43 | + }, |
| 44 | + JSXClosingElement(node, state) { |
| 45 | + state.commands.push('</'); |
| 46 | + |
| 47 | + handle(node.name, state); |
| 48 | + |
| 49 | + state.commands.push('>'); |
| 50 | + }, |
| 51 | + JSXNamespacedName(node, state) { |
| 52 | + handle(node.namespace, state); |
| 53 | + state.commands.push(':'); |
| 54 | + handle(node.name, state); |
| 55 | + }, |
| 56 | + JSXIdentifier(node, state) { |
| 57 | + state.commands.push(node.name); |
| 58 | + }, |
| 59 | + JSXMemberExpression(node, state) { |
| 60 | + handle(node.object, state); |
| 61 | + state.commands.push('.'); |
| 62 | + handle(node.property, state); |
| 63 | + }, |
| 64 | + JSXText(node, state) { |
| 65 | + state.commands.push(node.value); |
| 66 | + }, |
| 67 | + JSXAttribute(node, state) { |
| 68 | + handle(node.name, state); |
| 69 | + if (node.value) { |
| 70 | + state.commands.push('='); |
| 71 | + handle(node.value, state); |
| 72 | + } |
| 73 | + }, |
| 74 | + JSXEmptyExpression(node, state) {}, |
| 75 | + JSXFragment(node, state) { |
| 76 | + handle(node.openingFragment, state); |
| 77 | + |
| 78 | + if (node.children.length > 0) { |
| 79 | + state.commands.push(indent); |
| 80 | + } |
| 81 | + for (const child of node.children) { |
| 82 | + handle(child, state); |
| 83 | + |
| 84 | + if (child !== node.children.at(-1)) { |
| 85 | + state.commands.push(newline); |
| 86 | + } |
| 87 | + } |
| 88 | + if (node.children.length > 0) { |
| 89 | + state.commands.push(dedent); |
| 90 | + } |
| 91 | + |
| 92 | + handle(node.closingFragment, state); |
| 93 | + }, |
| 94 | + JSXOpeningFragment(node, state) { |
| 95 | + state.commands.push('<>'); |
| 96 | + }, |
| 97 | + JSXClosingFragment(node, state) { |
| 98 | + state.commands.push('</>'); |
| 99 | + }, |
| 100 | + JSXExpressionContainer(node, state) { |
| 101 | + state.commands.push('{'); |
| 102 | + |
| 103 | + handle(node.expression, state); |
| 104 | + |
| 105 | + state.commands.push('}'); |
| 106 | + }, |
| 107 | + JSXSpreadChild(node, state) { |
| 108 | + state.commands.push('{...'); |
| 109 | + |
| 110 | + handle(node.expression, state); |
| 111 | + |
| 112 | + state.commands.push('}'); |
| 113 | + }, |
| 114 | + JSXSpreadAttribute(node, state) { |
| 115 | + state.commands.push('{...'); |
| 116 | + |
| 117 | + handle(node.argument, state); |
| 118 | + |
| 119 | + state.commands.push('}'); |
| 120 | + } |
| 121 | +}; |
0 commit comments