core
core copied to clipboard
fix(runtime-core): warn about negative number in v-for
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-corechange. This one was failing silently as it's using afor loopcompared to theruntime-coreusing anew Array(source)wheresourcewas-1, hence resulting in theRangeError: Invalid array lengtherror message. -
I did a second commit for the
runtime-corepackage, 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)
Summary by CodeRabbit
-
Bug Fixes
- Improved validation for list rendering to require a positive integer, preventing negative or non-integer values from being accepted.
- Updated warning messages to clearly state that a positive integer is expected for list rendering.
- Ensured that no warnings are issued and no items are rendered when the input is zero.
-
Tests
- Added and updated test cases to cover edge cases for numeric inputs, including negative numbers, zero, and undefined values.