From 35227c8a8099d57cadc40d30d3a0568ff5588206 Mon Sep 17 00:00:00 2001 From: wangshunnn Date: Sun, 7 Apr 2024 18:51:28 +0800 Subject: [PATCH 1/4] fix(typescript): transform source file for tsc without `incremental` refs https://github.com/vuejs/language-tools/issues/4099 --- packages/typescript/lib/node/decorateProgram.ts | 15 ++++----------- packages/typescript/lib/node/transform.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/typescript/lib/node/decorateProgram.ts b/packages/typescript/lib/node/decorateProgram.ts index 3893b199..2ff6f856 100644 --- a/packages/typescript/lib/node/decorateProgram.ts +++ b/packages/typescript/lib/node/decorateProgram.ts @@ -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, transformSourceFile } from './transform'; export function decorateProgram(language: Language, program: ts.Program) { @@ -48,16 +48,9 @@ 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()); - } - } - return sourceFile; + return sourceFile && transformSourceFile(language, sourceFile); }; } diff --git a/packages/typescript/lib/node/transform.ts b/packages/typescript/lib/node/transform.ts index 626a451d..f6bb4c00 100644 --- a/packages/typescript/lib/node/transform.ts +++ b/packages/typescript/lib/node/transform.ts @@ -38,6 +38,7 @@ export function transformDiagnostic(language: Language, ...diagnostic, start: sourceSpan.start, length: sourceSpan.length, + file: transformSourceFile(language, diagnostic.file), }); } } @@ -52,6 +53,16 @@ export function transformDiagnostic(language: Language, return transformedDiagnostics.get(diagnostic) as T | undefined; } +// fix https://github.com/vuejs/language-tools/issues/4099 without `incremental` +export function transformSourceFile(language: Language, sourceFile: ts.SourceFile): ts.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; +} + export function transformFileTextChanges(language: Language, changes: ts.FileTextChanges, filter: (data: CodeInformation) => boolean): ts.FileTextChanges | undefined { const [_, source] = getServiceScript(language, changes.fileName); if (source) { From ff660940a5b81d8bbe566c2511c3ad425805ff67 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 7 Apr 2024 20:58:58 +0800 Subject: [PATCH 2/4] perf: transform once --- packages/typescript/lib/node/transform.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/typescript/lib/node/transform.ts b/packages/typescript/lib/node/transform.ts index f6bb4c00..d18805fb 100644 --- a/packages/typescript/lib/node/transform.ts +++ b/packages/typescript/lib/node/transform.ts @@ -3,6 +3,7 @@ import type * as ts from 'typescript'; import { getServiceScript, notEmpty } from './utils'; const transformedDiagnostics = new WeakMap(); +const transformedSourceFile = new WeakSet(); export function transformCallHierarchyItem(language: Language, item: ts.CallHierarchyItem, filter: (data: CodeInformation) => boolean): ts.CallHierarchyItem { const span = transformSpan(language, item.file, item.span, filter); @@ -55,6 +56,10 @@ export function transformDiagnostic(language: Language, // fix https://github.com/vuejs/language-tools/issues/4099 without `incremental` export function transformSourceFile(language: Language, sourceFile: ts.SourceFile): ts.SourceFile { + if (transformedSourceFile.has(sourceFile)) { + return sourceFile; + } + transformedSourceFile.add(sourceFile); const [serviceScript, sourceScript] = getServiceScript(language, sourceFile.fileName); if (serviceScript) { sourceFile.text = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength()) From cfd6ea8f136147930466f663195fd84f4c70a752 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 7 Apr 2024 21:00:47 +0800 Subject: [PATCH 3/4] refactor: improve expressiveness --- packages/typescript/lib/node/decorateProgram.ts | 7 +++++-- packages/typescript/lib/node/transform.ts | 9 +++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/typescript/lib/node/decorateProgram.ts b/packages/typescript/lib/node/decorateProgram.ts index 2ff6f856..d3d3eaa0 100644 --- a/packages/typescript/lib/node/decorateProgram.ts +++ b/packages/typescript/lib/node/decorateProgram.ts @@ -1,7 +1,7 @@ import type { Language } from '@volar/language-core'; import type * as ts from 'typescript'; import { notEmpty } from './utils'; -import { transformDiagnostic, transformSourceFile } from './transform'; +import { transformDiagnostic, fillSourceFileText } from './transform'; export function decorateProgram(language: Language, program: ts.Program) { @@ -51,6 +51,9 @@ export function decorateProgram(language: Language, program: ts.Program) { // fix https://github.com/vuejs/language-tools/issues/4099 with `incremental` program.getSourceFileByPath = path => { const sourceFile = getSourceFileByPath(path); - return sourceFile && transformSourceFile(language, sourceFile); + if (sourceFile) { + fillSourceFileText(language, sourceFile); + } + return sourceFile; }; } diff --git a/packages/typescript/lib/node/transform.ts b/packages/typescript/lib/node/transform.ts index d18805fb..6bd149bb 100644 --- a/packages/typescript/lib/node/transform.ts +++ b/packages/typescript/lib/node/transform.ts @@ -35,11 +35,12 @@ export function transformDiagnostic(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, length: sourceSpan.length, - file: transformSourceFile(language, diagnostic.file), + file: diagnostic.file, }); } } @@ -55,9 +56,9 @@ export function transformDiagnostic(language: Language, } // fix https://github.com/vuejs/language-tools/issues/4099 without `incremental` -export function transformSourceFile(language: Language, sourceFile: ts.SourceFile): ts.SourceFile { +export function fillSourceFileText(language: Language, sourceFile: ts.SourceFile) { if (transformedSourceFile.has(sourceFile)) { - return sourceFile; + return; } transformedSourceFile.add(sourceFile); const [serviceScript, sourceScript] = getServiceScript(language, sourceFile.fileName); @@ -65,7 +66,7 @@ export function transformSourceFile(language: Language, sourceFile: ts.SourceFil sourceFile.text = sourceScript.snapshot.getText(0, sourceScript.snapshot.getLength()) + sourceFile.text.substring(sourceScript.snapshot.getLength()); } - return sourceFile; + return; } export function transformFileTextChanges(language: Language, changes: ts.FileTextChanges, filter: (data: CodeInformation) => boolean): ts.FileTextChanges | undefined { From c8390c799453f9c6d6a537b1aef9a3c895c87737 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sun, 7 Apr 2024 21:01:17 +0800 Subject: [PATCH 4/4] Update transform.ts --- packages/typescript/lib/node/transform.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/typescript/lib/node/transform.ts b/packages/typescript/lib/node/transform.ts index 6bd149bb..514f69cc 100644 --- a/packages/typescript/lib/node/transform.ts +++ b/packages/typescript/lib/node/transform.ts @@ -40,7 +40,6 @@ export function transformDiagnostic(language: Language, ...diagnostic, start: sourceSpan.start, length: sourceSpan.length, - file: diagnostic.file, }); } }