forbid-dom-props.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // ------------------------------------------------------------------------------
  8. // Constants
  9. // ------------------------------------------------------------------------------
  10. const DEFAULTS = [];
  11. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'Forbid certain props on DOM Nodes',
  18. category: 'Best Practices',
  19. recommended: false,
  20. url: docsUrl('forbid-dom-props')
  21. },
  22. schema: [{
  23. type: 'object',
  24. properties: {
  25. forbid: {
  26. type: 'array',
  27. items: {
  28. type: 'string',
  29. minLength: 1
  30. },
  31. uniqueItems: true
  32. }
  33. },
  34. additionalProperties: false
  35. }]
  36. },
  37. create: function(context) {
  38. function isForbidden(prop) {
  39. const configuration = context.options[0] || {};
  40. const forbid = configuration.forbid || DEFAULTS;
  41. return forbid.indexOf(prop) >= 0;
  42. }
  43. return {
  44. JSXAttribute: function(node) {
  45. const tag = node.parent.name.name;
  46. if (!(tag && tag[0] !== tag[0].toUpperCase())) {
  47. // This is a Component, not a DOM node, so exit.
  48. return;
  49. }
  50. const prop = node.name.name;
  51. if (!isForbidden(prop)) {
  52. return;
  53. }
  54. context.report({
  55. node: node,
  56. message: `Prop \`${prop}\` is forbidden on DOM Nodes`
  57. });
  58. }
  59. };
  60. }
  61. };