From 9475dc681f123ca8218a00bb067179611037f2d9 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 1 Nov 2014 19:28:42 +1100 Subject: [PATCH] finish jsx generator --- lib/6to5/generators/jsx.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/6to5/generators/jsx.js b/lib/6to5/generators/jsx.js index 196a4780e4..fb0aadf93f 100644 --- a/lib/6to5/generators/jsx.js +++ b/lib/6to5/generators/jsx.js @@ -1,3 +1,5 @@ +var _ = require("lodash"); + exports.XJSAttribute = function (node, print) { print(node.name); if (node.value) { @@ -34,22 +36,44 @@ exports.XJSExpressionContainer = function (node, print) { this.push("}"); }; -exports.XJSElement = function () { - throw new Error("XJSElement"); +exports.XJSElement = function (node, print) { + var self = this; + + var open = node.openingElement; + print(open); + if (open.selfClosing) return; + + this.indent(); + _.each(node.children, function (child) { + if (child.type === "Literal" && typeof child.value === "string") { + if (/\S/.test(child.value)) { + return self.push(child.value.replace(/^\s+|\s+$/g, "")); + } else if (/\n/.test(child.value)) { + return self.newline(); + } + } + + print(child); + }); + this.dedent(); + + print(node.closingElement); }; exports.XJSOpeningElement = function (node, print) { this.push("<"); print(node.name); - if (node.attributes.length < 0) { + if (node.attributes.length > 0) { this.push(" "); this.printJoin(print, node.attributes, " "); } this.push(node.selfClosing ? " />" : ">");; }; -exports.XJSClosingElement = function (node) { - this.push(""); +exports.XJSClosingElement = function (node, print) { + this.push(""); }; exports.XJSEmptyExpression = function () {