no-is-mounted.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @fileoverview Prevent usage of isMounted
  3. * @author Joe Lencioni
  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 isMounted',
  14. category: 'Best Practices',
  15. recommended: true,
  16. url: docsUrl('no-is-mounted')
  17. },
  18. messages: {
  19. noIsMounted: 'Do not use isMounted'
  20. },
  21. schema: []
  22. },
  23. create(context) {
  24. // --------------------------------------------------------------------------
  25. // Public
  26. // --------------------------------------------------------------------------
  27. return {
  28. CallExpression(node) {
  29. const callee = node.callee;
  30. if (callee.type !== 'MemberExpression') {
  31. return;
  32. }
  33. if (callee.object.type !== 'ThisExpression' || callee.property.name !== 'isMounted') {
  34. return;
  35. }
  36. const ancestors = context.getAncestors(callee);
  37. for (let i = 0, j = ancestors.length; i < j; i++) {
  38. if (ancestors[i].type === 'Property' || ancestors[i].type === 'MethodDefinition') {
  39. context.report({
  40. node: callee,
  41. messageId: 'noIsMounted'
  42. });
  43. break;
  44. }
  45. }
  46. }
  47. };
  48. }
  49. };