source: imaps-frontend/node_modules/eslint-plugin-react/lib/util/variable.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/**
2 * @fileoverview Utility functions for React components detection
3 * @author Yannick Croissant
4 */
5
6'use strict';
7
8const getScope = require('./eslint').getScope;
9
10/**
11 * Search a particular variable in a list
12 * @param {Array} variables The variables list.
13 * @param {string} name The name of the variable to search.
14 * @returns {Boolean} True if the variable was found, false if not.
15 */
16function findVariable(variables, name) {
17 return variables.some((variable) => variable.name === name);
18}
19
20/**
21 * Find and return a particular variable in a list
22 * @param {Array} variables The variables list.
23 * @param {string} name The name of the variable to search.
24 * @returns {Object} Variable if the variable was found, null if not.
25 */
26function getVariable(variables, name) {
27 return variables.find((variable) => variable.name === name);
28}
29
30/**
31 * Searches for a variable in the given scope.
32 *
33 * @param {Object} context The current rule context.
34 * @param {ASTNode} node The node to start looking from.
35 * @param {string} name The name of the variable to search.
36 * @returns {Object | undefined} Variable if the variable was found, undefined if not.
37 */
38function getVariableFromContext(context, node, name) {
39 let scope = getScope(context, node);
40
41 while (scope) {
42 let variable = getVariable(scope.variables, name);
43
44 if (!variable && scope.childScopes.length) {
45 variable = getVariable(scope.childScopes[0].variables, name);
46
47 if (!variable && scope.childScopes[0].childScopes.length) {
48 variable = getVariable(scope.childScopes[0].childScopes[0].variables, name);
49 }
50 }
51
52 if (variable) {
53 return variable;
54 }
55 scope = scope.upper;
56 }
57 return undefined;
58}
59
60/**
61 * Find a variable by name in the current scope.
62 * @param {Object} context The current rule context.
63 * @param {ASTNode} node The node to check. Must be an Identifier node.
64 * @param {string} name Name of the variable to look for.
65 * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
66 */
67function findVariableByName(context, node, name) {
68 const variable = getVariableFromContext(context, node, name);
69
70 if (!variable || !variable.defs[0] || !variable.defs[0].node) {
71 return null;
72 }
73
74 if (variable.defs[0].node.type === 'TypeAlias') {
75 return variable.defs[0].node.right;
76 }
77
78 if (variable.defs[0].type === 'ImportBinding') {
79 return variable.defs[0].node;
80 }
81
82 return variable.defs[0].node.init;
83}
84
85/**
86 * Returns the latest definition of the variable.
87 * @param {Object} variable
88 * @returns {Object | undefined} The latest variable definition or undefined.
89 */
90function getLatestVariableDefinition(variable) {
91 return variable.defs[variable.defs.length - 1];
92}
93
94module.exports = {
95 findVariable,
96 findVariableByName,
97 getVariable,
98 getVariableFromContext,
99 getLatestVariableDefinition,
100};
Note: See TracBrowser for help on using the repository browser.