Skip to content

Commit

Permalink
fix: handle diagnostic without positon (fix #149)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jul 4, 2022
1 parent 3427b67 commit 029e83c
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 32 deletions.
5 changes: 4 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"private": true,
"name": "docs"
"name": "docs",
"dependencies": {
"vue": "^3.2.37"
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"vite": "^2.7.13",
"vite-plugin-checker": "workspace:*",
"vitepress": "^1.0.0-alpha.4",
"vue": "^3.2.37",
"ws": "^8.5.0",
"zx": "^1.14.2"
}
Expand Down
20 changes: 9 additions & 11 deletions packages/runtime/src/components/Diagnostic.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'vue-tsc': '#64b587',
}
const fileRE = /(?:[a-zA-Z]:\\|\/).*?:\d+:\d+/g
const fileRE = /(?:[a-zA-Z]:\\|\/).*(:\d+:\d+)?/g
const codeframeRE = /^(?:>?\s+\d+\s+\|.*|\s+\|\s*\^.*)\r?\n/gm
codeframeRE.lastIndex = 0
$: hasFrame = diagnostic.frame && codeframeRE.test(diagnostic.frame)
Expand All @@ -17,9 +17,12 @@
$: stackLinks = calcLink(diagnostic.stack)
$: [file] = (diagnostic.loc?.file || diagnostic.id || 'unknown file').split(`?`)
$: errorSource = diagnostic.loc
? { ...calcLink(`${file}:${diagnostic.loc.line}:${diagnostic.loc.column}`)[0], linkFiles: true }
: { text: file, linkFiles: false }
$: errorSource = {
...calcLink(
`${file}` + (diagnostic.loc ? `:${diagnostic.loc.line}:${diagnostic.loc.column}` : '')
)[0],
linkFiles: true,
}
function calcLink(text) {
let curIndex = 0
Expand Down Expand Up @@ -50,13 +53,8 @@
><span class={`message-body message-body-${diagnostic.level}`}>{message}</span>
</pre>
<!-- svelte-ignore a11y-missing-attribute -->
<pre class="file">{#if errorSource.linkFiles}<a class="file-link" on:click={errorSource.onclick}
>{errorSource.textContent}</a
>
{:else}
{errorSource.text}
{/if}
</pre>
<pre class="file"><a class="file-link" on:click={errorSource.onclick}>{errorSource.textContent}</a
></pre>
{#if hasFrame}
<pre class="frame"><code class="frame-code">{diagnostic.frame}</code></pre>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function normalizeTsDiagnostic(d: TsDiagnostic): NormalizedDiagnostic {
const message = flattenDiagnosticMessageText(d.messageText, os.EOL)

let loc: SourceLocation | undefined
const pos = d.start === undefined ? null : d.file?.getLineAndCharacterOfPosition(d.start)
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),
Expand Down
Loading

0 comments on commit 029e83c

Please sign in to comment.