@@ -11,6 +11,34 @@ const variableUtil = require('./variable');
1111const pragmaUtil = require ( './pragma' ) ;
1212const astUtil = require ( './ast' ) ;
1313
14+ function getId ( node ) {
15+ return node && node . range . join ( ':' ) ;
16+ }
17+
18+
19+ function usedPropTypesAreEquivalent ( propA , propB ) {
20+ if ( propA . name === propB . name ) {
21+ if ( ! propA . allNames && ! propB . allNames ) {
22+ return true ;
23+ } else if ( Array . isArray ( propA . allNames ) && Array . isArray ( propB . allNames ) && propA . allNames . join ( '' ) === propB . allNames . join ( '' ) ) {
24+ return true ;
25+ }
26+ return false ;
27+ }
28+ return false ;
29+ }
30+
31+ function mergeUsedPropTypes ( propsList , newPropsList ) {
32+ const propsToAdd = [ ] ;
33+ newPropsList . forEach ( newProp => {
34+ const newPropisAlreadyInTheList = propsList . some ( prop => usedPropTypesAreEquivalent ( prop , newProp ) ) ;
35+ if ( ! newPropisAlreadyInTheList ) {
36+ propsToAdd . push ( newProp ) ;
37+ }
38+ } ) ;
39+ return propsList . concat ( propsToAdd ) ;
40+ }
41+
1442/**
1543 * Components
1644 */
@@ -19,10 +47,6 @@ class Components {
1947 this . _list = { } ;
2048 }
2149
22- _getId ( node ) {
23- return node && node . range . join ( ':' ) ;
24- }
25-
2650 /**
2751 * Add a node to the components list, or update it if it's already in the list
2852 *
@@ -31,7 +55,7 @@ class Components {
3155 * @returns {Object } Added component object
3256 */
3357 add ( node , confidence ) {
34- const id = this . _getId ( node ) ;
58+ const id = getId ( node ) ;
3559 if ( this . _list [ id ] ) {
3660 if ( confidence === 0 || this . _list [ id ] . confidence === 0 ) {
3761 this . _list [ id ] . confidence = 0 ;
@@ -54,7 +78,7 @@ class Components {
5478 * @returns {Object } Component object, undefined if the component is not found
5579 */
5680 get ( node ) {
57- const id = this . _getId ( node ) ;
81+ const id = getId ( node ) ;
5882 return this . _list [ id ] ;
5983 }
6084
@@ -65,13 +89,13 @@ class Components {
6589 * @param {Object } props Additional properties to add to the component.
6690 */
6791 set ( node , props ) {
68- while ( node && ! this . _list [ this . _getId ( node ) ] ) {
92+ while ( node && ! this . _list [ getId ( node ) ] ) {
6993 node = node . parent ;
7094 }
7195 if ( ! node ) {
7296 return ;
7397 }
74- const id = this . _getId ( node ) ;
98+ const id = getId ( node ) ;
7599 let copyUsedPropTypes ;
76100 if ( this . _list [ id ] ) {
77101 // usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
@@ -80,7 +104,7 @@ class Components {
80104 }
81105 this . _list [ id ] = util . _extend ( this . _list [ id ] , props ) ;
82106 if ( this . _list [ id ] && props . usedPropTypes ) {
83- this . _list [ id ] . usedPropTypes = this . _mergeUsedPropTypes ( copyUsedPropTypes || [ ] , props . usedPropTypes ) ;
107+ this . _list [ id ] . usedPropTypes = mergeUsedPropTypes ( copyUsedPropTypes || [ ] , props . usedPropTypes ) ;
84108 }
85109 }
86110
@@ -113,7 +137,7 @@ class Components {
113137 if ( component ) {
114138 const newUsedProps = ( this . _list [ i ] . usedPropTypes || [ ] ) . filter ( propType => ! propType . node || propType . node . kind !== 'init' ) ;
115139
116- const componentId = this . _getId ( component . node ) ;
140+ const componentId = getId ( component . node ) ;
117141 usedPropTypes [ componentId ] = ( usedPropTypes [ componentId ] || [ ] ) . concat ( newUsedProps ) ;
118142 }
119143 }
@@ -123,7 +147,7 @@ class Components {
123147 if ( ! has ( this . _list , j ) || this . _list [ j ] . confidence < 2 ) {
124148 continue ;
125149 }
126- const id = this . _getId ( this . _list [ j ] . node ) ;
150+ const id = getId ( this . _list [ j ] . node ) ;
127151 list [ j ] = this . _list [ j ] ;
128152 if ( usedPropTypes [ id ] ) {
129153 list [ j ] . usedPropTypes = ( list [ j ] . usedPropTypes || [ ] ) . concat ( usedPropTypes [ id ] ) ;
@@ -148,29 +172,6 @@ class Components {
148172 }
149173 return length ;
150174 }
151-
152- _mergeUsedPropTypes ( propsList , newPropsList ) {
153- const propsToAdd = [ ] ;
154- newPropsList . forEach ( newProp => {
155- const newPropisAlreadyInTheList = propsList . some ( prop => this . _usedPropTypesAreEquivalent ( prop , newProp ) ) ;
156- if ( ! newPropisAlreadyInTheList ) {
157- propsToAdd . push ( newProp ) ;
158- }
159- } ) ;
160- return propsList . concat ( propsToAdd ) ;
161- }
162-
163- _usedPropTypesAreEquivalent ( propA , propB ) {
164- if ( propA . name === propB . name ) {
165- if ( ! propA . allNames && ! propB . allNames ) {
166- return true ;
167- } else if ( Array . isArray ( propA . allNames ) && Array . isArray ( propB . allNames ) && propA . allNames . join ( '' ) === propB . allNames . join ( '' ) ) {
168- return true ;
169- }
170- return false ;
171- }
172- return false ;
173- }
174175}
175176
176177function componentRule ( rule , context ) {
0 commit comments