babel-plugin-hyperlit
Advanced tools
Comparing version 0.0.1 to 0.0.2
import {h} from 'hyperapp' | ||
export default (tag, props, children) => { | ||
children = children.flat(2) | ||
children = children.flat() | ||
if (typeof tag === 'function') return tag(props, children) | ||
return h(tag, props, children) | ||
} |
96
index.js
@@ -0,1 +1,2 @@ | ||
const NEXT = 0 | ||
@@ -14,5 +15,4 @@ const TEXT = 1 | ||
const parser = (h) => { | ||
const parse = (strs, vals, jstart, istart) => { | ||
const parser = h => { | ||
const parse = (strs, vals, jstart, istart) => { | ||
let ch, | ||
@@ -28,24 +28,41 @@ buffer = '', | ||
const makenode = children => { | ||
list.push(h(tagname, props, children)) | ||
mode = NEXT | ||
} | ||
const gotText = trim => { | ||
if (trim) buffer = buffer.trimEnd() | ||
if (!buffer) return | ||
list.push(buffer) | ||
buffer = '' | ||
} | ||
const recurse = () => { | ||
let r = parse(strs, vals, j, i + 1) | ||
list.push(h(tagname, props, r[0])) | ||
makenode(r[0]) | ||
j = r[1] | ||
i = r[2] | ||
mode = NEXT | ||
} | ||
const gotTagName = () => { | ||
const gotTagName = (m=mode) => { | ||
tagname = buffer | ||
props = [{}] | ||
mode = m | ||
} | ||
const gotText = () => { | ||
list.push(buffer.trim()) | ||
const gotContent = () => { | ||
if(vals[j]) list.push(vals[j]) | ||
} | ||
const defaultProp = (m = mode) => { | ||
props[0][buffer] = true | ||
mode = m | ||
} | ||
const gotProp = v => { | ||
props[0][propname] = v | ||
mode = PROPS | ||
} | ||
for (j = jstart; j < strs.length; j++) { | ||
if (mode == PROPVAL) { | ||
props[0][propname] = vals[j - 1] | ||
mode = PROPS | ||
} | ||
for (i = istart; i < strs[j].length; i++) { | ||
@@ -56,10 +73,9 @@ ch = strs[j][i] | ||
mode = TAG | ||
tagname = '' | ||
} else if (!ws(ch)) { | ||
mode = TEXT | ||
buffer = ch | ||
mode = TEXT | ||
} | ||
} else if (mode == TEXT) { | ||
if (ch == '<') { | ||
gotText() | ||
gotText(true) | ||
mode = TAG | ||
@@ -82,7 +98,5 @@ } else { | ||
if (ws(ch)) { | ||
gotTagName() | ||
mode = PROPS | ||
gotTagName(PROPS) | ||
} else if (ch == '/') { | ||
gotTagName() | ||
mode = SELFCLOSING | ||
gotTagName(SELFCLOSING) | ||
} else if (ch == '>') { | ||
@@ -96,4 +110,3 @@ gotTagName() | ||
if (ch == '>') { | ||
list.push(h(tagname, props, [])) | ||
mode = NEXT | ||
makenode([]) | ||
} | ||
@@ -114,2 +127,9 @@ } else if (mode == PROPS) { | ||
mode = PROPVAL | ||
} else if (ch == '>') { | ||
defaultProp() | ||
recurse() | ||
} else if (ch == '/') { | ||
defaultProp(SELFCLOSING) | ||
} else if (ws(ch)) { | ||
defaultProp(PROPS) | ||
} else { | ||
@@ -121,10 +141,9 @@ buffer += ch | ||
mode = PROPVALSTR | ||
buffer = '' | ||
buffer = [''] | ||
} | ||
} else if (mode == PROPVALSTR) { | ||
if (ch == '"') { | ||
props[0][propname] = buffer | ||
mode = PROPS | ||
gotProp(buffer) | ||
} else { | ||
buffer += ch | ||
buffer[buffer.length - 1] += ch | ||
} | ||
@@ -139,28 +158,21 @@ } | ||
} else if (mode == TEXT) { | ||
gotText() | ||
mode = NEXT | ||
gotText(!vals[j]) | ||
gotContent() | ||
} else if (mode == PROPS) { | ||
props = [ ...props, vals[j] ] | ||
} else if (mode == PROPVAL) { | ||
gotProp(vals[j]) | ||
} else if (mode == PROPVALSTR) { | ||
props[0][propname] = vals[j] | ||
mode = PROPS | ||
istart = 1 | ||
buffer.push(vals[j]) | ||
} else if (mode == NEXT) { | ||
gotContent() | ||
} | ||
if (j < strs.length - 1 && mode == NEXT) { | ||
list = [...list, vals[j]] | ||
} | ||
} | ||
if (mode == TEXT) { | ||
gotText() | ||
} | ||
list = list.length == 1 ? list[0] : list | ||
return [list, j, i] | ||
return [list.length == 1 ? list[0] : list, j, i] | ||
} | ||
return (strs, ...vals) => parse(strs, vals, 0, 0)[0] | ||
} | ||
module.exports = ({types: t}) => { | ||
@@ -188,4 +200,4 @@ | ||
node = t.binaryExpression('+', node, value); | ||
}); | ||
}); | ||
return t.objectProperty(propertyName(key), node); | ||
@@ -192,0 +204,0 @@ }); |
{ | ||
"name": "babel-plugin-hyperlit", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "babel transform for hyperlit to hyperapp", | ||
@@ -9,13 +9,4 @@ "main": "index.js", | ||
}, | ||
"devDependencies": {}, | ||
"scripts": { | ||
"test": "babel ./src.js" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"babel": { | ||
"plugins": [ | ||
"./index.js" | ||
] | ||
} | ||
"license": "MIT" | ||
} |
8089
3
223