no-is-mounted.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @fileoverview Prevent usage of isMounted
  3. * @author Joe Lencioni
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const report = require('../util/report');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. const messages = {
  12. noIsMounted: 'Do not use isMounted',
  13. };
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'Disallow usage of isMounted',
  18. category: 'Best Practices',
  19. recommended: true,
  20. url: docsUrl('no-is-mounted'),
  21. },
  22. messages,
  23. schema: [],
  24. },
  25. create(context) {
  26. return {
  27. CallExpression(node) {
  28. const callee = node.callee;
  29. if (callee.type !== 'MemberExpression') {
  30. return;
  31. }
  32. if (callee.object.type !== 'ThisExpression' || callee.property.name !== 'isMounted') {
  33. return;
  34. }
  35. const ancestors = context.getAncestors(callee);
  36. for (let i = 0, j = ancestors.length; i < j; i++) {
  37. if (ancestors[i].type === 'Property' || ancestors[i].type === 'MethodDefinition') {
  38. report(context, messages.noIsMounted, 'noIsMounted', {
  39. node: callee,
  40. });
  41. break;
  42. }
  43. }
  44. },
  45. };
  46. },
  47. };