| 1 | /**
|
|---|
| 2 | * @fileoverview Prevent JSX prop spreading the same expression multiple times
|
|---|
| 3 | * @author Simon Schick
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | const docsUrl = require('../util/docsUrl');
|
|---|
| 9 | const report = require('../util/report');
|
|---|
| 10 |
|
|---|
| 11 | // ------------------------------------------------------------------------------
|
|---|
| 12 | // Rule Definition
|
|---|
| 13 | // ------------------------------------------------------------------------------
|
|---|
| 14 |
|
|---|
| 15 | const messages = {
|
|---|
| 16 | noMultiSpreading: 'Spreading the same expression multiple times is forbidden',
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | /** @type {import('eslint').Rule.RuleModule} */
|
|---|
| 20 | module.exports = {
|
|---|
| 21 | meta: {
|
|---|
| 22 | docs: {
|
|---|
| 23 | description: 'Disallow JSX prop spreading the same identifier multiple times',
|
|---|
| 24 | category: 'Best Practices',
|
|---|
| 25 | recommended: false,
|
|---|
| 26 | url: docsUrl('jsx-props-no-spread-multi'),
|
|---|
| 27 | },
|
|---|
| 28 | messages,
|
|---|
| 29 | },
|
|---|
| 30 |
|
|---|
| 31 | create(context) {
|
|---|
| 32 | return {
|
|---|
| 33 | JSXOpeningElement(node) {
|
|---|
| 34 | const spreads = node.attributes.filter(
|
|---|
| 35 | (attr) => attr.type === 'JSXSpreadAttribute'
|
|---|
| 36 | && attr.argument.type === 'Identifier'
|
|---|
| 37 | );
|
|---|
| 38 | if (spreads.length < 2) {
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 | // We detect duplicate expressions by their identifier
|
|---|
| 42 | const identifierNames = new Set();
|
|---|
| 43 | spreads.forEach((spread) => {
|
|---|
| 44 | if (identifierNames.has(spread.argument.name)) {
|
|---|
| 45 | report(context, messages.noMultiSpreading, 'noMultiSpreading', {
|
|---|
| 46 | node: spread,
|
|---|
| 47 | });
|
|---|
| 48 | }
|
|---|
| 49 | identifierNames.add(spread.argument.name);
|
|---|
| 50 | });
|
|---|
| 51 | },
|
|---|
| 52 | };
|
|---|
| 53 | },
|
|---|
| 54 | };
|
|---|