@@ -73,11 +73,31 @@ module.exports = {
7373 const reactPragma = pragmaUtil . getFromContext ( context ) ;
7474 const fragmentPragma = pragmaUtil . getFragmentFromContext ( context ) ;
7575
76+ function isKeyAfterSpread ( attributes ) {
77+ let hasFoundSpread = false ;
78+ return attributes . some ( ( attribute ) => {
79+ if ( attribute . type === 'JSXSpreadAttribute' ) {
80+ hasFoundSpread = true ;
81+ return false ;
82+ }
83+ if ( attribute . type !== 'JSXAttribute' ) {
84+ return false ;
85+ }
86+ return hasFoundSpread && propName ( attribute ) === 'key' ;
87+ } ) ;
88+ }
89+
7690 function checkIteratorElement ( node ) {
77- if ( node . type === 'JSXElement' && ! hasProp ( node . openingElement . attributes , 'key' ) ) {
78- report ( context , messages . missingIterKey , 'missingIterKey' , {
79- node,
80- } ) ;
91+ if ( node . type === 'JSXElement' ) {
92+ if ( ! hasProp ( node . openingElement . attributes , 'key' ) ) {
93+ report ( context , messages . missingIterKey , 'missingIterKey' , { node } ) ;
94+ } else {
95+ const attrs = node . openingElement . attributes ;
96+
97+ if ( checkKeyMustBeforeSpread && isKeyAfterSpread ( attrs ) ) {
98+ report ( context , messages . keyBeforeSpread , 'keyBeforeSpread' , { node } ) ;
99+ }
100+ }
81101 } else if ( checkFragmentShorthand && node . type === 'JSXFragment' ) {
82102 report ( context , messages . missingIterKeyUsePrag , 'missingIterKeyUsePrag' , {
83103 node,
@@ -115,20 +135,6 @@ module.exports = {
115135 return returnStatements ;
116136 }
117137
118- function isKeyAfterSpread ( attributes ) {
119- let hasFoundSpread = false ;
120- return attributes . some ( ( attribute ) => {
121- if ( attribute . type === 'JSXSpreadAttribute' ) {
122- hasFoundSpread = true ;
123- return false ;
124- }
125- if ( attribute . type !== 'JSXAttribute' ) {
126- return false ;
127- }
128- return hasFoundSpread && propName ( attribute ) === 'key' ;
129- } ) ;
130- }
131-
132138 /**
133139 * Checks if the given node is a function expression or arrow function,
134140 * and checks if there is a missing key prop in return statement's arguments
0 commit comments