jsx-no-undef.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @fileoverview Disallow undeclared variables in JSX
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. const eslintUtil = require('../util/eslint');
  8. const jsxUtil = require('../util/jsx');
  9. const report = require('../util/report');
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. const messages = {
  14. undefined: '\'{{identifier}}\' is not defined.',
  15. };
  16. /** @type {import('eslint').Rule.RuleModule} */
  17. module.exports = {
  18. meta: {
  19. docs: {
  20. description: 'Disallow undeclared variables in JSX',
  21. category: 'Possible Errors',
  22. recommended: true,
  23. url: docsUrl('jsx-no-undef'),
  24. },
  25. messages,
  26. schema: [{
  27. type: 'object',
  28. properties: {
  29. allowGlobals: {
  30. type: 'boolean',
  31. },
  32. },
  33. additionalProperties: false,
  34. }],
  35. },
  36. create(context) {
  37. const config = context.options[0] || {};
  38. const allowGlobals = config.allowGlobals || false;
  39. /**
  40. * Compare an identifier with the variables declared in the scope
  41. * @param {ASTNode} node - Identifier or JSXIdentifier node
  42. * @returns {void}
  43. */
  44. function checkIdentifierInJSX(node) {
  45. let scope = eslintUtil.getScope(context, node);
  46. const sourceCode = eslintUtil.getSourceCode(context);
  47. const sourceType = sourceCode.ast.sourceType;
  48. const scopeUpperBound = !allowGlobals && sourceType === 'module' ? 'module' : 'global';
  49. let variables = scope.variables;
  50. let i;
  51. let len;
  52. // Ignore 'this' keyword (also maked as JSXIdentifier when used in JSX)
  53. if (node.name === 'this') {
  54. return;
  55. }
  56. while (scope.type !== scopeUpperBound && scope.type !== 'global') {
  57. scope = scope.upper;
  58. variables = scope.variables.concat(variables);
  59. }
  60. if (scope.childScopes.length) {
  61. variables = scope.childScopes[0].variables.concat(variables);
  62. // Temporary fix for babel-eslint
  63. if (scope.childScopes[0].childScopes.length) {
  64. variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
  65. }
  66. }
  67. for (i = 0, len = variables.length; i < len; i++) {
  68. if (variables[i].name === node.name) {
  69. return;
  70. }
  71. }
  72. report(context, messages.undefined, 'undefined', {
  73. node,
  74. data: {
  75. identifier: node.name,
  76. },
  77. });
  78. }
  79. return {
  80. JSXOpeningElement(node) {
  81. switch (node.name.type) {
  82. case 'JSXIdentifier':
  83. if (jsxUtil.isDOMComponent(node)) {
  84. return;
  85. }
  86. node = node.name;
  87. break;
  88. case 'JSXMemberExpression':
  89. node = node.name;
  90. do {
  91. node = node.object;
  92. } while (node && node.type !== 'JSXIdentifier');
  93. break;
  94. case 'JSXNamespacedName':
  95. return;
  96. default:
  97. break;
  98. }
  99. checkIdentifierInJSX(node);
  100. },
  101. };
  102. },
  103. };