Skip to content

Commit bc5435d

Browse files
authored
Merge pull request jsx-eslint#1322 from DianaSuvorova/issue_#1309
Fix an issue with unused prop types. Fixes jsx-eslint#1309.
2 parents 8139569 + 095af73 commit bc5435d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/util/Components.js

+33
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ const doctrine = require('doctrine');
1010
const variableUtil = require('./variable');
1111
const pragmaUtil = require('./pragma');
1212

13+
const usedPropTypesAreEquivalent = (propA, propB) => {
14+
if (propA.name === propB.name) {
15+
if (!propA.allNames && !propB.allNames) {
16+
return true;
17+
} else if (Array.isArray(propA.allNames) && Array.isArray(propB.allNames) && propA.allNames.join('') === propB.allNames.join('')) {
18+
return true;
19+
}
20+
return false;
21+
}
22+
return false;
23+
};
24+
25+
const mergeUsedPropTypes = (propsList, newPropsList) => {
26+
const propsToAdd = [];
27+
newPropsList.forEach(newProp => {
28+
const newPropisAlreadyInTheList = propsList.some(prop => usedPropTypesAreEquivalent(prop, newProp));
29+
if (!newPropisAlreadyInTheList) {
30+
propsToAdd.push(newProp);
31+
}
32+
});
33+
return propsList.concat(propsToAdd);
34+
};
35+
36+
1337
/**
1438
* Components
1539
* @class
@@ -70,7 +94,16 @@ Components.prototype.set = function(node, props) {
7094
return;
7195
}
7296
const id = this._getId(node);
97+
let copyUsedPropTypes;
98+
if (this._list[id]) {
99+
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
100+
// preserving original array so it can be merged later on.
101+
copyUsedPropTypes = this._list[id].usedPropTypes && this._list[id].usedPropTypes.slice();
102+
}
73103
this._list[id] = util._extend(this._list[id], props);
104+
if (this._list[id] && props.usedPropTypes) {
105+
this._list[id].usedPropTypes = mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes);
106+
}
74107
};
75108

76109
/**

tests/lib/rules/no-unused-prop-types.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ ruleTester.run('no-unused-prop-types', rule, {
18971897
'const Thing = (props) => (',
18981898
' <div>',
18991899
' {(() => {',
1900-
' if(props.enabled){',
1900+
' if(props.enabled && props.test){',
19011901
' return (',
19021902
' <span>Enabled!</span>',
19031903
' )',
@@ -1910,13 +1910,13 @@ ruleTester.run('no-unused-prop-types', rule, {
19101910
');',
19111911

19121912
'Thing.propTypes = {',
1913-
' enabled: React.PropTypes.bool',
1913+
' enabled: React.PropTypes.bool,',
1914+
' test: React.PropTypes.bool',
19141915
'};'
19151916
].join('\n')
19161917
}
19171918
],
19181919

1919-
19201920
invalid: [
19211921
{
19221922
code: [

0 commit comments

Comments
 (0)