]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat(warn): detect missing param in nested absolute paths
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 17:00:43 +0000 (19:00 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 1 May 2020 17:00:43 +0000 (19:00 +0200)
__tests__/warnings.spec.ts
src/matcher/index.ts

index a188219a7b66d3652cbb6b57abef224f40fa82eb..7b54705b5d39b627cb6db94c697cbce2dfe85664 100644 (file)
@@ -49,6 +49,28 @@ describe('warnings', () => {
     ).toHaveBeenWarned()
   })
 
+  it('warns if a child with absolute path is missing a parent param', async () => {
+    createRouter({
+      history: createMemoryHistory(),
+      routes: [
+        {
+          path: '/:a',
+          component,
+          children: [
+            {
+              path: ':b',
+              component,
+              children: [{ path: '/:a/b', component }],
+            },
+          ],
+        },
+      ],
+    })
+    expect(
+      `Absolute path "/:a/b" should have the exact same param named "b" as its parent "/:a/:b".`
+    ).toHaveBeenWarned()
+  })
+
   it('warns if an alias has a param with the same name but different', async () => {
     createRouter({
       history: createMemoryHistory(),
index 02552b74ba5f2116734ab35128a836953bc9c4c3..d4eeb1eb88e823cba666c535226a8f8acbedf45e 100644 (file)
@@ -102,6 +102,9 @@ export function createRouterMatcher(
       // create the object before hand so it can be passed to children
       matcher = createRouteRecordMatcher(normalizedRecord, parent, options)
 
+      if (__DEV__ && parent && path[0] === '/')
+        checkMissingParamsInAbsolutePath(matcher, parent)
+
       // if we are an alias we must tell the original record that we exist
       // so we can be removed
       if (originalRecord) {
@@ -390,4 +393,16 @@ function checkSameParams(a: RouteRecordMatcher, b: RouteRecordMatcher) {
   }
 }
 
+function checkMissingParamsInAbsolutePath(
+  record: RouteRecordMatcher,
+  parent: RouteRecordMatcher
+) {
+  for (let key of parent.keys) {
+    if (!record.keys.find(isSameParam.bind(null, key)))
+      return warn(
+        `Absolute path "${record.record.path}" should have the exact same param named "${key.name}" as its parent "${parent.record.path}".`
+      )
+  }
+}
+
 export { PathParserOptions, _PathParserOptions }