File tree Expand file tree Collapse file tree 2 files changed +76
-3
lines changed
packages/router/src/unplugin/core Expand file tree Collapse file tree 2 files changed +76
-3
lines changed Original file line number Diff line number Diff line change @@ -243,6 +243,74 @@ definePage({
243243 } )
244244 } )
245245
246+ it ( 'extracts alias as a string' , ( ) => {
247+ const code = vue `
248+ <script setup>
249+ definePage({
250+ alias: '/other',
251+ })
252+ </script>
253+ `
254+ expect ( extractDefinePageInfo ( code , 'src/pages/test.vue' ) ) . toEqual ( {
255+ alias : [ '/other' ] ,
256+ } )
257+ } )
258+
259+ it ( 'extracts alias as an array of strings' , ( ) => {
260+ const code = vue `
261+ <script setup>
262+ definePage({
263+ alias: ['/a', '/b'],
264+ })
265+ </script>
266+ `
267+ expect ( extractDefinePageInfo ( code , 'src/pages/test.vue' ) ) . toEqual ( {
268+ alias : [ '/a' , '/b' ] ,
269+ } )
270+ } )
271+
272+ it ( 'warns on invalid alias (number)' , ( ) => {
273+ const code = vue `
274+ <script setup>
275+ definePage({
276+ alias: 123,
277+ })
278+ </script>
279+ `
280+ expect ( extractDefinePageInfo ( code , 'src/pages/test.vue' ) ) . toEqual ( { } )
281+ expect (
282+ 'route alias must be a string literal or an array of string literals'
283+ ) . toHaveBeenWarned ( )
284+ } )
285+
286+ it ( 'warns on invalid alias (variable)' , ( ) => {
287+ const code = vue `
288+ <script setup>
289+ const someVar = '/other'
290+ definePage({
291+ alias: someVar,
292+ })
293+ </script>
294+ `
295+ expect ( extractDefinePageInfo ( code , 'src/pages/test.vue' ) ) . toEqual ( { } )
296+ expect (
297+ 'route alias must be a string literal or an array of string literals'
298+ ) . toHaveBeenWarned ( )
299+ } )
300+
301+ it ( 'filters non-string elements from alias array' , ( ) => {
302+ const code = vue `
303+ <script setup>
304+ definePage({
305+ alias: ['/a', 123, '/b'],
306+ })
307+ </script>
308+ `
309+ expect ( extractDefinePageInfo ( code , 'src/pages/test.vue' ) ) . toEqual ( {
310+ alias : [ '/a' , '/b' ] ,
311+ } )
312+ } )
313+
246314 it ( 'extract name skipped when non existent' , async ( ) => {
247315 expect (
248316 extractDefinePageInfo (
Original file line number Diff line number Diff line change @@ -213,6 +213,7 @@ type DefinePageParamsInfo = NonNullable<CustomRouteBlock['params']>
213213export interface DefinePageInfo {
214214 name ?: string | false
215215 path ?: string
216+ alias ?: string [ ]
216217 params ?: CustomRouteBlock [ 'params' ]
217218}
218219
@@ -286,6 +287,8 @@ export function extractDefinePageInfo(
286287 } else {
287288 routeInfo . path = prop . value . value
288289 }
290+ } else if ( prop . key . name === 'alias' ) {
291+ routeInfo . alias = extractRouteAlias ( prop . value , id )
289292 } else if ( prop . key . name === 'params' ) {
290293 if ( prop . value . type === 'ObjectExpression' ) {
291294 routeInfo . params = extractParamsInfo ( prop . value , id )
@@ -418,23 +421,25 @@ function extractPathParams(
418421 return pathParams
419422}
420423
421- // TODO: use
422424export function extractRouteAlias (
423425 aliasValue : ObjectProperty [ 'value' ] ,
424426 id : string
425- ) : string [ ] | void {
427+ ) : string [ ] | undefined {
426428 if (
427429 aliasValue . type !== 'StringLiteral' &&
428430 aliasValue . type !== 'ArrayExpression'
429431 ) {
430- warn ( `route alias must be a string literal. Found in "${ id } ".` )
432+ warn (
433+ `route alias must be a string literal or an array of string literals. Found in "${ id } ".`
434+ )
431435 } else {
432436 return aliasValue . type === 'StringLiteral'
433437 ? [ aliasValue . value ]
434438 : aliasValue . elements
435439 . filter ( node => node ?. type === 'StringLiteral' )
436440 . map ( el => el . value )
437441 }
442+ return undefined
438443}
439444
440445const getIdentifiers = ( stmts : Statement [ ] ) => {
You can’t perform that action at this time.
0 commit comments