Skip to content

Commit c71dc02

Browse files
committed
Move private functions out of the class
1 parent e73a7ad commit c71dc02

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

lib/util/Components.js

+35-34
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,34 @@ const doctrine = require('doctrine');
1010
const variableUtil = require('./variable');
1111
const pragmaUtil = require('./pragma');
1212

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+
1341
/**
1442
* Components
1543
*/
@@ -18,10 +46,6 @@ class Components {
1846
this._list = {};
1947
}
2048

21-
_getId(node) {
22-
return node && node.range.join(':');
23-
}
24-
2549
/**
2650
* Add a node to the components list, or update it if it's already in the list
2751
*
@@ -30,7 +54,7 @@ class Components {
3054
* @returns {Object} Added component object
3155
*/
3256
add(node, confidence) {
33-
const id = this._getId(node);
57+
const id = getId(node);
3458
if (this._list[id]) {
3559
if (confidence === 0 || this._list[id].confidence === 0) {
3660
this._list[id].confidence = 0;
@@ -53,7 +77,7 @@ class Components {
5377
* @returns {Object} Component object, undefined if the component is not found
5478
*/
5579
get(node) {
56-
const id = this._getId(node);
80+
const id = getId(node);
5781
return this._list[id];
5882
}
5983

@@ -64,13 +88,13 @@ class Components {
6488
* @param {Object} props Additional properties to add to the component.
6589
*/
6690
set(node, props) {
67-
while (node && !this._list[this._getId(node)]) {
91+
while (node && !this._list[getId(node)]) {
6892
node = node.parent;
6993
}
7094
if (!node) {
7195
return;
7296
}
73-
const id = this._getId(node);
97+
const id = getId(node);
7498
let copyUsedPropTypes;
7599
if (this._list[id]) {
76100
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
@@ -79,7 +103,7 @@ class Components {
79103
}
80104
this._list[id] = util._extend(this._list[id], props);
81105
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);
83107
}
84108
}
85109

@@ -112,7 +136,7 @@ class Components {
112136
if (component) {
113137
const newUsedProps = (this._list[i].usedPropTypes || []).filter(propType => !propType.node || propType.node.kind !== 'init');
114138

115-
const componentId = this._getId(component.node);
139+
const componentId = getId(component.node);
116140
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
117141
}
118142
}
@@ -122,7 +146,7 @@ class Components {
122146
if (!has(this._list, j) || this._list[j].confidence < 2) {
123147
continue;
124148
}
125-
const id = this._getId(this._list[j].node);
149+
const id = getId(this._list[j].node);
126150
list[j] = this._list[j];
127151
if (usedPropTypes[id]) {
128152
list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]);
@@ -147,29 +171,6 @@ class Components {
147171
}
148172
return length;
149173
}
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-
}
173174
}
174175

175176
function componentRule(rule, context) {

0 commit comments

Comments
 (0)