fix(runtime-core): warn about negative number in v-for #12308
+26
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
When using
v-for
with a negative value, let's say-1
, an error occurs in the console:RangeError: Invalid array length
You can see a reproduction here.
Proposed solution
If the number wasn't an integer, we would get a nice warning:
I'm just extending this warning to also includes negative integers.
This way, it's way easier to understand that the issue arises in the
v-for
and goes into debugging the issue faster.You can see a reproduction of the fix live here. (the console is correctly warning
[Vue warn]: The v-for range expects a positive integer value but got -1.
)Side note
I did a commit for the
server-renderer
, to stay aligned with theruntime-core
change.This one was failing silently as it's using a
for loop
compared to theruntime-core
using anew Array(source)
wheresource
was-1
, hence resulting in theRangeError: Invalid array length
error message.I did a second commit for the
runtime-core
package, the one I want to fix.(actually this issue arised from using the carousel from NuxtUI, but I see that the page / indicators calculation has now been fixed but I still think that this fix is nice to have)