Skip to content

Commit 8259a09

Browse files
committed
feat: emit runtime warning for invalid format in query params
1 parent feedef9 commit 8259a09

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

packages/router/src/unplugin/codegen/generateRouteResolver.spec.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
import { ImportsMap } from '../core/utils'
1010
import type { ParamParsersMap } from './generateParamParsers'
1111
import { generateAliasWarnings } from './generateAliasWarnings'
12-
import { mockWarn } from '../../tests/vitest-mock-warn'
1312

1413
const DEFAULT_OPTIONS = resolveOptions({})
1514
let DEFAULT_STATE: Parameters<typeof generateRouteRecord>[0]['state'] = {
@@ -234,8 +233,6 @@ describe('generateRouteRecordQuery', () => {
234233
})
235234

236235
describe('raw param parsers', () => {
237-
mockWarn()
238-
239236
function rawParsersMap(name: string): ParamParsersMap {
240237
return new Map([
241238
[
@@ -266,7 +263,7 @@ describe('generateRouteRecordQuery', () => {
266263
)
267264
})
268265

269-
it('forces format=array and warns when user specifies format=value', () => {
266+
it('forces format=array and emits a runtime warn when user specifies format=value', () => {
270267
const node = new PrefixTree(DEFAULT_OPTIONS).insert('a', 'a.vue')
271268
node.value.setEditOverride('params', {
272269
query: { tags: { parser: 'set', format: 'value' } },
@@ -279,12 +276,13 @@ describe('generateRouteRecordQuery', () => {
279276
expect(result).toContain(
280277
`new MatcherPatternQueryParam('tags', 'tags', 'array', _normalized_PARAM_PARSER__set)`
281278
)
282-
expect(
279+
expect(result).toContain('console.warn(')
280+
expect(result).toContain(
283281
`Query param "tags" in route "/a" uses raw param parser "set"`
284-
).toHaveBeenWarned()
282+
)
285283
})
286284

287-
it('keeps user format=array for raw parsers without warning', () => {
285+
it('keeps user format=array for raw parsers without emitting a runtime warn', () => {
288286
const node = new PrefixTree(DEFAULT_OPTIONS).insert('a', 'a.vue')
289287
node.value.setEditOverride('params', {
290288
query: { tags: { parser: 'set', format: 'array' } },
@@ -297,6 +295,7 @@ describe('generateRouteRecordQuery', () => {
297295
expect(result).toContain(
298296
`new MatcherPatternQueryParam('tags', 'tags', 'array', _normalized_PARAM_PARSER__set)`
299297
)
298+
expect(result).not.toContain('console.warn(')
300299
})
301300

302301
it('does not force array for non-raw parsers', () => {

packages/router/src/unplugin/codegen/generateRouteResolver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@ ${queryParams
397397
const isRawParser = !!(
398398
param.parser && paramParsersMap.get(param.parser)?.isRaw
399399
)
400-
if (isRawParser && param.format === 'value') {
401-
console.warn(
402-
`Query param "${param.paramName}" in route "${node.fullPath}" uses raw param parser "${param.parser}" but specifies \`format: 'value'\`. The format is ignored because raw parsers always receive the array form.`
403-
)
404-
}
400+
const invalidFormatWarn =
401+
isRawParser &&
402+
param.format === 'value' &&
403+
`console.warn(${toStringLiteral(`Query param "${param.paramName}" in route "${node.fullPath}" uses raw param parser "${param.parser}" but specifies \`format: 'value'\`. The format is ignored because raw parsers always receive the array form. Set it to 'array' to silence the warning.`)}) ||`
404+
405405
const format = isRawParser ? 'array' : param.format || 'value'
406406
407407
const args = [
@@ -424,7 +424,7 @@ ${queryParams
424424
args.push(String(param.required))
425425
}
426426
427-
return ` new MatcherPatternQueryParam(${args.join(', ')})`
427+
return ` ${invalidFormatWarn || ''}new MatcherPatternQueryParam(${args.join(', ')})`
428428
})
429429
.join(',\n')}
430430
],`

0 commit comments

Comments
 (0)