Skip to content

Commit

Permalink
fix: vue-tsc does not show ts errors in the terminal with `--incremen…
Browse files Browse the repository at this point in the history
  • Loading branch information
wangshunnn committed Apr 2, 2024
1 parent bd85b9c commit a772447
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 26 additions & 2 deletions packages/typescript/lib/node/proxyCreateProgram.ts
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -20,7 +23,7 @@ export function proxyCreateProgram(
.map(plugin => plugin.typescript?.extraFileExtensions.map(({ extension }) => `.${extension}`) ?? [])
.flat();
const sourceFileToSnapshotMap = new WeakMap<ts.SourceFile, ts.IScriptSnapshot>();
const language = createLanguage(
language = createLanguage(
languagePlugins,
ts.sys.useCaseSensitiveFileNames,
fileName => {
Expand Down Expand Up @@ -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())),
};
},
});
}
9 changes: 9 additions & 0 deletions packages/typescript/lib/quickstart/runTsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a772447

Please sign in to comment.