Skip to content

Commit

Permalink
Move private functions out of the class
Browse files Browse the repository at this point in the history
  • Loading branch information
jseminck committed Nov 14, 2017
1 parent a0d47cf commit 04a42a9
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions lib/util/Components.js
Expand Up @@ -11,6 +11,34 @@ const variableUtil = require('./variable');
const pragmaUtil = require('./pragma');
const astUtil = require('./ast');

function getId(node) {
return node && node.range.join(':');
}


function usedPropTypesAreEquivalent(propA, propB) {
if (propA.name === propB.name) {
if (!propA.allNames && !propB.allNames) {
return true;
} else if (Array.isArray(propA.allNames) && Array.isArray(propB.allNames) && propA.allNames.join('') === propB.allNames.join('')) {
return true;
}
return false;
}
return false;
}

function mergeUsedPropTypes(propsList, newPropsList) {
const propsToAdd = [];
newPropsList.forEach(newProp => {
const newPropisAlreadyInTheList = propsList.some(prop => usedPropTypesAreEquivalent(prop, newProp));
if (!newPropisAlreadyInTheList) {
propsToAdd.push(newProp);
}
});
return propsList.concat(propsToAdd);
}

/**
* Components
*/
Expand All @@ -19,10 +47,6 @@ class Components {
this._list = {};
}

_getId(node) {
return node && node.range.join(':');
}

/**
* Add a node to the components list, or update it if it's already in the list
*
Expand All @@ -31,7 +55,7 @@ class Components {
* @returns {Object} Added component object
*/
add(node, confidence) {
const id = this._getId(node);
const id = getId(node);
if (this._list[id]) {
if (confidence === 0 || this._list[id].confidence === 0) {
this._list[id].confidence = 0;
Expand All @@ -54,7 +78,7 @@ class Components {
* @returns {Object} Component object, undefined if the component is not found
*/
get(node) {
const id = this._getId(node);
const id = getId(node);
return this._list[id];
}

Expand All @@ -65,13 +89,13 @@ class Components {
* @param {Object} props Additional properties to add to the component.
*/
set(node, props) {
while (node && !this._list[this._getId(node)]) {
while (node && !this._list[getId(node)]) {
node = node.parent;
}
if (!node) {
return;
}
const id = this._getId(node);
const id = getId(node);
let copyUsedPropTypes;
if (this._list[id]) {
// usedPropTypes is an array. _extend replaces existing array with a new one which caused issue #1309.
Expand All @@ -80,7 +104,7 @@ class Components {
}
this._list[id] = util._extend(this._list[id], props);
if (this._list[id] && props.usedPropTypes) {
this._list[id].usedPropTypes = this._mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes);
this._list[id].usedPropTypes = mergeUsedPropTypes(copyUsedPropTypes || [], props.usedPropTypes);
}
}

Expand Down Expand Up @@ -113,7 +137,7 @@ class Components {
if (component) {
const newUsedProps = (this._list[i].usedPropTypes || []).filter(propType => !propType.node || propType.node.kind !== 'init');

const componentId = this._getId(component.node);
const componentId = getId(component.node);
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
}
}
Expand All @@ -123,7 +147,7 @@ class Components {
if (!has(this._list, j) || this._list[j].confidence < 2) {
continue;
}
const id = this._getId(this._list[j].node);
const id = getId(this._list[j].node);
list[j] = this._list[j];
if (usedPropTypes[id]) {
list[j].usedPropTypes = (list[j].usedPropTypes || []).concat(usedPropTypes[id]);
Expand All @@ -148,29 +172,6 @@ class Components {
}
return length;
}

_mergeUsedPropTypes(propsList, newPropsList) {
const propsToAdd = [];
newPropsList.forEach(newProp => {
const newPropisAlreadyInTheList = propsList.some(prop => this._usedPropTypesAreEquivalent(prop, newProp));
if (!newPropisAlreadyInTheList) {
propsToAdd.push(newProp);
}
});
return propsList.concat(propsToAdd);
}

_usedPropTypesAreEquivalent(propA, propB) {
if (propA.name === propB.name) {
if (!propA.allNames && !propB.allNames) {
return true;
} else if (Array.isArray(propA.allNames) && Array.isArray(propB.allNames) && propA.allNames.join('') === propB.allNames.join('')) {
return true;
}
return false;
}
return false;
}
}

function componentRule(rule, context) {
Expand Down

0 comments on commit 04a42a9

Please sign in to comment.