@@ -10,6 +10,34 @@ const doctrine = require('doctrine');
10
10
const variableUtil = require ( './variable' ) ;
11
11
const pragmaUtil = require ( './pragma' ) ;
12
12
13
+ function getId ( node ) {
14
+ return node && node . range . join ( ':' ) ;
15
+ }
16
+
17
+
18
+ function usedPropTypesAreEquivalent ( propA , propB ) {
19
+ if ( propA . name === propB . name ) {
20
+ if ( ! propA . allNames && ! propB . allNames ) {
21
+ return true ;
22
+ } else if ( Array . isArray ( propA . allNames ) && Array . isArray ( propB . allNames ) && propA . allNames . join ( '' ) === propB . allNames . join ( '' ) ) {
23
+ return true ;
24
+ }
25
+ return false ;
26
+ }
27
+ return false ;
28
+ }
29
+
30
+ function mergeUsedPropTypes ( propsList , newPropsList ) {
31
+ const propsToAdd = [ ] ;
32
+ newPropsList . forEach ( newProp => {
33
+ const newPropisAlreadyInTheList = propsList . some ( prop => usedPropTypesAreEquivalent ( prop , newProp ) ) ;
34
+ if ( ! newPropisAlreadyInTheList ) {
35
+ propsToAdd . push ( newProp ) ;
36
+ }
37
+ } ) ;
38
+ return propsList . concat ( propsToAdd ) ;
39
+ }
40
+
13
41
/**
14
42
* Components
15
43
*/
@@ -18,10 +46,6 @@ class Components {
18
46
this . _list = { } ;
19
47
}
20
48
21
- _getId ( node ) {
22
- return node && node . range . join ( ':' ) ;
23
- }
24
-
25
49
/**
26
50
* Add a node to the components list, or update it if it's already in the list
27
51
*
@@ -30,7 +54,7 @@ class Components {
30
54
* @returns {Object } Added component object
31
55
*/
32
56
add ( node , confidence ) {
33
- const id = this . _getId ( node ) ;
57
+ const id = getId ( node ) ;
34
58
if ( this . _list [ id ] ) {
35
59
if ( confidence === 0 || this . _list [ id ] . confidence === 0 ) {
36
60
this . _list [ id ] . confidence = 0 ;
@@ -53,7 +77,7 @@ class Components {
53
77
* @returns {Object } Component object, undefined if the component is not found
54
78
*/
55
79
get ( node ) {
56
- const id = this . _getId ( node ) ;
80
+ const id = getId ( node ) ;
57
81
return this . _list [ id ] ;
58
82
}
59
83
@@ -64,13 +88,13 @@ class Components {
64
88
* @param {Object } props Additional properties to add to the component.
65
89
*/
66
90
set ( node , props ) {
67
- while ( node && ! this . _list [ this . _getId ( node ) ] ) {
91
+ while ( node && ! this . _list [ getId ( node ) ] ) {
68
92
node = node . parent ;
69
93
}
70
94
if ( ! node ) {
71
95
return ;
72
96
}
73
- const id = this . _getId ( node ) ;
97
+ const id = getId ( node ) ;
74
98
let copyUsedPropTypes ;
75
99
if ( this . _list [ id ] ) {
76
100
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
@@ -79,7 +103,7 @@ class Components {
79
103
}
80
104
this . _list [ id ] = util . _extend ( this . _list [ id ] , props ) ;
81
105
if ( this . _list [ id ] && props . usedPropTypes ) {
82
- this . _list [ id ] . usedPropTypes = this . _mergeUsedPropTypes ( copyUsedPropTypes || [ ] , props . usedPropTypes ) ;
106
+ this . _list [ id ] . usedPropTypes = mergeUsedPropTypes ( copyUsedPropTypes || [ ] , props . usedPropTypes ) ;
83
107
}
84
108
}
85
109
@@ -112,7 +136,7 @@ class Components {
112
136
if ( component ) {
113
137
const newUsedProps = ( this . _list [ i ] . usedPropTypes || [ ] ) . filter ( propType => ! propType . node || propType . node . kind !== 'init' ) ;
114
138
115
- const componentId = this . _getId ( component . node ) ;
139
+ const componentId = getId ( component . node ) ;
116
140
usedPropTypes [ componentId ] = ( usedPropTypes [ componentId ] || [ ] ) . concat ( newUsedProps ) ;
117
141
}
118
142
}
@@ -122,7 +146,7 @@ class Components {
122
146
if ( ! has ( this . _list , j ) || this . _list [ j ] . confidence < 2 ) {
123
147
continue ;
124
148
}
125
- const id = this . _getId ( this . _list [ j ] . node ) ;
149
+ const id = getId ( this . _list [ j ] . node ) ;
126
150
list [ j ] = this . _list [ j ] ;
127
151
if ( usedPropTypes [ id ] ) {
128
152
list [ j ] . usedPropTypes = ( list [ j ] . usedPropTypes || [ ] ) . concat ( usedPropTypes [ id ] ) ;
@@ -147,29 +171,6 @@ class Components {
147
171
}
148
172
return length ;
149
173
}
150
-
151
- _mergeUsedPropTypes ( propsList , newPropsList ) {
152
- const propsToAdd = [ ] ;
153
- newPropsList . forEach ( newProp => {
154
- const newPropisAlreadyInTheList = propsList . some ( prop => this . _usedPropTypesAreEquivalent ( prop , newProp ) ) ;
155
- if ( ! newPropisAlreadyInTheList ) {
156
- propsToAdd . push ( newProp ) ;
157
- }
158
- } ) ;
159
- return propsList . concat ( propsToAdd ) ;
160
- }
161
-
162
- _usedPropTypesAreEquivalent ( propA , propB ) {
163
- if ( propA . name === propB . name ) {
164
- if ( ! propA . allNames && ! propB . allNames ) {
165
- return true ;
166
- } else if ( Array . isArray ( propA . allNames ) && Array . isArray ( propB . allNames ) && propA . allNames . join ( '' ) === propB . allNames . join ( '' ) ) {
167
- return true ;
168
- }
169
- return false ;
170
- }
171
- return false ;
172
- }
173
174
}
174
175
175
176
function componentRule ( rule , context ) {
0 commit comments