From a030d15018ca348d0be24d47b6759ea81c668b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 29 Jun 2023 22:01:58 +0800 Subject: [PATCH] fix: don't transform empty string to boolean closes #653 --- packages/babel-plugin-jsx/src/transform-vue-jsx.ts | 3 ++- packages/babel-plugin-jsx/src/utils.ts | 10 +++++++--- packages/babel-plugin-jsx/test/index.test.tsx | 9 +++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-jsx/src/transform-vue-jsx.ts b/packages/babel-plugin-jsx/src/transform-vue-jsx.ts index 57895a0..18e2e6d 100644 --- a/packages/babel-plugin-jsx/src/transform-vue-jsx.ts +++ b/packages/babel-plugin-jsx/src/transform-vue-jsx.ts @@ -16,6 +16,7 @@ import { transformJSXSpreadAttribute, transformJSXSpreadChild, transformJSXText, + transformText, walksScope, } from './utils'; import SlotFlags from './slotFlags'; @@ -36,7 +37,7 @@ const getJSXAttributeValue = ( return transformJSXElement(valuePath, state); } if (valuePath.isStringLiteral()) { - return transformJSXText(valuePath); + return t.stringLiteral(transformText(valuePath.node.value)); } if (valuePath.isJSXExpressionContainer()) { return transformJSXExpressionContainer(valuePath); diff --git a/packages/babel-plugin-jsx/src/utils.ts b/packages/babel-plugin-jsx/src/utils.ts index 0361f55..9b4ab4f 100644 --- a/packages/babel-plugin-jsx/src/utils.ts +++ b/packages/babel-plugin-jsx/src/utils.ts @@ -140,8 +140,12 @@ export const getJSXAttributeName = (path: NodePath): string => { export const transformJSXText = ( path: NodePath ): t.StringLiteral | null => { - const { node } = path; - const lines = node.value.split(/\r\n|\n|\r/); + const str = transformText(path.node.value); + return str !== '' ? t.stringLiteral(str) : null; +}; + +export const transformText = (text: string) => { + const lines = text.split(/\r\n|\n|\r/); let lastNonEmptyLine = 0; @@ -182,7 +186,7 @@ export const transformJSXText = ( } } - return str !== '' ? t.stringLiteral(str) : null; + return str; }; /** diff --git a/packages/babel-plugin-jsx/test/index.test.tsx b/packages/babel-plugin-jsx/test/index.test.tsx index f136224..f675787 100644 --- a/packages/babel-plugin-jsx/test/index.test.tsx +++ b/packages/babel-plugin-jsx/test/index.test.tsx @@ -254,6 +254,15 @@ describe('Transform JSX', () => { expect(calls).toEqual(expect.arrayContaining([3, 4])); }); + + test('empty string', () => { + const wrapper = shallowMount({ + setup() { + return () =>

; + }, + }); + expect(wrapper.html()).toBe('

'); + }); }); describe('directive', () => {