@croquiscom/babel-plugin-mjsx
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -98,18 +98,8 @@ 'use strict'; | ||
function buildChildren(node) { | ||
return (node.children || node.elements).map(convertAttributeValue).filter(function (child) { | ||
return node.children.map(convertAttributeValue).filter(function (child) { | ||
return !t.isJSXEmptyExpression(child); | ||
}).map(function (child) { | ||
if (t.isStringLiteral(child) || t.isJSXText(child)) { | ||
var text = cleanStringLiteral(child.value); | ||
if (!text) { | ||
return; | ||
} | ||
var newChild = t.objectExpression([]); | ||
newChild.properties.push(t.objectProperty(t.identifier('tag'), t.stringLiteral('#'))); | ||
newChild.properties.push(t.objectProperty(t.identifier('children'), text)); | ||
return newChild; | ||
return cleanStringLiteral(child.value); | ||
} | ||
if (t.isArrayExpression(child)) { | ||
return t.arrayExpression(buildChildren(child)); | ||
} | ||
return child; | ||
@@ -158,11 +148,2 @@ }).filter(function (child) { | ||
function getLoneText(node) { | ||
if (node.children.length === 1) { | ||
var child = convertAttributeValue(node.children[0]); | ||
if (t.isStringLiteral(child) || t.isJSXText(child)) { | ||
return child.value; | ||
} | ||
} | ||
} | ||
return { | ||
@@ -211,11 +192,3 @@ inherits: require('babel-plugin-syntax-jsx'), | ||
if (node.children.length) { | ||
var loneText = getLoneText(node); | ||
if (!isComp && loneText) { | ||
loneText = cleanStringLiteral(loneText); | ||
if (loneText) { | ||
pushElemProp('text', loneText); | ||
} | ||
} else { | ||
pushElemProp('children', t.arrayExpression(flatten(buildChildren(node)))); | ||
} | ||
pushElemProp('children', t.arrayExpression(flatten(buildChildren(node)))); | ||
} | ||
@@ -265,40 +238,39 @@ | ||
if (isComp) { | ||
var properties = obj.properties; | ||
var attrs = t.objectExpression([]); | ||
var children = t.arrayExpression([]); | ||
var properties = obj.properties; | ||
var attrs = t.objectExpression([]); | ||
var children = t.arrayExpression([]); | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = properties[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var child = _step3.value; | ||
try { | ||
for (var _iterator3 = properties[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var child = _step3.value; | ||
if (child.key.name === 'attrs') { | ||
attrs = child.value; | ||
} else if (child.key.name === 'children') { | ||
children = child.value; | ||
} | ||
if (child.key.name === 'attrs') { | ||
attrs = child.value; | ||
} else if (child.key.name === 'children') { | ||
children = child.value; | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
path.replaceWith(t.callExpression(t.identifier('m'), [t.identifier(tag.value), attrs, children]), node); | ||
} else { | ||
path.replaceWith(obj, node); | ||
if (isComp) { | ||
tag = t.identifier(tag.value); | ||
} | ||
path.replaceWith(t.callExpression(t.identifier('m'), [tag, attrs, children]), node); | ||
} | ||
@@ -305,0 +277,0 @@ } |
{ | ||
"name": "@croquiscom/babel-plugin-mjsx", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Mithril precompilation JSX plugin for babel", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
"use strict"; | ||
result = { | ||
tag: "div", | ||
attrs: { disabled: true, readonly: false } | ||
}; | ||
result = m("div", { disabled: true, readonly: false }, []); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
attrs: { className: "123" } | ||
}; | ||
result = m("div", { className: "123" }, []); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
children: [] | ||
}; | ||
result = m("div", {}, []); |
@@ -5,9 +5,2 @@ "use strict"; | ||
result = { | ||
tag: "div", | ||
children: [m(Component, { foo: "bar" }, [{ | ||
tag: "#", | ||
children: "Component children" | ||
}])], | ||
attrs: _extends({ style: "1" }, a) | ||
}; | ||
result = m("div", _extends({ style: "1" }, a), [m(Component, { foo: "bar" }, ["Component children"])]); |
"use strict"; | ||
result = { | ||
tag: "blink", | ||
text: "NO!" | ||
}; | ||
result = m("blink", {}, ["NO!"]); |
@@ -1,3 +0,1 @@ | ||
result = { | ||
tag: "div" | ||
}; | ||
result = m("div", {}, []); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
children: [{ | ||
tag: "label", | ||
attrs: { htmlFor: "hello" } | ||
}], | ||
attrs: { onclick: function onclick() {} } | ||
}; | ||
result = m("div", { onclick: function onclick() {} }, [m("label", { htmlFor: "hello" }, [])]); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
attrs: { className: "class1 class2", id: "myid", title: "hello" } | ||
}; | ||
result = m("div", { className: "class1 class2", id: "myid", title: "hello" }, []); |
"use strict"; | ||
result = { | ||
tag: "html", | ||
children: [{ | ||
tag: "head", | ||
children: [{ | ||
tag: "title", | ||
text: "Hello World" | ||
}, { | ||
tag: "style", | ||
children: [{ | ||
tag: "#", | ||
children: "* { box-sizing: border-box; }" | ||
}], | ||
attrs: { type: "text/css" } | ||
}, { | ||
tag: "script", | ||
attrs: { src: "./file.js", type: "text/javascript" } | ||
}, { | ||
tag: "meta", | ||
attrs: { name: "encoding", value: "utf8" } | ||
}] | ||
}, { | ||
tag: "body", | ||
children: [{ | ||
tag: "div" | ||
}, { | ||
tag: "a", | ||
text: "bar", | ||
attrs: { href: "foo" } | ||
}, { | ||
tag: "br" | ||
}] | ||
}] | ||
}; | ||
result = m("html", {}, [m("head", {}, [m("title", {}, ["Hello World"]), m("style", { type: "text/css" }, ["* { box-sizing: border-box; }"]), m("script", { src: "./file.js", type: "text/javascript" }, []), m("meta", { name: "encoding", value: "utf8" }, [])]), m("body", {}, [m("div", {}, []), m("a", { href: "foo" }, ["bar"]), m("br", {}, [])])]); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
attrs: { "if": "kw", "for": "kw", of: "notkw", "debugger": "kw", async: "notkw", "this": "kw" } | ||
}; | ||
result = m("div", { "if": "kw", "for": "kw", of: "notkw", "debugger": "kw", async: "notkw", "this": "kw" }, []); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
attrs: { title: "123" } | ||
}; | ||
result = m("div", { title: "123" }, []); |
@@ -5,9 +5,2 @@ "use strict"; | ||
result = { | ||
tag: "div", | ||
children: [{ | ||
tag: "div", | ||
attrs: props | ||
}], | ||
attrs: _extends({ className: "test", id: id }, props, { key: "test", "data-expanded": expanded }, props.attrs) | ||
}; | ||
result = m("div", _extends({ className: "test", id: id }, props, { key: "test", "data-expanded": expanded }, props.attrs), [m("div", props, [])]); |
"use strict"; | ||
result = { | ||
tag: "div", | ||
children: [{ | ||
tag: "#", | ||
children: "Hello World!" | ||
}, { | ||
tag: "div", | ||
text: " Bonjour " | ||
}, { | ||
tag: "div", | ||
text: "What's up doc?" | ||
}, { | ||
tag: "div", | ||
children: [[{ | ||
tag: "#", | ||
children: "More " | ||
}, { | ||
tag: "span", | ||
text: "·" | ||
}, { | ||
tag: "#", | ||
children: " Text here" | ||
}]] | ||
}] | ||
}; | ||
result = m("div", {}, ["Hello World!", m("div", {}, [" Bonjour "]), m("div", {}, ["What's up doc?"]), m("div", {}, [["More ", m("span", {}, ["·"]), " Text here"]])]); |
@@ -5,5 +5,2 @@ "use strict"; | ||
// JS calls toString on the key of objects | ||
result = { | ||
tag: "div", | ||
attrs: { title: title } | ||
}; | ||
result = m("div", { title: title }, []); |
20187
454