no-find-dom-node.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @fileoverview Prevent usage of findDOMNode
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'Prevent usage of findDOMNode',
  14. category: 'Best Practices',
  15. recommended: true,
  16. url: docsUrl('no-find-dom-node')
  17. },
  18. messages: {
  19. noFindDOMNode: 'Do not use findDOMNode. It doesn’t work with function components and is deprecated in StrictMode. See https://reactjs.org/docs/react-dom.html#finddomnode'
  20. },
  21. schema: []
  22. },
  23. create(context) {
  24. // --------------------------------------------------------------------------
  25. // Public
  26. // --------------------------------------------------------------------------
  27. return {
  28. CallExpression(node) {
  29. const callee = node.callee;
  30. const isfindDOMNode = (callee.name === 'findDOMNode')
  31. || (callee.property && callee.property.name === 'findDOMNode');
  32. if (!isfindDOMNode) {
  33. return;
  34. }
  35. context.report({
  36. node: callee,
  37. messageId: 'noFindDOMNode'
  38. });
  39. }
  40. };
  41. }
  42. };