-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { readFileSync } from 'fs' | ||
import os from 'os' | ||
import strip from 'strip-ansi' | ||
import { ErrorPayload } from 'vite' | ||
import chalk from 'chalk' | ||
|
||
import { codeFrameColumns, SourceLocation } from '@babel/code-frame' | ||
|
||
import { uriToAbsPath } from './utils' | ||
|
||
// TODO: remove ./codeFrame.ts and ./utils.ts | ||
|
||
import type { | ||
Diagnostic as TsDiagnostic, | ||
flattenDiagnosticMessageText as flattenDiagnosticMessageTextType, | ||
LineAndCharacter, | ||
} from 'typescript' | ||
|
||
interface NormalizedDiagnostic { | ||
/** error message */ | ||
message?: string | ||
/** error conclusion */ | ||
conclusion?: string | ||
/** error stack */ | ||
stack?: string | string[] | ||
/** file name */ | ||
id?: string | ||
/** checker diagnostic source */ | ||
checker: string | ||
/** raw code frame generated by @babel/code-frame */ | ||
codeFrame?: string | ||
/** code frame, but striped */ | ||
stripedCodeFrame?: string | ||
/** error code location */ | ||
loc?: SourceLocation | ||
/** error level */ | ||
level?: 'error' | 'warning' | ||
} | ||
|
||
export function diagnosticToTerminalLog(d: NormalizedDiagnostic): string { | ||
const level = | ||
d.level === 'error' | ||
? chalk.red.bold('ERROR') + ' ' | ||
: d.level === 'warning' | ||
? chalk.yellow.bold('WARNING') + ' ' | ||
: '' | ||
const file = chalk.green.bold('FILE') + ' ' | ||
return [level + d.message, file + d.id, d.codeFrame + os.EOL, d.conclusion] | ||
.filter(Boolean) | ||
.join(os.EOL) | ||
} | ||
|
||
export function diagnosticToViteError(d: NormalizedDiagnostic): ErrorPayload['err'] { | ||
let loc: ErrorPayload['err']['loc'] | ||
if (d.loc) { | ||
loc = { | ||
file: d.id, | ||
line: d.loc.start.line + 1, | ||
column: typeof d.loc.start.column === 'number' ? d.loc.start.column + 1 : 0, | ||
} | ||
} | ||
|
||
return { | ||
message: d.message ?? '', | ||
stack: | ||
typeof d.stack === 'string' ? d.stack : Array.isArray(d.stack) ? d.stack.join(os.EOL) : '', | ||
id: d.id, | ||
frame: d.codeFrame, | ||
plugin: `vite-plugin-checker(${d.checker})`, | ||
loc, | ||
} | ||
} | ||
|
||
export function createFrame({ | ||
source, | ||
location, | ||
}: { | ||
/** file source code */ | ||
source: string | ||
location: SourceLocation | ||
}) { | ||
const frame = codeFrameColumns(source, location, { | ||
// worker tty did not fork parent process stdout, let's make a workaround | ||
forceColor: true, | ||
}) | ||
.split('\n') | ||
.map((line) => ' ' + line) | ||
.join(os.EOL) | ||
|
||
return frame | ||
} | ||
|
||
export function tsLocationToBabelLocation( | ||
tsLoc: Record<'start' | 'end', LineAndCharacter /** 0-based */> | ||
): SourceLocation { | ||
return { | ||
start: { line: tsLoc.start.line + 1, column: tsLoc.start.character + 1 }, | ||
end: { line: tsLoc.end.line + 1, column: tsLoc.end.character + 1 }, | ||
} | ||
} | ||
|
||
export function normalizeDiagnostic(d: TsDiagnostic): NormalizedDiagnostic { | ||
if (isTsDiagnostic(d)) return normalizeTsDiagnostic(d) | ||
throw Error(`unsupported diagnostic, only support TypeScript / VLS for now.`) | ||
} | ||
|
||
/* ------------------------------- TypeScript ------------------------------- */ | ||
|
||
export function isTsDiagnostic(d: any): d is TsDiagnostic { | ||
return ( | ||
'category' in d && | ||
'code' in d && | ||
'file' in d && | ||
'start' in d && | ||
'length' in d && | ||
'messageText' in d | ||
) | ||
} | ||
|
||
export function normalizeTsDiagnostic(d: TsDiagnostic): NormalizedDiagnostic { | ||
const fileName = d.file?.fileName | ||
const { | ||
flattenDiagnosticMessageText, | ||
}: { | ||
flattenDiagnosticMessageText: typeof flattenDiagnosticMessageTextType | ||
} = require('typescript') | ||
|
||
const message = flattenDiagnosticMessageText(d.messageText, os.EOL) | ||
|
||
let loc: SourceLocation | undefined | ||
const pos = d.start === undefined ? null : d.file?.getLineAndCharacterOfPosition(d.start) | ||
if (pos && d.file && typeof d.start === 'number' && typeof d.length === 'number') { | ||
loc = tsLocationToBabelLocation({ | ||
start: d.file?.getLineAndCharacterOfPosition(d.start), | ||
end: d.file?.getLineAndCharacterOfPosition(d.start + d.length), | ||
}) | ||
} | ||
|
||
let codeFrame: string | undefined | ||
if (loc) { | ||
codeFrame = createFrame({ | ||
source: d.file!.text, | ||
location: loc, | ||
}) | ||
} | ||
|
||
return { | ||
message, | ||
conclusion: '', | ||
codeFrame, | ||
stripedCodeFrame: codeFrame && strip(codeFrame), | ||
id: fileName, | ||
checker: 'TypeScript', | ||
loc, | ||
level: 'error', | ||
} | ||
} | ||
|
||
/* ----------------------------------- VLS ---------------------------------- */ | ||
|
||
/* --------------------------------- vue-tsc -------------------------------- */ | ||
|
||
/* --------------------------------- ESLint --------------------------------- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters