Skip to content

Commit 1344f1d

Browse files
committed
strip spaces but leave line returns
1 parent a18c30b commit 1344f1d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/rules/jsx-one-expression-per-line.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@ module.exports = {
8787

8888
childrenGroupedByLine[line].forEach((child, i) => {
8989
let prevChild;
90+
let nextChild;
91+
9092
if (i === firstIndex) {
9193
if (line === openingElementEndLine) {
9294
prevChild = openingElement;
9395
}
9496
} else {
9597
prevChild = childrenGroupedByLine[line][i - 1];
9698
}
97-
let nextChild;
99+
98100
if (i === lastIndex) {
99101
if (line === closingElementStartLine) {
100102
nextChild = closingElement;
@@ -109,6 +111,7 @@ module.exports = {
109111
(child.type === 'Literal' && child.raw.match(/^ /)) ||
110112
sourceCode.isSpaceBetweenTokens(prevChild, child);
111113
}
114+
112115
function spaceBetweenNext () {
113116
return (nextChild.type === 'Literal' && nextChild.raw.match(/^ /)) ||
114117
(child.type === 'Literal' && child.raw.match(/ $/)) ||
@@ -155,18 +158,20 @@ module.exports = {
155158

156159
const nodeToReport = details.node;
157160
const descriptor = details.descriptor;
158-
const source = details.source;
161+
const source = details.source.replace(/(^ +| +(?=\n)*$)/g, '');
159162

160163
const leadingSpaceString = details.leadingSpace ? '\n{\' \'}' : '';
161164
const trailingSpaceString = details.trailingSpace ? '{\' \'}\n' : '';
162165
const leadingNewLineString = details.leadingNewLine ? '\n' : '';
163166
const trailingNewLineString = details.trailingNewLine ? '\n' : '';
164167

168+
const replaceText = `${leadingSpaceString}${leadingNewLineString}${source}${trailingNewLineString}${trailingSpaceString}`;
169+
165170
context.report({
166171
node: nodeToReport,
167172
message: `\`${descriptor}\` must be placed on a new line`,
168173
fix: function (fixer) {
169-
return fixer.replaceText(nodeToReport, `${leadingSpaceString}${leadingNewLineString}${source}${trailingNewLineString}${trailingSpaceString}`);
174+
return fixer.replaceText(nodeToReport, replaceText);
170175
}
171176
});
172177
});

tests/lib/rules/jsx-one-expression-per-line.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
132132
'<div>',
133133
' {"foo"}',
134134
'{\' \'}',
135-
' bar',
135+
'bar',
136136
'</div>'
137137
].join('\n'),
138138
errors: [
@@ -377,7 +377,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
377377
'<div>',
378378
' <input />',
379379
'{\' \'}',
380-
' foo',
380+
'foo',
381381
'</div>'
382382
].join('\n'),
383383
errors: [{message: '` foo` must be placed on a new line'}],
@@ -444,7 +444,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
444444
'<div>',
445445
' {"foo"}',
446446
'{\' \'}',
447-
' bar',
447+
'bar',
448448
'</div>'
449449
].join('\n'),
450450
errors: [{message: '` bar` must be placed on a new line'}],
@@ -678,7 +678,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
678678
' <Foo>',
679679
' <Bar>',
680680
'{\' \'}',
681-
' baz ',
681+
'baz',
682682
'{\' \'}',
683683
'</Bar>',
684684
' </Foo>',
@@ -707,6 +707,24 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
707707
{message: '` baz` must be placed on a new line'}
708708
],
709709
parserOptions: parserOptions
710+
}, {
711+
// Would be nice to handle in one pass, but multipass works fine.
712+
code: [
713+
'<App>',
714+
' foo {"bar"}',
715+
'</App>'
716+
].join('\n'),
717+
output: [
718+
'<App>',
719+
' foo ',
720+
'{\' \'}',
721+
'{"bar"}',
722+
'</App>'
723+
].join('\n'),
724+
errors: [
725+
{message: '`{"bar"}` must be placed on a new line'}
726+
],
727+
parserOptions: parserOptions
710728
}, {
711729
// Would be nice to handle in one pass, but multipass works fine.
712730
code: [
@@ -722,7 +740,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
722740
'{\' \'}',
723741
'{"bar"}',
724742
'{\' \'}',
725-
' baz',
743+
'baz',
726744
'</App>'
727745
].join('\n'),
728746
errors: [
@@ -770,7 +788,7 @@ ruleTester.run('jsx-one-expression-per-line', rule, {
770788
'{\' \'}',
771789
'{"bar"}',
772790
'{\' \'}',
773-
' baz',
791+
'baz',
774792
'',
775793
'</App>'
776794
].join('\n'),

0 commit comments

Comments
 (0)