Good day. I need to use a loop for…of to iterate through values iterator, which is returned by the method keys() URLSearchParams object.
But when I try to declare a loop I get this error:
for…of statements are not supported. Use transforms: { forOf: false } to skip transformation and disable this error, or transforms: { dangerousForOf: true } if you know what you’re doing (155:8)
As I understand it, this setting is Buble? I was trying to find to solve this problem, but to no avail.
dangerousForOf: true fixes the issue you’re currently running into, and modules: false tells Buble it’s okay to ignore the export and import statements in your code (I started getting errors about those as soon as I added the first transforms option). https://buble.surge.sh/guide/#using-es-modules
Thanks to @knowler for helping me get that config right!
Sorry, I missed it in their docs before, but the way Buble compiles for...of statements doesn’t work with objects that implement iterators (like URLSearchParams), only with simpler array-like objects that have a length property.
The result is that Buble compiles your for...of statement into an old-school for structure that doesn’t work with URLSearchParams or UrlSearchParams.keys(). You can try playing with this in your dev console to see what happens in each step:
var searchParams = new URLSearchParams("key1=value1&key2=value2");
for (var i = 0, list = searchParams.keys(); i < list.length; i += 1) {
var key = list[i];
console.log(key); // displays "key1", "key2"
}
There’s no such thing as list.length or list[i]. The Buble output would need to call .next() or use a different approach that works with iterators.
You’ve got at least a couple options:
If you’re happy with the browser support for for...of you can just tell Buble not to process it at all: