@@ -30,39 +30,42 @@ export default createRule<[], MessageID>({
3030 name : RULE_NAME ,
3131 create ( context ) {
3232 if ( ! context . sourceCode . text . includes ( "createContext" ) ) return { } ;
33- const contexts = new Set < TSESTree . CallExpression > ( ) ;
34- const displayNameAssignments = new Set < TSESTree . AssignmentExpression > ( ) ;
33+ // `React.createContext` calls
34+ const createCalls : TSESTree . CallExpression [ ] = [ ] ;
35+ // `context.displayName = ...` assignment expressions
36+ const displayNameAssignments : TSESTree . AssignmentExpression [ ] = [ ] ;
3537 return {
3638 CallExpression ( node ) {
3739 if ( ! isCreateContextCall ( node , context ) ) return ;
38- contexts . add ( node ) ;
40+ createCalls . push ( node ) ;
3941 } ,
4042 [ DISPLAY_NAME_ASSIGNMENT_SELECTOR ] ( node ) {
41- displayNameAssignments . add ( node ) ;
43+ displayNameAssignments . push ( node ) ;
4244 } ,
4345 "Program:exit" ( ) {
44- for ( const ctx of contexts ) {
45- const id = VAR . getVariableId ( ctx ) ;
46+ for ( const call of createCalls ) {
47+ const id = VAR . getVariableId ( call ) ;
4648 if ( id == null ) {
4749 context . report ( {
4850 messageId : "noMissingContextDisplayName" ,
49- node : ctx ,
51+ node : call ,
5052 } ) ;
5153 continue ;
5254 }
53- const hasDisplayNameAssignment = [ ...displayNameAssignments ] . some ( ( node ) => {
54- const left = node . left ;
55- if ( left . type !== T . MemberExpression ) return false ;
56- const object = left . object ;
57- return VAR . isVariableIdEqual ( id , object , [
58- context . sourceCode . getScope ( id ) ,
59- context . sourceCode . getScope ( object ) ,
60- ] ) ;
61- } ) ;
55+ const hasDisplayNameAssignment = displayNameAssignments
56+ . some ( ( node ) => {
57+ const left = node . left ;
58+ if ( left . type !== T . MemberExpression ) return false ;
59+ const object = left . object ;
60+ return VAR . isVariableIdEqual ( id , object , [
61+ context . sourceCode . getScope ( id ) ,
62+ context . sourceCode . getScope ( object ) ,
63+ ] ) ;
64+ } ) ;
6265 if ( ! hasDisplayNameAssignment ) {
6366 context . report ( {
6467 messageId : "noMissingContextDisplayName" ,
65- node : ctx ,
68+ node : call ,
6669 } ) ;
6770 }
6871 }
0 commit comments