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

Watch for selectors defined in the actual file #21

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/rules/checkUnusedFluxDependenciesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import * as ts from "typescript";
import * as Lint from "tslint";
import { IOptions } from "tslint";

const watchedIdentifiers: { [key: string]: "store" | "selector" } = {};
type TFluxDependency = "store" | "selector";

enum FLUX_FACTORY {
CONNECT = "connect",
SELECT = "select"
}

const watchedIdentifiers: { [key: string]: TFluxDependency } = {};

export class Rule extends Lint.Rules.AbstractRule {
public static NOT_LISTENING = (name: string) =>
Expand Down Expand Up @@ -44,9 +51,13 @@ const getImports = (node: ts.ImportDeclaration) => {
];
};

const addToWatchedIdentifiers = (
const addToWatchedIdentifiers = (name: string, type: TFluxDependency) => {
watchedIdentifiers[name] = type;
};

const addToWatchedIdentifiersFromImportNode = (
node: ts.ImportDeclaration,
type: "store" | "selector",
type: TFluxDependency,
onlyDefault?: boolean
) => {
if (onlyDefault) {
Expand All @@ -61,7 +72,7 @@ const addToWatchedIdentifiers = (
}

for (const i of getImports(node)) {
watchedIdentifiers[i] = type;
addToWatchedIdentifiers(i, type);
}
};

Expand Down Expand Up @@ -215,11 +226,11 @@ class NoUnusedDependenciesWalker extends Lint.RuleWalker {
const moduleName = (node.moduleSpecifier as ts.StringLiteral).text;

if (moduleName.match(/store/i)) {
addToWatchedIdentifiers(node, "store", true);
addToWatchedIdentifiersFromImportNode(node, "store", true);
}

if (moduleName.match(/selectors/i)) {
addToWatchedIdentifiers(node, "selector");
addToWatchedIdentifiersFromImportNode(node, "selector");
}

super.visitImportDeclaration(node);
Expand All @@ -230,9 +241,19 @@ class NoUnusedDependenciesWalker extends Lint.RuleWalker {

if (
node.expression.kind === ts.SyntaxKind.Identifier &&
// support select lib and aso connect called as argument, for instance with 'compose'
(identifier === "select" || identifier === "connect")
(identifier === FLUX_FACTORY.SELECT ||
identifier === FLUX_FACTORY.CONNECT)
) {
if (
identifier === FLUX_FACTORY.SELECT &&
node.parent.kind === ts.SyntaxKind.VariableDeclaration
) {
const selectorName = ((node.parent as ts.VariableDeclaration)
.name as ts.Identifier).text;

addToWatchedIdentifiers(selectorName, "selector");
}

this.checkDependencies(node);
}

Expand Down
21 changes: 21 additions & 0 deletions test/rules/check-unused-flux-dependencies/test.10.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { show, hide } from 'selectors/some';

const superSelect = select([show, hide], () => {
return {
a: show(),
b: hide(),
}
})

export default connect([show, hide, superSelect], () => ({
~~~~~~~~~~~ [The "superSelect" dependency is unused. This has performance impact!]
a: show(),
b: hide(),
}))(component);

export default connect([show, hide], () => ({
a: show(),
b: hide(),
b: superSelect()
~~~~~~~~~~~ [You forgot to listen for the "superSelect" dependency!]
}))(component);
14 changes: 14 additions & 0 deletions test/rules/check-unused-flux-dependencies/test.8.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { show, hide } from 'selectors/some';

const anotherSelect = select([show, hide], () => {
return {
a: show(),
b: hide(),
}
})

export default select([anotherSelect], () => {
return {
magic: anotherSelect(),
}
})
15 changes: 15 additions & 0 deletions test/rules/check-unused-flux-dependencies/test.9.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { show, hide } from 'selectors/some';

const anotherSelect = select([show, hide], () => {
return {
a: show(),
b: hide(),
}
})

export default select([], () => {
return {
magic: anotherSelect(),
~~~~~~~~~~~~~ [You forgot to listen for the "anotherSelect" dependency!]
}
})