react-in-jsx-scope.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // -----------------------------------------------------------------------------
  10. // Rule Definition
  11. // -----------------------------------------------------------------------------
  12. module.exports = {
  13. meta: {
  14. docs: {
  15. description: 'Prevent missing React when using JSX',
  16. category: 'Possible Errors',
  17. recommended: true,
  18. url: docsUrl('react-in-jsx-scope')
  19. },
  20. messages: {
  21. notInScope: '\'{{name}}\' must be in scope when using JSX'
  22. },
  23. schema: []
  24. },
  25. create(context) {
  26. const pragma = pragmaUtil.getFromContext(context);
  27. function checkIfReactIsInScope(node) {
  28. const variables = variableUtil.variablesInScope(context);
  29. if (variableUtil.findVariable(variables, pragma)) {
  30. return;
  31. }
  32. context.report({
  33. node,
  34. messageId: 'notInScope',
  35. data: {
  36. name: pragma
  37. }
  38. });
  39. }
  40. return {
  41. JSXOpeningElement: checkIfReactIsInScope,
  42. JSXOpeningFragment: checkIfReactIsInScope
  43. };
  44. }
  45. };