dash: Fix some JS warnings

We currently use "array[index]" to test whether an array has an
element at index before using it. However nowadays gjs warns about
accessing non-existent array elements, so the test itself already
produces a warning. Avoid this by checking the array length before
using an index to access an element.

https://bugzilla.gnome.org/show_bug.cgi?id=781471
This commit is contained in:
Florian Müllner 2017-06-13 03:41:42 +02:00
parent 091fb4ba2e
commit db81ef3e95

View File

@ -756,42 +756,44 @@ const Dash = new Lang.Class({
let newIndex = 0; let newIndex = 0;
let oldIndex = 0; let oldIndex = 0;
while (newIndex < newApps.length || oldIndex < oldApps.length) { while (newIndex < newApps.length || oldIndex < oldApps.length) {
let oldApp = oldApps.length > oldIndex ? oldApps[oldIndex] : null;
let newApp = newApps.length > newIndex ? newApps[newIndex] : null;
// No change at oldIndex/newIndex // No change at oldIndex/newIndex
if (oldApps[oldIndex] == newApps[newIndex]) { if (oldApp == newApp) {
oldIndex++; oldIndex++;
newIndex++; newIndex++;
continue; continue;
} }
// App removed at oldIndex // App removed at oldIndex
if (oldApps[oldIndex] && if (oldApp && newApps.indexOf(oldApp) == -1) {
newApps.indexOf(oldApps[oldIndex]) == -1) {
removedActors.push(children[oldIndex]); removedActors.push(children[oldIndex]);
oldIndex++; oldIndex++;
continue; continue;
} }
// App added at newIndex // App added at newIndex
if (newApps[newIndex] && if (newApp && oldApps.indexOf(newApp) == -1) {
oldApps.indexOf(newApps[newIndex]) == -1) { addedItems.push({ app: newApp,
addedItems.push({ app: newApps[newIndex], item: this._createAppItem(newApp),
item: this._createAppItem(newApps[newIndex]),
pos: newIndex }); pos: newIndex });
newIndex++; newIndex++;
continue; continue;
} }
// App moved // App moved
let insertHere = newApps[newIndex + 1] && let nextApp = newApps.length > newIndex + 1 ? newApps[newIndex + 1]
newApps[newIndex + 1] == oldApps[oldIndex]; : null;
let insertHere = nextApp && nextApp == oldApp;
let alreadyRemoved = removedActors.reduce(function(result, actor) { let alreadyRemoved = removedActors.reduce(function(result, actor) {
let removedApp = actor.child._delegate.app; let removedApp = actor.child._delegate.app;
return result || removedApp == newApps[newIndex]; return result || removedApp == newApp;
}, false); }, false);
if (insertHere || alreadyRemoved) { if (insertHere || alreadyRemoved) {
let newItem = this._createAppItem(newApps[newIndex]); let newItem = this._createAppItem(newApp);
addedItems.push({ app: newApps[newIndex], addedItems.push({ app: newApp,
item: newItem, item: newItem,
pos: newIndex + removedActors.length }); pos: newIndex + removedActors.length });
newIndex++; newIndex++;