Skip to content

Commit f30faa7

Browse files
feat: improve duplicate key error for keyed each blocks (#8411)
Closes #8410 --------- Co-authored-by: Simon H <[email protected]>
1 parent 236ffa8 commit f30faa7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/runtime/internal/keyed_each.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,18 @@ export function update_keyed_each(old_blocks, dirty, get_key, dynamic, ctx, list
108108
}
109109

110110
export function validate_each_keys(ctx, list, get_context, get_key) {
111-
const keys = new Set();
111+
const keys = new Map();
112112
for (let i = 0; i < list.length; i++) {
113113
const key = get_key(get_context(ctx, list, i));
114114
if (keys.has(key)) {
115-
throw new Error('Cannot have duplicate keys in a keyed each');
115+
let value = '';
116+
try {
117+
value = `with value '${String(key)}' `;
118+
} catch (e) {
119+
// can't stringify
120+
}
121+
throw new Error(`Cannot have duplicate keys in a keyed each: Keys at index ${keys.get(key)} and ${i} ${value}are duplicates`);
116122
}
117-
keys.add(key);
123+
keys.set(key, i);
118124
}
119125
}

test/runtime/samples/keyed-each-dev-unique/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export default {
33
dev: true
44
},
55

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

0 commit comments

Comments
 (0)