variable.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 {Array} 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 {Array} 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. module.exports = {
  65. findVariable: findVariable,
  66. findVariableByName: findVariableByName,
  67. getVariable: getVariable,
  68. variablesInScope: variablesInScope
  69. };