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. module.exports = {
  17. meta: {
  18. docs: {
  19. description: 'Disallow missing React when using JSX',
  20. category: 'Possible Errors',
  21. recommended: true,
  22. url: docsUrl('react-in-jsx-scope'),
  23. },
  24. messages,
  25. schema: [],
  26. },
  27. create(context) {
  28. const pragma = pragmaUtil.getFromContext(context);
  29. function checkIfReactIsInScope(node) {
  30. const variables = variableUtil.variablesInScope(context);
  31. if (variableUtil.findVariable(variables, 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. };