Skip to content

Commit b016d8e

Browse files
author
Simon Schick
committed
fixup! [New] add jsx-props-no-spread-multi
1 parent e0f2872 commit b016d8e

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ module.exports = [
338338
| [jsx-one-expression-per-line](docs/rules/jsx-one-expression-per-line.md) | Require one JSX element per line | | | 🔧 | | |
339339
| [jsx-pascal-case](docs/rules/jsx-pascal-case.md) | Enforce PascalCase for user-defined JSX components | | | | | |
340340
| [jsx-props-no-multi-spaces](docs/rules/jsx-props-no-multi-spaces.md) | Disallow multiple spaces between inline JSX props | | | 🔧 | | |
341-
| [jsx-props-no-spread-multi](docs/rules/jsx-props-no-spread-multi.md) | Disallow JSX prop spreading the same expression multiple times | | | | | |
341+
| [jsx-props-no-spread-multi](docs/rules/jsx-props-no-spread-multi.md) | Disallow JSX prop spreading the same identifier multiple times | | | | | |
342342
| [jsx-props-no-spreading](docs/rules/jsx-props-no-spreading.md) | Disallow JSX prop spreading | | | | | |
343343
| [jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce defaultProps declarations alphabetical sorting | | | | ||
344344
| [jsx-sort-props](docs/rules/jsx-sort-props.md) | Enforce props alphabetical sorting | | | 🔧 | | |

docs/rules/jsx-props-no-spread-multi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Disallow JSX prop spreading the same expression multiple times (`react/jsx-props-no-spread-multi`)
1+
# Disallow JSX prop spreading the same identifier multiple times (`react/jsx-props-no-spread-multi`)
22

33
<!-- end auto-generated rule header -->
44

lib/rules/jsx-props-no-spread-multi.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,10 @@ const messages = {
1616
noMultiSpreading: 'Spreading the same expression multiple times is forbidden',
1717
};
1818

19-
const ignoredAstProperties = new Set(['parent', 'range', 'loc', 'start', 'end', '_babelType']);
20-
21-
/**
22-
* Filter for JSON.stringify that omits circular and position structures.
23-
*
24-
* @param {string} key
25-
* @param {*} value
26-
* @returns {*}
27-
*/
28-
const propertyFilter = (key, value) => (ignoredAstProperties.has(key) ? undefined : value);
29-
3019
module.exports = {
3120
meta: {
3221
docs: {
33-
description: 'Disallow JSX prop spreading the same expression multiple times',
22+
description: 'Disallow JSX prop spreading the same identifier multiple times',
3423
category: 'Best Practices',
3524
recommended: false,
3625
url: docsUrl('jsx-props-no-spread-multi'),
@@ -41,21 +30,22 @@ module.exports = {
4130
create(context) {
4231
return {
4332
JSXOpeningElement(node) {
44-
const spreads = node.attributes.filter((attr) => attr.type === 'JSXSpreadAttribute');
33+
const spreads = node.attributes.filter(
34+
(attr) => attr.type === 'JSXSpreadAttribute' &&
35+
attr.argument.type === 'Identifier'
36+
);
4537
if (spreads.length < 2) {
4638
return;
4739
}
48-
// We detect duplicate expressions by hashing the ast nodes
49-
const argumentHashes = new Set();
40+
// We detect duplicate expressions by their identifier
41+
const identifierNames = new Set();
5042
for (const spread of spreads) {
51-
// TODO: Deep compare ast function?
52-
const hash = JSON.stringify(spread.argument, propertyFilter);
53-
if (argumentHashes.has(hash)) {
43+
if (identifierNames.has(spread.attr.name)) {
5444
report(context, messages.noMultiSpreading, 'noMultiSpreading', {
5545
node: spread,
5646
});
5747
}
58-
argumentHashes.add(hash);
48+
identifierNames.add(spread.attr.name);
5949
}
6050
},
6151
};

tests/lib/rules/jsx-props-no-spread-multi.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ ruleTester.run('jsx-props-no-spread-multi', rule, {
6666
<div {...props} {...props} {...props} />
6767
`,
6868
errors: [expectedError, expectedError],
69-
},
70-
{
71-
code: `
72-
const func = () => ({});
73-
<div {...func()} {...func()} />
74-
`,
75-
errors: [expectedError],
76-
},
69+
}
7770
]),
7871
});

0 commit comments

Comments
 (0)