PHP 7.4.33
Preview: variable.js Size: 2.81 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/eslint-plugin-react/lib/util/variable.js
/**
 * @fileoverview Utility functions for React components detection
 * @author Yannick Croissant
 */

'use strict';

const getScope = require('./eslint').getScope;

/**
 * Search a particular variable in a list
 * @param {Array} variables The variables list.
 * @param {string} name The name of the variable to search.
 * @returns {boolean} True if the variable was found, false if not.
 */
function findVariable(variables, name) {
  return variables.some((variable) => variable.name === name);
}

/**
 * Find and return a particular variable in a list
 * @param {Array} variables The variables list.
 * @param {string} name The name of the variable to search.
 * @returns {Object} Variable if the variable was found, null if not.
 */
function getVariable(variables, name) {
  return variables.find((variable) => variable.name === name);
}

/**
 * Searches for a variable in the given scope.
 *
 * @param {Object} context The current rule context.
 * @param {ASTNode} node The node to start looking from.
 * @param {string} name The name of the variable to search.
 * @returns {Object | undefined} Variable if the variable was found, undefined if not.
 */
function getVariableFromContext(context, node, name) {
  let scope = getScope(context, node);

  while (scope) {
    let variable = getVariable(scope.variables, name);

    if (!variable && scope.childScopes.length) {
      variable = getVariable(scope.childScopes[0].variables, name);

      if (!variable && scope.childScopes[0].childScopes.length) {
        variable = getVariable(scope.childScopes[0].childScopes[0].variables, name);
      }
    }

    if (variable) {
      return variable;
    }
    scope = scope.upper;
  }
  return undefined;
}

/**
 * Find a variable by name in the current scope.
 * @param {Object} context The current rule context.
 * @param {ASTNode} node The node to check. Must be an Identifier node.
 * @param  {string} name Name of the variable to look for.
 * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
 */
function findVariableByName(context, node, name) {
  const variable = getVariableFromContext(context, node, name);

  if (!variable || !variable.defs[0] || !variable.defs[0].node) {
    return null;
  }

  if (variable.defs[0].node.type === 'TypeAlias') {
    return variable.defs[0].node.right;
  }

  if (variable.defs[0].type === 'ImportBinding') {
    return variable.defs[0].node;
  }

  return variable.defs[0].node.init;
}

/**
 * Returns the latest definition of the variable.
 * @param {Object} variable
 * @returns {Object | undefined} The latest variable definition or undefined.
 */
function getLatestVariableDefinition(variable) {
  return variable.defs[variable.defs.length - 1];
}

module.exports = {
  findVariable,
  findVariableByName,
  getVariable,
  getVariableFromContext,
  getLatestVariableDefinition,
};

Directory Contents

Dirs: 0 × Files: 84
Name Size Perms Modified Actions
390 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
155 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.06 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
5.06 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.11 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
11.91 KB lrw-r--r-- 2025-03-28 11:04:36
Edit Download
2.34 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
466 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
29.37 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.36 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
517 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
4.71 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
311 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
128 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
8.30 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
104 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
112 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
173 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
207 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
131 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
308 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
413 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
169 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.27 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
327 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
188 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
428 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
115 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
150 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.18 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
133 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
160 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
894 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
182 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
201 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.64 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
123 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
167 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
387 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.93 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
480 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
5.25 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
140 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
119 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
611 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
172 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
137 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.48 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
201 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
127 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
302 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
233 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
241 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
3.66 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
214 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
117 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
266 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
400 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
231 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.17 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.65 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
501 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
4.14 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
799 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
176 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
45.31 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.89 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
419 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
7.18 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
398 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
168 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.81 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
143 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
111 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
235 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
638 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
168 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
18.14 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.68 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
359 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
2.81 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
319 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
158 B lrw-r--r-- 2025-03-28 11:04:37
Edit Download
6.80 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).