@@ -73,11 +73,35 @@ 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' , {
94
+ node,
95
+ } ) ;
96
+ } else {
97
+ const attrs = node . openingElement . attributes ;
98
+
99
+ if ( checkKeyMustBeforeSpread && isKeyAfterSpread ( attrs ) ) {
100
+ report ( context , messages . keyBeforeSpread , 'keyBeforeSpread' , {
101
+ node : node . type === 'ArrayExpression' ? node : node . parent ,
102
+ } ) ;
103
+ }
104
+ }
81
105
} else if ( checkFragmentShorthand && node . type === 'JSXFragment' ) {
82
106
report ( context , messages . missingIterKeyUsePrag , 'missingIterKeyUsePrag' , {
83
107
node,
@@ -115,20 +139,6 @@ module.exports = {
115
139
return returnStatements ;
116
140
}
117
141
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
142
/**
133
143
* Checks if the given node is a function expression or arrow function,
134
144
* and checks if there is a missing key prop in return statement's arguments
0 commit comments