Skip to content

Commit c5ebfac

Browse files
committed
fix: Do not trim all spaces
1 parent c2f486b commit c5ebfac

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

Diff for: packages/babel-plugin-transform-vue-jsx/src/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ const getTag = (t, path) => {
6363
*/
6464
const getChildren = (t, paths) =>
6565
paths
66-
.map(path => {
66+
.map((path, index) => {
6767
if (path.isJSXText()) {
68-
return transformJSXText(t, path)
68+
return transformJSXText(t, path, index === 0 ? -1 : index === paths.length - 1 ? 1 : 0)
6969
}
7070
if (path.isJSXExpressionContainer()) {
7171
return transformJSXExpressionContainer(t, path)
@@ -360,14 +360,25 @@ const transformJSXMemberExpression = (t, path) => {
360360
return t.memberExpression(transformedObject, transformedProperty)
361361
}
362362

363+
/**
364+
* Trim text from JSX expressions depending on position
365+
* @param string string
366+
* @param position -1 for left, 0 for middle and 1 for right
367+
* @returns string
368+
*/
369+
const trimText = (string, position) => (position === 0 ? string : string.replace(position === -1 ? /^\s*/ : /\s*$/, ''))
370+
363371
/**
364372
* Transform JSXText to StringLiteral
365373
* @param t
366374
* @param path JSXText
375+
* @param position -1 for left, 0 for middle and 1 for right
367376
* @returns StringLiteral
368377
*/
369-
const transformJSXText = (t, path) =>
370-
path.get('value').node.match(/^\s*$/) ? null : t.stringLiteral(path.get('value').node)
378+
const transformJSXText = (t, path, position) => {
379+
const string = trimText(path.get('value').node, position)
380+
return string ? t.stringLiteral(string) : null
381+
}
371382

372383
/**
373384
* Transform JSXExpressionContainer to Expression

Diff for: packages/babel-plugin-transform-vue-jsx/test/snapshot.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const tests = [
5151
},
5252
{
5353
name: 'Combined content',
54-
from: `render(h => <div>test{test}{...test}<br/></div>)`,
55-
to: `render(h => h("div", ["test", test, ...test, h("br")]));`,
54+
from: `render(h => <div> test{test} {...test}<br/> </div>)`,
55+
to: `render(h => h("div", ["test", test, " ", ...test, h("br")]));`,
5656
},
5757
{
5858
name: 'Plain attrs',

0 commit comments

Comments
 (0)