Skip to content

Commit

Permalink
Fix bug: duplicate "h"
Browse files Browse the repository at this point in the history
  • Loading branch information
AkimotoAkari committed Aug 17, 2018
1 parent 3c6dcca commit 2a584ec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ module.exports = function (babel) {
if (isInsideJsxExpression(t, path)) {
return
}
// do nothing if there is a `const h = ...` already
const duplicateHChecker = {
hasH: false
}
path.traverse({
Declaration (path) {
const container = path.container[0]
if (['const', 'let'].includes(container.kind) && container.declarations[0].id.name === 'h') {
console.warn('Duplicate declaration "h" detected. Plugin will not inject h.')
this.hasH = true
}
}
}, duplicateHChecker)
if (duplicateHChecker.hasH) {
return
}
const isRender = path.node.key.name === 'render'
// inject h otherwise
path.get('body').unshiftContainer('body', t.variableDeclaration('const', [
Expand Down
54 changes: 54 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,60 @@ describe('babel-plugin-transform-vue-jsx', () => {
expect(nested.children[0].text).to.equal('bar')
})

it('duplicate h from this should be skipped', () => {
const fun = (obj) => {
const vnode = render(h => obj.rende.call({ $createElement: h }))
expect(vnode.tag).to.equal('div')
expect(vnode.children[0].text).to.equal('test')
}

const exported = {
methods: {
callFun () {
fun({
testProp: 1,
rende () {
/* eslint-disable no-unused-vars */
const h = this.$createElement
return (
<div>test</div>
)
}
})
}
}
}

exported.methods.callFun()
})

it('duplicate h from arguments should be skipped', () => {
const fun = (obj) => {
const vnode = render(h => obj.rende.call({ $createElement: () => {
throw new Error('this.$createElement should not be called at this time.')
} }, h))
expect(vnode.tag).to.equal('div')
expect(vnode.children[0].text).to.equal('test')
}

const exported = {
methods: {
callFun () {
fun({
testProp: 1,
rende (h) {
return (
<div>test</div>
)
}
})
}
}
}

exported.methods.callFun()
})

it('h injection in object getters', () => {
const obj = {
get computed () {
Expand Down

0 comments on commit 2a584ec

Please sign in to comment.