Skip to content

Commit

Permalink
fix(compiler-core): elements with dynamic keys should be forced into …
Browse files Browse the repository at this point in the history
…blocks

fix #916
  • Loading branch information
yyx990803 committed Apr 7, 2020
1 parent 2c9374d commit d531686
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ exports[`compiler: hoistStatic transform should NOT hoist element with dynamic k
return function render(_ctx, _cache) {
with (_ctx) {
const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = _Vue
const { openBlock: _openBlock, createBlock: _createBlock, createVNode: _createVNode } = _Vue
return (_openBlock(), _createBlock(\\"div\\", null, [
_createVNode(\\"div\\", { key: foo })
(_openBlock(), _createBlock(\\"div\\", { key: foo }))
]))
}
}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports[`compiler: v-for codegen keyed v-for 1`] = `
return function render(_ctx, _cache) {
with (_ctx) {
const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock, createVNode: _createVNode } = _Vue
const { renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock } = _Vue
return (_openBlock(true), _createBlock(_Fragment, null, _renderList(items, (item) => {
return (_openBlock(), _createBlock(\\"span\\", { key: item }))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,17 @@ describe('compiler: element transform', () => {
isBlock: true
})
})

// #938
test('element with dynamic keys should be forced into blocks', () => {
const ast = parse(`<div><div :key="foo" /></div>`)
transform(ast, {
nodeTransforms: [transformElement]
})
expect((ast as any).children[0].children[0].codegenNode).toMatchObject({
type: NodeTypes.VNODE_CALL,
tag: `"div"`,
isBlock: true
})
})
})
16 changes: 10 additions & 6 deletions packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ export const transformElement: NodeTransform = (node, context) => {
let dynamicPropNames: string[] | undefined
let vnodeDirectives: VNodeCall['directives']

// <svg> and <foreignObject> must be forced into blocks so that block
// updates inside get proper isSVG flag at runtime. (#639, #643)
// This is technically web-specific, but splitting the logic out of core
// leads to too much unnecessary complexity.
let shouldUseBlock =
!isComponent && (tag === 'svg' || tag === 'foreignObject')
!isComponent &&
// <svg> and <foreignObject> must be forced into blocks so that block
// updates inside get proper isSVG flag at runtime. (#639, #643)
// This is technically web-specific, but splitting the logic out of core
// leads to too much unnecessary complexity.
(tag === 'svg' ||
tag === 'foreignObject' ||
// #938: elements with dynamic keys should be forced into blocks
findProp(node, 'key', true))

// props
if (props.length > 0) {
Expand Down Expand Up @@ -188,7 +192,7 @@ export const transformElement: NodeTransform = (node, context) => {
vnodePatchFlag,
vnodeDynamicProps,
vnodeDirectives,
shouldUseBlock,
!!shouldUseBlock,

This comment has been minimized.

Copy link
@dsonet

dsonet Apr 8, 2020

Contributor

It's better to move !! to findProp(node, 'key', true).

false /* isForBlock */,
node.loc
)
Expand Down

1 comment on commit d531686

@jods4
Copy link
Contributor

@jods4 jods4 commented on d531686 Apr 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit description is wrong: this fixes #938 not #916. (and you closed the wrong issue)

Please sign in to comment.