@@ -247,6 +247,39 @@ function getWrapperFunctions(context, pragma) {
247
247
] ) ;
248
248
}
249
249
250
+ // eslint-disable-next-line valid-jsdoc
251
+ /**
252
+ * Merge many eslint rules into one
253
+ * @param {{[_: string]: Function}[] } rules the returned values for eslint rule.create(context)
254
+ * @returns {{[_: string]: Function} } merged rule
255
+ */
256
+ function mergeRules ( rules ) {
257
+ /** @type {Map<string, Function[]> } */
258
+ const handlersByKey = new Map ( ) ;
259
+ rules . forEach ( ( rule ) => {
260
+ Object . keys ( rule ) . forEach ( ( key ) => {
261
+ const fns = handlersByKey . get ( key ) ;
262
+ if ( ! fns ) {
263
+ handlersByKey . set ( key , [ rule [ key ] ] ) ;
264
+ } else {
265
+ fns . push ( rule [ key ] ) ;
266
+ }
267
+ } ) ;
268
+ } ) ;
269
+
270
+ /** @type {{[key: string]: Function} } */
271
+ const rule = { } ;
272
+ handlersByKey . forEach ( ( fns , key ) => {
273
+ rule [ key ] = function mergedHandler ( node ) {
274
+ fns . forEach ( ( fn ) => {
275
+ fn ( node ) ;
276
+ } ) ;
277
+ } ;
278
+ } ) ;
279
+
280
+ return rule ;
281
+ }
282
+
250
283
function componentRule ( rule , context ) {
251
284
const pragma = pragmaUtil . getFromContext ( context ) ;
252
285
const sourceCode = context . getSourceCode ( ) ;
@@ -859,44 +892,21 @@ function componentRule(rule, context) {
859
892
} ,
860
893
} ;
861
894
862
- // Update the provided rule instructions to add the component detection
863
895
const ruleInstructions = rule ( context , components , utils ) ;
864
- const updatedRuleInstructions = Object . assign ( { } , ruleInstructions ) ;
865
896
const propTypesInstructions = propTypesUtil ( context , components , utils ) ;
866
897
const usedPropTypesInstructions = usedPropTypesUtil ( context , components , utils ) ;
867
898
const defaultPropsInstructions = defaultPropsUtil ( context , components , utils ) ;
868
- const allKeys = new Set ( Object . keys ( detectionInstructions ) . concat (
869
- Object . keys ( propTypesInstructions ) ,
870
- Object . keys ( usedPropTypesInstructions ) ,
871
- Object . keys ( defaultPropsInstructions ) ,
872
- Object . keys ( reactImportInstructions )
873
- ) ) ;
874
899
875
- allKeys . forEach ( ( instruction ) => {
876
- updatedRuleInstructions [ instruction ] = ( node ) => {
877
- if ( instruction in detectionInstructions ) {
878
- detectionInstructions [ instruction ] ( node ) ;
879
- }
880
- if ( instruction in propTypesInstructions ) {
881
- propTypesInstructions [ instruction ] ( node ) ;
882
- }
883
- if ( instruction in usedPropTypesInstructions ) {
884
- usedPropTypesInstructions [ instruction ] ( node ) ;
885
- }
886
- if ( instruction in defaultPropsInstructions ) {
887
- defaultPropsInstructions [ instruction ] ( node ) ;
888
- }
889
- if ( instruction in reactImportInstructions ) {
890
- reactImportInstructions [ instruction ] ( node ) ;
891
- }
892
- if ( ruleInstructions [ instruction ] ) {
893
- return ruleInstructions [ instruction ] ( node ) ;
894
- }
895
- } ;
896
- } ) ;
900
+ const mergedRule = mergeRules ( [
901
+ detectionInstructions ,
902
+ propTypesInstructions ,
903
+ usedPropTypesInstructions ,
904
+ defaultPropsInstructions ,
905
+ reactImportInstructions ,
906
+ ruleInstructions ,
907
+ ] ) ;
897
908
898
- // Return the updated rule instructions
899
- return updatedRuleInstructions ;
909
+ return mergedRule ;
900
910
}
901
911
902
912
module . exports = Object . assign ( Components , {
0 commit comments