Skip to content
Closed
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
36 changes: 36 additions & 0 deletions packages/vite-plugin-checker/__tests__/sources.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { expect, it } from 'vitest'
import { normalizePath, readSources } from '../src/sources'

it.each([
'with space.ts',
'percent%.ts',
'hash#.ts',
'中文.ts',
])('reads a diagnostic file URL for %s', async (name) => {
const root = await mkdtemp(path.join(tmpdir(), 'checker-url-'))
try {
const file = path.join(root, name)
await writeFile(file, 'debugger;\n')
const normalized = normalizePath(pathToFileURL(file).href, root)
expect(normalized).toBe(file)
expect((await readSources([normalized])).get(normalized)).toBe(
await readFile(file, 'utf8'),
)
} finally {
await rm(root, { recursive: true, force: true })
}
})

it('leaves ordinary relative and absolute paths unchanged', () => {
const root = path.resolve('/project')
expect(normalizePath('src/a%20.ts', root)).toBe(
path.join(root, 'src/a%20.ts'),
)
expect(normalizePath(path.join(root, 'src/a.ts'), root)).toBe(
path.join(root, 'src/a.ts'),
)
})
7 changes: 4 additions & 3 deletions packages/vite-plugin-checker/src/sources.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

export function normalizePath(p: string, cwd: string) {
let filename = p
// Strip file:// URL scheme (oxlint outputs file:// URLs in JSON format)
if (filename && filename.startsWith('file://')) {
filename = filename.slice(7)
// Decode file URLs, including escaped characters and platform-specific paths.
if (filename.startsWith('file://')) {
filename = fileURLToPath(filename)
}
if (filename) {
filename = path.isAbsolute(filename)
Expand Down
Loading