fix: don't transform empty string to boolean

closes #653
This commit is contained in:
三咲智子 Kevin Deng 2023-06-29 22:01:58 +08:00
parent e73b065360
commit a030d15018
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
3 changed files with 18 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import {
transformJSXSpreadAttribute, transformJSXSpreadAttribute,
transformJSXSpreadChild, transformJSXSpreadChild,
transformJSXText, transformJSXText,
transformText,
walksScope, walksScope,
} from './utils'; } from './utils';
import SlotFlags from './slotFlags'; import SlotFlags from './slotFlags';
@ -36,7 +37,7 @@ const getJSXAttributeValue = (
return transformJSXElement(valuePath, state); return transformJSXElement(valuePath, state);
} }
if (valuePath.isStringLiteral()) { if (valuePath.isStringLiteral()) {
return transformJSXText(valuePath); return t.stringLiteral(transformText(valuePath.node.value));
} }
if (valuePath.isJSXExpressionContainer()) { if (valuePath.isJSXExpressionContainer()) {
return transformJSXExpressionContainer(valuePath); return transformJSXExpressionContainer(valuePath);

View File

@ -140,8 +140,12 @@ export const getJSXAttributeName = (path: NodePath<t.JSXAttribute>): string => {
export const transformJSXText = ( export const transformJSXText = (
path: NodePath<t.JSXText | t.StringLiteral> path: NodePath<t.JSXText | t.StringLiteral>
): t.StringLiteral | null => { ): t.StringLiteral | null => {
const { node } = path; const str = transformText(path.node.value);
const lines = node.value.split(/\r\n|\n|\r/); return str !== '' ? t.stringLiteral(str) : null;
};
export const transformText = (text: string) => {
const lines = text.split(/\r\n|\n|\r/);
let lastNonEmptyLine = 0; let lastNonEmptyLine = 0;
@ -182,7 +186,7 @@ export const transformJSXText = (
} }
} }
return str !== '' ? t.stringLiteral(str) : null; return str;
}; };
/** /**

View File

@ -254,6 +254,15 @@ describe('Transform JSX', () => {
expect(calls).toEqual(expect.arrayContaining([3, 4])); expect(calls).toEqual(expect.arrayContaining([3, 4]));
}); });
test('empty string', () => {
const wrapper = shallowMount({
setup() {
return () => <h1 title=""></h1>;
},
});
expect(wrapper.html()).toBe('<h1 title=""></h1>');
});
}); });
describe('directive', () => { describe('directive', () => {