@@ -50,30 +50,42 @@ describe('pages module', () => {
50
50
const englishPages = chain ( pages )
51
51
. filter ( [ 'languageCode' , 'en' ] )
52
52
. filter ( 'redirect_from' )
53
- . map ( ( pages ) => pick ( pages , [ 'redirect_from' , 'applicableVersions' ] ) )
53
+ . map ( ( pages ) => pick ( pages , [ 'redirect_from' , 'applicableVersions' , 'fullPath' ] ) )
54
54
. value ( )
55
55
56
+ // Map from redirect path to Set of file paths
57
+ const redirectToFiles = new Map ( )
56
58
const versionedRedirects = [ ]
57
59
58
60
englishPages . forEach ( ( page ) => {
59
61
page . redirect_from . forEach ( ( redirect ) => {
60
62
page . applicableVersions . forEach ( ( version ) => {
61
- versionedRedirects . push ( removeFPTFromPath ( path . posix . join ( '/' , version , redirect ) ) )
63
+ const versioned = removeFPTFromPath ( path . posix . join ( '/' , version , redirect ) )
64
+ versionedRedirects . push ( { path : versioned , file : page . fullPath } )
65
+ if ( ! redirectToFiles . has ( versioned ) ) {
66
+ redirectToFiles . set ( versioned , new Set ( ) )
67
+ }
68
+ redirectToFiles . get ( versioned ) . add ( page . fullPath )
62
69
} )
63
70
} )
64
71
} )
65
72
66
- const duplicates = versionedRedirects . reduce ( ( acc , el , i , arr ) => {
67
- if ( arr . indexOf ( el ) !== i && acc . indexOf ( el ) < 0 ) acc . push ( el )
68
- return acc
69
- } , [ ] )
70
-
71
- const message = `Found ${ duplicates . length } duplicate redirect_from ${
72
- duplicates . length === 1 ? 'path' : 'paths'
73
- } .
74
- Ensure that you don't define the same path more than once in the redirect_from property in a single file and across all English files.
75
- You may also receive this error if you have defined the same children property more than once.\n
76
- ${ duplicates . join ( '\n' ) } `
73
+ // Only consider as duplicate if more than one unique file defines the same redirect
74
+ const duplicates = Array . from ( redirectToFiles . entries ( ) )
75
+ . filter ( ( [ _ , files ] ) => files . size > 1 )
76
+ . map ( ( [ path ] ) => path )
77
+
78
+ // Build a detailed message with sources for each duplicate
79
+ const message =
80
+ `Found ${ duplicates . length } duplicate redirect_from path${ duplicates . length === 1 ? '' : 's' } .
81
+ Ensure that you don't define the same path more than once in the redirect_from property in a single file and across all English files.
82
+ You may also receive this error if you have defined the same children property more than once.\n` +
83
+ duplicates
84
+ . map ( ( dup ) => {
85
+ const files = Array . from ( redirectToFiles . get ( dup ) || [ ] )
86
+ return `${ dup } \n Defined in:\n ${ files . join ( '\n ' ) } `
87
+ } )
88
+ . join ( '\n\n' )
77
89
expect ( duplicates . length , message ) . toBe ( 0 )
78
90
} )
79
91
0 commit comments