@@ -45,12 +45,14 @@ function mergeUsedPropTypes(propsList, newPropsList) {
45
45
return propsList . concat ( propsToAdd ) ;
46
46
}
47
47
48
+ const Lists = new WeakMap ( ) ;
49
+
48
50
/**
49
51
* Components
50
52
*/
51
53
class Components {
52
54
constructor ( ) {
53
- this . _list = { } ;
55
+ Lists . set ( this , { } ) ;
54
56
}
55
57
56
58
/**
@@ -62,19 +64,20 @@ class Components {
62
64
*/
63
65
add ( node , confidence ) {
64
66
const id = getId ( node ) ;
65
- if ( this . _list [ id ] ) {
66
- if ( confidence === 0 || this . _list [ id ] . confidence === 0 ) {
67
- this . _list [ id ] . confidence = 0 ;
67
+ const list = Lists . get ( this ) ;
68
+ if ( list [ id ] ) {
69
+ if ( confidence === 0 || list [ id ] . confidence === 0 ) {
70
+ list [ id ] . confidence = 0 ;
68
71
} else {
69
- this . _list [ id ] . confidence = Math . max ( this . _list [ id ] . confidence , confidence ) ;
72
+ list [ id ] . confidence = Math . max ( list [ id ] . confidence , confidence ) ;
70
73
}
71
- return this . _list [ id ] ;
74
+ return list [ id ] ;
72
75
}
73
- this . _list [ id ] = {
76
+ list [ id ] = {
74
77
node,
75
78
confidence
76
79
} ;
77
- return this . _list [ id ] ;
80
+ return list [ id ] ;
78
81
}
79
82
80
83
/**
@@ -85,8 +88,9 @@ class Components {
85
88
*/
86
89
get ( node ) {
87
90
const id = getId ( node ) ;
88
- if ( this . _list [ id ] && this . _list [ id ] . confidence >= 1 ) {
89
- return this . _list [ id ] ;
91
+ const item = Lists . get ( this ) [ id ] ;
92
+ if ( item && item . confidence >= 1 ) {
93
+ return item ;
90
94
}
91
95
return null ;
92
96
}
@@ -98,13 +102,14 @@ class Components {
98
102
* @param {Object } props Additional properties to add to the component.
99
103
*/
100
104
set ( node , props ) {
101
- let component = this . _list [ getId ( node ) ] ;
105
+ const list = Lists . get ( this ) ;
106
+ let component = list [ getId ( node ) ] ;
102
107
while ( ! component ) {
103
108
node = node . parent ;
104
109
if ( ! node ) {
105
110
return ;
106
111
}
107
- component = this . _list [ getId ( node ) ] ;
112
+ component = list [ getId ( node ) ] ;
108
113
}
109
114
110
115
Object . assign (
@@ -126,14 +131,15 @@ class Components {
126
131
* @returns {Object } Components list
127
132
*/
128
133
list ( ) {
134
+ const thisList = Lists . get ( this ) ;
129
135
const list = { } ;
130
136
const usedPropTypes = { } ;
131
137
132
138
// Find props used in components for which we are not confident
133
- Object . keys ( this . _list ) . filter ( i => this . _list [ i ] . confidence < 2 ) . forEach ( ( i ) => {
139
+ Object . keys ( thisList ) . filter ( i => thisList [ i ] . confidence < 2 ) . forEach ( ( i ) => {
134
140
let component = null ;
135
141
let node = null ;
136
- node = this . _list [ i ] . node ;
142
+ node = thisList [ i ] . node ;
137
143
while ( ! component && node . parent ) {
138
144
node = node . parent ;
139
145
// Stop moving up if we reach a decorator
@@ -143,7 +149,7 @@ class Components {
143
149
component = this . get ( node ) ;
144
150
}
145
151
if ( component ) {
146
- const newUsedProps = ( this . _list [ i ] . usedPropTypes || [ ] ) . filter ( propType => ! propType . node || propType . node . kind !== 'init' ) ;
152
+ const newUsedProps = ( thisList [ i ] . usedPropTypes || [ ] ) . filter ( propType => ! propType . node || propType . node . kind !== 'init' ) ;
147
153
148
154
const componentId = getId ( component . node ) ;
149
155
@@ -152,9 +158,9 @@ class Components {
152
158
} ) ;
153
159
154
160
// Assign used props in not confident components to the parent component
155
- Object . keys ( this . _list ) . filter ( j => this . _list [ j ] . confidence >= 2 ) . forEach ( ( j ) => {
156
- const id = getId ( this . _list [ j ] . node ) ;
157
- list [ j ] = this . _list [ j ] ;
161
+ Object . keys ( thisList ) . filter ( j => thisList [ j ] . confidence >= 2 ) . forEach ( ( j ) => {
162
+ const id = getId ( thisList [ j ] . node ) ;
163
+ list [ j ] = thisList [ j ] ;
158
164
if ( usedPropTypes [ id ] ) {
159
165
list [ j ] . usedPropTypes = mergeUsedPropTypes ( list [ j ] . usedPropTypes || [ ] , usedPropTypes [ id ] ) ;
160
166
}
@@ -169,7 +175,8 @@ class Components {
169
175
* @returns {Number } Components list length
170
176
*/
171
177
length ( ) {
172
- return Object . keys ( this . _list ) . filter ( i => this . _list [ i ] . confidence >= 2 ) . length ;
178
+ const list = Lists . get ( this ) ;
179
+ return Object . keys ( list ) . filter ( i => list [ i ] . confidence >= 2 ) . length ;
173
180
}
174
181
}
175
182
0 commit comments