Skip to content

Commit

Permalink
fix(typescript): transform source file for tsc without incremental (#…
Browse files Browse the repository at this point in the history
…162)

* fix(typescript): transform source file for tsc without `incremental`

refs vuejs/language-tools#4099

---------

Co-authored-by: Johnson Chu <johnsoncodehk@gmail.com>
  • Loading branch information
wangshunnn and johnsoncodehk authored Apr 7, 2024
1 parent c1c0f0b commit 28f6a85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 4 additions & 8 deletions packages/typescript/lib/node/decorateProgram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Language } from '@volar/language-core';
import type * as ts from 'typescript';
import { getServiceScript, notEmpty } from './utils';
import { transformDiagnostic } from './transform';
import { notEmpty } from './utils';
import { transformDiagnostic, fillSourceFileText } from './transform';

export function decorateProgram(language: Language, program: ts.Program) {

Expand Down Expand Up @@ -48,15 +48,11 @@ export function decorateProgram(language: Language, program: ts.Program) {
.filter(notEmpty);
};

// fix https://github.com/vuejs/language-tools/issues/4099
// fix https://github.com/vuejs/language-tools/issues/4099 with `incremental`
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());
}
fillSourceFileText(language, sourceFile);
}
return sourceFile;
};
Expand Down
16 changes: 16 additions & 0 deletions packages/typescript/lib/node/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type * as ts from 'typescript';
import { getServiceScript, notEmpty } from './utils';

const transformedDiagnostics = new WeakMap<ts.Diagnostic, ts.Diagnostic | undefined>();
const transformedSourceFile = new WeakSet<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 @@ -34,6 +35,7 @@ export function transformDiagnostic<T extends ts.Diagnostic>(language: Language,
if (serviceScript) {
const sourceSpan = transformTextSpan(sourceScript, map, { start: diagnostic.start, length: diagnostic.length }, shouldReportDiagnostics);
if (sourceSpan) {
fillSourceFileText(language, diagnostic.file);
transformedDiagnostics.set(diagnostic, {
...diagnostic,
start: sourceSpan.start,
Expand All @@ -52,6 +54,20 @@ export function transformDiagnostic<T extends ts.Diagnostic>(language: Language,
return transformedDiagnostics.get(diagnostic) as T | undefined;
}

// fix https://github.com/vuejs/language-tools/issues/4099 without `incremental`
export function fillSourceFileText(language: Language, sourceFile: ts.SourceFile) {
if (transformedSourceFile.has(sourceFile)) {
return;
}
transformedSourceFile.add(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;
}

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 28f6a85

Please sign in to comment.