Skip to content

Commit db5f73c

Browse files
committed
fix: vls can report multiple errors
1 parent 7862d13 commit db5f73c

File tree

6 files changed

+23
-108
lines changed

6 files changed

+23
-108
lines changed

packages/vite-plugin-checker/src/@runtime/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const WS_CHECKER_RECONNECT_EVENT = 'vite-plugin-checker:reconnect'
66

77
export function inject() {
88
const socketProtocol = null || (location.protocol === 'https:' ? 'wss' : 'ws')
9-
const socketHost = `${null || location.hostname}:${'3000'}`
9+
const socketHost = `${location.hostname}:${location.port}`
1010
const socket = new WebSocket(`${socketProtocol}://${socketHost}`, 'vite-hmr')
1111

1212
socket.addEventListener('message', async ({ data: dataStr }) => {

packages/vite-plugin-checker/src/DiagnosticCache.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/vite-plugin-checker/src/FileDiagnosticManager.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@ import type { NormalizedDiagnostic } from './logger'
22

33
class FileDiagnosticManager {
44
public diagnostics: NormalizedDiagnostic[] = []
5+
private initialized = false
56

7+
/**
8+
* Only used when initializing the manager
9+
*/
610
public initWith(diagnostics: NormalizedDiagnostic[]) {
11+
if (this.initialized) {
12+
throw new Error('FileDiagnosticManager is already initialized')
13+
}
14+
715
diagnostics.forEach((d) => {
816
this.diagnostics.push(d)
917
})
18+
19+
this.initialized = true
1020
}
1121

1222
public getDiagnostics(fileName?: string) {
@@ -17,13 +27,9 @@ class FileDiagnosticManager {
1727
return this.diagnostics
1828
}
1929

20-
// public get lastDiagnostic() {
21-
// return this.diagnostics[this.diagnostics.length - 1]
22-
// }
23-
24-
public setFile(fileName: string, next: NormalizedDiagnostic[] | null) {
30+
public updateByFileId(fileId: string, next: NormalizedDiagnostic[] | null) {
2531
for (let i = 0; i < this.diagnostics.length; i++) {
26-
if (this.diagnostics[i].id === fileName) {
32+
if (this.diagnostics[i].id === fileId) {
2733
this.diagnostics.splice(i, 1)
2834
i--
2935
}
@@ -33,19 +39,6 @@ class FileDiagnosticManager {
3339
this.diagnostics.push(...next)
3440
}
3541
}
36-
37-
// public updateFile(next: NormalizedDiagnostic[] | null) {
38-
// for (let i = 0; i < this.diagnostics.length; i++) {
39-
// if (this.diagnostics[i].loc?.start.line === fileName) {
40-
// this.diagnostics.splice(i, 1)
41-
// i--
42-
// }
43-
// }
44-
45-
// if (next?.length) {
46-
// this.diagnostics.push(...next)
47-
// }
48-
// }
4942
}
5043

5144
export { FileDiagnosticManager }

packages/vite-plugin-checker/src/checkers/eslint/main.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
consoleLog,
1212
diagnosticToTerminalLog,
1313
diagnosticToViteError,
14-
NormalizedDiagnostic,
1514
toViteCustomPayload,
1615
normalizeEslintDiagnostic,
1716
} from '../../logger'
@@ -35,73 +34,40 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
3534
const options = optionator.parse(pluginConfig.eslint.lintCommand)
3635
const translatedOptions = translateOptions(options) as ESLint.Options
3736

38-
// const extensions = config.extensions ?? ['.js']
39-
// const overrideConfigFile = pluginConfig.eslint.configFile
40-
// ? { overrideConfigFile: pluginConfig.eslint.configFile }
41-
// : {}
4237
const eslint = new ESLint({
4338
cwd: root,
4439
...translatedOptions,
4540
...pluginConfig.eslint.devOptions,
46-
// extensions,
47-
// ...overrideConfigFile,
4841
})
4942

50-
// invariant(pluginConfig.eslint, 'config.eslint should not be `false`')
51-
// invariant(
52-
// pluginConfig.eslint.files,
53-
// `eslint.files is required, but got ${pluginConfig.eslint.files}`
54-
// )
55-
56-
// const paths =
57-
// typeof pluginConfig.eslint.files === 'string'
58-
// ? [pluginConfig.eslint.files]
59-
// : pluginConfig.eslint.files
60-
// const paths = config.
61-
62-
// let diagnosticsCache: NormalizedDiagnostic[] = []
63-
6443
const dispatchDiagnostics = () => {
6544
const diagnostics = manager.getDiagnostics()
6645
diagnostics.forEach((d) => {
6746
consoleLog(diagnosticToTerminalLog(d, 'ESLint'))
6847
})
6948

70-
// const lastErr = diagnosticsCache[0]
71-
7249
if (overlay) {
7350
parentPort?.postMessage({
7451
type: ACTION_TYPES.overlayError,
7552
payload: toViteCustomPayload(
7653
'eslint',
7754
diagnostics.map((d) => diagnosticToViteError(d))
7855
),
79-
80-
// payload: lastErr
81-
// ? {
82-
// type: 'error',
83-
// err: diagnosticToViteError(lastErr),
84-
// }
85-
// : null,
8656
})
8757
}
8858
}
8959

9060
const handleFileChange = async (filePath: string, type: 'change' | 'unlink') => {
91-
// if (!extensions.includes(path.extname(filePath))) return
9261
const absPath = path.resolve(root, filePath)
9362

9463
if (type === 'unlink') {
95-
manager.setFile(absPath, [])
96-
// diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath)
64+
manager.updateByFileId(absPath, [])
9765
} else if (type === 'change') {
9866
const diagnosticsOfChangedFile = await eslint.lintFiles(filePath)
9967
const newDiagnostics = diagnosticsOfChangedFile
10068
.map((d) => normalizeEslintDiagnostic(d))
10169
.flat(1)
102-
// const absPath = diagnosticsOfChangedFile[0].filePath
103-
// diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath).concat(newDiagnostics)
104-
manager.setFile(absPath, newDiagnostics)
70+
manager.updateByFileId(absPath, newDiagnostics)
10571
}
10672

10773
dispatchDiagnostics()

packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { DeepPartial } from '../../types'
3737
import { getInitParams, VlsOptions } from './initParams'
3838

3939
import type { ErrorPayload } from 'vite'
40-
import { FileDiagnosticCache } from '../../DiagnosticCache'
40+
import { FileDiagnosticManager } from '../../FileDiagnosticManager'
4141
enum DOC_VERSION {
4242
init = -1,
4343
}
@@ -46,7 +46,7 @@ export type LogLevel = typeof logLevels[number]
4646
export const logLevels = ['ERROR', 'WARN', 'INFO', 'HINT'] as const
4747

4848
let disposeSuppressConsole: ReturnType<typeof suppressConsole>
49-
const diagnosticCache = new FileDiagnosticCache()
49+
const fileDiagnosticManager = new FileDiagnosticManager()
5050

5151
export const logLevel2Severity = {
5252
ERROR: DiagnosticSeverity.Error,
@@ -59,7 +59,7 @@ export interface DiagnosticOptions {
5959
watch: boolean
6060
verbose: boolean
6161
config: DeepPartial<VlsOptions> | null
62-
errorCallback?: (diagnostic: PublishDiagnosticsParams, viteError: ErrorPayload['err']) => void
62+
errorCallback?: (diagnostic: PublishDiagnosticsParams, viteError: ErrorPayload['err'][]) => void
6363
}
6464

6565
export async function diagnostics(
@@ -165,14 +165,14 @@ export async function prepareClientConnection(
165165
const absFilePath = URI.parse(publishDiagnostics.uri).fsPath
166166
publishDiagnostics.diagnostics = filterDiagnostics(publishDiagnostics.diagnostics, severity)
167167
const nextDiagnosticInFile = await normalizePublishDiagnosticParams(publishDiagnostics)
168-
diagnosticCache.setFile(absFilePath, nextDiagnosticInFile)
168+
fileDiagnosticManager.updateByFileId(absFilePath, nextDiagnosticInFile)
169169

170-
const res = diagnosticCache.getDiagnostics()
170+
const res = fileDiagnosticManager.getDiagnostics()
171171
vlsConsoleLog(os.EOL)
172172
vlsConsoleLog(res.map((d) => diagnosticToTerminalLog(d, 'VLS')).join(os.EOL))
173173

174-
if (diagnosticCache.lastDiagnostic) {
175-
const normalized = diagnosticToViteError(diagnosticCache.lastDiagnostic)
174+
if (res) {
175+
const normalized = diagnosticToViteError(res)
176176
options.errorCallback?.(publishDiagnostics, normalized)
177177
}
178178
}

packages/vite-plugin-checker/src/checkers/vls/main.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,7 @@ export const createDiagnostic: CreateDiagnostic<'vls'> = (pluginConfig) => {
1919
if (!overlay) return
2020
parentPort?.postMessage({
2121
type: ACTION_TYPES.overlayError,
22-
payload: toViteCustomPayload('vls', overlayErr ? [overlayErr] : []),
23-
// payload: overlayErr
24-
// ? {
25-
// type: 'error',
26-
// err: overlayErr,
27-
// }
28-
// : null,
22+
payload: toViteCustomPayload('vls', overlayErr ? overlayErr : []),
2923
})
3024
}
3125

0 commit comments

Comments
 (0)