Skip to content

Commit 24190c6

Browse files
authored
Merge pull request #1398 from jseminck/components-as-class
Refactor Components into ES6 class syntax
2 parents 64cf2d7 + cf2d6f6 commit 24190c6

File tree

1 file changed

+128
-124
lines changed

1 file changed

+128
-124
lines changed

lib/util/Components.js

+128-124
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const variableUtil = require('./variable');
1111
const pragmaUtil = require('./pragma');
1212
const astUtil = require('./ast');
1313

14-
const usedPropTypesAreEquivalent = (propA, propB) => {
14+
function getId(node) {
15+
return node && node.range.join(':');
16+
}
17+
18+
19+
function usedPropTypesAreEquivalent(propA, propB) {
1520
if (propA.name === propB.name) {
1621
if (!propA.allNames && !propB.allNames) {
1722
return true;
@@ -21,9 +26,9 @@ const usedPropTypesAreEquivalent = (propA, propB) => {
2126
return false;
2227
}
2328
return false;
24-
};
29+
}
2530

26-
const mergeUsedPropTypes = (propsList, newPropsList) => {
31+
function mergeUsedPropTypes(propsList, newPropsList) {
2732
const propsToAdd = [];
2833
newPropsList.forEach(newProp => {
2934
const newPropisAlreadyInTheList = propsList.some(prop => usedPropTypesAreEquivalent(prop, newProp));
@@ -32,143 +37,142 @@ const mergeUsedPropTypes = (propsList, newPropsList) => {
3237
}
3338
});
3439
return propsList.concat(propsToAdd);
35-
};
36-
40+
}
3741

3842
/**
3943
* Components
40-
* @class
4144
*/
42-
function Components() {
43-
this._list = {};
44-
this._getId = function(node) {
45-
return node && node.range.join(':');
46-
};
47-
}
45+
class Components {
46+
constructor() {
47+
this._list = {};
48+
}
4849

49-
/**
50-
* Add a node to the components list, or update it if it's already in the list
51-
*
52-
* @param {ASTNode} node The AST node being added.
53-
* @param {Number} confidence Confidence in the component detection (0=banned, 1=maybe, 2=yes)
54-
* @returns {Object} Added component object
55-
*/
56-
Components.prototype.add = function(node, confidence) {
57-
const id = this._getId(node);
58-
if (this._list[id]) {
59-
if (confidence === 0 || this._list[id].confidence === 0) {
60-
this._list[id].confidence = 0;
61-
} else {
62-
this._list[id].confidence = Math.max(this._list[id].confidence, confidence);
50+
/**
51+
* Add a node to the components list, or update it if it's already in the list
52+
*
53+
* @param {ASTNode} node The AST node being added.
54+
* @param {Number} confidence Confidence in the component detection (0=banned, 1=maybe, 2=yes)
55+
* @returns {Object} Added component object
56+
*/
57+
add(node, confidence) {
58+
const id = getId(node);
59+
if (this._list[id]) {
60+
if (confidence === 0 || this._list[id].confidence === 0) {
61+
this._list[id].confidence = 0;
62+
} else {
63+
this._list[id].confidence = Math.max(this._list[id].confidence, confidence);
64+
}
65+
return this._list[id];
6366
}
67+
this._list[id] = {
68+
node: node,
69+
confidence: confidence
70+
};
6471
return this._list[id];
6572
}
66-
this._list[id] = {
67-
node: node,
68-
confidence: confidence
69-
};
70-
return this._list[id];
71-
};
72-
73-
/**
74-
* Find a component in the list using its node
75-
*
76-
* @param {ASTNode} node The AST node being searched.
77-
* @returns {Object} Component object, undefined if the component is not found
78-
*/
79-
Components.prototype.get = function(node) {
80-
const id = this._getId(node);
81-
return this._list[id];
82-
};
8373

84-
/**
85-
* Update a component in the list
86-
*
87-
* @param {ASTNode} node The AST node being updated.
88-
* @param {Object} props Additional properties to add to the component.
89-
*/
90-
Components.prototype.set = function(node, props) {
91-
while (node && !this._list[this._getId(node)]) {
92-
node = node.parent;
93-
}
94-
if (!node) {
95-
return;
96-
}
97-
const id = this._getId(node);
98-
let copyUsedPropTypes;
99-
if (this._list[id]) {
100-
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
101-
// preserving original array so it can be merged later on.
102-
copyUsedPropTypes = this._list[id].usedPropTypes && this._list[id].usedPropTypes.slice();
103-
}
104-
this._list[id] = util._extend(this._list[id], props);
105-
if (this._list[id] && props.usedPropTypes) {
106-
this._list[id].usedPropTypes = mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes);
74+
/**
75+
* Find a component in the list using its node
76+
*
77+
* @param {ASTNode} node The AST node being searched.
78+
* @returns {Object} Component object, undefined if the component is not found
79+
*/
80+
get(node) {
81+
const id = getId(node);
82+
return this._list[id];
10783
}
108-
};
10984

110-
/**
111-
* Return the components list
112-
* Components for which we are not confident are not returned
113-
*
114-
* @returns {Object} Components list
115-
*/
116-
Components.prototype.list = function() {
117-
const list = {};
118-
const usedPropTypes = {};
119-
// Find props used in components for which we are not confident
120-
for (const i in this._list) {
121-
if (!has(this._list, i) || this._list[i].confidence >= 2) {
122-
continue;
123-
}
124-
let component = null;
125-
let node = null;
126-
node = this._list[i].node;
127-
while (!component && node.parent) {
85+
/**
86+
* Update a component in the list
87+
*
88+
* @param {ASTNode} node The AST node being updated.
89+
* @param {Object} props Additional properties to add to the component.
90+
*/
91+
set(node, props) {
92+
while (node && !this._list[getId(node)]) {
12893
node = node.parent;
129-
// Stop moving up if we reach a decorator
130-
if (node.type === 'Decorator') {
131-
break;
132-
}
133-
component = this.get(node);
13494
}
135-
if (component) {
136-
const newUsedProps = (this._list[i].usedPropTypes || []).filter(propType => !propType.node || propType.node.kind !== 'init');
137-
138-
const componentId = this._getId(component.node);
139-
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
95+
if (!node) {
96+
return;
97+
}
98+
const id = getId(node);
99+
let copyUsedPropTypes;
100+
if (this._list[id]) {
101+
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
102+
// preserving original array so it can be merged later on.
103+
copyUsedPropTypes = this._list[id].usedPropTypes && this._list[id].usedPropTypes.slice();
104+
}
105+
this._list[id] = util._extend(this._list[id], props);
106+
if (this._list[id] && props.usedPropTypes) {
107+
this._list[id].usedPropTypes = mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes);
140108
}
141109
}
142-
// Assign used props in not confident components to the parent component
143-
for (const j in this._list) {
144-
if (!has(this._list, j) || this._list[j].confidence < 2) {
145-
continue;
110+
111+
/**
112+
* Return the components list
113+
* Components for which we are not confident are not returned
114+
*
115+
* @returns {Object} Components list
116+
*/
117+
list() {
118+
const list = {};
119+
const usedPropTypes = {};
120+
121+
// Find props used in components for which we are not confident
122+
for (const i in this._list) {
123+
if (!has(this._list, i) || this._list[i].confidence >= 2) {
124+
continue;
125+
}
126+
let component = null;
127+
let node = null;
128+
node = this._list[i].node;
129+
while (!component && node.parent) {
130+
node = node.parent;
131+
// Stop moving up if we reach a decorator
132+
if (node.type === 'Decorator') {
133+
break;
134+
}
135+
component = this.get(node);
136+
}
137+
if (component) {
138+
const newUsedProps = (this._list[i].usedPropTypes || []).filter(propType => !propType.node || propType.node.kind !== 'init');
139+
140+
const componentId = getId(component.node);
141+
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
142+
}
146143
}
147-
const id = this._getId(this._list[j].node);
148-
list[j] = this._list[j];
149-
if (usedPropTypes[id]) {
150-
list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]);
144+
145+
// Assign used props in not confident components to the parent component
146+
for (const j in this._list) {
147+
if (!has(this._list, j) || this._list[j].confidence < 2) {
148+
continue;
149+
}
150+
const id = getId(this._list[j].node);
151+
list[j] = this._list[j];
152+
if (usedPropTypes[id]) {
153+
list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]);
154+
}
151155
}
156+
return list;
152157
}
153-
return list;
154-
};
155158

156-
/**
157-
* Return the length of the components list
158-
* Components for which we are not confident are not counted
159-
*
160-
* @returns {Number} Components list length
161-
*/
162-
Components.prototype.length = function() {
163-
let length = 0;
164-
for (const i in this._list) {
165-
if (!has(this._list, i) || this._list[i].confidence < 2) {
166-
continue;
159+
/**
160+
* Return the length of the components list
161+
* Components for which we are not confident are not counted
162+
*
163+
* @returns {Number} Components list length
164+
*/
165+
length() {
166+
let length = 0;
167+
for (const i in this._list) {
168+
if (!has(this._list, i) || this._list[i].confidence < 2) {
169+
continue;
170+
}
171+
length++;
167172
}
168-
length++;
173+
return length;
169174
}
170-
return length;
171-
};
175+
}
172176

173177
function componentRule(rule, context) {
174178
const createClass = pragmaUtil.getCreateClassFromContext(context);
@@ -654,8 +658,8 @@ function componentRule(rule, context) {
654658
return updatedRuleInstructions;
655659
}
656660

657-
Components.detect = function(rule) {
658-
return componentRule.bind(this, rule);
659-
};
660-
661-
module.exports = Components;
661+
module.exports = Object.assign(Components, {
662+
detect(rule) {
663+
return componentRule.bind(this, rule);
664+
}
665+
});

0 commit comments

Comments
 (0)