Skip to content

Commit d50df15

Browse files
committed
Use object.fromentries; fix multiple concats
1 parent 48e4be0 commit d50df15

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

lib/util/Components.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,10 @@ function componentRule(rule, context) {
698698
const updatedRuleInstructions = util._extend({}, ruleInstructions);
699699
const propTypesInstructions = propTypes(context, components, utils);
700700
const defaultPropsInstructions = defaultProps(context, components, utils);
701-
const allKeys = new Set(Object.keys(detectionInstructions).concat(Object.keys(propTypesInstructions))
702-
.concat(Object.keys(defaultPropsInstructions)));
701+
const allKeys = new Set(Object.keys(detectionInstructions).concat(
702+
Object.keys(propTypesInstructions),
703+
Object.keys(defaultPropsInstructions)
704+
));
703705
allKeys.forEach(instruction => {
704706
updatedRuleInstructions[instruction] = function(node) {
705707
if (instruction in detectionInstructions) {

lib/util/defaultProps.js

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

6+
const fromEntries = require('object.fromentries');
67
const astUtil = require('./ast');
78
const propsUtil = require('./props');
89
const variableUtil = require('./variable');
@@ -85,10 +86,11 @@ module.exports = function defaultPropsInstructions(context, components, utils) {
8586
}
8687

8788
const defaults = component.defaultProps || {};
88-
const newDefaultProps = defaultProps.reduce((acc, prop) => {
89-
acc[prop.name] = prop;
90-
return acc;
91-
}, Object.assign({}, defaults));
89+
const newDefaultProps = Object.assign(
90+
{},
91+
defaults,
92+
fromEntries(defaultProps.map(prop => [prop.name, prop]))
93+
);
9294

9395
components.set(component.node, {
9496
defaultProps: newDefaultProps

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"doctrine": "^2.1.0",
2929
"has": "^1.0.3",
3030
"jsx-ast-utils": "^2.0.1",
31+
"object.fromentries": "^2.0.0",
3132
"prop-types": "^15.6.2"
3233
},
3334
"devDependencies": {

0 commit comments

Comments
 (0)