Skip to content

Commit

Permalink
fix(mp): 修复非uni-app-x mergeVirtualHostAttributes编译结果错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Dec 31, 2024
1 parent dfce260 commit ba3e435
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@ describe('complier: options with mergeVirtualHostAttributes', () => {
test('root node id with mergeVirtualHostAttributes', () => {
assert(
`<image id="id1"/>`,
`<image class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\" id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? 'id1' : virtualHostId}}\"/>`,
`<image class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\" id=\"{{a || virtualHostId === '' ? 'id1' : virtualHostId}}\"/>`,
`(_ctx, _cache) => {
return {}
return { a: 'id' in _ctx.$.type.props }
}`,
options
)
assert(
`<image :id="id1"/>`,
`<image id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? a : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`<image id=\"{{b || virtualHostId === '' ? a : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`(_ctx, _cache) => {
return { a: _ctx.id1 }
return { a: _ctx.id1, b: 'id' in _ctx.$.type.props }
}`,
options
)
assert(
`<image id="id1" :id="id1"/>`,
`<image id=\"{{'id' in _ctx.$.type.props || virtualHostId === '' ? 'id1' : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`<image id=\"{{a || virtualHostId === '' ? 'id1' : virtualHostId}}\" class=\"{{[virtualHostClass]}}\" style=\"{{virtualHostStyle}}\" hidden=\"{{virtualHostHidden}}\"/>`,
`(_ctx, _cache) => {
return {}
return { a: 'id' in _ctx.$.type.props }
}`,
options
)
Expand Down
33 changes: 22 additions & 11 deletions packages/uni-mp-compiler/src/transforms/transformId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
type BinaryExpression,
type Expression,
type Identifier,
binaryExpression,
conditionalExpression,
identifier,
Expand Down Expand Up @@ -70,20 +72,29 @@ export function rewriteId(
* virtualHostId存在且id不在props内时,virtualHostId绑定到element上
* TODO class、style也有类似的问题,但是用法并不常见,暂时遗留
*/
let idInPropsExpr: BinaryExpression | Identifier = binaryExpression(
'in',
stringLiteral('id'),
memberExpression(
memberExpression(
memberExpression(identifier('_ctx'), identifier('$')),
identifier('type')
),
identifier('props')
)
)
if (!isX) {
idInPropsExpr = identifier(
rewriteExpression(
createSimpleExpression(genBabelExpr(idInPropsExpr)),
context
).content
)
}
idBindingExpr = conditionalExpression(
logicalExpression(
'||',
binaryExpression(
'in',
stringLiteral('id'),
memberExpression(
memberExpression(
memberExpression(identifier('_ctx'), identifier('$')),
identifier('type')
),
identifier('props')
)
),
idInPropsExpr,
binaryExpression('===', genVirtualHostId(isX), stringLiteral(''))
),
idBindingExpr,
Expand Down

0 comments on commit ba3e435

Please sign in to comment.