Skip to content

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

Merged
merged 3 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor Author

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 for reduce 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 :)

Copy link
Contributor

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.

if (!newPropisAlreadyInTheList) {
propsToAdd.push(newProp);
}
});
return propsList.concat(propsToAdd);
};


/**
* Components
* @class
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Copy link
Contributor

@jseminck jseminck Jul 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that copyUsedPropTypes is going to be assigned a value but that it will not be used?

The condition for assigning the value is: if (this._list[id])
The condition for using the value is: if (this._list[id] && props.usedPropTypes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
};

/**
Expand Down
29 changes: 26 additions & 3 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,7 @@ ruleTester.run('no-unused-prop-types', rule, {
'const Thing = (props) => (',
' <div>',
' {(() => {',
' if(props.enabled){',
' if(props.enabled && props.test){',
' return (',
' <span>Enabled!</span>',
' )',
Expand All @@ -1910,13 +1910,36 @@ ruleTester.run('no-unused-prop-types', rule, {
');',

'Thing.propTypes = {',
' enabled: React.PropTypes.bool',
' enabled: React.PropTypes.bool,',
' test: React.PropTypes.bool',
'};'
].join('\n')
}, {
// issue 1268
code: [
'export default function SampleComp(props) {',
' function buildText(text) {',
' return (',
' <span>',
' {text}',
' {props.append}',
' </span>',
' );',
' }',
' return (',
' <div>',
' {buildText(props.text)}',
' </div>',
' );',
' }',
'SampleComp.propTypes = {',
' text: PropTypes.string,',
' append: PropTypes.string',
'};'
].join('\n')
}
],


invalid: [
{
code: [
Expand Down