From 93d36a036a38745e3dc4d955554420b600a47581 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 31 Jul 2025 18:04:36 +0200 Subject: [PATCH] refactor: use warn --- packages/router/src/devtools.ts | 3 ++- .../route-resolver/matchers/matcher-pattern.ts | 3 ++- .../route-resolver/resolver-static.ts | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/router/src/devtools.ts b/packages/router/src/devtools.ts index e4bef80d..f2511dce 100644 --- a/packages/router/src/devtools.ts +++ b/packages/router/src/devtools.ts @@ -18,6 +18,7 @@ import { UseLinkDevtoolsContext } from './RouterLink' import { RouterViewDevtoolsContext } from './RouterView' import { assign, isArray } from './utils' import { RouteLocationNormalized } from './typed-routes' +import { warn } from './warning' /** * Copies a route location and removes any problematic properties that cannot be shown in devtools (e.g. Vue instances). @@ -80,7 +81,7 @@ export function addDevtools(app: App, router: Router, matcher: RouterMatcher) { }, api => { if (typeof api.now !== 'function') { - console.warn( + warn( '[Vue Router]: You seem to be using an outdated version of Vue Devtools. Are you still using the Beta release instead of the stable one? You can find the links at https://devtools.vuejs.org/guide/installation.html.' ) } diff --git a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts index 399ba1b0..f939aa57 100644 --- a/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts +++ b/packages/router/src/experimental/route-resolver/matchers/matcher-pattern.ts @@ -1,3 +1,4 @@ +import { warn } from '../../../warning' import { decode, MatcherQueryParams } from '../resolver-abstract' import { miss } from './errors' @@ -236,7 +237,7 @@ export class MatcherPatternPathDynamic< } if (__DEV__ && i !== match.length) { - console.warn( + warn( `Regexp matched ${match.length} params, but ${i} params are defined. Found when matching "${path}" against ${String(this.re)}` ) } diff --git a/packages/router/src/experimental/route-resolver/resolver-static.ts b/packages/router/src/experimental/route-resolver/resolver-static.ts index 4b7068be..0b6efb26 100644 --- a/packages/router/src/experimental/route-resolver/resolver-static.ts +++ b/packages/router/src/experimental/route-resolver/resolver-static.ts @@ -22,6 +22,7 @@ import type { MatcherPatternQuery, MatcherPatternHash, } from './matchers/matcher-pattern' +import { warn } from '../../warning' // TODO: find a better name than static that doesn't conflict with static params // maybe fixed or simple @@ -151,7 +152,7 @@ export function createStaticResolver< if (typeof to === 'object' && (to.name || to.path == null)) { // relative location by path or by name if (__DEV__ && to.name == null && currentLocation == null) { - console.warn( + warn( `Cannot resolve relative location "${JSON.stringify(to)}"without a current location. This will throw in production.`, to ) @@ -175,8 +176,17 @@ export function createStaticResolver< // either one of them must be defined and is catched by the dev only warn above const name = to.name ?? currentLocation!.name const record = recordMap.get(name)! - if (__DEV__ && (!record || !name)) { - throw new Error(`Record "${String(name)}" not found`) + + if (__DEV__) { + if (!record || !name) { + throw new Error(`Record "${String(name)}" not found`) + } + + if (typeof to === 'object' && to.hash?.startsWith('#')) { + warn( + `A \`hash\` should always start with the character "#". Replace "${to.hash}" with "#${to.hash}".` + ) + } } // unencoded params in a formatted form that the user came up with -- 2.47.2