jsx-no-undef.js 3.1 KB

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