Skip to content

Commit 1ac4508

Browse files
committed
Simplify
1 parent e0397de commit 1ac4508

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

packages/eslint-plugin-react-hooks/src/RulesOfHooks.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -135,57 +135,36 @@ export default {
135135
create(context) {
136136
const codePathReactHooksMapStack = [];
137137
const codePathSegmentStack = [];
138-
const useEventViolations = new Map();
138+
const useEventViolations = new Set();
139139

140140
/**
141-
* For a given AST node, traverse into it and add all useEvent definitions, namespaced by the
142-
* scope in which the definition was created. We can do this in non-Program nodes because we can
143-
* rely on the assumption that useEvent functions can only be declared within a component or
144-
* hook.
141+
* For a given AST node, traverse into it and add all useEvent definitions. We can do this in
142+
* non-Program nodes because we can rely on the assumption that useEvent functions can only be
143+
* declared within a component or hook.
145144
*
146-
* @param {Scope} scope https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scope-interface
147145
* @param {Node} node
148146
*/
149-
function addAllUseEventViolations(scope, node) {
147+
function addAllUseEventViolations(node) {
150148
traverse(context, node, childNode => {
151149
if (isUseEventVariableDeclarator(childNode)) {
152-
addUseEventViolation(scope, childNode.id);
150+
useEventViolations.add(childNode.id);
153151
}
154152
});
155153
}
156154

157-
/**
158-
* Add a single useEvent violation.
159-
*
160-
* @param {Scope} scope https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scope-interface
161-
* @param {Node<Identifier>} ident
162-
*/
163-
function addUseEventViolation(scope, ident) {
164-
const scopedViolations = useEventViolations.get(scope) || new Set();
165-
scopedViolations.add(ident);
166-
if (!useEventViolations.has(scope)) {
167-
useEventViolations.set(scope, scopedViolations);
168-
}
169-
}
170-
171155
/**
172156
* Resolve a useEvent violation, ie the useEvent created function was called.
173157
*
174158
* @param {Scope} scope https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scope-interface
175159
* @param {Node<Identifier>} ident
176160
*/
177161
function resolveUseEventViolation(scope, ident) {
178-
if (scope.references == null) return;
162+
if (scope.references == null || useEventViolations.size === 0) return;
179163
for (const ref of scope.references) {
180164
if (ref.resolved == null) continue;
181-
// Look up the referenced variables used in the current scope, and record that we invoked
182-
// a useEvent created function against its scope.
183-
const scopedViolations = useEventViolations.get(ref.resolved.scope);
184-
if (scopedViolations == null) continue;
185-
if (scopedViolations.size === 0) break; // All violations have been resolved
186165
const [useEventFunctionIdentifier] = ref.resolved.identifiers;
187166
if (ident.name === useEventFunctionIdentifier.name) {
188-
scopedViolations.delete(useEventFunctionIdentifier);
167+
useEventViolations.delete(useEventFunctionIdentifier);
189168
break;
190169
}
191170
}
@@ -632,29 +611,27 @@ export default {
632611
FunctionDeclaration(node) {
633612
// function MyComponent() { const onClick = useEvent(...) }
634613
if (isInsideComponentOrHook(node)) {
635-
addAllUseEventViolations(context.getScope(), node);
614+
addAllUseEventViolations(node);
636615
}
637616
},
638617

639618
ArrowFunctionExpression(node) {
640619
// const MyComponent = () => { const onClick = useEvent(...) }
641620
if (isInsideComponentOrHook(node)) {
642-
addAllUseEventViolations(context.getScope(), node);
621+
addAllUseEventViolations(node);
643622
}
644623
},
645624

646625
'Program:exit'(_node) {
647-
for (const scopedViolations of useEventViolations.values()) {
648-
for (const node of scopedViolations) {
649-
context.report({
650-
node,
651-
message:
652-
'Functions created with React Hook "useEvent" must be invoked locally in the ' +
653-
`component it's defined in. \`${context.getSource(
654-
node,
655-
)}\` is a useEvent function that is being passed by reference.`,
656-
});
657-
}
626+
for (const node of useEventViolations.values()) {
627+
context.report({
628+
node,
629+
message:
630+
'Functions created with React Hook "useEvent" must be invoked locally in the ' +
631+
`component it's defined in. \`${context.getSource(
632+
node,
633+
)}\` is a useEvent function that is being passed by reference.`,
634+
});
658635
}
659636
},
660637
};

0 commit comments

Comments
 (0)