diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts index 7c80ee4955975a74e7968546146a051c710b7875..e64eb48e6554fda66bcecdc1ab44d65de71b9c3a 100644 --- a/packages/uni-mp-compiler/__tests__/component.spec.ts +++ b/packages/uni-mp-compiler/__tests__/component.spec.ts @@ -88,4 +88,13 @@ describe('compiler: transform component', () => { } ) }) + test(`component with boolean attribute`, () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return {} +}` + ) + }) }) diff --git a/packages/uni-mp-compiler/src/transforms/transformComponent.ts b/packages/uni-mp-compiler/src/transforms/transformComponent.ts index 3f964f13c10479c38f4cdc17c7061084ea0995b6..da05eb4b541c7a8426339cb56b7227a386959c2b 100644 --- a/packages/uni-mp-compiler/src/transforms/transformComponent.ts +++ b/packages/uni-mp-compiler/src/transforms/transformComponent.ts @@ -1,4 +1,4 @@ -import { ComponentNode } from '@vue/compiler-core' +import { ComponentNode, NodeTypes } from '@vue/compiler-core' import { createAttributeNode, createBindDirectiveNode, @@ -12,6 +12,7 @@ export const transformComponent: NodeTransform = (node, context) => { return } addVueId(node, context) + processBooleanAttr(node) return function postTransformComponent() { context.vueIds.pop() } @@ -54,3 +55,18 @@ function addVueId(node: ComponentNode, context: TransformContext) { } return node.props.push(createAttributeNode(ATTR_VUE_ID, value)) } +/** + * => + * 否则部分平台(快手)可能获取到的 accordion 是空字符串 + * @param param0 + */ +function processBooleanAttr({ props }: ComponentNode) { + props.forEach((prop, index) => { + if ( + prop.type === NodeTypes.ATTRIBUTE && + typeof prop.value === 'undefined' + ) { + props.splice(index, 1, createBindDirectiveNode(prop.name, 'true')) + } + }) +}