Skip to content

Commit

Permalink
Update to use variable util to find var by name
Browse files Browse the repository at this point in the history
  • Loading branch information
jomasti committed Jan 4, 2019
1 parent 6abe62a commit e2f8fee
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions lib/rules/prefer-exact-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,6 @@ module.exports = {
);
}

/**
* Find a variable by name in the current scope.
* @param {string} name Name of the variable to look for.
* @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
* @todo Refactor multiple usages of this function to variable util
*/
function findVariableByName(name) {
const variable = variableUtil.variablesInScope(context).find(item => item.name === 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;
}

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

return {
ClassProperty: function(node) {
if (!propsUtil.isPropTypesDeclaration(node)) {
Expand Down Expand Up @@ -110,7 +90,7 @@ module.exports = {
});
} else if (hasGenericTypeAnnotation(node)) {
const identifier = node.typeAnnotation.typeAnnotation.id.name;
const propsDefinition = findVariableByName(identifier);
const propsDefinition = variableUtil.findVariableByName(context, identifier);
if (isNonExactObjectTypeAnnotation(propsDefinition)) {
context.report({
node: node,
Expand All @@ -133,7 +113,7 @@ module.exports = {
});
} else if (right.type === 'Identifier') {
const identifier = right.name;
const propsDefinition = findVariableByName(identifier);
const propsDefinition = variableUtil.findVariableByName(context, identifier);
if (isNonEmptyObjectExpression(propsDefinition)) {
context.report({
node: node,
Expand Down

0 comments on commit e2f8fee

Please sign in to comment.