jsx-no-undef.js 2.9 KB

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