From b1689b6c9cabb855e51ed96b00ac43f6a40b1f80 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 3 Nov 2021 17:41:55 +0800 Subject: [PATCH] wip(mp): boolean attribute --- .../__tests__/component.spec.ts | 9 +++++++++ .../src/transforms/transformComponent.ts | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/uni-mp-compiler/__tests__/component.spec.ts b/packages/uni-mp-compiler/__tests__/component.spec.ts index 7c80ee4955..e64eb48e65 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 3f964f13c1..da05eb4b54 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')) + } + }) +} -- GitLab