@@ -11,6 +11,34 @@ const variableUtil = require('./variable');
11
11
const pragmaUtil = require ( './pragma' ) ;
12
12
const astUtil = require ( './ast' ) ;
13
13
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
+
14
42
/**
15
43
* Components
16
44
*/
@@ -19,10 +47,6 @@ class Components {
19
47
this . _list = { } ;
20
48
}
21
49
22
- _getId ( node ) {
23
- return node && node . range . join ( ':' ) ;
24
- }
25
-
26
50
/**
27
51
* Add a node to the components list, or update it if it's already in the list
28
52
*
@@ -31,7 +55,7 @@ class Components {
31
55
* @returns {Object } Added component object
32
56
*/
33
57
add ( node , confidence ) {
34
- const id = this . _getId ( node ) ;
58
+ const id = getId ( node ) ;
35
59
if ( this . _list [ id ] ) {
36
60
if ( confidence === 0 || this . _list [ id ] . confidence === 0 ) {
37
61
this . _list [ id ] . confidence = 0 ;
@@ -54,7 +78,7 @@ class Components {
54
78
* @returns {Object } Component object, undefined if the component is not found
55
79
*/
56
80
get ( node ) {
57
- const id = this . _getId ( node ) ;
81
+ const id = getId ( node ) ;
58
82
return this . _list [ id ] ;
59
83
}
60
84
@@ -65,13 +89,13 @@ class Components {
65
89
* @param {Object } props Additional properties to add to the component.
66
90
*/
67
91
set ( node , props ) {
68
- while ( node && ! this . _list [ this . _getId ( node ) ] ) {
92
+ while ( node && ! this . _list [ getId ( node ) ] ) {
69
93
node = node . parent ;
70
94
}
71
95
if ( ! node ) {
72
96
return ;
73
97
}
74
- const id = this . _getId ( node ) ;
98
+ const id = getId ( node ) ;
75
99
let copyUsedPropTypes ;
76
100
if ( this . _list [ id ] ) {
77
101
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
@@ -80,7 +104,7 @@ class Components {
80
104
}
81
105
this . _list [ id ] = util . _extend ( this . _list [ id ] , props ) ;
82
106
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 ) ;
84
108
}
85
109
}
86
110
@@ -113,7 +137,7 @@ class Components {
113
137
if ( component ) {
114
138
const newUsedProps = ( this . _list [ i ] . usedPropTypes || [ ] ) . filter ( propType => ! propType . node || propType . node . kind !== 'init' ) ;
115
139
116
- const componentId = this . _getId ( component . node ) ;
140
+ const componentId = getId ( component . node ) ;
117
141
usedPropTypes [ componentId ] = ( usedPropTypes [ componentId ] || [ ] ) . concat ( newUsedProps ) ;
118
142
}
119
143
}
@@ -123,7 +147,7 @@ class Components {
123
147
if ( ! has ( this . _list , j ) || this . _list [ j ] . confidence < 2 ) {
124
148
continue ;
125
149
}
126
- const id = this . _getId ( this . _list [ j ] . node ) ;
150
+ const id = getId ( this . _list [ j ] . node ) ;
127
151
list [ j ] = this . _list [ j ] ;
128
152
if ( usedPropTypes [ id ] ) {
129
153
list [ j ] . usedPropTypes = ( list [ j ] . usedPropTypes || [ ] ) . concat ( usedPropTypes [ id ] ) ;
@@ -148,29 +172,6 @@ class Components {
148
172
}
149
173
return length ;
150
174
}
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
- }
174
175
}
175
176
176
177
function componentRule ( rule , context ) {
0 commit comments