@@ -135,57 +135,36 @@ export default {
135
135
create ( context ) {
136
136
const codePathReactHooksMapStack = [ ] ;
137
137
const codePathSegmentStack = [ ] ;
138
- const useEventViolations = new Map ( ) ;
138
+ const useEventViolations = new Set ( ) ;
139
139
140
140
/**
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.
145
144
*
146
- * @param {Scope } scope https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scope-interface
147
145
* @param {Node } node
148
146
*/
149
- function addAllUseEventViolations ( scope , node ) {
147
+ function addAllUseEventViolations ( node ) {
150
148
traverse ( context , node , childNode => {
151
149
if ( isUseEventVariableDeclarator ( childNode ) ) {
152
- addUseEventViolation ( scope , childNode . id ) ;
150
+ useEventViolations . add ( childNode . id ) ;
153
151
}
154
152
} ) ;
155
153
}
156
154
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
-
171
155
/**
172
156
* Resolve a useEvent violation, ie the useEvent created function was called.
173
157
*
174
158
* @param {Scope } scope https://eslint.org/docs/latest/developer-guide/scope-manager-interface#scope-interface
175
159
* @param {Node<Identifier> } ident
176
160
*/
177
161
function resolveUseEventViolation ( scope , ident ) {
178
- if ( scope . references == null ) return ;
162
+ if ( scope . references == null || useEventViolations . size === 0 ) return ;
179
163
for ( const ref of scope . references ) {
180
164
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
186
165
const [ useEventFunctionIdentifier ] = ref . resolved . identifiers ;
187
166
if ( ident . name === useEventFunctionIdentifier . name ) {
188
- scopedViolations . delete ( useEventFunctionIdentifier ) ;
167
+ useEventViolations . delete ( useEventFunctionIdentifier ) ;
189
168
break ;
190
169
}
191
170
}
@@ -632,29 +611,27 @@ export default {
632
611
FunctionDeclaration ( node ) {
633
612
// function MyComponent() { const onClick = useEvent(...) }
634
613
if ( isInsideComponentOrHook ( node ) ) {
635
- addAllUseEventViolations ( context . getScope ( ) , node ) ;
614
+ addAllUseEventViolations ( node ) ;
636
615
}
637
616
} ,
638
617
639
618
ArrowFunctionExpression ( node ) {
640
619
// const MyComponent = () => { const onClick = useEvent(...) }
641
620
if ( isInsideComponentOrHook ( node ) ) {
642
- addAllUseEventViolations ( context . getScope ( ) , node ) ;
621
+ addAllUseEventViolations ( node ) ;
643
622
}
644
623
} ,
645
624
646
625
'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
+ } ) ;
658
635
}
659
636
} ,
660
637
} ;
0 commit comments