jsx-uses-react.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * @fileoverview Prevent React to be marked as unused
  3. * @author Glen Mailer
  4. */
  5. 'use strict';
  6. const pragmaUtil = require('../util/pragma');
  7. const docsUrl = require('../util/docsUrl');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. module.exports = {
  12. // eslint-disable-next-line eslint-plugin/prefer-message-ids -- https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues/292
  13. meta: {
  14. docs: {
  15. description: 'Disallow React to be incorrectly marked as unused',
  16. category: 'Best Practices',
  17. recommended: true,
  18. url: docsUrl('jsx-uses-react'),
  19. },
  20. schema: [],
  21. },
  22. create(context) {
  23. const pragma = pragmaUtil.getFromContext(context);
  24. const fragment = pragmaUtil.getFragmentFromContext(context);
  25. function handleOpeningElement() {
  26. context.markVariableAsUsed(pragma);
  27. }
  28. // --------------------------------------------------------------------------
  29. // Public
  30. // --------------------------------------------------------------------------
  31. return {
  32. JSXOpeningElement: handleOpeningElement,
  33. JSXOpeningFragment: handleOpeningElement,
  34. JSXFragment() {
  35. context.markVariableAsUsed(fragment);
  36. },
  37. };
  38. },
  39. };