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
…tal` (#158)

refs vuejs/language-tools#4194

---------

Co-authored-by: Johnson Chu <johnsoncodehk@gmail.com>
  • Loading branch information
wangshunnn and johnsoncodehk committed Apr 4, 2024
1 parent e009c8b commit 4c9b7cc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
16 changes: 15 additions & 1 deletion packages/typescript/lib/node/decorateProgram.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Language } from '@volar/language-core';
import type * as ts from 'typescript';
import { notEmpty } from './utils';
import { getServiceScript, notEmpty } from './utils';
import { transformDiagnostic } from './transform';

export function decorateProgram(language: Language, program: ts.Program) {
Expand All @@ -11,6 +11,7 @@ export function decorateProgram(language: Language, program: ts.Program) {
const getSyntacticDiagnostics = program.getSyntacticDiagnostics;
const getSemanticDiagnostics = program.getSemanticDiagnostics;
const getGlobalDiagnostics = program.getGlobalDiagnostics;
const getSourceFileByPath = program.getSourceFileByPath;

// for tsc --noEmit --watch
// @ts-ignore
Expand Down Expand Up @@ -46,4 +47,17 @@ export function decorateProgram(language: Language, program: ts.Program) {
.map(d => transformDiagnostic(language, d))
.filter(notEmpty);
};

// fix https://github.com/vuejs/language-tools/issues/4099
program.getSourceFileByPath = path => {
const sourceFile = getSourceFileByPath(path);
if (sourceFile) {
const [serviceScript, sourceScript] = getServiceScript(language, sourceFile.fileName);
if (serviceScript) {
sourceFile.text = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength())
+ sourceFile.text.substring(sourceScript.snapshot.getLength());
}
}
return sourceFile;
};
}
4 changes: 2 additions & 2 deletions packages/typescript/lib/node/proxyCreateProgram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as ts from 'typescript';
import { decorateProgram } from './decorateProgram';
import { LanguagePlugin, createLanguage } from '@volar/language-core';
import type * as ts from 'typescript';
import { createResolveModuleName } from '../resolveModuleName';
import { decorateProgram } from './decorateProgram';

export function proxyCreateProgram(
ts: typeof import('typescript'),
Expand Down
12 changes: 0 additions & 12 deletions packages/typescript/lib/node/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type * as ts from 'typescript';
import { getServiceScript, notEmpty } from './utils';

const transformedDiagnostics = new WeakMap<ts.Diagnostic, ts.Diagnostic | undefined>();
const transformedSourceFiles = new WeakMap<ts.SourceFile, ts.SourceFile>();

export function transformCallHierarchyItem(language: Language, item: ts.CallHierarchyItem, filter: (data: CodeInformation) => boolean): ts.CallHierarchyItem {
const span = transformSpan(language, item.file, item.span, filter);
Expand Down Expand Up @@ -39,7 +38,6 @@ export function transformDiagnostic<T extends ts.Diagnostic>(language: Language,
...diagnostic,
start: sourceSpan.start,
length: sourceSpan.length,
file: transformSourceFile(diagnostic.file, sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength())),
});
}
}
Expand All @@ -54,16 +52,6 @@ export function transformDiagnostic<T extends ts.Diagnostic>(language: Language,
return transformedDiagnostics.get(diagnostic) as T | undefined;
}

export function transformSourceFile(sourceFile: ts.SourceFile, sourceText: string): ts.SourceFile {
if (!transformedSourceFiles.has(sourceFile)) {
transformedSourceFiles.set(sourceFile, {
...sourceFile,
text: sourceText,
});
}
return transformedSourceFiles.get(sourceFile)!;
}

export function transformFileTextChanges(language: Language, changes: ts.FileTextChanges, filter: (data: CodeInformation) => boolean): ts.FileTextChanges | undefined {
const [_, source] = getServiceScript(language, changes.fileName);
if (source) {
Expand Down

0 comments on commit 4c9b7cc

Please sign in to comment.