feat: improve duplicate key error for keyed each blocks ()

Closes 

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/8515/head
Rafi Strauss 2 years ago committed by GitHub
parent 236ffa833d
commit f30faa702d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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) { 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++) { for (let i = 0; i < list.length; i++) {
const key = get_key(get_context(ctx, list, i)); const key = get_key(get_context(ctx, list, i));
if (keys.has(key)) { 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);
} }
} }

@ -3,5 +3,5 @@ export default {
dev: true 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'
}; };

Loading…
Cancel
Save