Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim de Beer committed Jul 16, 2017
1 parent 2b03fab commit b52e19a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 36 deletions.
5 changes: 0 additions & 5 deletions src/transpile/element/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const parseExpressionContainer = (status, node) => {
// also need to take status path into account for subscriptions
if (prop.type === 'struct') {
const listeners = getListeners(status, 'new')

// status from current subs path
// most simple

Expand All @@ -47,11 +46,8 @@ const parseExpressionContainer = (status, node) => {
}
), 'update')
// REUSE

// default value === '' since we use it as text here...

// remove needs to be handled seperately -- then it will become an empty string as well

const id = ++status.id
let newValue, updateValue
if (prop.expression.type === 'inline') {
Expand All @@ -62,7 +58,6 @@ const parseExpressionContainer = (status, node) => {
? `s.get(${prop.val.map(string).join(',')}, '').compute()`
: `s.get([${prop.val.map(string).join(',')}], '').compute()`
)

// this is too simplistic only make the var one time then re-use
updateValue = prop.expression.val.replace(new RegExp(prop.key, 'g'), 's.compute()')
}
Expand Down
23 changes: 9 additions & 14 deletions src/transpile/element/expression.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { walker, showcode, isEqual, extractPath } = require('../util')
const { walker, isEqual, extractPath } = require('../util')
var cnt = 0

const createPropFromExpression = (status, node) => {
Expand All @@ -19,33 +19,28 @@ const createPropFromExpression = (status, node) => {
while (i--) {
const arg = args.val[i]
if (arg.val === name) {
showcode(status.code, child)
const path = extractPath(status, child)
console.log('PATH:', path)

if (!props) {
if (!val.type) {
val.type = 'struct'
val.key = `__${++cnt}__`
// PATHS!

val.key = `__${++cnt}__` // this is qwrong
val.val = path
const replacement = arg.default
? `(${val.key} || ${arg.default})`
: val.key
: val.key // call this replacement key

// path resolve when there is a prop

val.expression = { type: 'inline', replacement: [] }
val.expression.replacement.push([ child, replacement ])
} else {
// also not enough key can be an array....
// isEqual (array)
//
// console.log(args.val[i], val.val)
// not enough need more e.g. paths
if (args.val[i].key === val.val[0]) {
// this cam become a util
if (isEqual(path, val.val)) {
console.log('isEqual dont reparse', val)
// if multiple add reference and re-write all keys
// also dont use val.key make it expression.key
// if a reference parse all reffed things? and replace the keys
// need to think of a system for this
const replacement = arg.default
? `(${val.key} || ${arg.default})`
: val.key
Expand Down
8 changes: 2 additions & 6 deletions src/transpile/element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ const parseJSXElement = (status, node) => {
// type === 'empty'
// type === 'ObjectPattern'
// type === 'Identifier'
// status.props
// if !props all is subscription, also need to get subscription path so you know where you are
// add it on status
const openingElement = node.openingElement
if (openingElement.id) openingElement.id = false // nodes are re-used so need to become false
const parentId = createElement(status, openingElement)
Expand All @@ -53,7 +50,6 @@ const parseJSXElement = (status, node) => {
plainText(status, child)
// parseAttributes(status, child, parentId)
})
// nested functions for events and any for example
return parentId
}

Expand Down Expand Up @@ -91,11 +87,11 @@ exports.parseElement = (status, node) => {
// here you dont need to parse the args at all
// find jsx element else there is switch / any involved
parseJSXElement(
merge(status, { args: status.args || { type: 'empty' } }),
merge(status, { args: status.args || { type: 'Empty' } }),
node
)
} else {
console.log('parseElement: cannot parse as element!')
console.log('parseElement: cannot parse node as element!')
showcode(status.code, node)
}
}
14 changes: 4 additions & 10 deletions src/transpile/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,24 @@ const showcode = (str, start, end) => { // eslint-disable-line
}

const extractPath = (status, node) => {
// if (node.type === 'MemberExpression')
if (node.parent && node.parent.type === 'MemberExpression') {
// console.log('ok is an object')
const path = []
// showcode(status, node.parent.parent)
let child = node.parent
if (node.type === 'Identifier') {
path.push(node.name)
}
while (child && child.type === 'MemberExpression') {
if (child.parent && child.parent.type === 'CallExpression') {
// do special shit here
// console.log(child.parent)
// do special shit here -- need to inline .root .parent
// make hooks for those perhaps? --- method calls are special
// also make it possible to add root in your path with a default val?
break
}
path.push(child.property.name)
child = child.parent
}
// path.push(node.name)
console.log('||| --->')
return path
// while check for properties
} else if (node.type === 'Identifier') {
console.log('isIdentifier')
return [ node.name ]
}
}
Expand All @@ -98,7 +92,7 @@ const assembleFunctions = subs => {
}

const string = val => {
// do checks if its no allready like this
// do checks if its not allready like this
return `'${val}'`
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const fs = require('fs')
const { join } = require('path')
const parse = require('../')
const test = require('tape')
// const test = require('tape')

const createTest = (folder, file) => {
const code = fs.readFileSync(join(__dirname, folder, file + '.js')).toString()
Expand Down
1 change: 1 addition & 0 deletions test/props/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const App = ({ field }) => {
<h1>X{field.nested}X</h1>
<h2>{field.a.b.c.toUpperCase()}</h2>
<h3>{field}</h3>
<h4>X{field.nested.a + '!' + field.nested.a}</h4>
</div>
}

Expand Down
14 changes: 14 additions & 0 deletions test/props/result/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports =
var _6 = tree._[6] = document.createElement('h3')
_1.appendChild(_6)
_6.appendChild((tree._[7] = document.createTextNode(s.get('field', '').compute())))
var _8 = tree._[8] = document.createElement('h4')
_1.appendChild(_8)
_8.appendChild(document.createTextNode('X'))
_8.appendChild((tree._[9] = document.createTextNode(s.get(['field', 'nested', 'a'], '').compute().nested.a + '!' + s.get(['field', 'nested', 'a'], '').compute().nested.a)))
}
},
field:
Expand All @@ -39,6 +43,16 @@ module.exports =
update: (s, type, subs, tree) => {
getParent(tree, 3).nodeValue = s.compute().nested
}
},
a:
{
val: 'shallow',
_:
{
update: (s, type, subs, tree) => {
getParent(tree, 9).nodeValue = s.compute().nested.a + '!' + s.compute().nested.a
}
}
}
},
a:
Expand Down

0 comments on commit b52e19a

Please sign in to comment.