Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Commit

Permalink
Allow recursive selectors and correctly watch for Identifier and Prop…
Browse files Browse the repository at this point in the history
…ertyAccessExpression (#38)

* Allow recursive selectors

* Correctly watch for Identifier and PropertyAccessExpression
  • Loading branch information
jukben authored May 1, 2019
1 parent 3711dfe commit d407344
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/rules/checkUnusedFluxDependenciesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,24 @@ class NoUnusedDependenciesWalker extends Lint.RuleWalker {
const [dependencyNode, implementationNode] = node.arguments;

if (
dependencyNode.kind === ts.SyntaxKind.ArrayLiteralExpression &&
implementationNode.kind === ts.SyntaxKind.ArrowFunction
ts.isArrowFunction(implementationNode) &&
ts.isArrayLiteralExpression(dependencyNode)
) {
const selectorsNodesIdentifiers = (dependencyNode as ts.ArrayLiteralExpression).elements.filter(
a => a.kind === ts.SyntaxKind.Identifier
) as Array<ts.Identifier>;
const selectorsNodesIdentifiers = dependencyNode.elements.filter(
ts.isIdentifier
);

const dependencies = selectorsNodesIdentifiers.map(
element => element.text
);

if (
ts.isVariableDeclaration(node.parent) &&
ts.isIdentifier(node.parent.name)
) {
dependencies.push(node.parent.name.text);
}

const usedDependencies: Array<string> = [];

const addToUsed = (node: ts.Node, identifier: string) => {
Expand Down Expand Up @@ -150,10 +158,20 @@ class NoUnusedDependenciesWalker extends Lint.RuleWalker {

const cb = (startNode: ts.Node): void => {
if (
startNode.kind === ts.SyntaxKind.Identifier &&
startNode.parent.kind !== ts.SyntaxKind.TypeReference
ts.isIdentifier(startNode) &&
!ts.isTypeReferenceNode(startNode.parent) &&
!ts.isPropertyAccessExpression(startNode.parent)
) {
const identifier = startNode.text;

addToUsed(startNode, identifier);
}

if (
ts.isPropertyAccessExpression(startNode) &&
ts.isIdentifier(startNode.expression)
) {
const identifier = (startNode as ts.Identifier).text;
const identifier = startNode.expression.text;

addToUsed(startNode, identifier);
}
Expand All @@ -169,9 +187,9 @@ class NoUnusedDependenciesWalker extends Lint.RuleWalker {
);

unusedDependencies.forEach(unusedSelector => {
const unusedDependenciesNode = (dependencyNode as ts.ArrayLiteralExpression).elements.find(
element => (element as ts.Identifier).text === unusedSelector
);
const unusedDependenciesNode = dependencyNode.elements
.filter(ts.isIdentifier)
.find(element => element.text === unusedSelector);

if (unusedDependenciesNode) {
this.addIssue(
Expand Down
13 changes: 13 additions & 0 deletions test/rules/check-unused-flux-dependencies/test.13.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const superRecursiveSelector = select(
[],
(): boolean => {
return superRecursiveSelector
},
);

export default select(
[],
(): boolean => {
return notASelector.superRecursiveSelector
},
);

0 comments on commit d407344

Please sign in to comment.