Skip to content

Commit 115e4b9

Browse files
jomastiljharb
authored andcommitted
[Refactor] Move findVariableByName to variable util
1 parent adf5d81 commit 115e4b9

5 files changed

+30
-86
lines changed

lib/rules/default-props-match-prop-types.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ module.exports = {
4040
const allowRequiredDefaults = configuration.allowRequiredDefaults || false;
4141
const propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
4242

43-
/**
44-
* Find a variable by name in the current scope.
45-
* @param {string} name Name of the variable to look for.
46-
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
47-
*/
48-
function findVariableByName(name) {
49-
const variable = variableUtil.variablesInScope(context).find(item => item.name === name);
50-
51-
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
52-
return null;
53-
}
54-
55-
if (variable.defs[0].node.type === 'TypeAlias') {
56-
return variable.defs[0].node.right;
57-
}
58-
59-
return variable.defs[0].node.init;
60-
}
61-
6243
/**
6344
* Try to resolve the node passed in to a variable in the current scope. If the node passed in is not
6445
* an Identifier, then the node is simply returned.
@@ -67,7 +48,7 @@ module.exports = {
6748
*/
6849
function resolveNodeValue(node) {
6950
if (node.type === 'Identifier') {
70-
return findVariableByName(node.name);
51+
return variableUtil.findVariableByName(context, node.name);
7152
}
7253
if (
7354
node.type === 'CallExpression' &&
@@ -89,7 +70,7 @@ module.exports = {
8970
return null;
9071
}
9172

92-
return findVariableByName(node.id.name);
73+
return variableUtil.findVariableByName(context, node.id.name);
9374
}
9475

9576
function resolveUnionTypeAnnotation(node) {
@@ -129,7 +110,7 @@ module.exports = {
129110
annotation = resolveGenericTypeAnnotation(type);
130111

131112
if (annotation && annotation.id) {
132-
annotation = findVariableByName(annotation.id.name);
113+
annotation = variableUtil.findVariableByName(context, annotation.id.name);
133114
}
134115

135116
if (!annotation || !annotation.properties) {
@@ -156,7 +137,7 @@ module.exports = {
156137
properties = getPropertiesFromIntersectionTypeAnnotationNode(annotation);
157138
} else {
158139
if (annotation && annotation.id) {
159-
annotation = findVariableByName(annotation.id.name);
140+
annotation = variableUtil.findVariableByName(context, annotation.id.name);
160141
}
161142

162143
properties = annotation ? (annotation.properties || []) : [];

lib/rules/forbid-prop-types.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,6 @@ module.exports = {
7070
return false;
7171
}
7272

73-
/**
74-
* Find a variable by name in the current scope.
75-
* @param {string} name Name of the variable to look for.
76-
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
77-
*/
78-
function findVariableByName(name) {
79-
const variable = variableUtil.variablesInScope(context).find(item => item.name === name);
80-
81-
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
82-
return null;
83-
}
84-
85-
if (variable.defs[0].node.type === 'TypeAlias') {
86-
return variable.defs[0].node.right;
87-
}
88-
89-
return variable.defs[0].node.init;
90-
}
91-
92-
9373
/**
9474
* Checks if propTypes declarations are forbidden
9575
* @param {Array} declarations The array of AST nodes being checked.
@@ -136,7 +116,7 @@ module.exports = {
136116
checkProperties(node.properties);
137117
break;
138118
case 'Identifier':
139-
const propTypesObject = findVariableByName(node.name);
119+
const propTypesObject = variableUtil.findVariableByName(context, node.name);
140120
if (propTypesObject && propTypesObject.properties) {
141121
checkProperties(propTypesObject.properties);
142122
}

lib/rules/require-default-props.js

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,6 @@ module.exports = {
4141
const configuration = context.options[0] || {};
4242
const forbidDefaultForRequired = configuration.forbidDefaultForRequired || false;
4343

44-
/**
45-
* Find a variable by name in the current scope.
46-
* @param {string} name Name of the variable to look for.
47-
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
48-
*/
49-
function findVariableByName(name) {
50-
const variable = variableUtil.variablesInScope(context).find(item => item.name === name);
51-
52-
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
53-
return null;
54-
}
55-
56-
if (variable.defs[0].node.type === 'TypeAlias') {
57-
return variable.defs[0].node.right;
58-
}
59-
60-
return variable.defs[0].node.init;
61-
}
62-
6344
/**
6445
* Try to resolve the node passed in to a variable in the current scope. If the node passed in is not
6546
* an Identifier, then the node is simply returned.
@@ -68,7 +49,7 @@ module.exports = {
6849
*/
6950
function resolveNodeValue(node) {
7051
if (node.type === 'Identifier') {
71-
return findVariableByName(node.name);
52+
return variableUtil.findVariableByName(context, node.name);
7253
}
7354
if (
7455
node.type === 'CallExpression' &&
@@ -91,7 +72,7 @@ module.exports = {
9172
return null;
9273
}
9374

94-
return findVariableByName(node.id.name);
75+
return variableUtil.findVariableByName(context, node.id.name);
9576
}
9677

9778
function resolveUnionTypeAnnotation(node) {
@@ -133,7 +114,7 @@ module.exports = {
133114
let annotation = resolveGenericTypeAnnotation(node.typeAnnotation);
134115

135116
if (annotation && annotation.id) {
136-
annotation = findVariableByName(annotation.id.name);
117+
annotation = variableUtil.findVariableByName(context, annotation.id.name);
137118
}
138119

139120
properties = annotation ? (annotation.properties || []) : [];

lib/rules/sort-prop-types.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,6 @@ module.exports = {
6969
);
7070
}
7171

72-
/**
73-
* Find a variable by name in the current scope.
74-
* @param {string} name Name of the variable to look for.
75-
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
76-
*/
77-
function findVariableByName(name) {
78-
const variable = variableUtil.variablesInScope(context).find(item => item.name === name);
79-
80-
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
81-
return null;
82-
}
83-
84-
if (variable.defs[0].node.type === 'TypeAlias') {
85-
return variable.defs[0].node.right;
86-
}
87-
88-
return variable.defs[0].node.init;
89-
}
90-
9172
/**
9273
* Checks if propTypes declarations are sorted
9374
* @param {Array} declarations The array of AST nodes being checked.
@@ -159,7 +140,7 @@ module.exports = {
159140
checkSorted(node.properties);
160141
break;
161142
case 'Identifier':
162-
const propTypesObject = findVariableByName(node.name);
143+
const propTypesObject = variableUtil.findVariableByName(context, node.name);
163144
if (propTypesObject && propTypesObject.properties) {
164145
checkSorted(propTypesObject.properties);
165146
}

lib/util/variable.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,29 @@ function variablesInScope(context) {
5151
return variables;
5252
}
5353

54+
/**
55+
* Find a variable by name in the current scope.
56+
* @param {Object} context The current rule context.
57+
* @param {string} name Name of the variable to look for.
58+
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
59+
*/
60+
function findVariableByName(context, name) {
61+
const variable = getVariable(variablesInScope(context), name);
62+
63+
if (!variable || !variable.defs[0] || !variable.defs[0].node) {
64+
return null;
65+
}
66+
67+
if (variable.defs[0].node.type === 'TypeAlias') {
68+
return variable.defs[0].node.right;
69+
}
70+
71+
return variable.defs[0].node.init;
72+
}
73+
5474
module.exports = {
5575
findVariable: findVariable,
76+
findVariableByName: findVariableByName,
5677
getVariable: getVariable,
5778
variablesInScope: variablesInScope
5879
};

0 commit comments

Comments
 (0)