Importing a polyfill directly doesn't seem like a good solution, especially when we can implement this function without the spread operator. (And in anycase the transpiler should take care of this not us)
The current version looks like this:
function debounce(fn, delay) {
return function debounced() {
let args = arguments;
cancelDebounce(debounced);
debounced.id = setTimeout(function() {
fn(...(null, args));
}, delay);
};
}
And it can be re-written like this:
function debounce(fn, delay) {
return function debounced() {
let args = Array.prototype.slice.call(arguments, 0);
cancelDebounce(debounced);
debounced.id = setTimeout(function() {
fn.apply(null, args);
}, delay);
};
}
NOTE: I'm aware that using apply
is not considered a good practise when using ES6+ by some, but neither is using arguments
so at this point does it really matter?
That being said, once the format task kicks in (pretter-eslint
) the snippet above gets transformed (before it gets transpiled) to:
function debounce(fn, delay) {
return function debounced() {
let args = Array.prototype.slice.call(arguments, 0);
cancelDebounce(debounced);
debounced.id = setTimeout(function() {
fn(...args); // 👈no more apply, spread is here again
}, delay);
};
}
Which gets transpiled to:
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } // 👈Array.from is expected to be available, which is not the case in IE so how do we solve this?
function debounce(fn, delay) {
return function debounced() {
var args = Array.prototype.slice.call(arguments, 0);
cancelDebounce(debounced);
debounced.id = setTimeout(function () {
fn.apply(undefined, _toConsumableArray(args));
}, delay);
};
}
This is probably because our .eslintrc
extends eslint-config-liferay
which extends eslint-config-google
which has
'prefer-rest-params': 2,
'prefer-spread': 2
Which does actually change apply
for spread.
If any one has an idea on how to solve this without changing our config or massive changes, I'd appreciate your feedback.
Thanks