Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 23e25e5

Browse files
committedJun 30, 2022
fix: resolve relative paths to /
Fix #1410
1 parent 4e631a3 commit 23e25e5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎__tests__/location.spec.ts

+3
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ describe('resolveRelativePath', () => {
329329
expect(resolveRelativePath('./../add', '/')).toBe('/add')
330330
expect(resolveRelativePath('../../add', '/')).toBe('/add')
331331
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')
332335
})
333336

334337
it('ignores it location is absolute', () => {

‎src/location.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,24 @@ export function resolveRelativePath(to: string, from: string): string {
215215

216216
for (toPosition = 0; toPosition < toSegments.length; toPosition++) {
217217
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
222228
else break
223229
}
224230

225231
return (
226232
fromSegments.slice(0, position).join('/') +
227233
'/' +
228234
toSegments
235+
// ensure we use at least the last element in the toSegments
229236
.slice(toPosition - (toPosition === toSegments.length ? 1 : 0))
230237
.join('/')
231238
)

0 commit comments

Comments
 (0)
Failed to load comments.