Skip to content

Commit f6e889e

Browse files
committed
fix(jsx-newline): add suggested changes
1 parent 2186b4f commit f6e889e

File tree

4 files changed

+73
-23
lines changed

4 files changed

+73
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ Enable the rules that you would like to use.
171171
* [react/jsx-key](docs/rules/jsx-key.md): Report missing `key` props in iterators/collection literals
172172
* [react/jsx-max-depth](docs/rules/jsx-max-depth.md): Validate JSX maximum depth
173173
* [react/jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md): Limit maximum of props on a single line in JSX (fixable)
174+
* [react/jsx-newline](docs/rules/jsx-newline.md): Enforce a new line after jsx elements and expressions (fixable)
174175
* [react/jsx-no-bind](docs/rules/jsx-no-bind.md): Prevents usage of Function.prototype.bind and arrow functions in React component props
175176
* [react/jsx-no-comment-textnodes](docs/rules/jsx-no-comment-textnodes.md): Comments inside children section of tag should be placed inside braces
176177
* [react/jsx-no-duplicate-props](docs/rules/jsx-no-duplicate-props.md): Enforce no duplicate props

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const allRules = {
3131
'jsx-key': require('./lib/rules/jsx-key'),
3232
'jsx-max-depth': require('./lib/rules/jsx-max-depth'),
3333
'jsx-max-props-per-line': require('./lib/rules/jsx-max-props-per-line'),
34+
'jsx-newline': require('./lib/rules/jsx-newline'),
3435
'jsx-no-bind': require('./lib/rules/jsx-no-bind'),
3536
'jsx-no-comment-textnodes': require('./lib/rules/jsx-no-comment-textnodes'),
3637
'jsx-no-duplicate-props': require('./lib/rules/jsx-no-duplicate-props'),
@@ -91,8 +92,7 @@ const allRules = {
9192
'state-in-constructor': require('./lib/rules/state-in-constructor'),
9293
'static-property-placement': require('./lib/rules/static-property-placement'),
9394
'style-prop-object': require('./lib/rules/style-prop-object'),
94-
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children'),
95-
'jsx-newline': require('./lib/rules/jsx-newline')
95+
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children')
9696
};
9797
/* eslint-enable */
9898

lib/rules/jsx-newline.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
'use strict';
77

8-
const arrayIncludes = require('array-includes');
98
const docsUrl = require('../util/docsUrl');
109

1110
// ------------------------------------------------------------------------------
@@ -29,13 +28,13 @@ module.exports = {
2928
'Program:exit'() {
3029
jsxElementParents.forEach((parent) => {
3130
parent.children.forEach((element, index, elements) => {
32-
if (arrayIncludes(['JSXElement', 'JSXExpressionContainer'], element.type)) {
31+
if (element.type === 'JSXElement' || element.type === 'JSXExpressionContainer') {
3332
const firstAdjacentSibling = elements[index + 1];
3433
const secondAdjacentSibling = elements[index + 2];
3534
if (
3635
firstAdjacentSibling
3736
&& secondAdjacentSibling
38-
&& arrayIncludes(['Literal', 'JSXText'], firstAdjacentSibling.type)
37+
&& (firstAdjacentSibling.type === 'Literal' || firstAdjacentSibling.type === 'JSXText')
3938
// Check adjacent sibling has the proper amount of newlines
4039
&& !/\n\s*\n/.test(firstAdjacentSibling.value)
4140
) {
@@ -45,6 +44,7 @@ module.exports = {
4544
fix(fixer) {
4645
return fixer.replaceText(
4746
firstAdjacentSibling,
47+
// double the last newline.
4848
sourceCode.getText(firstAdjacentSibling)
4949
.replace(/(\n)(?!.*\1)/g, '\n\n')
5050
);

tests/lib/rules/jsx-newline.js

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
const RuleTester = require('eslint').RuleTester;
1313
const rule = require('../../../lib/rules/jsx-newline');
14-
1514
const parsers = require('../../helpers/parsers');
1615

1716
const parserOptions = {
@@ -26,8 +25,7 @@ const parserOptions = {
2625
// Tests
2726
// ------------------------------------------------------------------------------
2827

29-
const ruleTester = new RuleTester({parserOptions});
30-
ruleTester.run('jsx-newline', rule, {
28+
const tests = {
3129
valid: [
3230
`
3331
<div>
@@ -52,18 +50,7 @@ ruleTester.run('jsx-newline', rule, {
5250
<ErrorMessage />
5351
)}
5452
</div>
55-
`,
56-
{
57-
code: `
58-
<>
59-
<Button>{data.label}</Button>
60-
Test
61-
62-
<span>Should be in new line</span>
63-
</>
64-
`,
65-
parser: parsers.BABEL_ESLINT
66-
}
53+
`
6754
],
6855
invalid: [
6956
{
@@ -179,7 +166,70 @@ ruleTester.run('jsx-newline', rule, {
179166
{message: 'JSX element should start in a new line'},
180167
{message: 'JSX element should start in a new line'}
181168
]
182-
},
169+
}
170+
]
171+
};
172+
173+
// Run tests with default parser
174+
new RuleTester({parserOptions}).run('jsx-newline', rule, tests);
175+
176+
// Run tests with babel parser
177+
const ruleTesterBabelParser = new RuleTester({parserOptions, parser: parsers.BABEL_ESLINT});
178+
ruleTesterBabelParser.run('jsx-newline', rule, tests);
179+
ruleTesterBabelParser.run('jsx-newline', rule, {
180+
valid: [
181+
{
182+
code: `
183+
<>
184+
<Button>{data.label}</Button>
185+
Test
186+
187+
<span>Should be in new line</span>
188+
</>
189+
`
190+
}
191+
],
192+
invalid: [
193+
{
194+
code: `
195+
<>
196+
<Button>{data.label}</Button>
197+
Test
198+
<span>Should be in new line</span>
199+
</>
200+
`,
201+
output: `
202+
<>
203+
<Button>{data.label}</Button>
204+
Test
205+
206+
<span>Should be in new line</span>
207+
</>
208+
`,
209+
errors: [
210+
{message: 'JSX element should start in a new line'}
211+
]
212+
}
213+
]
214+
});
215+
216+
// Run tests with typescript parser
217+
const ruleTesterTypeScriptParser = new RuleTester({parserOptions, parser: parsers.TYPESCRIPT_ESLINT});
218+
ruleTesterTypeScriptParser.run('jsx-newline', rule, tests);
219+
ruleTesterTypeScriptParser.run('jsx-newline', rule, {
220+
valid: [
221+
{
222+
code: `
223+
<>
224+
<Button>{data.label}</Button>
225+
Test
226+
227+
<span>Should be in new line</span>
228+
</>
229+
`
230+
}
231+
],
232+
invalid: [
183233
{
184234
code: `
185235
<>
@@ -198,8 +248,7 @@ ruleTester.run('jsx-newline', rule, {
198248
`,
199249
errors: [
200250
{message: 'JSX element should start in a new line'}
201-
],
202-
parser: parsers.BABEL_ESLINT
251+
]
203252
}
204253
]
205254
});

0 commit comments

Comments
 (0)