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

fixed typescript checker output errors live update #217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
50 changes: 38 additions & 12 deletions packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
diagnosticToRuntimeError,
diagnosticToTerminalLog,
ensureCall,
NormalizedDiagnostic,
normalizeTsDiagnostic,
toClientPayload,
wrapCheckerSummary,
Expand All @@ -19,12 +20,23 @@ import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types

const __filename = fileURLToPath(import.meta.url)
let createServeAndBuild

// check diagnostic is all same
const checkSameDiagnostic = function (a: NormalizedDiagnostic, b: NormalizedDiagnostic) {
return (
a.id === b.id &&
a.loc?.start.line === b.loc?.start.line &&
a.loc?.start.column === b.loc?.start.column &&
a.loc?.end?.line === b.loc?.end?.line &&
a.loc?.end?.column === b.loc?.end?.column &&
a.message === b.message &&
a.level === b.level
)
}
const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
let overlay = true
let terminal = true
let currDiagnostics: DiagnosticToRuntime[] = []

let normalizedDiagnosticsStore: NormalizedDiagnostic[] = []
return {
config: async ({ enableOverlay, enableTerminal }) => {
overlay = enableOverlay
Expand Down Expand Up @@ -58,9 +70,12 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
if (normalizedDiagnostic === null) {
return
}

currDiagnostics.push(diagnosticToRuntimeError(normalizedDiagnostic))
logChunk += os.EOL + diagnosticToTerminalLog(normalizedDiagnostic, 'TypeScript')
// delete repeat diagnostic
normalizedDiagnosticsStore = normalizedDiagnosticsStore.filter(
(n) => !checkSameDiagnostic(n, normalizedDiagnostic)
)
// push new diagnostic to store
normalizedDiagnosticsStore.push(normalizedDiagnostic)
}

const reportWatchStatusChanged: ts.WatchStatusReporter = (
Expand All @@ -71,6 +86,15 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
// eslint-disable-next-line max-params
) => {
if (diagnostic.code === 6031) return

// reset current time output
logChunk = ''
currDiagnostics = []
// use cached normalizedDiagnostics to generate outputs
normalizedDiagnosticsStore.forEach((n) => {
currDiagnostics.push(diagnosticToRuntimeError(n))
logChunk += os.EOL + diagnosticToTerminalLog(n, 'TypeScript')
})
// https://github.com/microsoft/TypeScript/issues/32542
// https://github.com/microsoft/TypeScript/blob/dc237b317ed4bbccd043ddda802ffde00362a387/src/compiler/diagnosticMessages.json#L4086-L4088
switch (diagnostic.code) {
Expand All @@ -80,22 +104,24 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
logChunk = ''
// currErr = null
currDiagnostics = []
normalizedDiagnosticsStore = []
return
case 6193: // 1 Error
case 6194: // 0 errors or 2+ errors
// reset normalizedDiagnostics after reportDiagnostic
if (overlay) {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toClientPayload('typescript', currDiagnostics),
ensureCall(() => {
parentPort?.postMessage({
type: ACTION_TYPES.overlayError,
payload: toClientPayload('typescript', currDiagnostics),
})
})
}
}
// reset for next time
normalizedDiagnosticsStore = []

ensureCall(() => {
if (errorCount === 0) {
logChunk = ''
}

if (terminal) {
consoleLog(
logChunk +
Expand Down