Open Zearin opened 1 year ago
(Thinking on this a bit more this morning)
I’m not technical enough to understand the nuances of an AST, so maybe there is a reason that the line of code mentioned above (AstSerializer.getLoopContent()
)—which depends on the type
field in the object returned by Looping.parse()
—is expecting that type
field to be only Object
or Array
.
(…aaand this is where my ignorance of how of parsers and ASTs work may show…)
Wouldn’t it be better to just use the Iterator Protocol on whatever is being looped over? That way, users of WebC can expect iteration on their data to work just as it does anywhere else.
(I can see this is also connected with the Virtual Machine evaluating the code inline, but I don’t fully understand how.)
(How could I forget?! 🤭)
The WebC documentation says:
It works with Objects and any Iterable (String, Array, Map, Set, etc).
Started a test case for this bug in #180.
(Renamed this issue from:
bug: “loopContent.map is not a function (via TypeError)”
To:
bug:
webc:for
does not work with any Iterable
I’m trying to loop over some data in a WebC component. After struggling on and off for a few weeks, I remembered that I could set breakpoints in VSCode and use the debugger to hone in on the problem.
I discovered this line of the ast, where
loopContent
is a JavaScriptMap
object, but (inside of asyncgetLoopContent()
), the AST call toLooping.parse()
has reported its type asArray
.Since the AST is expecting an array, it when it tries to call
.map()
onloopContent
, it throws.(I tried using both
webc:for="(key, val) of this.$data
andwebc:for="(key, val) of this.$data.entries()
. The same error happens, because neither are actually anArray
object, butMap
andMapIterator
.)