no-this-in-sfc.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * @fileoverview Report "this" being used in stateless functional components.
  3. */
  4. 'use strict';
  5. const Components = require('../util/Components');
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Constants
  9. // ------------------------------------------------------------------------------
  10. const ERROR_MESSAGE = 'Stateless functional components should not use this';
  11. // ------------------------------------------------------------------------------
  12. // Rule Definition
  13. // ------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. docs: {
  17. description: 'Report "this" being used in stateless components',
  18. category: 'Possible Errors',
  19. recommended: false,
  20. url: docsUrl('no-this-in-sfc')
  21. },
  22. schema: []
  23. },
  24. create: Components.detect((context, components, utils) => ({
  25. MemberExpression(node) {
  26. const component = components.get(utils.getParentStatelessComponent());
  27. if (!component) {
  28. return;
  29. }
  30. if (node.object.type === 'ThisExpression') {
  31. context.report({
  32. node: node,
  33. message: ERROR_MESSAGE
  34. });
  35. }
  36. }
  37. }))
  38. };