@dolphinweex/dof-weex-vue-precompiler
Version:
a precompiler for weex-vue-render.
56 lines (51 loc) • 1.22 kB
JavaScript
const esprima = require('esprima-next')
exports.genPropNode = function (k, v) {
return {
type: 'Property',
key: {
type: 'Identifier',
name: k
},
computed: false,
value: {
type: 'Literal',
value: v
},
kind: 'init',
method: false,
shorthand: false
}
}
exports.parseAst = function (val) {
let statement = 'a = '
if (typeof val === 'object') {
statement += `${JSON.stringify(val)}`
}
else {
statement += val
}
const ast = esprima.parse(statement)
.body[0].expression.right
return ast
}
const forAliasRE = /([^]*?)\s+(?:in|of)\s+([^]*)/
const forIteratorRE = /,([^,\}\]]*)(?:,([^,\}\]]*))?$/
const stripParensRE = /^\(|\)$/g
exports.parseFor = function (exp) {
const inMatch = exp.match(forAliasRE)
if (!inMatch) return
const res = {}
res.for = inMatch[2].trim()
const alias = inMatch[1].trim().replace(stripParensRE, '')
const iteratorMatch = alias.match(forIteratorRE)
if (iteratorMatch) {
res.alias = alias.replace(forIteratorRE, '')
res.iterator1 = iteratorMatch[1].trim()
if (iteratorMatch[2]) {
res.iterator2 = iteratorMatch[2].trim()
}
} else {
res.alias = alias
}
return res
}