Skip to content

Commit a030d15

Browse files
committed
fix: don't transform empty string to boolean
closes #653
1 parent e73b065 commit a030d15

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

packages/babel-plugin-jsx/src/transform-vue-jsx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
transformJSXSpreadAttribute,
1717
transformJSXSpreadChild,
1818
transformJSXText,
19+
transformText,
1920
walksScope,
2021
} from './utils';
2122
import SlotFlags from './slotFlags';
@@ -36,7 +37,7 @@ const getJSXAttributeValue = (
3637
return transformJSXElement(valuePath, state);
3738
}
3839
if (valuePath.isStringLiteral()) {
39-
return transformJSXText(valuePath);
40+
return t.stringLiteral(transformText(valuePath.node.value));
4041
}
4142
if (valuePath.isJSXExpressionContainer()) {
4243
return transformJSXExpressionContainer(valuePath);

packages/babel-plugin-jsx/src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ export const getJSXAttributeName = (path: NodePath<t.JSXAttribute>): string => {
140140
export const transformJSXText = (
141141
path: NodePath<t.JSXText | t.StringLiteral>
142142
): t.StringLiteral | null => {
143-
const { node } = path;
144-
const lines = node.value.split(/\r\n|\n|\r/);
143+
const str = transformText(path.node.value);
144+
return str !== '' ? t.stringLiteral(str) : null;
145+
};
146+
147+
export const transformText = (text: string) => {
148+
const lines = text.split(/\r\n|\n|\r/);
145149

146150
let lastNonEmptyLine = 0;
147151

@@ -182,7 +186,7 @@ export const transformJSXText = (
182186
}
183187
}
184188

185-
return str !== '' ? t.stringLiteral(str) : null;
189+
return str;
186190
};
187191

188192
/**

packages/babel-plugin-jsx/test/index.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ describe('Transform JSX', () => {
254254

255255
expect(calls).toEqual(expect.arrayContaining([3, 4]));
256256
});
257+
258+
test('empty string', () => {
259+
const wrapper = shallowMount({
260+
setup() {
261+
return () => <h1 title=""></h1>;
262+
},
263+
});
264+
expect(wrapper.html()).toBe('<h1 title=""></h1>');
265+
});
257266
});
258267

259268
describe('directive', () => {

0 commit comments

Comments
 (0)