Skip to content

Commit

Permalink
feat: display code frame
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Mar 29, 2021
1 parent 028c4ab commit 9a9eca8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org/
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"typescript": "3.x || 4.x"
},
"devDependencies": {
"@types/babel__code-frame": "^7.0.2",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.31",
Expand Down Expand Up @@ -44,6 +45,7 @@
},
"homepage": "https://github.com/fi3ework/vite-plugin-fork-ts-checker#readme",
"dependencies": {
"@babel/code-frame": "^7.12.13",
"commander": "^7.1.0",
"debug": "^4.3.1",
"esbuild": "^0.8.52",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 27 additions & 20 deletions src/apiMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ const formatHost: ts.FormatDiagnosticsHost = {
getNewLine: () => ts.sys.newLine,
}

function toViteError(diagnostic: ts.Diagnostic): ErrorPayload['err'] {
function toViteError(d: ts.Diagnostic): ErrorPayload['err'] {
const pos = d.start === undefined ? null : d.file?.getLineAndCharacterOfPosition(d.start)
let loc: ErrorPayload['err']['loc'] = undefined
if (pos) {
loc = {
file: d.file?.fileName,
line: pos.line + 1,
column: pos.character + 1,
}
}

return {
message: 'string',
stack: 'a/b/c/d',
id: 'string',
frame: 'string',
plugin: 'string',
pluginCode: 'string',
// loc?: {
// file?: string
// line: number
// column: number
// }
// message: ts.flattenDiagnosticMessageText(d.messageText, formatHost.getNewLine()),
message: d.messageText.toString() + d.file?.getText(),
stack: '',
id: d.file?.fileName,
plugin: 'vite-plugin-fork-ts-checker',
loc,
}
}

Expand Down Expand Up @@ -63,14 +68,18 @@ export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
}

const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
const { file } = diagnostic
console.error(
'Error',
diagnostic.code,
':',
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
)
return toViteError(diagnostic)
// return toViteError(diagnostic)

if (!currErr) {
console.log('🔥', diagnostic)
currErr = toViteError(diagnostic)
}
}

const reportWatchStatusChanged: ts.WatchStatusReporter = (
Expand All @@ -80,20 +89,18 @@ export function createDiagnosis(userOptions: Partial<DiagnoseOptions> = {}) {
errorCount
) => {
// https://github.com/microsoft/TypeScript/issues/32542
console.log(diagnostic)
switch (diagnostic.code) {
case 6031: // Initial build
case 6032: // Incremental build
// clear current error and use the newer error from compiler
currErr = null
break
case 6193: // 1 Error
currErr = toViteError(diagnostic)
// currErr = toViteError(diagnostic)
case 6194: // 0 errors or 2+ errors
if (errorCount === 0 || errorCount === undefined) {
} else {
if (!currErr) currErr = toViteError(diagnostic)
}
// if (errorCount !== 0 && !currErr) {
// currErr = toViteError(diagnostic)
// }

if (currErr) {
server.ws.send({
Expand Down
14 changes: 14 additions & 0 deletions src/codeFrame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { codeFrameColumns } from '@babel/code-frame'

const rawLines = `class Foo {
constructor() {
console.log("hello");
}
}`
const location = { start: { line: 2, column: 17 }, end: { line: 4, column: 3 } }

const result = codeFrameColumns(rawLines, location, {
/* options */
})

console.log(result)

0 comments on commit 9a9eca8

Please sign in to comment.