-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[New]jsx-newline
: Enforce a new line after jsx elements and expressions
#2693
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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 (element.type === 'JSXElement' || element.type === 'JSXExpressionContainer') { | ||
const firstAdjacentSibling = elements[index + 1]; | ||
const secondAdjacentSibling = elements[index + 2]; | ||
if ( | ||
firstAdjacentSibling | ||
&& secondAdjacentSibling | ||
&& (firstAdjacentSibling.type === 'Literal' || firstAdjacentSibling.type === 'JSXText') | ||
// 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, | ||
// double the last newline. | ||
sourceCode.getText(firstAdjacentSibling) | ||
.replace(/(\n)(?!.*\1)/g, '\n\n') | ||
); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
}, | ||
':matches(JSXElement, JSXFragment) > :matches(JSXElement, JSXExpressionContainer)': (node) => { | ||
jsxElementParents.add(node.parent); | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
/** | ||
* @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 tests = { | ||
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> | ||
` | ||
], | ||
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'} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
const advanceFeatTest = { | ||
valid: [ | ||
{ | ||
code: ` | ||
<> | ||
<Button>{data.label}</Button> | ||
Test | ||
|
||
<span>Should be in new line</span> | ||
</> | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
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'} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
// Run tests with default parser | ||
new RuleTester({parserOptions}).run('jsx-newline', rule, tests); | ||
|
||
// Run tests with babel parser | ||
let ruleTester = new RuleTester({parserOptions, parser: parsers.BABEL_ESLINT}); | ||
ruleTester.run('jsx-newline', rule, tests); | ||
ruleTester.run('jsx-newline', rule, advanceFeatTest); | ||
|
||
// Run tests with typescript parser | ||
ruleTester = new RuleTester({parserOptions, parser: parsers.TYPESCRIPT_ESLINT}); | ||
ruleTester.run('jsx-newline', rule, tests); | ||
ruleTester.run('jsx-newline', rule, advanceFeatTest); | ||
|
||
ruleTester = new RuleTester({parserOptions, parser: parsers['@TYPESCRIPT_ESLINT']}); | ||
ruleTester.run('jsx-newline', rule, { | ||
valid: parsers.TS(tests.valid), | ||
invalid: parsers.TS(tests.invalid) | ||
}); | ||
ruleTester.run('jsx-newline', rule, { | ||
valid: parsers.TS(advanceFeatTest.valid), | ||
invalid: parsers.TS(advanceFeatTest.invalid) | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how is this not a warning? there's no newline after the element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Good part of the issue description the same example is giving without a newline. That is why the rule only warns about newlines between elements and expressions, no literals like text and numbers.