Skip to content

Commit

Permalink
fix: vls can report multiple errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Feb 6, 2022
1 parent 7862d13 commit db5f73c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 108 deletions.
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/@runtime/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const WS_CHECKER_RECONNECT_EVENT = 'vite-plugin-checker:reconnect'

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

socket.addEventListener('message', async ({ data: dataStr }) => {
Expand Down
38 changes: 0 additions & 38 deletions packages/vite-plugin-checker/src/DiagnosticCache.ts

This file was deleted.

31 changes: 12 additions & 19 deletions packages/vite-plugin-checker/src/FileDiagnosticManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ import type { NormalizedDiagnostic } from './logger'

class FileDiagnosticManager {
public diagnostics: NormalizedDiagnostic[] = []
private initialized = false

/**
* Only used when initializing the manager
*/
public initWith(diagnostics: NormalizedDiagnostic[]) {
if (this.initialized) {
throw new Error('FileDiagnosticManager is already initialized')
}

diagnostics.forEach((d) => {
this.diagnostics.push(d)
})

this.initialized = true
}

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

// public get lastDiagnostic() {
// return this.diagnostics[this.diagnostics.length - 1]
// }

public setFile(fileName: string, next: NormalizedDiagnostic[] | null) {
public updateByFileId(fileId: string, next: NormalizedDiagnostic[] | null) {
for (let i = 0; i < this.diagnostics.length; i++) {
if (this.diagnostics[i].id === fileName) {
if (this.diagnostics[i].id === fileId) {
this.diagnostics.splice(i, 1)
i--
}
Expand All @@ -33,19 +39,6 @@ class FileDiagnosticManager {
this.diagnostics.push(...next)
}
}

// public updateFile(next: NormalizedDiagnostic[] | null) {
// for (let i = 0; i < this.diagnostics.length; i++) {
// if (this.diagnostics[i].loc?.start.line === fileName) {
// this.diagnostics.splice(i, 1)
// i--
// }
// }

// if (next?.length) {
// this.diagnostics.push(...next)
// }
// }
}

export { FileDiagnosticManager }
38 changes: 2 additions & 36 deletions packages/vite-plugin-checker/src/checkers/eslint/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
consoleLog,
diagnosticToTerminalLog,
diagnosticToViteError,
NormalizedDiagnostic,
toViteCustomPayload,
normalizeEslintDiagnostic,
} from '../../logger'
Expand All @@ -35,73 +34,40 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
const options = optionator.parse(pluginConfig.eslint.lintCommand)
const translatedOptions = translateOptions(options) as ESLint.Options

// const extensions = config.extensions ?? ['.js']
// const overrideConfigFile = pluginConfig.eslint.configFile
// ? { overrideConfigFile: pluginConfig.eslint.configFile }
// : {}
const eslint = new ESLint({
cwd: root,
...translatedOptions,
...pluginConfig.eslint.devOptions,
// extensions,
// ...overrideConfigFile,
})

// invariant(pluginConfig.eslint, 'config.eslint should not be `false`')
// invariant(
// pluginConfig.eslint.files,
// `eslint.files is required, but got ${pluginConfig.eslint.files}`
// )

// const paths =
// typeof pluginConfig.eslint.files === 'string'
// ? [pluginConfig.eslint.files]
// : pluginConfig.eslint.files
// const paths = config.

// let diagnosticsCache: NormalizedDiagnostic[] = []

const dispatchDiagnostics = () => {
const diagnostics = manager.getDiagnostics()
diagnostics.forEach((d) => {
consoleLog(diagnosticToTerminalLog(d, 'ESLint'))
})

// const lastErr = diagnosticsCache[0]

if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload(
'eslint',
diagnostics.map((d) => diagnosticToViteError(d))
),

// payload: lastErr
// ? {
// type: 'error',
// err: diagnosticToViteError(lastErr),
// }
// : null,
})
}
}

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

if (type === 'unlink') {
manager.setFile(absPath, [])
// diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath)
manager.updateByFileId(absPath, [])
} else if (type === 'change') {
const diagnosticsOfChangedFile = await eslint.lintFiles(filePath)
const newDiagnostics = diagnosticsOfChangedFile
.map((d) => normalizeEslintDiagnostic(d))
.flat(1)
// const absPath = diagnosticsOfChangedFile[0].filePath
// diagnosticsCache = diagnosticsCache.filter((d) => d.id !== absPath).concat(newDiagnostics)
manager.setFile(absPath, newDiagnostics)
manager.updateByFileId(absPath, newDiagnostics)
}

dispatchDiagnostics()
Expand Down
14 changes: 7 additions & 7 deletions packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { DeepPartial } from '../../types'
import { getInitParams, VlsOptions } from './initParams'

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

let disposeSuppressConsole: ReturnType<typeof suppressConsole>
const diagnosticCache = new FileDiagnosticCache()
const fileDiagnosticManager = new FileDiagnosticManager()

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

export async function diagnostics(
Expand Down Expand Up @@ -165,14 +165,14 @@ export async function prepareClientConnection(
const absFilePath = URI.parse(publishDiagnostics.uri).fsPath
publishDiagnostics.diagnostics = filterDiagnostics(publishDiagnostics.diagnostics, severity)
const nextDiagnosticInFile = await normalizePublishDiagnosticParams(publishDiagnostics)
diagnosticCache.setFile(absFilePath, nextDiagnosticInFile)
fileDiagnosticManager.updateByFileId(absFilePath, nextDiagnosticInFile)

const res = diagnosticCache.getDiagnostics()
const res = fileDiagnosticManager.getDiagnostics()
vlsConsoleLog(os.EOL)
vlsConsoleLog(res.map((d) => diagnosticToTerminalLog(d, 'VLS')).join(os.EOL))

if (diagnosticCache.lastDiagnostic) {
const normalized = diagnosticToViteError(diagnosticCache.lastDiagnostic)
if (res) {
const normalized = diagnosticToViteError(res)
options.errorCallback?.(publishDiagnostics, normalized)
}
}
Expand Down
8 changes: 1 addition & 7 deletions packages/vite-plugin-checker/src/checkers/vls/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ export const createDiagnostic: CreateDiagnostic<'vls'> = (pluginConfig) => {
if (!overlay) return
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toViteCustomPayload('vls', overlayErr ? [overlayErr] : []),
// payload: overlayErr
// ? {
// type: 'error',
// err: overlayErr,
// }
// : null,
payload: toViteCustomPayload('vls', overlayErr ? overlayErr : []),
})
}

Expand Down

0 comments on commit db5f73c

Please sign in to comment.