From a772447d984ecd119b8f8dc9352c9f17c9199e01 Mon Sep 17 00:00:00 2001 From: wangshunnn Date: Tue, 2 Apr 2024 21:10:32 +0800 Subject: [PATCH] fix: vue-tsc does not show ts errors in the terminal with `--incremental` refs https://github.com/vuejs/language-tools/issues/4194 --- .../typescript/lib/node/proxyCreateProgram.ts | 28 +++++++++++++++++-- packages/typescript/lib/quickstart/runTsc.ts | 9 ++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/typescript/lib/node/proxyCreateProgram.ts b/packages/typescript/lib/node/proxyCreateProgram.ts index 190232c3..0abbd9d5 100644 --- a/packages/typescript/lib/node/proxyCreateProgram.ts +++ b/packages/typescript/lib/node/proxyCreateProgram.ts @@ -1,7 +1,10 @@ import type * as ts from 'typescript'; import { decorateProgram } from './decorateProgram'; -import { LanguagePlugin, createLanguage } from '@volar/language-core'; +import { Language, LanguagePlugin, createLanguage } from '@volar/language-core'; import { createResolveModuleName } from '../resolveModuleName'; +import { transformSourceFile } from './transform'; + +let language: Language; export function proxyCreateProgram( ts: typeof import('typescript'), @@ -20,7 +23,7 @@ export function proxyCreateProgram( .map(plugin => plugin.typescript?.extraFileExtensions.map(({ extension }) => `.${extension}`) ?? []) .flat(); const sourceFileToSnapshotMap = new WeakMap(); - const language = createLanguage( + language = createLanguage( languagePlugins, ts.sys.useCaseSensitiveFileNames, fileName => { @@ -154,3 +157,24 @@ function assert(condition: unknown, message: string): asserts condition { throw new Error(message); } } + +export function proxyConvertToDiagnosticRelatedInformation( + original: Function, +) { + return new Proxy(original, { + apply: (target, thisArg, args) => { + const diagnostic = Reflect.apply(target, thisArg, args) as ts.DiagnosticRelatedInformation; + const { file } = diagnostic; + const sourceScript = file ? language.scripts.get(file.fileName) : undefined; + + if (!sourceScript) { + return diagnostic; + } + + return { + ...diagnostic, + file: transformSourceFile(file!, sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength())), + }; + }, + }); +} diff --git a/packages/typescript/lib/quickstart/runTsc.ts b/packages/typescript/lib/quickstart/runTsc.ts index 02f32fd6..5e1cfc5c 100644 --- a/packages/typescript/lib/quickstart/runTsc.ts +++ b/packages/typescript/lib/quickstart/runTsc.ts @@ -42,6 +42,15 @@ export function runTsc( + s.replace('createProgram', '_createProgram') ); + // proxy convertToDiagnosticRelatedInformation for `--incremental` + try { + tsc = replace(tsc, /function convertToDiagnosticRelatedInformation\(.+\) {/, s => + `var convertToDiagnosticRelatedInformation = require(${JSON.stringify(proxyApiPath)})` + + `.proxyConvertToDiagnosticRelatedInformation(_convertToDiagnosticRelatedInformation);\n` + + s.replace('convertToDiagnosticRelatedInformation', '_convertToDiagnosticRelatedInformation') + ); + } catch { /*ignore*/ } + return tsc; } return (readFileSync as any)(...args);