-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Issue #1309 #1322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #1309 #1322
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,30 @@ const doctrine = require('doctrine'); | |
const variableUtil = require('./variable'); | ||
const pragmaUtil = require('./pragma'); | ||
|
||
const usedPropTypesAreEquivalent = (propA, propB) => { | ||
if (propA.name === propB.name) { | ||
if (!propA.allNames && !propB.allNames) { | ||
return true; | ||
} else if (Array.isArray(propA.allNames) && Array.isArray(propB.allNames) && propA.allNames.join('') === propB.allNames.join('')) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
|
||
const mergeUsedPropTypes = (propsList, newPropsList) => { | ||
const propsToAdd = []; | ||
newPropsList.forEach(newProp => { | ||
const newPropisAlreadyInTheList = propsList.some(prop => usedPropTypesAreEquivalent(prop, newProp)); | ||
if (!newPropisAlreadyInTheList) { | ||
propsToAdd.push(newProp); | ||
} | ||
}); | ||
return propsList.concat(propsToAdd); | ||
}; | ||
|
||
|
||
/** | ||
* Components | ||
* @class | ||
|
@@ -70,7 +94,16 @@ Components.prototype.set = function(node, props) { | |
return; | ||
} | ||
const id = this._getId(node); | ||
let copyUsedPropTypes; | ||
if (this._list[id]) { | ||
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ljharb added a comment here with some explanation for the change. LMK if there is a better place for it. |
||
// preserving original array so it can be merged later on. | ||
copyUsedPropTypes = this._list[id].usedPropTypes && this._list[id].usedPropTypes.slice(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible that The condition for assigning the value is: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, it is possible. |
||
} | ||
this._list[id] = util._extend(this._list[id], props); | ||
if (this._list[id] && props.usedPropTypes) { | ||
this._list[id].usedPropTypes = mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes); | ||
} | ||
}; | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jseminck , thanks for the review. You are correct about
some
function. Updated it to be more concise. As forreduce
I have only used it for very simple logic. If you can rewrite this fnc, I will be happy to try it and copy paste your code if it works :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is good as well.