jsx.js 2.9 KB

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