Skip to content

Commit b801624

Browse files
authored
Merge pull request #1576 from sharmilajesupaul/shar--remove-newline-before-paren
Potential autofix improvement for jsx-wrap-mulitlines
2 parents c072c89 + 6a41c60 commit b801624

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

lib/rules/jsx-wrap-multilines.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,21 @@ module.exports = {
104104
return node.loc.start.line !== node.loc.end.line;
105105
}
106106

107-
function report(node, message, fixerFn) {
107+
function report(node, message, fix) {
108108
context.report({
109-
node: node,
110-
message: message,
111-
fix: fixerFn
109+
node,
110+
message,
111+
fix
112112
});
113113
}
114114

115+
function trimTokenBeforeNewline(node, tokenBefore) {
116+
// if the token before the jsx is a bracket or curly brace
117+
// we don't want a space between the opening parentheses and the multiline jsx
118+
const isBracket = tokenBefore.value === '{' || tokenBefore.value === '[';
119+
return `${tokenBefore.value.trim()}${isBracket ? '' : ' '}`;
120+
}
121+
115122
function check(node, type) {
116123
if (!node || node.type !== 'JSXElement') {
117124
return;
@@ -125,7 +132,21 @@ module.exports = {
125132

126133
if (option === 'parens-new-line' && isMultilines(node)) {
127134
if (!isParenthesised(node)) {
128-
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
135+
const tokenBefore = sourceCode.getTokenBefore(node, {includeComments: true});
136+
const tokenAfter = sourceCode.getTokenAfter(node, {includeComments: true});
137+
if (tokenBefore.loc.end.line < node.loc.start.line) {
138+
// Strip newline after operator if parens newline is specified
139+
report(
140+
node,
141+
MISSING_PARENS,
142+
fixer => fixer.replaceTextRange(
143+
[tokenBefore.range[0], tokenAfter.range[0]],
144+
`${trimTokenBeforeNewline(node, tokenBefore)}(\n${sourceCode.getText(node)}\n)`
145+
)
146+
);
147+
} else {
148+
report(node, MISSING_PARENS, fixer => fixer.replaceText(node, `(\n${sourceCode.getText(node)}\n)`));
149+
}
129150
} else if (needsNewLines(node)) {
130151
report(node, PARENS_NEW_LINES, fixer => fixer.replaceText(node, `\n${sourceCode.getText(node)}\n`));
131152
}
@@ -171,7 +192,7 @@ module.exports = {
171192
}
172193
},
173194

174-
'ArrowFunctionExpression:exit': function (node) {
195+
'ArrowFunctionExpression:exit': function(node) {
175196
const arrowBody = node.body;
176197
const type = 'arrow';
177198

@@ -195,7 +216,7 @@ module.exports = {
195216
}
196217
},
197218

198-
JSXAttribute: function (node) {
219+
JSXAttribute: function(node) {
199220
const type = 'prop';
200221
if (isEnabled(type) && node.value && node.value.type === 'JSXExpressionContainer') {
201222
check(node.value.expression, type);

tests/lib/rules/jsx-wrap-multilines.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ const LOGICAL_NO_PAREN = `
249249
</div>
250250
`;
251251

252+
const LOGICAL_PAREN_NEW_LINE_AUTOFIX = `
253+
<div>
254+
{foo && (
255+
<div>
256+
<p>Hello World</p>
257+
</div>
258+
)}
259+
</div>
260+
`;
261+
252262
const LOGICAL_PAREN_NEW_LINE = `
253263
<div>
254264
{foo && (
@@ -291,6 +301,16 @@ const ATTR_PAREN_NEW_LINE = `
291301
</div>
292302
`;
293303

304+
const ATTR_PAREN_NEW_LINE_AUTOFIX = `
305+
<div prop={(
306+
<div>
307+
<p>Hello</p>
308+
</div>
309+
)}>
310+
<p>Hello</p>
311+
</div>
312+
`;
313+
294314
function addNewLineSymbols(code) {
295315
return code.replace(/\(</g, '(\n<').replace(/>\)/g, '>\n)');
296316
}
@@ -640,7 +660,7 @@ ruleTester.run('jsx-wrap-multilines', rule, {
640660
errors: [{message: PARENS_NEW_LINES}]
641661
}, {
642662
code: LOGICAL_NO_PAREN,
643-
output: addNewLineSymbols(LOGICAL_PAREN),
663+
output: LOGICAL_PAREN_NEW_LINE_AUTOFIX,
644664
options: [{logical: 'parens-new-line'}],
645665
errors: [{message: MISSING_PARENS}]
646666
}, {
@@ -650,9 +670,8 @@ ruleTester.run('jsx-wrap-multilines', rule, {
650670
errors: [{message: PARENS_NEW_LINES}]
651671
}, {
652672
code: ATTR_NO_PAREN,
653-
output: addNewLineSymbols(ATTR_PAREN),
673+
output: ATTR_PAREN_NEW_LINE_AUTOFIX,
654674
options: [{prop: 'parens-new-line'}],
655675
errors: [{message: MISSING_PARENS}]
656-
}
657-
]
676+
}]
658677
});

0 commit comments

Comments
 (0)