react-in-jsx-scope.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @fileoverview Prevent missing React when using JSX
  3. * @author Glen Mailer
  4. */
  5. 'use strict';
  6. const variableUtil = require('../util/variable');
  7. const pragmaUtil = require('../util/pragma');
  8. const docsUrl = require('../util/docsUrl');
  9. const report = require('../util/report');
  10. // -----------------------------------------------------------------------------
  11. // Rule Definition
  12. // -----------------------------------------------------------------------------
  13. const messages = {
  14. notInScope: '\'{{name}}\' must be in scope when using JSX',
  15. };
  16. /** @type {import('eslint').Rule.RuleModule} */
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. description: 'Disallow missing React when using JSX',
  21. category: 'Possible Errors',
  22. recommended: true,
  23. url: docsUrl('react-in-jsx-scope'),
  24. },
  25. messages,
  26. schema: [],
  27. },
  28. create(context) {
  29. const pragma = pragmaUtil.getFromContext(context);
  30. function checkIfReactIsInScope(node) {
  31. if (variableUtil.getVariableFromContext(context, node, pragma)) {
  32. return;
  33. }
  34. report(context, messages.notInScope, 'notInScope', {
  35. node,
  36. data: {
  37. name: pragma,
  38. },
  39. });
  40. }
  41. return {
  42. JSXOpeningElement: checkIfReactIsInScope,
  43. JSXOpeningFragment: checkIfReactIsInScope,
  44. };
  45. },
  46. };