Skip to content

Commit

Permalink
Use object.fromentries; fix multiple concats
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzherdev committed Sep 8, 2018
1 parent de531ee commit 3f41753
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,10 @@ function componentRule(rule, context) {
const updatedRuleInstructions = util._extend({}, ruleInstructions);
const propTypesInstructions = propTypes(context, components, utils);
const defaultPropsInstructions = defaultProps(context, components, utils);
const allKeys = new Set(Object.keys(detectionInstructions).concat(Object.keys(propTypesInstructions))
.concat(Object.keys(defaultPropsInstructions)));
const allKeys = new Set(Object.keys(detectionInstructions).concat(
Object.keys(propTypesInstructions),
Object.keys(defaultPropsInstructions)
));
allKeys.forEach(instruction => {
updatedRuleInstructions[instruction] = function(node) {
if (instruction in detectionInstructions) {
Expand Down
10 changes: 6 additions & 4 deletions lib/util/defaultProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
'use strict';

const fromEntries = require('object.fromentries');
const astUtil = require('./ast');
const propsUtil = require('./props');
const variableUtil = require('./variable');
Expand Down Expand Up @@ -85,10 +86,11 @@ module.exports = function defaultPropsInstructions(context, components, utils) {
}

const defaults = component.defaultProps || {};
const newDefaultProps = defaultProps.reduce((acc, prop) => {
acc[prop.name] = prop;
return acc;
}, Object.assign({}, defaults));
const newDefaultProps = Object.assign(
{},
defaults,
fromEntries(defaultProps.map(prop => [prop.name, prop]))
);

components.set(component.node, {
defaultProps: newDefaultProps
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"doctrine": "^2.1.0",
"has": "^1.0.3",
"jsx-ast-utils": "^2.0.1",
"object.fromentries": "^2.0.0",
"prop-types": "^15.6.2"
},
"devDependencies": {
Expand Down

0 comments on commit 3f41753

Please sign in to comment.