Skip to content

Commit 14fd70d

Browse files
committed
[Refactor] Components: use a closed-over WeakMap instead of a not-private property
1 parent 56e669f commit 14fd70d

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

lib/util/Components.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ function mergeUsedPropTypes(propsList, newPropsList) {
4545
return propsList.concat(propsToAdd);
4646
}
4747

48+
const Lists = new WeakMap();
49+
4850
/**
4951
* Components
5052
*/
5153
class Components {
5254
constructor() {
53-
this._list = {};
55+
Lists.set(this, {});
5456
}
5557

5658
/**
@@ -62,19 +64,20 @@ class Components {
6264
*/
6365
add(node, confidence) {
6466
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;
6871
} else {
69-
this._list[id].confidence = Math.max(this._list[id].confidence, confidence);
72+
list[id].confidence = Math.max(list[id].confidence, confidence);
7073
}
71-
return this._list[id];
74+
return list[id];
7275
}
73-
this._list[id] = {
76+
list[id] = {
7477
node,
7578
confidence
7679
};
77-
return this._list[id];
80+
return list[id];
7881
}
7982

8083
/**
@@ -85,8 +88,9 @@ class Components {
8588
*/
8689
get(node) {
8790
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;
9094
}
9195
return null;
9296
}
@@ -98,13 +102,14 @@ class Components {
98102
* @param {Object} props Additional properties to add to the component.
99103
*/
100104
set(node, props) {
101-
let component = this._list[getId(node)];
105+
const list = Lists.get(this);
106+
let component = list[getId(node)];
102107
while (!component) {
103108
node = node.parent;
104109
if (!node) {
105110
return;
106111
}
107-
component = this._list[getId(node)];
112+
component = list[getId(node)];
108113
}
109114

110115
Object.assign(
@@ -126,14 +131,15 @@ class Components {
126131
* @returns {Object} Components list
127132
*/
128133
list() {
134+
const thisList = Lists.get(this);
129135
const list = {};
130136
const usedPropTypes = {};
131137

132138
// 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) => {
134140
let component = null;
135141
let node = null;
136-
node = this._list[i].node;
142+
node = thisList[i].node;
137143
while (!component && node.parent) {
138144
node = node.parent;
139145
// Stop moving up if we reach a decorator
@@ -143,7 +149,7 @@ class Components {
143149
component = this.get(node);
144150
}
145151
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');
147153

148154
const componentId = getId(component.node);
149155

@@ -152,9 +158,9 @@ class Components {
152158
});
153159

154160
// 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];
158164
if (usedPropTypes[id]) {
159165
list[j].usedPropTypes = mergeUsedPropTypes(list[j].usedPropTypes || [], usedPropTypes[id]);
160166
}
@@ -169,7 +175,8 @@ class Components {
169175
* @returns {Number} Components list length
170176
*/
171177
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;
173180
}
174181
}
175182

0 commit comments

Comments
 (0)