]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): warn multiple params with same name
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 09:27:38 +0000 (11:27 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 24 May 2020 09:28:17 +0000 (11:28 +0200)
__tests__/warnings.spec.ts
src/matcher/pathMatcher.ts

index ec25c745718b061a065cb733d5686d5d1a44c44c..26255078b5f16d9b955ffde442f908d5daadc837 100644 (file)
@@ -146,4 +146,21 @@ describe('warnings', () => {
     await expect(router.push({ path: '//not-valid' })).resolves.toBe(undefined)
     expect('cannot start with multiple slashes').toHaveBeenWarned()
   })
+
+  it('warns if path contains the same param multiple times', () => {
+    const history = createMemoryHistory()
+    createRouter({
+      history,
+      routes: [
+        {
+          path: '/:id',
+          component,
+          children: [{ path: ':id', component }],
+        },
+      ],
+    })
+    expect(
+      'duplicated params with name "id" for path "/:id/:id"'
+    ).toHaveBeenWarned()
+  })
 })
index fd4745dc4453971c2e11f492f0c7ec36dba4a513..f9bfcf6ebb9b3c1aada8f2887096fbbd9ebada1c 100644 (file)
@@ -5,6 +5,7 @@ import {
   PathParserOptions,
 } from './pathParserRanker'
 import { tokenizePath } from './pathTokenizer'
+import { warn } from '../warning'
 
 export interface RouteRecordMatcher extends PathParser {
   record: RouteRecord
@@ -20,6 +21,19 @@ export function createRouteRecordMatcher(
   options?: PathParserOptions
 ): RouteRecordMatcher {
   const parser = tokensToParser(tokenizePath(record.path), options)
+
+  // warn against params with the same name
+  if (__DEV__) {
+    const existingKeys = new Set<string>()
+    for (const key of parser.keys) {
+      if (existingKeys.has(key.name))
+        warn(
+          `Found duplicated params with name "${key.name}" for path "${record.path}". Only the last one will be available on "$route.params".`
+        )
+      existingKeys.add(key.name)
+    }
+  }
+
   const matcher: RouteRecordMatcher = {
     ...parser,
     record,