variable.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @fileoverview Utility functions for React components detection
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. /**
  7. * Search a particular variable in a list
  8. * @param {Array} variables The variables list.
  9. * @param {string} name The name of the variable to search.
  10. * @returns {Boolean} True if the variable was found, false if not.
  11. */
  12. function findVariable(variables, name) {
  13. return variables.some((variable) => variable.name === name);
  14. }
  15. /**
  16. * Find and return a particular variable in a list
  17. * @param {Array} variables The variables list.
  18. * @param {string} name The name of the variable to search.
  19. * @returns {Object} Variable if the variable was found, null if not.
  20. */
  21. function getVariable(variables, name) {
  22. return variables.find((variable) => variable.name === name);
  23. }
  24. /**
  25. * List all variable in a given scope
  26. *
  27. * Contain a patch for babel-eslint to avoid https://github.com/babel/babel-eslint/issues/21
  28. *
  29. * @param {Object} context The current rule context.
  30. * @returns {Array} The variables list
  31. */
  32. function variablesInScope(context) {
  33. let scope = context.getScope();
  34. let variables = scope.variables;
  35. while (scope.type !== 'global') {
  36. scope = scope.upper;
  37. variables = scope.variables.concat(variables);
  38. }
  39. if (scope.childScopes.length) {
  40. variables = scope.childScopes[0].variables.concat(variables);
  41. if (scope.childScopes[0].childScopes.length) {
  42. variables = scope.childScopes[0].childScopes[0].variables.concat(variables);
  43. }
  44. }
  45. variables.reverse();
  46. return variables;
  47. }
  48. /**
  49. * Find a variable by name in the current scope.
  50. * @param {Object} context The current rule context.
  51. * @param {string} name Name of the variable to look for.
  52. * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
  53. */
  54. function findVariableByName(context, name) {
  55. const variable = getVariable(variablesInScope(context), name);
  56. if (!variable || !variable.defs[0] || !variable.defs[0].node) {
  57. return null;
  58. }
  59. if (variable.defs[0].node.type === 'TypeAlias') {
  60. return variable.defs[0].node.right;
  61. }
  62. return variable.defs[0].node.init;
  63. }
  64. /**
  65. * Returns the latest definition of the variable.
  66. * @param {Object} variable
  67. * @returns {Object | undefined} The latest variable definition or undefined.
  68. */
  69. function getLatestVariableDefinition(variable) {
  70. return variable.defs[variable.defs.length - 1];
  71. }
  72. module.exports = {
  73. findVariable,
  74. findVariableByName,
  75. getVariable,
  76. variablesInScope,
  77. getLatestVariableDefinition
  78. };