Skip to content

Commit

Permalink
feat: improve duplicate key error for keyed each blocks (#8411)
Browse files Browse the repository at this point in the history
Closes #8410

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
  • Loading branch information
rafistrauss and dummdidumm authored Apr 18, 2023
1 parent 236ffa8 commit f30faa7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/runtime/internal/keyed_each.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,18 @@ export function update_keyed_each(old_blocks, dirty, get_key, dynamic, ctx, list
}

export function validate_each_keys(ctx, list, get_context, get_key) {
const keys = new Set();
const keys = new Map();
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');
let value = '';
try {
value = `with value '${String(key)}' `;
} catch (e) {
// can't stringify
}
throw new Error(`Cannot have duplicate keys in a keyed each: Keys at index ${keys.get(key)} and ${i} ${value}are duplicates`);
}
keys.add(key);
keys.set(key, i);
}
}
2 changes: 1 addition & 1 deletion test/runtime/samples/keyed-each-dev-unique/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ export default {
dev: true
},

error: 'Cannot have duplicate keys in a keyed each'
error: 'Cannot have duplicate keys in a keyed each: Keys at index 0 and 3 with value \'1\' are duplicates'
};

0 comments on commit f30faa7

Please sign in to comment.