Skip to content

Commit

Permalink
feat: VLS use logger
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 17, 2021
1 parent 975162d commit 558dacb
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 204 deletions.
21 changes: 0 additions & 21 deletions packages/vite-plugin-checker/__tests__/unit/logger.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ts from 'typescript'
import { parentPort } from 'worker_threads'

import { Checker, CheckerAbility } from '../../Checker'
import { diagnosticToTerminalLog, diagnosticToViteError, normalizeDiagnostic } from '../../logger'
import { diagnosticToTerminalLog, diagnosticToViteError, normalizeTsDiagnostic } from '../../logger'
import { ensureCall } from '../../utils'

import type { CreateDiagnostic } from '../../types'
Expand Down Expand Up @@ -46,7 +46,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {

// https://github.com/microsoft/TypeScript/blob/a545ab1ac2cb24ff3b1aaf0bfbfb62c499742ac2/src/compiler/watch.ts#L12-L28
const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
const formattedDiagnostics = normalizeDiagnostic(diagnostic)
const formattedDiagnostics = normalizeTsDiagnostic(diagnostic)
if (!currErr) {
currErr = diagnosticToViteError(formattedDiagnostics)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/checkers/vls/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { Command, Option } from 'commander'
import { commander } from 'vite-plugin-checker-vls'

import { diagnostics, LogLevel, logLevels } from './commands/diagnostics'
import { diagnostics, LogLevel, logLevels } from './diagnostics'

function getVersion(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import chalk from 'chalk'
// import chokidar from 'chokidar'
import fs from 'fs'
// import glob from 'glob'
import os from 'os'
import path from 'path'
import { Duplex } from 'stream'
Expand All @@ -14,28 +12,22 @@ import {
vscodeUri,
} from 'vite-plugin-checker-vls'
import { PublishDiagnosticsParams } from 'vscode-languageclient/node'
import {
normalizePublishDiagnosticParams,
diagnosticToTerminalLog,
normalizeLspDiagnostic,
} from '../../logger'

// type URI = vscodeUri.URI
// const { URI } = vscodeUri
// import { URI } from 'vscode-uri'
import { codeFrameColumns } from '@babel/code-frame'

import { lspDiagnosticToViteError, range2Location, uriToAbsPath } from '../../../utils'
import { getInitParams } from '../initParams'
import { lspDiagnosticToViteError } from '../../utils'
import { getInitParams } from './initParams'

// import { VLS } from 'vls'
const { VLS } = vls
const {
createConnection,
createProtocolConnection,
// DiagnosticSeverity,
DidChangeTextDocumentNotification,
DidOpenTextDocumentNotification,
// InitializeParams,
InitializeRequest,
// InitializeResult,
// Logger,
// ServerCapabilities,
StreamMessageReader,
StreamMessageWriter,
} = vscodeLanguageserverNode
Expand Down Expand Up @@ -160,31 +152,34 @@ async function prepareClientConnection(workspaceUri: vscodeUri.URI, options: Dia
)

// hijack sendDiagnostics
serverConnection.sendDiagnostics = (diagnostics) => {
serverConnection.sendDiagnostics = async (diagnostics) => {
disposeSuppressConsole?.()
if (diagnostics.version === DOC_VERSION.init) {
return
}

if (!diagnostics.diagnostics.length) {
prettyDiagnosticsLog({
diagnostics: [],
absFilePath: '',
fileText: '',
showNoError: false,
})
return
// prettyDiagnosticsLog({
// diagnostics: [],
// absFilePath: '',
// fileText: '',
// showNoError: false,
// })
}

const overlayErr = lspDiagnosticToViteError(diagnostics)
if (!overlayErr) return

prettyDiagnosticsLog({
diagnostics: diagnostics.diagnostics,
absFilePath: uriToAbsPath(diagnostics.uri),
fileText: overlayErr.fileText,
showNoError: false,
})
const res = await normalizePublishDiagnosticParams(diagnostics)
console.log(os.EOL)
console.log(res.map((d) => diagnosticToTerminalLog(d)).join(os.EOL))
// prettyDiagnosticsLog({
// diagnostics: diagnostics.diagnostics,
// absFilePath: uriToAbsPath(diagnostics.uri),
// fileText: overlayErr.fileText,
// showNoError: false,
// })

options.errorCallback?.(diagnostics, overlayErr)
}
Expand Down Expand Up @@ -283,12 +278,32 @@ async function getDiagnostics(
.filter((r) => r.severity && r.severity <= severity)

if (diagnostics.length > 0) {
logChunk += prettyDiagnosticsLog({
absFilePath,
fileText,
diagnostics: diagnostics,
doLog: false,
})
// const res = await normalizePublishDiagnosticParams({
// diagnostics,
// uri: diagnostics[0].,
// })

logChunk +=
os.EOL +
diagnostics
.map((d) =>
diagnosticToTerminalLog(
normalizeLspDiagnostic({
diagnostic: d,
absFilePath,
fileText,
})
)
)
.join(os.EOL)

// logChunk += res.map((d) => diagnosticToTerminalLog(d)).join(os.EOL)
// logChunk += prettyDiagnosticsLog({
// absFilePath,
// fileText,
// diagnostics: diagnostics,
// doLog: false,
// })

diagnostics.forEach((d) => {
if (d.severity === vscodeLanguageserverNode.DiagnosticSeverity.Error) {
Expand Down Expand Up @@ -325,60 +340,60 @@ async function getDiagnostics(
return initialErrCount
}

function composeLspLog({
diagnostic,
absFilePath,
fileText,
}: {
diagnostic: Diagnostic
absFilePath: string
fileText: string
}) {
let logChunk = ''
const location = range2Location(diagnostic.range)
logChunk += `${os.EOL}${chalk.green.bold('FILE ')} ${absFilePath}:${location.start.line}:${
location.start.column
}${os.EOL}`

if (diagnostic.severity === vscodeLanguageserverNode.DiagnosticSeverity.Error) {
logChunk += `${chalk.red.bold('ERROR ')} ${diagnostic.message.trim()}`
} else {
logChunk += `${chalk.yellow.bold('WARN ')} ${diagnostic.message.trim()}`
}

logChunk += os.EOL + os.EOL
logChunk += codeFrameColumns(fileText, location)
return logChunk
}

export function prettyDiagnosticsLog({
diagnostics,
fileText,
absFilePath,
doLog = true,
showNoError = true,
}: {
diagnostics: Diagnostic[]
fileText: string
absFilePath: string
/** does log to terminal */
doLog?: boolean
showNoError?: boolean
}) {
if (!diagnostics.length && showNoError) {
doLog && console.log(chalk.green(`[VLS checker] No error found`))
return ''
}

const logs = diagnostics.map((d) => {
return composeLspLog({
diagnostic: d,
absFilePath,
fileText,
})
})

let logChunk = logs.join(os.EOL) + os.EOL
doLog && console.log(logChunk)
return logChunk
}
// function composeLspLog({
// diagnostic,
// absFilePath,
// fileText,
// }: {
// diagnostic: Diagnostic
// absFilePath: string
// fileText: string
// }) {
// let logChunk = ''
// const location = lspRange2Location(diagnostic.range)
// logChunk += `${os.EOL}${chalk.green.bold('FILE ')} ${absFilePath}:${location.start.line}:${
// location.start.column
// }${os.EOL}`

// if (diagnostic.severity === vscodeLanguageserverNode.DiagnosticSeverity.Error) {
// logChunk += `${chalk.red.bold('ERROR ')} ${diagnostic.message.trim()}`
// } else {
// logChunk += `${chalk.yellow.bold('WARN ')} ${diagnostic.message.trim()}`
// }

// logChunk += os.EOL + os.EOL
// logChunk += codeFrameColumns(fileText, location)
// return logChunk
// }

// export function prettyDiagnosticsLog({
// diagnostics,
// fileText,
// absFilePath,
// doLog = true,
// showNoError = true,
// }: {
// diagnostics: Diagnostic[]
// fileText: string
// absFilePath: string
// /** does log to terminal */
// doLog?: boolean
// showNoError?: boolean
// }) {
// if (!diagnostics.length && showNoError) {
// doLog && console.log(chalk.green(`[VLS checker] No error found`))
// return ''
// }

// const logs = diagnostics.map((d) => {
// return composeLspLog({
// diagnostic: d,
// absFilePath,
// fileText,
// })
// })

// let logChunk = logs.join(os.EOL) + os.EOL
// doLog && console.log(logChunk)
// return logChunk
// }
2 changes: 0 additions & 2 deletions packages/vite-plugin-checker/src/checkers/vls/initParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { vscodeLanguageserverProtocol, vscodeUri } from 'vite-plugin-checker-vls

type InitializeParams = vscodeLanguageserverProtocol.InitializeParams
type URI = vscodeUri.URI
// import { InitializeParams } from 'vscode-languageserver-protocol'
// import { URI } from 'vscode-uri'

export function getInitParams(workspaceUri: URI): InitializeParams {
const defaultVLSConfig = getDefaultVLSConfig()
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/checkers/vls/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parentPort } from 'worker_threads'

import { Checker, CheckerAbility } from '../../Checker'
import { DiagnosticOptions, diagnostics } from './commands/diagnostics'
import { DiagnosticOptions, diagnostics } from './diagnostics'

import type { CreateDiagnostic } from '../../types'

Expand Down
51 changes: 25 additions & 26 deletions packages/vite-plugin-checker/src/checkers/vueTsc/main.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { isMainThread } from 'worker_threads'
import { Checker, CheckerAbility } from '../../Checker'

import { createScript } from '../../worker'

import type { PluginConfig, CreateDiagnostic } from '../../types'
import type { UserConfig, ViteDevServer } from 'vite'
import type { CreateDiagnostic } from '../../types'

// TODO: watch mode is not supported for now
// we will wait for vue-tsc

/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*
*/
// @ts-ignore
export const createDiagnostic: CreateDiagnostic<Pick<PluginConfig, 'vueTsc'>> = (checkerConfig) => {
const createDiagnostic: CreateDiagnostic<'vueTsc'> = (pluginConfig) => {
return {
config: (config: UserConfig) => {
config: (config) => {
//
},
configureServer(server: ViteDevServer) {
configureServer(server) {
//
},
}
}

const { mainScript, workerScript } = createScript<Pick<PluginConfig, 'vueTsc'>>({
absFilename: __filename,
buildBin: ['vue-tsc', ['--noEmit']],
serverChecker: { createDiagnostic },
})!
export class VueTscChecker extends Checker<'vueTsc'> implements CheckerAbility {
public constructor() {
super({
name: 'typescript',
absFilePath: __filename,
build: { buildBin: ['vue-tsc', ['--noEmit']] },
createDiagnostic,
})
}

public sealConclusion() {}

if (isMainThread) {
const createServeAndBuild = mainScript()
module.exports.createServeAndBuild = createServeAndBuild
} else {
workerScript()
public init() {
const createServeAndBuild = super.initMainThread()
module.exports.createServeAndBuild = createServeAndBuild
super.initWorkerThread()
}
}

const vueTscChecker = new VueTscChecker()
vueTscChecker.prepare()
vueTscChecker.init()
Loading

0 comments on commit 558dacb

Please sign in to comment.