Skip to content

Commit

Permalink
[New] : Enforce a new line after jsx elements and expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jzabala committed Jul 1, 2020
1 parent 6fc4bc0 commit 2186b4f
Show file tree
Hide file tree
Showing 4 changed files with 335 additions and 1 deletion.
65 changes: 65 additions & 0 deletions docs/rules/jsx-newline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Enforce a new line after jsx elements and expressions (react/jsx-newline)

**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.

## Rule Details

This is a stylistic rule intended to make JSX code more readable by enforcing spaces between adjacent JSX elements and expressions.

The following patterns are considered warnings:

```jsx
<div>
<Button>{data.label}</Button>
<List />
</div>
```

```jsx
<div>
<Button>{data.label}</Button>
{showSomething === true && <Something />}
</div>
```

```jsx
<div>
{showSomething === true && <Something />}
{showSomethingElse === true ? (
<SomethingElse />
) : (
<ErrorMessage />
)}
</div>
```

The following patterns are **not** considered warnings:

```jsx
<div>
<Button>{data.label}</Button>

<List />

<Button>
<IconPreview />
Button 2

<span></span>
</Button>

{showSomething === true && <Something />}

<Button>Button 3</Button>

{showSomethingElse === true ? (
<SomethingElse />
) : (
<ErrorMessage />
)}
</div>
```

## When Not To Use It

You can turn this rule off if you are not concerned with spacing between your JSX elements and expressions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const allRules = {
'state-in-constructor': require('./lib/rules/state-in-constructor'),
'static-property-placement': require('./lib/rules/static-property-placement'),
'style-prop-object': require('./lib/rules/style-prop-object'),
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children')
'void-dom-elements-no-children': require('./lib/rules/void-dom-elements-no-children'),
'jsx-newline': require('./lib/rules/jsx-newline')
};
/* eslint-enable */

Expand Down
63 changes: 63 additions & 0 deletions lib/rules/jsx-newline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @fileoverview Enforce a new line after jsx elements and expressions.
* @author Johnny Zabala
*/

'use strict';

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

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'Enforce a new line after jsx elements and expressions',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('jsx-newline')
},
fixable: 'code'
},
create(context) {
const jsxElementParents = new Set();
const sourceCode = context.getSourceCode();
return {
'Program:exit'() {
jsxElementParents.forEach((parent) => {
parent.children.forEach((element, index, elements) => {
if (arrayIncludes(['JSXElement', 'JSXExpressionContainer'], element.type)) {
const firstAdjacentSibling = elements[index + 1];
const secondAdjacentSibling = elements[index + 2];
if (
firstAdjacentSibling
&& secondAdjacentSibling
&& arrayIncludes(['Literal', 'JSXText'], firstAdjacentSibling.type)
// Check adjacent sibling has the proper amount of newlines
&& !/\n\s*\n/.test(firstAdjacentSibling.value)
) {
context.report({
node: secondAdjacentSibling,
message: 'JSX element should start in a new line',
fix(fixer) {
return fixer.replaceText(
firstAdjacentSibling,
sourceCode.getText(firstAdjacentSibling)
.replace(/(\n)(?!.*\1)/g, '\n\n')
);
}
});
}
}
});
});
},
':matches(JSXElement, JSXFragment) > :matches(JSXElement, JSXExpressionContainer)': (node) => {
jsxElementParents.add(node.parent);
}
};
}
};
205 changes: 205 additions & 0 deletions tests/lib/rules/jsx-newline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
* @fileoverview Enforce a new line after jsx elements and expressions
* @author Johnny Zabala
*/

'use strict';

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-newline');

const parsers = require('../../helpers/parsers');

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
};

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-newline', rule, {
valid: [
`
<div>
<Button>{data.label}</Button>
<List />
<Button>
<IconPreview />
Button 2
<span></span>
</Button>
{showSomething === true && <Something />}
<Button>Button 3</Button>
{showSomethingElse === true ? (
<SomethingElse />
) : (
<ErrorMessage />
)}
</div>
`,
{
code: `
<>
<Button>{data.label}</Button>
Test
<span>Should be in new line</span>
</>
`,
parser: parsers.BABEL_ESLINT
}
],
invalid: [
{
code: `
<div>
<Button>{data.label}</Button>
<List />
</div>
`,
output: `
<div>
<Button>{data.label}</Button>
<List />
</div>
`,
errors: [{
message: 'JSX element should start in a new line'
}]
},
{
code: `
<div>
<Button>{data.label}</Button>
{showSomething === true && <Something />}
</div>
`,
output: `
<div>
<Button>{data.label}</Button>
{showSomething === true && <Something />}
</div>
`,
errors: [{
message: 'JSX element should start in a new line'
}]
},
{
code: `
<div>
{showSomething === true && <Something />}
<Button>{data.label}</Button>
</div>
`,
output: `
<div>
{showSomething === true && <Something />}
<Button>{data.label}</Button>
</div>
`,
errors: [{
message: 'JSX element should start in a new line'
}]
},
{
code: `
<div>
{showSomething === true && <Something />}
{showSomethingElse === true ? (
<SomethingElse />
) : (
<ErrorMessage />
)}
</div>
`,
output: `
<div>
{showSomething === true && <Something />}
{showSomethingElse === true ? (
<SomethingElse />
) : (
<ErrorMessage />
)}
</div>
`,
errors: [{
message: 'JSX element should start in a new line'
}]
},
{
code: `
<div>
<div>
<button></button>
<button></button>
</div>
<div>
<span></span>
<span></span>
</div>
</div>
`,
output: `
<div>
<div>
<button></button>
<button></button>
</div>
<div>
<span></span>
<span></span>
</div>
</div>
`,
errors: [
{message: 'JSX element should start in a new line'},
{message: 'JSX element should start in a new line'},
{message: 'JSX element should start in a new line'}
]
},
{
code: `
<>
<Button>{data.label}</Button>
Test
<span>Should be in new line</span>
</>
`,
output: `
<>
<Button>{data.label}</Button>
Test
<span>Should be in new line</span>
</>
`,
errors: [
{message: 'JSX element should start in a new line'}
],
parser: parsers.BABEL_ESLINT
}
]
});

0 comments on commit 2186b4f

Please sign in to comment.