Skip to content

Commit

Permalink
Updates .eslint to forbid for of loops | Fixes liferay#1031
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Castelain committed Jun 29, 2018
1 parent 2d064b4 commit 3484074
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@
"es6": true,
"jest": true
},
"extends": "liferay"
"extends": "liferay",
"plugins": [
"no-for-of-loops"
],
"rules": {
"no-for-of-loops/no-for-of-loops": 2
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"cross-env": "^5.1.3",
"eslint": "^4.10.0",
"eslint-config-liferay": "^2.0.8",
"eslint-plugin-no-for-of-loops": "1.0.0",
"eslint-plugin-react": "^7.4.0",
"http-server": "^0.9.0",
"husky": "^0.14.2",
Expand Down
70 changes: 45 additions & 25 deletions packages/clay-dataset-display/src/ClayDatasetDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ class ClayDatasetDisplay extends ClayComponent {
let totalItems = 0;

if (this.items) {
for (let item of this.items) {
for (let i = 0, l = this.items.length; i < l; i++) {
const item = this.items[i];

if (item.items) {
totalItems += item.items.length;

for (let childrenItem of item.items) {
for (let j = 0, k = item.items.length; j < k; j++) {
const childrenItem = item.items[j];

if (childrenItem.selected) {
selectedItems.push(childrenItem);
}
Expand All @@ -55,13 +59,24 @@ class ClayDatasetDisplay extends ClayComponent {
* @private
*/
_deselectAllItems() {
for (let item of this._selectedItems) {
item.selected = false;
}
this._selectedItems.map(item => (item.selected = false));

this._selectedItems = [];
}

/**
* Flattens an array of items.
* @param {Array} items
* @return {Array}
*/
_flattenItems(items) {
return items.reduce((list, value) => {
return list.concat(
Array.isArray(value) ? this._flattenItems(value) : value
);
}, []);
}

/**
* Returns the selected items.
* @private
Expand Down Expand Up @@ -151,7 +166,13 @@ class ClayDatasetDisplay extends ClayComponent {
let itemId = event.target.getAttribute('value');

if (!checkedStatus) {
for (let [index, item] of this._selectedItems.entries()) {
const entries = Object.entries(this._selectedItems);

for (let i = 0, l = entries.length; i < l; i++) {
const entry = entries[i];
const index = entry[0];
let item = entry[1];

if (
item[
this.views[this.selectedView].schema.inputValueField
Expand All @@ -166,9 +187,13 @@ class ClayDatasetDisplay extends ClayComponent {
let found = false;

if (this.items) {
for (let item of this.items) {
for (let i = 0, l = this.items.length; i < l; i++) {
const item = this.items[i];

if (item.items) {
for (let childrenItem of item.items) {
for (let j = 0, k = item.items.length; j < k; j++) {
const childrenItem = item.items[j];

if (
childrenItem[
this.views[this.selectedView].schema
Expand Down Expand Up @@ -249,12 +274,12 @@ class ClayDatasetDisplay extends ClayComponent {

this.views[this.selectedView].active = false;

const views = this.views.entries();
for (let i = 0, l = this.views.length; i < l; i++) {
const view = this.views[i];

for (let [index, view] of views) {
if (view === item) {
this.views[index].active = true;
this.selectedView = index;
this.views[i].active = true;
this.selectedView = i;
break;
}
}
Expand All @@ -266,23 +291,18 @@ class ClayDatasetDisplay extends ClayComponent {
* @private
*/
_selectAllItems() {
let selectedItems = [];

for (let item of this.items) {
if (item.items) {
for (let childrenItem of item.items) {
childrenItem.selected = true;

selectedItems.push(childrenItem);
}
const selectedItems = this.items.map(item => {
if (Array.isArray(item.items)) {
return item.items.map(item => {
item.selected = true;
});
} else {
item.selected = true;

selectedItems.push(item);
return item;
}
}
});

this._selectedItems = selectedItems;
this._selectedItems = this._flattenItems(selectedItems);
}
}

Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4412,6 +4412,10 @@ eslint-plugin-liferayportal@^1.0.0:
dependencies:
requireindex "~1.1.0"

eslint-plugin-no-for-of-loops@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-no-for-of-loops/-/eslint-plugin-no-for-of-loops-1.0.0.tgz#a13d91a8f1922f7fefedeab351dc0055994601f6"

eslint-plugin-react@^7.4.0:
version "7.7.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.7.0.tgz#f606c719dbd8a1a2b3d25c16299813878cca0160"
Expand Down

0 comments on commit 3484074

Please sign in to comment.