Skip to content

Commit f24c904

Browse files
committed
refactor(core): throw an error if filePath is not absolute
1 parent 0a2f7dc commit f24c904

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

packages/@vuepress/core/__tests__/page/resolvePageFilePath.spec.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ describe('core > page > resolvePageFilePath', () => {
3434
})
3535
})
3636

37-
it('should resolve path correctly if filePath is relative', () => {
38-
const resolved = resolvePageFilePath({
39-
app,
40-
options: {
41-
filePath: relativeFilePath,
42-
},
43-
})
44-
expect(resolved).toEqual({
45-
filePath: absoluteFilePath,
46-
filePathRelative: relativeFilePath,
47-
})
37+
it('should throw if filePath is relative', () => {
38+
const consoleError = console.error
39+
console.error = jest.fn()
40+
41+
expect(() =>
42+
resolvePageFilePath({
43+
app,
44+
options: {
45+
filePath: relativeFilePath,
46+
},
47+
})
48+
).toThrow()
49+
50+
expect(console.error).toHaveBeenCalled()
51+
52+
console.error = consoleError
4853
})
4954
})
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { path } from '@vuepress/utils'
1+
import { logger, path } from '@vuepress/utils'
22
import type { App, PageOptions } from '../types'
33

44
/**
55
* Resolve absolute and relative path of page file
66
*/
77
export const resolvePageFilePath = ({
88
app,
9-
options: { filePath },
9+
options,
1010
}: {
1111
app: App
1212
options: PageOptions
@@ -15,15 +15,21 @@ export const resolvePageFilePath = ({
1515
filePathRelative: string | null
1616
} => {
1717
// empty file path
18-
if (!filePath) {
18+
if (!options.filePath) {
1919
return {
2020
filePath: null,
2121
filePathRelative: null,
2222
}
2323
}
2424

25+
if (!path.isAbsolute(options.filePath)) {
26+
throw logger.createError(
27+
`filePath is not absolute file path: ${options.filePath}}`
28+
)
29+
}
30+
2531
return {
26-
filePath,
27-
filePathRelative: path.relative(app.dir.source(), filePath),
32+
filePath: options.filePath,
33+
filePathRelative: path.relative(app.dir.source(), options.filePath),
2834
}
2935
}

packages/@vuepress/core/src/page/resolvePagePath.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const resolvePagePath = ({
1818

1919
if (!pagePath) {
2020
throw logger.createError(
21-
`page path is empty, page options: ${JSON.stringify(options)}`
21+
`page path is empty, page options: ${JSON.stringify(options, null, 2)}`
2222
)
2323
}
2424

0 commit comments

Comments
 (0)