瀏覽代碼

Add warning for duplicate x-for keys

Caleb Porzio 1 年之前
父節點
當前提交
f6e87ce7ed
共有 1 個文件被更改,包括 11 次插入3 次删除
  1. 11 3
      packages/alpinejs/src/directives/x-for.js

+ 11 - 3
packages/alpinejs/src/directives/x-for.js

@@ -60,7 +60,11 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
             items = Object.entries(items).map(([key, value]) => {
             items = Object.entries(items).map(([key, value]) => {
                 let scope = getIterationScopeVariables(iteratorNames, value, key, items)
                 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)
                 scopes.push(scope)
             })
             })
@@ -68,7 +72,11 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
             for (let i = 0; i < items.length; i++) {
             for (let i = 0; i < items.length; i++) {
                 let scope = getIterationScopeVariables(iteratorNames, items[i], i, items)
                 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)
                 scopes.push(scope)
             }
             }
@@ -158,7 +166,7 @@ function loop(el, iteratorNames, evaluateItems, evaluateKey) {
             let marker = document.createElement('div')
             let marker = document.createElement('div')
 
 
             mutateDom(() => {
             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)
                 elForSpot.after(marker)
                 elInSpot.after(elForSpot)
                 elInSpot.after(elForSpot)