react-in-jsx-scope.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. schema: []
  21. },
  22. create: function(context) {
  23. const pragma = pragmaUtil.getFromContext(context);
  24. const NOT_DEFINED_MESSAGE = '\'{{name}}\' must be in scope when using JSX';
  25. function checkIfReactIsInScope(node) {
  26. const variables = variableUtil.variablesInScope(context);
  27. if (variableUtil.findVariable(variables, pragma)) {
  28. return;
  29. }
  30. context.report({
  31. node: node,
  32. message: NOT_DEFINED_MESSAGE,
  33. data: {
  34. name: pragma
  35. }
  36. });
  37. }
  38. return {
  39. JSXOpeningElement: checkIfReactIsInScope,
  40. JSXOpeningFragment: checkIfReactIsInScope
  41. };
  42. }
  43. };