2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -329,6 +329,9 @@ describe('resolveRelativePath', () => {
329
329
expect ( resolveRelativePath ( './../add' , '/' ) ) . toBe ( '/add' )
330
330
expect ( resolveRelativePath ( '../../add' , '/' ) ) . toBe ( '/add' )
331
331
expect ( resolveRelativePath ( '../../../add' , '/' ) ) . toBe ( '/add' )
332
+ expect ( resolveRelativePath ( 'a/add' , '/' ) ) . toBe ( '/a/add' )
333
+ expect ( resolveRelativePath ( './a/add' , '/' ) ) . toBe ( '/a/add' )
334
+ expect ( resolveRelativePath ( '../a/add' , '/' ) ) . toBe ( '/a/add' )
332
335
} )
333
336
334
337
it ( 'ignores it location is absolute' , ( ) => {
Original file line number Diff line number Diff line change @@ -215,17 +215,24 @@ export function resolveRelativePath(to: string, from: string): string {
215
215
216
216
for ( toPosition = 0 ; toPosition < toSegments . length ; toPosition ++ ) {
217
217
segment = toSegments [ toPosition ]
218
- // can't go below zero
219
- if ( position === 1 || segment === '.' ) continue
220
- if ( segment === '..' ) position --
221
- // found something that is not relative path
218
+
219
+ // we stay on the same position
220
+ if ( segment === '.' ) continue
221
+ // go up in the from array
222
+ if ( segment === '..' ) {
223
+ // we can't go below zero but we still need to increment toPosition
224
+ if ( position > 1 ) position --
225
+ // continue
226
+ }
227
+ // we reached a non relative path, we stop here
222
228
else break
223
229
}
224
230
225
231
return (
226
232
fromSegments . slice ( 0 , position ) . join ( '/' ) +
227
233
'/' +
228
234
toSegments
235
+ // ensure we use at least the last element in the toSegments
229
236
. slice ( toPosition - ( toPosition === toSegments . length ? 1 : 0 ) )
230
237
. join ( '/' )
231
238
)
0 commit comments