Skip to content

fix: Trim whitespaces properly #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions packages/babel-plugin-transform-vue-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ const getTag = (t, path) => {
*/
const getChildren = (t, paths) =>
paths
.map((path, index) => {
.map(path => {
if (path.isJSXText()) {
return transformJSXText(t, path, index === 0 ? -1 : index === paths.length - 1 ? 1 : 0)
return transformJSXText(t, path)
}
if (path.isJSXExpressionContainer()) {
return transformJSXExpressionContainer(t, path)
Expand Down Expand Up @@ -374,24 +374,56 @@ const transformJSXMemberExpression = (t, path) => {
return t.memberExpression(transformedObject, transformedProperty)
}

/**
* Trim text from JSX expressions depending on position
* @param string string
* @param position -1 for left, 0 for middle and 1 for right
* @returns string
*/
const trimText = (string, position) => (position === 0 ? string : string.replace(position === -1 ? /^\s*/ : /\s*$/, ''))

/**
* Transform JSXText to StringLiteral
* @param t
* @param path JSXText
* @param position -1 for left, 0 for middle and 1 for right
* @returns StringLiteral
*/
const transformJSXText = (t, path, position) => {
const string = trimText(path.get('value').node, position)
return string ? t.stringLiteral(string) : null
const transformJSXText = (t, path) => {
const node = path.node
const lines = node.value.split(/\r\n|\n|\r/)

let lastNonEmptyLine = 0

for (let i = 0; i < lines.length; i++) {
if (lines[i].match(/[^ \t]/)) {
lastNonEmptyLine = i
}
}

let str = ''

for (let i = 0; i < lines.length; i++) {
const line = lines[i]

const isFirstLine = i === 0
const isLastLine = i === lines.length - 1
const isLastNonEmptyLine = i === lastNonEmptyLine

// replace rendered whitespace tabs with spaces
let trimmedLine = line.replace(/\t/g, ' ')

// trim whitespace touching a newline
if (!isFirstLine) {
trimmedLine = trimmedLine.replace(/^[ ]+/, '')
}

// trim whitespace touching an endline
if (!isLastLine) {
trimmedLine = trimmedLine.replace(/[ ]+$/, '')
}

if (trimmedLine) {
if (!isLastNonEmptyLine) {
trimmedLine += ' '
}

str += trimmedLine
}
}

return str !== '' ? t.stringLiteral(str) : null
}

/**
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-plugin-transform-vue-jsx/test/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ render(h => [h(Alpha, ["test"]), h("Beta", ["test"])]);`,
},
{
name: 'Combined content',
from: `render(h => <div> test{test} {...test}<br/> </div>)`,
to: `render(h => h("div", ["test", test, " ", ...test, h("br")]));`,
from: `render(h => <div>
test{test} {...test}
<tag1 />
<tag2 />

Some text
goes here


</div>)`,
to: `render(h => h("div", ["test", test, " ", ...test, h("tag1"), h("tag2"), "Some text goes here"]));`,
},
{
name: 'Plain attrs',
Expand Down