提交 1f1229dd 编写于 作者: fxy060608's avatar fxy060608

fix(cli): output resource path for compiler errors #604

上级 535a3610
...@@ -585,6 +585,11 @@ describe('mp:compiler-extra', () => { ...@@ -585,6 +585,11 @@ describe('mp:compiler-extra', () => {
) )
}) })
it('generate event ', () => { it('generate event ', () => {
assertCodegen(
`<view @/>`,
`<view></view>`
)
assertCodegen( assertCodegen(
`<text v-for="item in items['metas']" :key="item['id']" class="title" @tap="handle(item['id'],item['title'])">{{item.title}}</text>`, `<text v-for="item in items['metas']" :key="item['id']" class="title" @tap="handle(item['id'],item['title'])">{{item.title}}</text>`,
`<block wx:for="{{items['metas']}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><text data-event-opts="{{[['tap',[['handle',['$0','$1'],[[['items.metas','id',item['id'],'id']],[['items.metas','id',item['id'],'title']]]]]]]}}" class="title" bindtap="__e">{{item.title}}</text></block>` `<block wx:for="{{items['metas']}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><text data-event-opts="{{[['tap',[['handle',['$0','$1'],[[['items.metas','id',item['id'],'id']],[['items.metas','id',item['id'],'title']]]]]]]}}" class="title" bindtap="__e">{{item.title}}</text></block>`
......
...@@ -2,7 +2,7 @@ const compiler = require('../lib') ...@@ -2,7 +2,7 @@ const compiler = require('../lib')
const res = compiler.compile( const res = compiler.compile(
` `
<view v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/> <view @/>
`, { `, {
resourcePath: '/User/fxy/Documents/test.wxml', resourcePath: '/User/fxy/Documents/test.wxml',
mp: { mp: {
......
...@@ -55,10 +55,18 @@ module.exports = { ...@@ -55,10 +55,18 @@ module.exports = {
} }
// console.log(`function render(){${res.render}}`) // console.log(`function render(){${res.render}}`)
const ast = parser.parse(`function render(){${res.render}}`) const ast = parser.parse(`function render(){${res.render}}`)
let template = ''
try {
res.render = generateScript(traverseScript(ast, state), state) res.render = generateScript(traverseScript(ast, state), state)
template = generateTemplate(traverseTemplate(ast, state), state)
let template = generateTemplate(traverseTemplate(ast, state), state) } catch (e) {
console.error(e)
throw new Error('Compile failed at ' + options.resourcePath.replace(
path.extname(options.resourcePath),
'.vue'
))
}
res.specialMethods = state.options.specialMethods || new Set() res.specialMethods = state.options.specialMethods || new Set()
delete state.options.specialMethods delete state.options.specialMethods
......
...@@ -202,7 +202,7 @@ function parseEventByCallExpression (callExpr, methods) { ...@@ -202,7 +202,7 @@ function parseEventByCallExpression (callExpr, methods) {
function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, tagName, ret) { function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, tagName, ret) {
const key = keyPath.node const key = keyPath.node
let type = key.value || key.name let type = key.value || key.name || ''
const isCustom = isComponent && !isNativeOn const isCustom = isComponent && !isNativeOn
...@@ -211,6 +211,9 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -211,6 +211,9 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
let isPassive = false let isPassive = false
let isOnce = false let isOnce = false
let methods = []
if (type) {
isPassive = type.charAt(0) === VUE_EVENT_MODIFIERS.passive isPassive = type.charAt(0) === VUE_EVENT_MODIFIERS.passive
type = isPassive ? type.slice(1) : type type = isPassive ? type.slice(1) : type
...@@ -223,8 +226,6 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -223,8 +226,6 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
const specialEvents = state.options.platform.specialEvents const specialEvents = state.options.platform.specialEvents
const isSpecialEvent = specialEvents[tagName] && Object.keys(specialEvents[tagName]).includes(type) const isSpecialEvent = specialEvents[tagName] && Object.keys(specialEvents[tagName]).includes(type)
let methods = []
if (!valuePath.isArrayExpression()) { if (!valuePath.isArrayExpression()) {
valuePath = [valuePath] valuePath = [valuePath]
} else { } else {
...@@ -329,6 +330,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -329,6 +330,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
} }
} }
}) })
}
return { return {
type, type,
...@@ -345,6 +347,10 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false, ...@@ -345,6 +347,10 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
function _processEvent (path, state, isComponent, isNativeOn = false, tagName, ret) { function _processEvent (path, state, isComponent, isNativeOn = false, tagName, ret) {
const opts = [] const opts = []
// remove invalid event
path.node.value.properties = path.node.value.properties.filter(property => {
return property.key.value || property.key.name
})
const len = path.node.value.properties.length const len = path.node.value.properties.length
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
const propertyPath = path.get(`value.properties.${i}`) const propertyPath = path.get(`value.properties.${i}`)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册