Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds committed Oct 31, 2018
1 parent 7fdaf21 commit bf7c57b
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/eslint-plugin-react-hooks/src/ReactiveDependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export default {
*/
function visitFunctionExpression(node) {
// We only want to lint nodes which are reactive hook callbacks.
if (!isReactiveHookCallback(node, options)) return;
if (!isReactiveHookCallback(node, options)) {
return;
}

// Get the reactive hook node.
const reactiveHook = node.parent.callee;
Expand All @@ -132,7 +134,9 @@ export default {
// second argument then the reactive callback will re-run on every render.
// So no need to check for dependency inclusion.
const declaredDependenciesNode = node.parent.arguments[1];
if (!declaredDependenciesNode) return;
if (!declaredDependenciesNode) {
return;
}

// Get the current scope.
const scope = context.getScope();
Expand All @@ -150,22 +154,30 @@ export default {
let currentScope = scope.upper;
while (currentScope) {
pureScopes.add(currentScope);
if (currentScope.type === 'function') break;
if (currentScope.type === 'function') {
break;
}
currentScope = currentScope.upper;
}
// If there is no parent function scope then there are no pure scopes.
// The ones we've collected so far are incorrect. So don't continue with
// the lint.
if (!currentScope) return;
if (!currentScope) {
return;
}
}

// Get dependencies from all our resolved references in pure scopes.
const dependencies = new Map();
for (const reference of scope.references) {
// If this reference is not resolved or it is not declared in a pure
// scope then we don't care about this reference.
if (!reference.resolved) continue;
if (!pureScopes.has(reference.resolved.scope)) continue;
if (!reference.resolved) {
continue;
}
if (!pureScopes.has(reference.resolved.scope)) {
continue;
}
// Narrow the scope of a dependency if it is, say, a member expression.
// Then normalize the narrowed dependency.

Expand All @@ -178,7 +190,9 @@ export default {
// Add the dependency to a map so we can make sure it is referenced
// again in our dependencies array.
let nodes = dependencies.get(dependency);
if (!nodes) dependencies.set(dependency, (nodes = []));
if (!nodes) {
dependencies.set(dependency, (nodes = []));
}
nodes.push(dependencyNode);
}

Expand All @@ -198,7 +212,9 @@ export default {
} else {
for (const declaredDependencyNode of declaredDependenciesNode.elements) {
// Skip elided elements.
if (declaredDependencyNode === null) continue;
if (declaredDependencyNode === null) {
continue;
}
// If we see a spread element then add a special warning.
if (declaredDependencyNode.type === 'SpreadElement') {
context.report(
Expand Down Expand Up @@ -343,11 +359,18 @@ function fastFindReferenceWithParent(start, target) {
while (queue.length) {
item = queue.shift();

if (isSameIdentifier(item, target)) return item;
if (!isAncestorNodeOf(item, target)) continue;
if (isSameIdentifier(item, target)) {
return item;
}

if (!isAncestorNodeOf(item, target)) {
continue;
}

for (let [key, value] of Object.entries(item)) {
if (key === 'parent') continue;
if (key === 'parent') {
continue;
}
if (isNodeLike(value)) {
value.parent = item;
queue.push(value);
Expand Down

0 comments on commit bf7c57b

Please sign in to comment.