Skip to content

Commit

Permalink
Add warning for duplicate x-for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jan 24, 2024
1 parent 2a9d9d8 commit f6e87ce
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/alpinejs/src/directives/x-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
items = Object.entries(items).map(([key, value]) => {
let scope = getIterationScopeVariables(iteratorNames, value, key, items)

evaluateKey(value => keys.push(value), { scope: { index: key, ...scope} })
evaluateKey(value => {
if (keys.includes(value)) warn('Duplicate key on x-for', el)

keys.push(value)
}, { scope: { index: key, ...scope} })

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

evaluateKey(value => keys.push(value), { scope: { index: i, ...scope} })
evaluateKey(value => {
if (keys.includes(value)) warn('Duplicate key on x-for', el)

keys.push(value)
}, { scope: { index: i, ...scope} })

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

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

elForSpot.after(marker)
elInSpot.after(elForSpot)
Expand Down

0 comments on commit f6e87ce

Please # to comment.