Skip to content

Commit 1a8b9d3

Browse files
authored
Fix initial spaces in text
Closes GH-4. Closes GH-5. Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 3150cca commit 1a8b9d3

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ export function buildJsx(tree, options = {}) {
189189
.replace(/\n+/g, '\n')
190190
// Drop final line feeds.
191191
.replace(/\n+$/, '')
192+
// Drop first line feeds.
193+
.replace(/^\n+/, '')
192194
// Replace line feeds with spaces.
193195
.replace(/\n/g, ' ')
194196

test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,44 @@ test('estree-util-build-jsx', (t) => {
847847
'should support skip whitespace only content'
848848
)
849849

850+
t.deepEqual(
851+
expression(
852+
buildJsx(parse(['<a>', ' line1', '</a>'].join('\n')), {pragma: 'h'})
853+
),
854+
{
855+
type: 'CallExpression',
856+
callee: {type: 'Identifier', name: 'h'},
857+
arguments: [
858+
{type: 'Literal', value: 'a'},
859+
{type: 'Literal', value: null},
860+
{type: 'Literal', value: 'line1'}
861+
],
862+
optional: false
863+
},
864+
'should trim strings with leading line feed'
865+
)
866+
867+
t.deepEqual(
868+
expression(
869+
buildJsx(parse(['<a>', ' line1{" "}', ' line2', '</a>'].join('\n')), {
870+
pragma: 'h'
871+
})
872+
),
873+
{
874+
type: 'CallExpression',
875+
callee: {type: 'Identifier', name: 'h'},
876+
arguments: [
877+
{type: 'Literal', value: 'a'},
878+
{type: 'Literal', value: null},
879+
{type: 'Literal', value: 'line1'},
880+
{type: 'Literal', value: ' '},
881+
{type: 'Literal', value: 'line2'}
882+
],
883+
optional: false
884+
},
885+
'should trim strings with leading line feed (multiline test)'
886+
)
887+
850888
t.equal(
851889
generate(
852890
buildJsx(parse('<>\n <a b c="d" e={f} {...g}>h</a>\n</>'), {
@@ -1524,7 +1562,6 @@ function parse(doc, clean, addComments) {
15241562

15251563
if (addComments !== false) tree.comments = comments
15261564

1527-
// @ts-expect-error: types are wrong.
15281565
if (clean !== false) walk(tree, {leave})
15291566

15301567
return JSON.parse(JSON.stringify(tree))

0 commit comments

Comments
 (0)