Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: webc:for does not work with any Iterable #179

Open
Zearin opened this issue May 20, 2023 · 4 comments
Open

bug: webc:for does not work with any Iterable #179

Zearin opened this issue May 20, 2023 · 4 comments

Comments

@Zearin
Copy link
Contributor

Zearin commented May 20, 2023

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 JavaScript Map object, but (inside of async getLoopContent()), the AST call to Looping.parse() has reported its type as Array.

Since the AST is expecting an array, it when it tries to call .map() on loopContent, it throws.

(I tried using both webc:for="(key, val) of this.$data and webc:for="(key, val) of this.$data.entries(). The same error happens, because neither are actually an Array object, but Map and MapIterator.)

@Zearin
Copy link
Contributor Author

Zearin commented May 21, 2023

(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.)

@Zearin
Copy link
Contributor Author

Zearin commented May 21, 2023

(How could I forget?! 🤭)

The WebC documentation says:

It works with Objects and any Iterable (String, Array, Map, Set, etc).

Zearin added a commit to Zearin/webc that referenced this issue May 21, 2023
Issue 179:
> bug: “`loopContent.map is not a function (via TypeError)`” 11ty#179
@Zearin
Copy link
Contributor Author

Zearin commented May 21, 2023

Started a test case for this bug in #180.

@Zearin
Copy link
Contributor Author

Zearin commented May 21, 2023

(Renamed this issue from:

bug: “loopContent.map is not a function (via TypeError)”

To:

bug: webc:for does not work with any Iterable

@Zearin Zearin changed the title bug: “loopContent.map is not a function (via TypeError) bug: webc:for does not work with any Iterable May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant