From 69d9776a0355d50d0dc4b583c4850ec5577cda6a Mon Sep 17 00:00:00 2001 From: Ryan Carniato Date: Sat, 4 May 2019 14:36:07 -0700 Subject: [PATCH] Fix text interpolation, Fixes #8 --- package-lock.json | 2 +- package.json | 2 +- src/index.js | 13 +-- test/__fixtures__/textInterpolation/code.js | 23 +++++ test/__fixtures__/textInterpolation/output.js | 87 +++++++++++++++++++ 5 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 test/__fixtures__/textInterpolation/code.js create mode 100644 test/__fixtures__/textInterpolation/output.js diff --git a/package-lock.json b/package-lock.json index 26bfd78..8d814a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "babel-plugin-jsx-dom-expressions", - "version": "0.7.3", + "version": "0.7.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index b06a00b..bd5c0f2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "babel-plugin-jsx-dom-expressions", "description": "A JSX to DOM plugin that wraps expressions for fine grained change detection", - "version": "0.7.3", + "version": "0.7.4", "author": "Ryan Carniato", "license": "MIT", "repository": { diff --git a/src/index.js b/src/index.js index 437156a..db23878 100644 --- a/src/index.js +++ b/src/index.js @@ -98,13 +98,7 @@ export default (babel) => { } function trimWhitespace(text) { - return text.split('\n').map((t, i) => { - if (/^\s*$/.test(t)) return ''; - if (i === 0) return t.replace(/\s+/g, ' '); - return t - .replace(/^\s+/g, '') - .replace(/\s+/g, ' '); - }).join(''); + return text.replace(/\n\s*/g, '').replace(/\s+/g, ' '); } function checkLength(children) { @@ -337,8 +331,9 @@ export default (babel) => { transformChildren(path, jsx, opts, results); return results; } else if (t.isJSXText(jsx)) { - if (/^\s*$/.test(jsx.value)) return null; - let results = { template: trimWhitespace(jsx.value), decl: [], exprs: [] }; + const text = trimWhitespace(jsx.value); + if (!text.length) return null; + const results = { template: text, decl: [], exprs: [] }; if (!info.skipId) results.id = path.scope.generateUidIdentifier("el$") return results; } else if (t.isJSXExpressionContainer(jsx)) { diff --git a/test/__fixtures__/textInterpolation/code.js b/test/__fixtures__/textInterpolation/code.js new file mode 100644 index 0000000..106b8d1 --- /dev/null +++ b/test/__fixtures__/textInterpolation/code.js @@ -0,0 +1,23 @@ +const name = 'Jake', + greeting = 'Welcome'; + +const trailing = Hello ; +const leading = John; +const extraSpaces = Hello John; + +const trailingExpr = Hello {name}; +const leadingExpr = {greeting} John; + +const multiExpr = {greeting} {name}; +const multiExprSpaced = {greeting} {name} ; + +const multiLine = + + Hello + + + +const multiLineTrailingSpace = + Hello + John + \ No newline at end of file diff --git a/test/__fixtures__/textInterpolation/output.js b/test/__fixtures__/textInterpolation/output.js new file mode 100644 index 0000000..bead37c --- /dev/null +++ b/test/__fixtures__/textInterpolation/output.js @@ -0,0 +1,87 @@ +const _tmpl$9 = document.createElement("template"); + +_tmpl$9.innerHTML = "Hello John"; + +const _tmpl$8 = document.createElement("template"); + +_tmpl$8.innerHTML = "Hello"; + +const _tmpl$7 = document.createElement("template"); + +_tmpl$7.innerHTML = " "; + +const _tmpl$6 = document.createElement("template"); + +_tmpl$6.innerHTML = " "; + +const _tmpl$5 = document.createElement("template"); + +_tmpl$5.innerHTML = " John"; + +const _tmpl$4 = document.createElement("template"); + +_tmpl$4.innerHTML = "Hello "; + +const _tmpl$3 = document.createElement("template"); + +_tmpl$3.innerHTML = "Hello John"; + +const _tmpl$2 = document.createElement("template"); + +_tmpl$2.innerHTML = " John"; + +const _tmpl$ = document.createElement("template"); + +_tmpl$.innerHTML = "Hello "; +const name = 'Jake', + greeting = 'Welcome'; + +const trailing = _tmpl$.content.firstChild.cloneNode(true); + +const leading = _tmpl$2.content.firstChild.cloneNode(true); + +const extraSpaces = _tmpl$3.content.firstChild.cloneNode(true); + +const trailingExpr = function () { + const _el$4 = _tmpl$4.content.firstChild.cloneNode(true), + _el$5 = _el$4.firstChild, + _el$6 = _el$5.nextSibling; + + r.insert(_el$4, name, null, _el$6); + return _el$4; +}(); + +const leadingExpr = function () { + const _el$7 = _tmpl$5.content.firstChild.cloneNode(true), + _el$8 = _el$7.firstChild; + + r.insert(_el$7, greeting, null, _el$8); + return _el$7; +}(); + +const multiExpr = function () { + const _el$9 = _tmpl$6.content.firstChild.cloneNode(true), + _el$10 = _el$9.firstChild, + _el$11 = _el$10.nextSibling, + _el$12 = _el$11.nextSibling; + + r.insert(_el$9, greeting, null, _el$10); + r.insert(_el$9, name, null, _el$12); + return _el$9; +}(); + +const multiExprSpaced = function () { + const _el$13 = _tmpl$7.content.firstChild.cloneNode(true), + _el$14 = _el$13.firstChild, + _el$15 = _el$14.nextSibling, + _el$16 = _el$15.nextSibling, + _el$17 = _el$16.nextSibling; + + r.insert(_el$13, greeting, null, _el$15); + r.insert(_el$13, name, null, _el$17); + return _el$13; +}(); + +const multiLine = _tmpl$8.content.firstChild.cloneNode(true); + +const multiLineTrailingSpace = _tmpl$9.content.firstChild.cloneNode(true); \ No newline at end of file