Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(helper/ssg): fix bug of joinPaths #2809

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/helper/ssg/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe('joinPath', () => {
expect(joinPaths('test', '...', 'test2')).toBe('test/.../test2') // single triple dot and single
expect(joinPaths('test', './test2', '.test3.')).toBe('test/test2/.test3.') // single and single with slash and single with dot
expect(joinPaths('test', '../', '.test2')).toBe('.test2') // single and parent and single with dot
expect(joinPaths('..', '..', 'test')).toBe('../../test') // parent and parent and single
expect(joinPaths('..', '..')).toBe('../..') // parent and parent
expect(joinPaths('.test../test2/../')).toBe('.test..') //shuffle
expect(joinPaths('.test./.test2/../')).toBe('.test.') //shuffle2
})
Expand Down
13 changes: 8 additions & 5 deletions src/helper/ssg/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const normalizePath = (path: string): string => {
return path.replace(/(\\)/g, '/').replace(/\/$/g, '')
}

const handleDotDot = (resultPaths: string[]): void => {
if (resultPaths.length === 0) {
const handleParent = (resultPaths: string[], beforeParentFlag: boolean): void => {
if (resultPaths.length === 0 || beforeParentFlag) {
resultPaths.push('..')
} else {
resultPaths.pop()
Expand All @@ -33,13 +33,16 @@ const handleNonDot = (path: string, resultPaths: string[]): void => {
}

const handleSegments = (paths: string[], resultPaths: string[]): void => {
let beforeParentFlag = false
for (const path of paths) {
// Handle `..` or `../`
// Handle `..`
if (path === '..') {
handleDotDot(resultPaths)
handleParent(resultPaths, beforeParentFlag)
beforeParentFlag = true
} else {
// Handle `.` or `./`
// Handle `.` or `abc`
handleNonDot(path, resultPaths)
beforeParentFlag = false
}
}
}
Expand Down