jsx-props-no-spread-multi.js 1.5 KB

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