forbid-dom-props.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @fileoverview Forbid certain props on DOM Nodes
  3. * @author David Vázquez
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const report = require('../util/report');
  8. // ------------------------------------------------------------------------------
  9. // Constants
  10. // ------------------------------------------------------------------------------
  11. const DEFAULTS = [];
  12. // ------------------------------------------------------------------------------
  13. // Rule Definition
  14. // ------------------------------------------------------------------------------
  15. /**
  16. * @param {Map<string, object>} forbidMap // { disallowList: null | string[], message: null | string }
  17. * @param {string} prop
  18. * @param {string} tagName
  19. * @returns {boolean}
  20. */
  21. function isForbidden(forbidMap, prop, tagName) {
  22. const options = forbidMap.get(prop);
  23. return options && (
  24. typeof tagName === 'undefined'
  25. || !options.disallowList
  26. || options.disallowList.indexOf(tagName) !== -1
  27. );
  28. }
  29. const messages = {
  30. propIsForbidden: 'Prop "{{prop}}" is forbidden on DOM Nodes',
  31. };
  32. module.exports = {
  33. meta: {
  34. docs: {
  35. description: 'Disallow certain props on DOM Nodes',
  36. category: 'Best Practices',
  37. recommended: false,
  38. url: docsUrl('forbid-dom-props'),
  39. },
  40. messages,
  41. schema: [{
  42. type: 'object',
  43. properties: {
  44. forbid: {
  45. type: 'array',
  46. items: {
  47. anyOf: [{
  48. type: 'string',
  49. }, {
  50. type: 'object',
  51. properties: {
  52. propName: {
  53. type: 'string',
  54. },
  55. disallowedFor: {
  56. type: 'array',
  57. uniqueItems: true,
  58. items: {
  59. type: 'string',
  60. },
  61. },
  62. message: {
  63. type: 'string',
  64. },
  65. },
  66. }],
  67. minLength: 1,
  68. },
  69. uniqueItems: true,
  70. },
  71. },
  72. additionalProperties: false,
  73. }],
  74. },
  75. create(context) {
  76. const configuration = context.options[0] || {};
  77. const forbid = new Map((configuration.forbid || DEFAULTS).map((value) => {
  78. const propName = typeof value === 'string' ? value : value.propName;
  79. return [propName, {
  80. disallowList: typeof value === 'string' ? null : (value.disallowedFor || null),
  81. message: typeof value === 'string' ? null : value.message,
  82. }];
  83. }));
  84. return {
  85. JSXAttribute(node) {
  86. const tag = node.parent.name.name;
  87. if (!(tag && typeof tag === 'string' && tag[0] !== tag[0].toUpperCase())) {
  88. // This is a Component, not a DOM node, so exit.
  89. return;
  90. }
  91. const prop = node.name.name;
  92. if (!isForbidden(forbid, prop, tag)) {
  93. return;
  94. }
  95. const customMessage = forbid.get(prop).message;
  96. report(context, customMessage || messages.propIsForbidden, !customMessage && 'propIsForbidden', {
  97. node,
  98. data: {
  99. prop,
  100. },
  101. });
  102. },
  103. };
  104. },
  105. };