You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 regels
1.8 KiB

  1. /**
  2. * @fileoverview Prevent usage of dangerous JSX props
  3. * @author Scott Andrews
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const jsxUtil = require('../util/jsx');
  8. // ------------------------------------------------------------------------------
  9. // Constants
  10. // ------------------------------------------------------------------------------
  11. const DANGEROUS_MESSAGE = 'Dangerous property \'{{name}}\' found';
  12. const DANGEROUS_PROPERTY_NAMES = [
  13. 'dangerouslySetInnerHTML'
  14. ];
  15. const DANGEROUS_PROPERTIES = DANGEROUS_PROPERTY_NAMES.reduce((props, prop) => {
  16. props[prop] = prop;
  17. return props;
  18. }, Object.create(null));
  19. // ------------------------------------------------------------------------------
  20. // Helpers
  21. // ------------------------------------------------------------------------------
  22. /**
  23. * Checks if a JSX attribute is dangerous.
  24. * @param {String} name - Name of the attribute to check.
  25. * @returns {boolean} Whether or not the attribute is dnagerous.
  26. */
  27. function isDangerous(name) {
  28. return name in DANGEROUS_PROPERTIES;
  29. }
  30. // ------------------------------------------------------------------------------
  31. // Rule Definition
  32. // ------------------------------------------------------------------------------
  33. module.exports = {
  34. meta: {
  35. docs: {
  36. description: 'Prevent usage of dangerous JSX props',
  37. category: 'Best Practices',
  38. recommended: false,
  39. url: docsUrl('no-danger')
  40. },
  41. schema: []
  42. },
  43. create: function(context) {
  44. return {
  45. JSXAttribute: function(node) {
  46. if (jsxUtil.isDOMComponent(node.parent) && isDangerous(node.name.name)) {
  47. context.report({
  48. node: node,
  49. message: DANGEROUS_MESSAGE,
  50. data: {
  51. name: node.name.name
  52. }
  53. });
  54. }
  55. }
  56. };
  57. }
  58. };