Skip to content

Commit

Permalink
Merge pull request #1946 from alexzherdev/used-prop-types-refactoring
Browse files Browse the repository at this point in the history
[Refactor] Extract used propTypes detection
  • Loading branch information
ljharb committed Sep 20, 2018
2 parents eb69dc5 + fd03bd7 commit bd083bf
Show file tree
Hide file tree
Showing 16 changed files with 656 additions and 908 deletions.
3 changes: 1 addition & 2 deletions lib/rules/boolean-prop-naming.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const propsUtil = require('../util/props');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -248,7 +247,7 @@ module.exports = {
}
}

if (!has(list, component) || (list[component].invalidProps || []).length) {
if (list[component].invalidProps && list[component].invalidProps.length > 0) {
reportInvalidNaming(list[component]);
}
});
Expand Down
9 changes: 2 additions & 7 deletions lib/rules/default-props-match-prop-types.js
Expand Up @@ -5,7 +5,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const variableUtil = require('../util/variable');
const annotations = require('../util/annotations');
Expand Down Expand Up @@ -595,11 +594,7 @@ module.exports = {
stack = null;
const list = components.list();

for (const component in list) {
if (!has(list, component)) {
continue;
}

Object.keys(list).forEach(component => {
// If no defaultProps could be found, we don't report anything.
if (!list[component].defaultProps) {
return;
Expand All @@ -609,7 +604,7 @@ module.exports = {
list[component].propTypes,
list[component].defaultProps || {}
);
}
});
}
};
})
Expand Down
8 changes: 2 additions & 6 deletions lib/rules/display-name.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -216,12 +215,9 @@ module.exports = {
'Program:exit': function() {
const list = components.list();
// Report missing display name for all components
for (const component in list) {
if (!has(list, component) || list[component].hasDisplayName) {
continue;
}
Object.keys(list).filter(component => !list[component].hasDisplayName).forEach(component => {
reportMissingDisplayName(list[component]);
}
});
}
};
})
Expand Down
17 changes: 7 additions & 10 deletions lib/rules/no-multi-comp.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -59,17 +58,15 @@ module.exports = {
}

const list = components.list();
let i = 0;

for (const component in list) {
if (!has(list, component) || isIgnored(list[component]) || ++i === 1) {
continue;
Object.keys(list).filter(component => !isIgnored(list[component])).forEach((component, i) => {
if (i >= 1) {
context.report({
node: list[component].node,
message: MULTI_COMP_MESSAGE
});
}
context.report({
node: list[component].node,
message: MULTI_COMP_MESSAGE
});
}
});
}
};
})
Expand Down
8 changes: 2 additions & 6 deletions lib/rules/no-set-state.js
Expand Up @@ -4,7 +4,6 @@
*/
'use strict';

const has = require('has');
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');

Expand Down Expand Up @@ -74,12 +73,9 @@ module.exports = {

'Program:exit': function() {
const list = components.list();
for (const component in list) {
if (!has(list, component) || isValid(list[component])) {
continue;
}
Object.keys(list).filter(component => !isValid(list[component])).forEach(component => {
reportSetStateUsages(list[component]);
}
});
}
};
})
Expand Down

0 comments on commit bd083bf

Please sign in to comment.