Skip to content

Commit

Permalink
fix(mp): mergeVirtualHostAttributes合并id时未考虑id是组件属性的场景
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Dec 31, 2024
1 parent 2495a5d commit dfce260
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,23 +139,23 @@ describe('complier: options with mergeVirtualHostAttributes', () => {
test('root node id with mergeVirtualHostAttributes', () => {
assert(
`<image id="id1"/>`,
`<image class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}" hidden="{{virtualHostHidden}}" id="{{virtualHostId !== '' ? virtualHostId : 'id1'}}"/>`,
`<image class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\" id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? 'id1' : virtualHostId}}\"/>`,
`(_ctx, _cache) => {
return {}
}`,
options
)
assert(
`<image :id="id1"/>`,
`<image id="{{virtualHostId !== '' ? virtualHostId : a}}" class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}" hidden="{{virtualHostHidden}}"/>`,
`<image id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? a : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`(_ctx, _cache) => {
return { a: _ctx.id1 }
}`,
options
)
assert(
`<image id="id1" :id="id1"/>`,
`<image id="{{virtualHostId !== '' ? virtualHostId : 'id1'}}" class="{{[virtualHostClass]}}" style="{{virtualHostStyle}}" hidden="{{virtualHostHidden}}"/>`,
`<image id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? 'id1' : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`(_ctx, _cache) => {
return {}
}`,
Expand All @@ -165,7 +165,7 @@ describe('complier: options with mergeVirtualHostAttributes', () => {
`<view :id="id1"></view>`,
`<view id=\"{{a}}\" style=\"{{$eS[a] + ';' + virtualHostStyle}}\" class=\"{{[virtualHostClass]}}\" hidden=\"{{virtualHostHidden}}\"></view>`,
`(_ctx, _cache) => {
const __returned__ = { a: _sei(_ctx.virtualHostId !== '' ? _ctx.virtualHostId : _ctx.id1, 'view'), b: _s(_ses(_ctx.virtualHostId !== '' ? _ctx.virtualHostId : _ctx.id1)) }
const __returned__ = { a: _sei('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? _ctx.id1 : _ctx.virtualHostId, 'view'), b: _s(_ses('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? _ctx.id1 : _ctx.virtualHostId)) }
return __returned__
}`,
optionsX
Expand All @@ -174,7 +174,7 @@ describe('complier: options with mergeVirtualHostAttributes', () => {
`<view id="page"><image ref="img" /></view>`,
`<view id=\"{{c}}\" style=\"{{$eS[c] + ';' + virtualHostStyle}}\" class=\"{{[virtualHostClass]}}\" hidden=\"{{virtualHostHidden}}\"><image ref=\"img\" id=\"r0-2a9ec0b0\" style=\"{{$eS[a]}}\"/></view>`,
`(_ctx, _cache) => {
const __returned__ = { a: _sei('r0-2a9ec0b0', 'image', 'img'), b: _s(_ses('r0-2a9ec0b0')), c: _sei(_ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page', 'view'), d: _s(_ses(_ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page')) }
const __returned__ = { a: _sei('r0-2a9ec0b0', 'image', 'img'), b: _s(_ses('r0-2a9ec0b0')), c: _sei('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId, 'view'), d: _s(_ses('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId)) }
return __returned__
}`,
optionsX
Expand All @@ -183,7 +183,7 @@ describe('complier: options with mergeVirtualHostAttributes', () => {
`<view id="page" ref="page"><image ref="img" /></view>`,
`<view ref=\"page\" id=\"{{c}}\" style=\"{{$eS[c] + ';' + virtualHostStyle}}\" class=\"{{[virtualHostClass]}}\" hidden=\"{{virtualHostHidden}}\"><image ref=\"img\" id=\"r0-2a9ec0b0\" style=\"{{$eS[a]}}\"/></view>`,
`(_ctx, _cache) => {
const __returned__ = { a: _sei('r0-2a9ec0b0', 'image', 'img'), b: _s(_ses('r0-2a9ec0b0')), c: _sei((_ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page') !== '' ? _ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page' : 'r1-2a9ec0b0', 'view', 'page'), d: _s(_ses((_ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page') !== '' ? _ctx.virtualHostId !== '' ? _ctx.virtualHostId : 'page' : 'r1-2a9ec0b0')) }
const __returned__ = { a: _sei('r0-2a9ec0b0', 'image', 'img'), b: _s(_ses('r0-2a9ec0b0')), c: _sei(('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId) !== '' ? 'id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId : 'r1-2a9ec0b0', 'view', 'page'), d: _s(_ses(('id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId) !== '' ? 'id' in _ctx.$.type.props || _ctx.virtualHostId === '' ? 'page' : _ctx.virtualHostId : 'r1-2a9ec0b0')) }
return __returned__
}`,
optionsX
Expand Down
26 changes: 23 additions & 3 deletions packages/uni-mp-compiler/src/transforms/transformId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
conditionalExpression,
identifier,
isStringLiteral,
logicalExpression,
memberExpression,
stringLiteral,
} from '@babel/types'
Expand Down Expand Up @@ -64,10 +65,29 @@ export function rewriteId(
value: '',
})
) {
/**
* 组件属性中声明id属性时,id不会被绑定到element上。`'id' in _ctx.$.type.props`
* virtualHostId存在且id不在props内时,virtualHostId绑定到element上
* TODO class、style也有类似的问题,但是用法并不常见,暂时遗留
*/
idBindingExpr = conditionalExpression(
binaryExpression('!==', genVirtualHostId(isX), stringLiteral('')),
genVirtualHostId(isX),
idBindingExpr
logicalExpression(
'||',
binaryExpression(
'in',
stringLiteral('id'),
memberExpression(
memberExpression(
memberExpression(identifier('_ctx'), identifier('$')),
identifier('type')
),
identifier('props')
)
),
binaryExpression('===', genVirtualHostId(isX), stringLiteral(''))
),
idBindingExpr,
genVirtualHostId(isX)
)
} else {
idBindingExpr = genVirtualHostId(isX)
Expand Down

0 comments on commit dfce260

Please sign in to comment.