Skip to content

Commit

Permalink
feat: console.log error source
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 17, 2021
1 parent 5b2d475 commit f99588d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/checkers/eslint/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const createDiagnostic: CreateDiagnostic<'eslint'> = (pluginConfig) => {
const diagnostics = await eslint.lintFiles(filePath)
const normalized = diagnostics.map((p) => normalizeEslintDiagnostic(p)).flat(1)
normalized.forEach((n) => {
console.log(diagnosticToTerminalLog(n))
console.log(diagnosticToTerminalLog(n, 'ESLint'))
})
diagnosticsCache[filePath] = normalized

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
currErr = diagnosticToViteError(formattedDiagnostics)
}

logChunk += os.EOL + diagnosticToTerminalLog(formattedDiagnostics)
logChunk += os.EOL + diagnosticToTerminalLog(formattedDiagnostics, 'TypeScript')
}

const reportWatchStatusChanged: ts.WatchStatusReporter = (
Expand Down
5 changes: 3 additions & 2 deletions packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async function prepareClientConnection(workspaceUri: URI, options: DiagnosticOpt
const res = await normalizePublishDiagnosticParams(publishDiagnostics)
const normalized = diagnosticToViteError(res)
console.log(os.EOL)
console.log(res.map((d) => diagnosticToTerminalLog(d)).join(os.EOL))
console.log(res.map((d) => diagnosticToTerminalLog(d, 'VLS')).join(os.EOL))

options.errorCallback?.(publishDiagnostics, normalized)
}
Expand Down Expand Up @@ -267,7 +267,8 @@ async function getDiagnostics(
diagnostic: d,
absFilePath,
fileText,
})
}),
'VLS'
)
)
.join(os.EOL)
Expand Down
19 changes: 13 additions & 6 deletions packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
flattenDiagnosticMessageText as flattenDiagnosticMessageTextType,
LineAndCharacter,
} from 'typescript'
import type { BuildInCheckerNames } from './types'

export interface NormalizedDiagnostic {
/** error message */
Expand Down Expand Up @@ -49,16 +50,22 @@ export enum DiagnosticLevel {
Message = 3,
}

export function diagnosticToTerminalLog(d: NormalizedDiagnostic): string {
export function diagnosticToTerminalLog(
d: NormalizedDiagnostic,
name?: 'TypeScript' | 'vue-tsc' | 'VLS' | 'ESLint'
): string {
const nameInLabel = name ? `(${name})` : ''
const boldBlack = chalk.bold.rgb(0, 0, 0)

const labelMap: Record<DiagnosticLevel, string> = {
[DiagnosticLevel.Error]: chalk.bold.bgRed.rgb(0, 0, 0)(' ERROR '),
[DiagnosticLevel.Warning]: chalk.bold.bgYellow.rgb(0, 0, 0)(' WARNING '),
[DiagnosticLevel.Suggestion]: chalk.bold.bgBlue.rgb(0, 0, 0)(' SUGGESTION '),
[DiagnosticLevel.Message]: chalk.bold.bgCyan.rgb(0, 0, 0)(' MESSAGE '),
[DiagnosticLevel.Error]: boldBlack.bgRedBright(` ERROR${nameInLabel} `),
[DiagnosticLevel.Warning]: boldBlack.bgYellowBright(` WARNING${nameInLabel} `),
[DiagnosticLevel.Suggestion]: boldBlack.bgBlueBright(` SUGGESTION${nameInLabel} `),
[DiagnosticLevel.Message]: boldBlack.bgCyanBright(` MESSAGE${nameInLabel} `),
}

const levelLabel = labelMap[d.level || DiagnosticLevel.Error]
const fileLabel = chalk.bgGreen.rgb(0, 0, 0).bold(' FILE ') + ' '
const fileLabel = boldBlack.bgCyanBright(' FILE ') + ' '
const position = d.loc
? chalk.yellow(d.loc.start.line) + ':' + chalk.yellow(d.loc.start.column)
: ''
Expand Down

0 comments on commit f99588d

Please sign in to comment.