Skip to content

Commit 84ddfb2

Browse files
committed
feat(tsgo): format diagnostics with code context in color terminals
1 parent 877631e commit 84ddfb2

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

‎src/generator/tsgo.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path'
2+
import process from 'node:process'
23
import { createDebug } from 'obug'
34
import { RE_DTS, RE_NODE_MODULES } from '../filename.ts'
45
import {
@@ -113,9 +114,7 @@ export class TsgoGenerator implements Generator {
113114
.flat()
114115
.filter(isMapperDiagnostic)
115116
if (diagnostics.length) {
116-
throw new Error(
117-
this.apiModule.formatDiagnostics(diagnostics, this.api),
118-
)
117+
throw new Error(this.formatDiagnostics(diagnostics))
119118
}
120119
}
121120
}
@@ -154,6 +153,13 @@ export class TsgoGenerator implements Generator {
154153
return this.options.compiler === 'tsrs' ? 'tsrs' : 'TypeScript Go'
155154
}
156155

156+
private formatDiagnostics(diagnostics: readonly Diagnostic[]): string {
157+
const format = process.stderr.hasColors?.()
158+
? this.apiModule!.formatDiagnosticsWithColorAndContext
159+
: this.apiModule!.formatDiagnostics
160+
return format(diagnostics, this.api!)
161+
}
162+
157163
private toTsFilename(fileName: string): string {
158164
return path.resolve(this.options.languageContext.toTsFilename(fileName))
159165
}
@@ -311,7 +317,7 @@ export class TsgoGenerator implements Generator {
311317
if (output.emitSkipped && output.diagnostics.length) {
312318
return {
313319
error: mapError(
314-
this.apiModule.formatDiagnostics(output.diagnostics, this.api),
320+
this.formatDiagnostics(output.diagnostics),
315321
tsFileName,
316322
fileName,
317323
),

0 commit comments

Comments
 (0)