Skip to content

Commit f6e87ce

Browse files
committed
Add warning for duplicate x-for keys
1 parent 2a9d9d8 commit f6e87ce

File tree

1 file changed

+11
-3
lines changed
  • packages/alpinejs/src/directives

1 file changed

+11
-3
lines changed

packages/alpinejs/src/directives/x-for.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
6060
items = Object.entries(items).map(([key, value]) => {
6161
let scope = getIterationScopeVariables(iteratorNames, value, key, items)
6262

63-
evaluateKey(value => keys.push(value), { scope: { index: key, ...scope} })
63+
evaluateKey(value => {
64+
if (keys.includes(value)) warn('Duplicate key on x-for', el)
65+
66+
keys.push(value)
67+
}, { scope: { index: key, ...scope} })
6468

6569
scopes.push(scope)
6670
})
6771
} else {
6872
for (let i = 0; i < items.length; i++) {
6973
let scope = getIterationScopeVariables(iteratorNames, items[i], i, items)
7074

71-
evaluateKey(value => keys.push(value), { scope: { index: i, ...scope} })
75+
evaluateKey(value => {
76+
if (keys.includes(value)) warn('Duplicate key on x-for', el)
77+
78+
keys.push(value)
79+
}, { scope: { index: i, ...scope} })
7280

7381
scopes.push(scope)
7482
}
@@ -158,7 +166,7 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
158166
let marker = document.createElement('div')
159167

160168
mutateDom(() => {
161-
if (! elForSpot) warn(`x-for ":key" is undefined or invalid`, templateEl)
169+
if (! elForSpot) warn(`x-for ":key" is undefined or invalid`, templateEl, keyForSpot, lookup)
162170

163171
elForSpot.after(marker)
164172
elInSpot.after(elForSpot)

0 commit comments

Comments
 (0)