jsx-no-undef.js 3.0 KB

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