Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

111 righe
2.8 KiB

  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. schema: [{
  20. type: 'object',
  21. properties: {
  22. allowGlobals: {
  23. type: 'boolean'
  24. }
  25. },
  26. additionalProperties: false
  27. }]
  28. },
  29. create: function(context) {
  30. const config = context.options[0] || {};
  31. const allowGlobals = config.allowGlobals || false;
  32. /**
  33. * Compare an identifier with the variables declared in the scope
  34. * @param {ASTNode} node - Identifier or JSXIdentifier node
  35. * @returns {void}
  36. */
  37. function checkIdentifierInJSX(node) {
  38. let scope = context.getScope();
  39. const sourceCode = context.getSourceCode();
  40. const sourceType = sourceCode.ast.sourceType;
  41. let variables = scope.variables;
  42. let scopeType = 'global';
  43. let i;
  44. let len;
  45. // Ignore 'this' keyword (also maked as JSXIdentifier when used in JSX)
  46. if (node.name === 'this') {
  47. return;
  48. }
  49. if (!allowGlobals && sourceType === 'module') {
  50. scopeType = 'module';
  51. }
  52. while (scope.type !== scopeType) {
  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: node,
  70. message: `'${node.name}' is not defined.`
  71. });
  72. }
  73. return {
  74. JSXOpeningElement: function(node) {
  75. switch (node.name.type) {
  76. case 'JSXIdentifier':
  77. if (jsxUtil.isDOMComponent(node)) {
  78. return;
  79. }
  80. node = node.name;
  81. break;
  82. case 'JSXMemberExpression':
  83. node = node.name;
  84. do {
  85. node = node.object;
  86. } while (node && node.type !== 'JSXIdentifier');
  87. break;
  88. case 'JSXNamespacedName':
  89. node = node.name.namespace;
  90. break;
  91. default:
  92. break;
  93. }
  94. checkIdentifierInJSX(node);
  95. }
  96. };
  97. }
  98. };