Skip to content

Commit bd083bf

Browse files
authored
Merge pull request jsx-eslint#1946 from alexzherdev/used-prop-types-refactoring
[Refactor] Extract used propTypes detection
2 parents eb69dc5 + fd03bd7 commit bd083bf

16 files changed

+656
-908
lines changed

lib/rules/boolean-prop-naming.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const propsUtil = require('../util/props');
109
const docsUrl = require('../util/docsUrl');
@@ -248,7 +247,7 @@ module.exports = {
248247
}
249248
}
250249

251-
if (!has(list, component) || (list[component].invalidProps || []).length) {
250+
if (list[component].invalidProps && list[component].invalidProps.length > 0) {
252251
reportInvalidNaming(list[component]);
253252
}
254253
});

lib/rules/default-props-match-prop-types.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
'use strict';
77

8-
const has = require('has');
98
const Components = require('../util/Components');
109
const variableUtil = require('../util/variable');
1110
const annotations = require('../util/annotations');
@@ -595,11 +594,7 @@ module.exports = {
595594
stack = null;
596595
const list = components.list();
597596

598-
for (const component in list) {
599-
if (!has(list, component)) {
600-
continue;
601-
}
602-
597+
Object.keys(list).forEach(component => {
603598
// If no defaultProps could be found, we don't report anything.
604599
if (!list[component].defaultProps) {
605600
return;
@@ -609,7 +604,7 @@ module.exports = {
609604
list[component].propTypes,
610605
list[component].defaultProps || {}
611606
);
612-
}
607+
});
613608
}
614609
};
615610
})

lib/rules/display-name.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const astUtil = require('../util/ast');
109
const docsUrl = require('../util/docsUrl');
@@ -216,12 +215,9 @@ module.exports = {
216215
'Program:exit': function() {
217216
const list = components.list();
218217
// Report missing display name for all components
219-
for (const component in list) {
220-
if (!has(list, component) || list[component].hasDisplayName) {
221-
continue;
222-
}
218+
Object.keys(list).filter(component => !list[component].hasDisplayName).forEach(component => {
223219
reportMissingDisplayName(list[component]);
224-
}
220+
});
225221
}
226222
};
227223
})

lib/rules/no-multi-comp.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
109

@@ -59,17 +58,15 @@ module.exports = {
5958
}
6059

6160
const list = components.list();
62-
let i = 0;
6361

64-
for (const component in list) {
65-
if (!has(list, component) || isIgnored(list[component]) || ++i === 1) {
66-
continue;
62+
Object.keys(list).filter(component => !isIgnored(list[component])).forEach((component, i) => {
63+
if (i >= 1) {
64+
context.report({
65+
node: list[component].node,
66+
message: MULTI_COMP_MESSAGE
67+
});
6768
}
68-
context.report({
69-
node: list[component].node,
70-
message: MULTI_COMP_MESSAGE
71-
});
72-
}
69+
});
7370
}
7471
};
7572
})

lib/rules/no-set-state.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
'use strict';
66

7-
const has = require('has');
87
const Components = require('../util/Components');
98
const docsUrl = require('../util/docsUrl');
109

@@ -74,12 +73,9 @@ module.exports = {
7473

7574
'Program:exit': function() {
7675
const list = components.list();
77-
for (const component in list) {
78-
if (!has(list, component) || isValid(list[component])) {
79-
continue;
80-
}
76+
Object.keys(list).filter(component => !isValid(list[component])).forEach(component => {
8177
reportSetStateUsages(list[component]);
82-
}
78+
});
8379
}
8480
};
8581
})

0 commit comments

Comments
 (0)