-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
feat: improve duplicate key error for keyed each blocks #8411
Conversation
@rafistrauss is attempting to deploy a commit to the Svelte Team on Vercel. A member of the Team first needs to authorize it. |
Since keys can be anything this should probably use something similar to
|
src/runtime/internal/keyed_each.ts
Outdated
@@ -112,7 +112,7 @@ export function validate_each_keys(ctx, list, get_context, get_key) { | |||
for (let i = 0; i < list.length; i++) { | |||
const key = get_key(get_context(ctx, list, i)); | |||
if (keys.has(key)) { | |||
throw new Error('Cannot have duplicate keys in a keyed each'); | |||
throw new Error(`Cannot have duplicate keys in a keyed each: ${key} is a duplicate`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrap around with quotes to have a clearer idea of when the key starts and ends, especially when having string value as key.
Also, i would suggest add JSON.stringify()
to differentiate between number and string value key
throw new Error(`Cannot have duplicate keys in a keyed each: ${key} is a duplicate`); | |
throw new Error(`Cannot have duplicate keys in a keyed each: \`${JSON.stringify(key)}\` is a duplicate`); |
For the problem that Prinzhorn mentioned, maybe using |
Instead of trying to find a good stringified representation, I suggest to instead print out the index of the duplicates. |
Why not both? The string itself is much easier if your data is being reordered or transformed, so having that is the primary benefit in my mind. Then, if that approach fails due to the issues raised, provide the index as a fallback |
Closes #8410
Adds the duplicate key in the error message, to allow for easier debugging.
Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests
npm test
and lint the project withnpm run lint