jsx-no-duplicate-props.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * @fileoverview Enforce no duplicate props
  3. * @author Markus Ånöstam
  4. */
  5. 'use strict';
  6. const has = require('has');
  7. const docsUrl = require('../util/docsUrl');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. description: 'Enforce no duplicate props',
  15. category: 'Possible Errors',
  16. recommended: true,
  17. url: docsUrl('jsx-no-duplicate-props')
  18. },
  19. messages: {
  20. noDuplicateProps: 'No duplicate props allowed'
  21. },
  22. schema: [{
  23. type: 'object',
  24. properties: {
  25. ignoreCase: {
  26. type: 'boolean'
  27. }
  28. },
  29. additionalProperties: false
  30. }]
  31. },
  32. create(context) {
  33. const configuration = context.options[0] || {};
  34. const ignoreCase = configuration.ignoreCase || false;
  35. return {
  36. JSXOpeningElement(node) {
  37. const props = {};
  38. node.attributes.forEach((decl) => {
  39. if (decl.type === 'JSXSpreadAttribute') {
  40. return;
  41. }
  42. let name = decl.name.name;
  43. if (typeof name !== 'string') {
  44. return;
  45. }
  46. if (ignoreCase) {
  47. name = name.toLowerCase();
  48. }
  49. if (has(props, name)) {
  50. context.report({
  51. node: decl,
  52. messageId: 'noDuplicateProps'
  53. });
  54. } else {
  55. props[name] = 1;
  56. }
  57. });
  58. }
  59. };
  60. }
  61. };