Skip to content

Commit 57a2057

Browse files
committed
feat: warn on colliding query with params
1 parent cc734c0 commit 57a2057

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎packages/router/src/diagnostics.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export const diagnostics = /*#__PURE__*/ defineDiagnostics({
3232
`The optional path param "${p.param}" is being removed with ${p.value}.`,
3333
fix: 'Use null to remove an optional path param.',
3434
},
35+
// --- experimental/route-resolver/resolver-fixed.ts ---
36+
VUE_ROUTER_R0123: {
37+
why: (p: { key: string }) =>
38+
`The query value of "${p.key}" is ignored because a param already sets this key.`,
39+
fix: (p: { key: string }) =>
40+
`Pass the value in \`params\` instead of \`query\`, or omit "${p.key}" from \`query\`.`,
41+
},
3542
// --- experimental/router.ts ---
3643
VUE_ROUTER_D0001: {
3744
why: (p: { name: string }) =>

‎packages/router/src/experimental/param-diagnostics.spec.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from 'vitest'
22
import { createFixedResolver } from './route-resolver/resolver-fixed'
3-
import { MatcherPatternPathDynamic } from './route-resolver/matchers/matcher-pattern'
3+
import {
4+
MatcherPatternPathDynamic,
5+
MatcherPatternPathStatic,
6+
} from './route-resolver/matchers/matcher-pattern'
7+
import { MatcherPatternQueryParam } from './route-resolver/matchers/matcher-pattern-query'
48
import { defineParamParserRaw } from './route-resolver/matchers/param-parsers'
59
import { mockWarn } from '../tests/vitest-mock-warn'
610

@@ -118,3 +122,30 @@ describe('optional param removal diagnostics', () => {
118122
})
119123
})
120124
})
125+
126+
describe('ignored query param diagnostics', () => {
127+
const r = createFixedResolver([
128+
{
129+
name: 'list',
130+
path: new MatcherPatternPathStatic('/'),
131+
query: [new MatcherPatternQueryParam('page', 'page', 'value')],
132+
},
133+
])
134+
const current = r.resolve('/?page=5')
135+
136+
it('warns when a query value is ignored for a param', () => {
137+
expect(r.resolve({ query: { page: ['3'] } }, current)).toMatchObject({
138+
fullPath: '/?page=5',
139+
})
140+
if (__DEV__) {
141+
expect('VUE_ROUTER_R0123').toHaveBeenWarnedTimes(1)
142+
expect('"page"').toHaveBeenWarned()
143+
}
144+
})
145+
146+
it('does not warn when the query value is used', () => {
147+
r.resolve({ query: { ...current.query, other: ['x'] } }, current)
148+
r.resolve({ name: 'list', params: {}, query: { page: ['3'] } })
149+
expect('VUE_ROUTER_R0123').not.toHaveBeenWarned()
150+
})
151+
})

‎packages/router/src/experimental/route-resolver/resolver-fixed.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type {
2828
import { NO_MATCH_LOCATION } from './resolver-abstract'
2929
import type { MatcherPatternQuery } from './matchers/matcher-pattern-query'
3030
import { warn } from '../../warning'
31+
import { diagnostics } from '../../diagnostics'
3132
import { assign } from '../../utils'
3233

3334
/**
@@ -276,6 +277,15 @@ export function createFixedResolver<
276277
)
277278
)
278279

280+
if (__DEV__) {
281+
for (const key in to.query) {
282+
// a param overrode the value passed in `to.query`
283+
if (String(query[key]) !== String(to.query[key])) {
284+
diagnostics.VUE_ROUTER_R0123({ key })
285+
}
286+
}
287+
}
288+
279289
const url: LocationNormalized = {
280290
// preserve other fields like `state` and `replace`
281291
...to,

0 commit comments

Comments
 (0)