@@ -73,11 +73,31 @@ module.exports = {
73
73
const reactPragma = pragmaUtil . getFromContext ( context ) ;
74
74
const fragmentPragma = pragmaUtil . getFragmentFromContext ( context ) ;
75
75
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
+
76
90
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
+ }
81
101
} else if ( checkFragmentShorthand && node . type === 'JSXFragment' ) {
82
102
report ( context , messages . missingIterKeyUsePrag , 'missingIterKeyUsePrag' , {
83
103
node,
@@ -115,20 +135,6 @@ module.exports = {
115
135
return returnStatements ;
116
136
}
117
137
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
-
132
138
/**
133
139
* Checks if the given node is a function expression or arrow function,
134
140
* and checks if there is a missing key prop in return statement's arguments
0 commit comments