@@ -1404,4 +1404,97 @@ describe('generateRouteResolver', () => {
14041404 expect ( warnings ) . not . toContain ( '"/members"' )
14051405 } )
14061406 } )
1407+
1408+ describe ( 'param parser filtering' , ( ) => {
1409+ type ParamParserEntry = NonNullable < ReturnType < ParamParsersMap [ 'get' ] > >
1410+ const uuidEntry = {
1411+ name : 'uuid' ,
1412+ typeName : 'Param_uuid' ,
1413+ relativePath : 'parsers/uuid' ,
1414+ absolutePath : '/abs/parsers/uuid' ,
1415+ } satisfies ParamParserEntry
1416+ const slugEntry = {
1417+ name : 'slug' ,
1418+ typeName : 'Param_slug' ,
1419+ relativePath : 'parsers/slug' ,
1420+ absolutePath : '/abs/parsers/slug' ,
1421+ } satisfies ParamParserEntry
1422+
1423+ it ( 'omits imports and normalized declarations for unused parsers' , ( ) => {
1424+ const tree = new PrefixTree ( DEFAULT_OPTIONS )
1425+ tree . insert ( 'users/[id=uuid]' , 'users/[id=uuid].vue' )
1426+
1427+ const importsMap = new ImportsMap ( )
1428+ const paramParsersMap : ParamParsersMap = new Map ( [
1429+ [ 'uuid' , uuidEntry ] ,
1430+ [ 'slug' , slugEntry ] ,
1431+ ] )
1432+
1433+ const resolver = generateRouteResolver (
1434+ tree ,
1435+ DEFAULT_OPTIONS ,
1436+ importsMap ,
1437+ paramParsersMap
1438+ )
1439+
1440+ expect ( resolver ) . toContain ( '_normalized_PARAM_PARSER__uuid' )
1441+ expect ( resolver ) . not . toContain ( 'PARAM_PARSER__slug' )
1442+ expect ( resolver ) . not . toContain ( '_normalized_PARAM_PARSER__slug' )
1443+
1444+ const imports = importsMap . toString ( )
1445+ expect ( imports ) . toContain ( "from '/abs/parsers/uuid'" )
1446+ expect ( imports ) . not . toContain ( "from '/abs/parsers/slug'" )
1447+ } )
1448+
1449+ it ( 'omits all parser imports when no route references any parser' , ( ) => {
1450+ const tree = new PrefixTree ( DEFAULT_OPTIONS )
1451+ tree . insert ( 'users/[id]' , 'users/[id].vue' )
1452+
1453+ const importsMap = new ImportsMap ( )
1454+ const paramParsersMap : ParamParsersMap = new Map ( [
1455+ [ 'uuid' , uuidEntry ] ,
1456+ [ 'slug' , slugEntry ] ,
1457+ ] )
1458+
1459+ const resolver = generateRouteResolver (
1460+ tree ,
1461+ DEFAULT_OPTIONS ,
1462+ importsMap ,
1463+ paramParsersMap
1464+ )
1465+
1466+ expect ( resolver ) . not . toContain ( '_normalizeParamParser' )
1467+ expect ( resolver ) . not . toContain ( 'PARAM_PARSER__uuid' )
1468+ expect ( resolver ) . not . toContain ( 'PARAM_PARSER__slug' )
1469+
1470+ const imports = importsMap . toString ( )
1471+ expect ( imports ) . not . toContain ( '_normalizeParamParser' )
1472+ expect ( imports ) . not . toContain ( "from '/abs/parsers/uuid'" )
1473+ expect ( imports ) . not . toContain ( "from '/abs/parsers/slug'" )
1474+ } )
1475+
1476+ it ( 'detects parsers referenced from query params' , ( ) => {
1477+ const tree = new PrefixTree ( DEFAULT_OPTIONS )
1478+ const node = tree . insert ( 'search' , 'search.vue' )
1479+ node . setCustomRouteBlock ( 'search.vue' , {
1480+ params : { query : { id : 'uuid' } } ,
1481+ } )
1482+
1483+ const importsMap = new ImportsMap ( )
1484+ const paramParsersMap : ParamParsersMap = new Map ( [
1485+ [ 'uuid' , uuidEntry ] ,
1486+ [ 'slug' , slugEntry ] ,
1487+ ] )
1488+
1489+ const resolver = generateRouteResolver (
1490+ tree ,
1491+ DEFAULT_OPTIONS ,
1492+ importsMap ,
1493+ paramParsersMap
1494+ )
1495+
1496+ expect ( resolver ) . toContain ( '_normalized_PARAM_PARSER__uuid' )
1497+ expect ( resolver ) . not . toContain ( '_normalized_PARAM_PARSER__slug' )
1498+ } )
1499+ } )
14071500} )
0 commit comments