core icon indicating copy to clipboard operation
core copied to clipboard

fix(runtime-core): warn about negative number in v-for

Open cyrilf opened this issue 1 year ago • 6 comments

Context

When using v-for with a negative value, let's say -1, an error occurs in the console:

RangeError: Invalid array length image

You can see a reproduction here.


Proposed solution

If the number wasn't an integer, we would get a nice warning:

image

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 the runtime-core change. This one was failing silently as it's using a for loop compared to the runtime-core using a new Array(source) where source was -1, hence resulting in the RangeError: 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)

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.

cyrilf avatar Nov 01 '24 12:11 cyrilf