Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): transform source file for tsc without incremental #162

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading