-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] add jsx-props-no-spread-multi
#3724
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
Merged
ljharb
merged 1 commit into
jsx-eslint:master
from
SimonSchick:feat/jsx-props-no-spread-multi
Jul 15, 2024
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 hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
# Disallow JSX prop spreading the same identifier multiple times (`react/jsx-props-no-spread-multi`) | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
Enforces that any unique expression is only spread once. | ||
Generally spreading the same expression twice is an indicator of a mistake since any attribute between the spreads may be overridden when the intent was not to. | ||
Even when that is not the case this will lead to unnecessary computations being performed. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```jsx | ||
<App {...props} myAttr="1" {...props} /> | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```jsx | ||
<App myAttr="1" {...props} /> | ||
<App {...props} myAttr="1" /> | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
When spreading the same expression multiple times yields different results. |
This file contains hidden or 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 hidden or 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,53 @@ | ||
/** | ||
* @fileoverview Prevent JSX prop spreading the same expression multiple times | ||
* @author Simon Schick | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const docsUrl = require('../util/docsUrl'); | ||
const report = require('../util/report'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const messages = { | ||
noMultiSpreading: 'Spreading the same expression multiple times is forbidden', | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow JSX prop spreading the same identifier multiple times', | ||
category: 'Best Practices', | ||
recommended: false, | ||
url: docsUrl('jsx-props-no-spread-multi'), | ||
}, | ||
messages, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
const spreads = node.attributes.filter( | ||
(attr) => attr.type === 'JSXSpreadAttribute' | ||
&& attr.argument.type === 'Identifier' | ||
); | ||
if (spreads.length < 2) { | ||
return; | ||
} | ||
// We detect duplicate expressions by their identifier | ||
const identifierNames = new Set(); | ||
spreads.forEach((spread) => { | ||
if (identifierNames.has(spread.argument.name)) { | ||
report(context, messages.noMultiSpreading, 'noMultiSpreading', { | ||
node: spread, | ||
}); | ||
} | ||
identifierNames.add(spread.argument.name); | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains hidden or 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 hidden or 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,71 @@ | ||
/** | ||
* @fileoverview Tests for jsx-props-no-spread-multi | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../../../lib/rules/jsx-props-no-spread-multi'); | ||
|
||
const parsers = require('../../helpers/parsers'); | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester({ parserOptions }); | ||
const expectedError = { messageId: 'noMultiSpreading' }; | ||
|
||
ruleTester.run('jsx-props-no-spread-multi', rule, { | ||
valid: parsers.all([ | ||
{ | ||
code: ` | ||
const a = {}; | ||
<App {...a} /> | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
const a = {}; | ||
const b = {}; | ||
<App {...a} {...b} /> | ||
`, | ||
}, | ||
]), | ||
|
||
invalid: parsers.all([ | ||
{ | ||
code: ` | ||
const props = {}; | ||
<App {...props} {...props} /> | ||
`, | ||
errors: [expectedError], | ||
}, | ||
{ | ||
code: ` | ||
const props = {}; | ||
<div {...props} a="a" {...props} /> | ||
`, | ||
errors: [expectedError], | ||
}, | ||
{ | ||
code: ` | ||
const props = {}; | ||
<div {...props} {...props} {...props} /> | ||
`, | ||
errors: [expectedError, expectedError], | ||
}, | ||
]), | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.