@volar/typescript 2.4.27 → 2.4.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/node/decorateLanguageServiceHost.js +24 -9
- package/lib/node/proxyCreateProgram.d.ts +5 -0
- package/lib/node/proxyCreateProgram.js +67 -21
- package/lib/node/proxyLanguageService.js +15 -8
- package/lib/node/transform.d.ts +4 -0
- package/lib/node/transform.js +6 -3
- package/lib/quickstart/runTsc.d.ts +1 -1
- package/lib/quickstart/runTsc.js +45 -26
- package/lib/resolveModuleName.js +24 -4
- package/package.json +30 -22
|
@@ -31,16 +31,29 @@ function decorateLanguageServiceHost(ts, language, languageServiceHost) {
|
|
|
31
31
|
if (pluginExtensions.length) {
|
|
32
32
|
const resolveModuleName = (0, resolveModuleName_1.createResolveModuleName)(ts, ts.sys.getFileSize, languageServiceHost, language.plugins, fileName => language.scripts.get(fileName));
|
|
33
33
|
const moduleResolutionCache = languageServiceHost.getModuleResolutionCache?.();
|
|
34
|
+
const hasPluginExtension = (moduleName) => pluginExtensions.some(ext => moduleName.endsWith(ext));
|
|
34
35
|
if (resolveModuleNameLiterals) {
|
|
35
36
|
languageServiceHost.resolveModuleNameLiterals = (moduleLiterals, containingFile, redirectedReference, options, containingSourceFile, ...rest) => {
|
|
36
37
|
const disposeFixup = (0, utils_1.fixupImpliedNodeFormatForFile)(ts, pluginExtensions, containingSourceFile, moduleResolutionCache?.getPackageJsonInfoCache(), languageServiceHost, options);
|
|
37
38
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
// Literals without a plugin extension go to the host's resolver first, so resolvers
|
|
40
|
+
// patched in by other TypeScript plugins keep working; the ones it cannot resolve
|
|
41
|
+
// are retried with Volar's, which also handles `resolveHiddenExtensions`.
|
|
42
|
+
const hostLiterals = moduleLiterals.filter(literal => !hasPluginExtension(literal.text));
|
|
43
|
+
const hostResults = hostLiterals.length
|
|
44
|
+
? resolveModuleNameLiterals(hostLiterals, containingFile, redirectedReference, options, containingSourceFile, ...rest)
|
|
45
|
+
: [];
|
|
46
|
+
const hostResultMap = new Map(hostLiterals.map((literal, i) => [literal, hostResults[i]]));
|
|
41
47
|
return moduleLiterals.map(moduleLiteral => {
|
|
48
|
+
const hostResult = hostResultMap.get(moduleLiteral);
|
|
49
|
+
if (hostResult?.resolvedModule) {
|
|
50
|
+
return hostResult;
|
|
51
|
+
}
|
|
42
52
|
const mode = ts.getModeForUsageLocation(containingSourceFile, moduleLiteral, options);
|
|
43
|
-
|
|
53
|
+
const result = resolveModuleName(moduleLiteral.text, containingFile, options,
|
|
54
|
+
// The host's failed resolution may already be in the shared cache.
|
|
55
|
+
hostResult ? undefined : moduleResolutionCache, redirectedReference, mode);
|
|
56
|
+
return hostResult && !result.resolvedModule ? hostResult : result;
|
|
44
57
|
});
|
|
45
58
|
}
|
|
46
59
|
finally {
|
|
@@ -50,12 +63,14 @@ function decorateLanguageServiceHost(ts, language, languageServiceHost) {
|
|
|
50
63
|
}
|
|
51
64
|
if (resolveModuleNames) {
|
|
52
65
|
languageServiceHost.resolveModuleNames = (moduleNames, containingFile, reusedNames, redirectedReference, options, containingSourceFile) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
66
|
+
const hostNames = moduleNames.filter(name => !hasPluginExtension(name));
|
|
67
|
+
const hostResults = hostNames.length
|
|
68
|
+
? resolveModuleNames(hostNames, containingFile, reusedNames, redirectedReference, options, containingSourceFile)
|
|
69
|
+
: [];
|
|
70
|
+
const hostResultMap = new Map(hostNames.map((name, i) => [name, hostResults[i]]));
|
|
56
71
|
return moduleNames.map(moduleName => {
|
|
57
|
-
return
|
|
58
|
-
.resolvedModule;
|
|
72
|
+
return hostResultMap.get(moduleName)
|
|
73
|
+
?? resolveModuleName(moduleName, containingFile, options, hostResultMap.has(moduleName) ? undefined : moduleResolutionCache, redirectedReference).resolvedModule;
|
|
59
74
|
});
|
|
60
75
|
};
|
|
61
76
|
}
|
|
@@ -4,3 +4,8 @@ export declare function proxyCreateProgram(ts: typeof import('typescript'), orig
|
|
|
4
4
|
languagePlugins: LanguagePlugin<string>[];
|
|
5
5
|
setup?(language: Language<string>): void;
|
|
6
6
|
}): typeof import("typescript").createProgram;
|
|
7
|
+
/**
|
|
8
|
+
* Drops the state kept for projects that the solution builder no longer builds.
|
|
9
|
+
* `isActiveProject` receives the canonical path of a project's config file.
|
|
10
|
+
*/
|
|
11
|
+
export declare function releaseRemovedProjects(createProgram: object, isActiveProject: (configFilePath: string) => boolean): void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.proxyCreateProgram = proxyCreateProgram;
|
|
4
|
+
exports.releaseRemovedProjects = releaseRemovedProjects;
|
|
4
5
|
const language_core_1 = require("@volar/language-core");
|
|
5
6
|
const common_1 = require("../common");
|
|
6
7
|
const resolveModuleName_1 = require("../resolveModuleName");
|
|
@@ -30,32 +31,36 @@ const objectEqual = (a, b) => {
|
|
|
30
31
|
}
|
|
31
32
|
return true;
|
|
32
33
|
};
|
|
34
|
+
const projectStateReleasers = new WeakMap();
|
|
33
35
|
function proxyCreateProgram(ts, original, create) {
|
|
34
36
|
const sourceFileSnapshots = new language_core_1.FileMap(ts.sys.useCaseSensitiveFileNames);
|
|
35
37
|
const parsedSourceFiles = new WeakMap();
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
// Keyed by the canonical `configFilePath`, so the projects of a solution builder
|
|
39
|
+
// (`tsc -b`) don't evict each other's state; programs without one share a slot.
|
|
40
|
+
const states = new Map();
|
|
41
|
+
// Bumped whenever a program's root files or options change. A kept module resolution
|
|
42
|
+
// cache may hold lookups that a file added or removed since then invalidates, so the
|
|
43
|
+
// other projects recreate theirs before they are built again.
|
|
44
|
+
let fileSetVersion = 0;
|
|
45
|
+
const proxy = new Proxy(original, {
|
|
41
46
|
apply: (target, thisArg, args) => {
|
|
42
47
|
const options = args[0];
|
|
43
48
|
assert(!!options.host, '!!options.host');
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
languagePlugins = created.languagePlugins;
|
|
49
|
+
const configFilePath = options.options.configFilePath;
|
|
50
|
+
const stateKey = typeof configFilePath === 'string'
|
|
51
|
+
? options.host.getCanonicalFileName(configFilePath)
|
|
52
|
+
: undefined;
|
|
53
|
+
let state = states.get(stateKey);
|
|
54
|
+
if (!state
|
|
55
|
+
|| !arrayEqual(options.rootNames, state.options.rootNames)
|
|
56
|
+
|| !objectEqual(options.options, state.options.options)) {
|
|
57
|
+
if (state) {
|
|
58
|
+
fileSetVersion++;
|
|
57
59
|
}
|
|
58
|
-
|
|
60
|
+
const moduleResolutionCache = createModuleResolutionCache(options.host, options.options);
|
|
61
|
+
const created = create(ts, options);
|
|
62
|
+
const languagePlugins = Array.isArray(created) ? created : created.languagePlugins;
|
|
63
|
+
const language = (0, language_core_1.createLanguage)([
|
|
59
64
|
...languagePlugins,
|
|
60
65
|
{ getLanguageId: common_1.resolveFileLanguageId },
|
|
61
66
|
], new language_core_1.FileMap(ts.sys.useCaseSensitiveFileNames), (fileName, includeFsFiles) => {
|
|
@@ -89,12 +94,21 @@ function proxyCreateProgram(ts, original, create) {
|
|
|
89
94
|
if ('setup' in created) {
|
|
90
95
|
created.setup?.(language);
|
|
91
96
|
}
|
|
97
|
+
state = { options, languagePlugins, language, moduleResolutionCache, fileSetVersion };
|
|
98
|
+
states.set(stateKey, state);
|
|
92
99
|
}
|
|
100
|
+
else if (state.fileSetVersion !== fileSetVersion) {
|
|
101
|
+
state.moduleResolutionCache = createModuleResolutionCache(options.host, options.options);
|
|
102
|
+
state.fileSetVersion = fileSetVersion;
|
|
103
|
+
}
|
|
104
|
+
const { languagePlugins, language, moduleResolutionCache } = state;
|
|
93
105
|
const originalHost = options.host;
|
|
94
106
|
const pluginExtensions = languagePlugins
|
|
95
107
|
.map(plugin => plugin.typescript?.extraFileExtensions.map(({ extension }) => `.${extension}`) ?? [])
|
|
96
108
|
.flat();
|
|
97
|
-
|
|
109
|
+
// TypeScript prefers `getSourceFileByPath` over `getSourceFile` when reusing an old
|
|
110
|
+
// program (e.g. `tsc --watch`), which would bypass the wrapper below.
|
|
111
|
+
options.host = { ...originalHost, getSourceFileByPath: undefined };
|
|
98
112
|
options.host.getSourceFile = (fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) => {
|
|
99
113
|
const originalSourceFile = originalHost.getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile);
|
|
100
114
|
if (!sourceFileSnapshots.has(fileName)
|
|
@@ -138,6 +152,8 @@ function proxyCreateProgram(ts, original, create) {
|
|
|
138
152
|
const parsedSourceFile = ts.createSourceFile(fileName, virtualContents, languageVersionOrOptions, undefined, serviceScript.scriptKind);
|
|
139
153
|
// @ts-expect-error
|
|
140
154
|
parsedSourceFile.version = originalSourceFile.version;
|
|
155
|
+
// @ts-expect-error
|
|
156
|
+
originalSourceFile.scriptKind = serviceScript.scriptKind;
|
|
141
157
|
parsedSourceFiles.set(originalSourceFile, parsedSourceFile);
|
|
142
158
|
}
|
|
143
159
|
if (getExtraServiceScripts) {
|
|
@@ -152,10 +168,20 @@ function proxyCreateProgram(ts, original, create) {
|
|
|
152
168
|
const resolveModuleName = (0, resolveModuleName_1.createResolveModuleName)(ts, ts.sys.getFileSize, originalHost, language.plugins, fileName => language.scripts.get(fileName));
|
|
153
169
|
const resolveModuleNameLiterals = originalHost.resolveModuleNameLiterals;
|
|
154
170
|
const resolveModuleNames = originalHost.resolveModuleNames;
|
|
171
|
+
// When any plugin opts in to `resolveHiddenExtensions`, the volar-aware
|
|
172
|
+
// resolver above is what makes extensionless imports of plugin-extension
|
|
173
|
+
// files resolvable (it answers `fileExists('Foo.d.ts')` as `true` when
|
|
174
|
+
// `Foo.<ext>` is on disk). If the host already has a `resolveModuleNameLiterals`
|
|
175
|
+
// — e.g. `tsc --watch` installs `resolutionCache.resolveModuleNameLiterals` —
|
|
176
|
+
// the original fast-path delegates extensionless imports to it, bypassing
|
|
177
|
+
// the wrapper and surfacing TS2307. Force the volar-aware resolver in
|
|
178
|
+
// that case.
|
|
179
|
+
const hasResolveHiddenExtensions = languagePlugins.some(plugin => plugin.typescript?.resolveHiddenExtensions);
|
|
155
180
|
options.host.resolveModuleNameLiterals = (moduleLiterals, containingFile, redirectedReference, compilerOptions, containingSourceFile, ...rest) => {
|
|
156
181
|
const disposeFixup = (0, utils_1.fixupImpliedNodeFormatForFile)(ts, pluginExtensions, containingSourceFile, moduleResolutionCache.getPackageJsonInfoCache(), originalHost, compilerOptions);
|
|
157
182
|
try {
|
|
158
183
|
if (resolveModuleNameLiterals
|
|
184
|
+
&& !hasResolveHiddenExtensions
|
|
159
185
|
&& moduleLiterals.every(name => !pluginExtensions.some(ext => name.text.endsWith(ext)))) {
|
|
160
186
|
return resolveModuleNameLiterals(moduleLiterals, containingFile, redirectedReference, compilerOptions, containingSourceFile, ...rest);
|
|
161
187
|
}
|
|
@@ -169,7 +195,9 @@ function proxyCreateProgram(ts, original, create) {
|
|
|
169
195
|
}
|
|
170
196
|
};
|
|
171
197
|
options.host.resolveModuleNames = (moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions, containingSourceFile) => {
|
|
172
|
-
if (resolveModuleNames
|
|
198
|
+
if (resolveModuleNames
|
|
199
|
+
&& !hasResolveHiddenExtensions
|
|
200
|
+
&& moduleNames.every(name => !pluginExtensions.some(ext => name.endsWith(ext)))) {
|
|
173
201
|
return resolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, compilerOptions, containingSourceFile);
|
|
174
202
|
}
|
|
175
203
|
return moduleNames.map(moduleName => {
|
|
@@ -182,6 +210,24 @@ function proxyCreateProgram(ts, original, create) {
|
|
|
182
210
|
return program;
|
|
183
211
|
},
|
|
184
212
|
});
|
|
213
|
+
function createModuleResolutionCache(host, options) {
|
|
214
|
+
return ts.createModuleResolutionCache(host.getCurrentDirectory(), host.getCanonicalFileName, options);
|
|
215
|
+
}
|
|
216
|
+
projectStateReleasers.set(proxy, isActiveProject => {
|
|
217
|
+
for (const key of states.keys()) {
|
|
218
|
+
if (key !== undefined && !isActiveProject(key)) {
|
|
219
|
+
states.delete(key);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
return proxy;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Drops the state kept for projects that the solution builder no longer builds.
|
|
227
|
+
* `isActiveProject` receives the canonical path of a project's config file.
|
|
228
|
+
*/
|
|
229
|
+
function releaseRemovedProjects(createProgram, isActiveProject) {
|
|
230
|
+
projectStateReleasers.get(createProgram)?.(isActiveProject);
|
|
185
231
|
}
|
|
186
232
|
function assert(condition, message) {
|
|
187
233
|
if (!condition) {
|
|
@@ -332,7 +332,7 @@ function provideCallHierarchyOutgoingCalls(language, provideCallHierarchyOutgoin
|
|
|
332
332
|
function organizeImports(language, organizeImports) {
|
|
333
333
|
return (args, formatOptions, preferences) => {
|
|
334
334
|
const unresolved = organizeImports(args, formatOptions, preferences);
|
|
335
|
-
return (0, transform_1.transformFileTextChanges)(language, unresolved, false, language_core_1.isCodeActionsEnabled);
|
|
335
|
+
return (0, transform_1.transformFileTextChanges)(language, unresolved, false, language_core_1.isCodeActionsEnabled, true) ?? [];
|
|
336
336
|
};
|
|
337
337
|
}
|
|
338
338
|
function getQuickInfoAtPosition(language, getQuickInfoAtPosition) {
|
|
@@ -515,15 +515,19 @@ function getEditsForRefactor(language, getEditsForRefactor) {
|
|
|
515
515
|
edits = getEditsForRefactor(fileName, formatOptions, positionOrRange, refactorName, actionName, preferences, interactiveRefactorArguments);
|
|
516
516
|
}
|
|
517
517
|
if (edits) {
|
|
518
|
-
|
|
519
|
-
|
|
518
|
+
const transformed = (0, transform_1.transformFileTextChanges)(language, edits.edits, false, language_core_1.isCodeActionsEnabled, true);
|
|
519
|
+
if (transformed) {
|
|
520
|
+
edits.edits = transformed;
|
|
521
|
+
return edits;
|
|
522
|
+
}
|
|
520
523
|
}
|
|
521
524
|
};
|
|
522
525
|
}
|
|
523
526
|
function getCombinedCodeFix(language, getCombinedCodeFix) {
|
|
524
527
|
return (...args) => {
|
|
525
528
|
const codeActions = getCombinedCodeFix(...args);
|
|
526
|
-
codeActions.changes = (0, transform_1.transformFileTextChanges)(language, codeActions.changes, false, language_core_1.isCodeActionsEnabled)
|
|
529
|
+
codeActions.changes = (0, transform_1.transformFileTextChanges)(language, codeActions.changes, false, language_core_1.isCodeActionsEnabled, true)
|
|
530
|
+
?? [];
|
|
527
531
|
return codeActions;
|
|
528
532
|
};
|
|
529
533
|
}
|
|
@@ -582,11 +586,14 @@ function getCodeFixesAtPosition(language, getCodeFixesAtPosition) {
|
|
|
582
586
|
else {
|
|
583
587
|
fixes = getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences);
|
|
584
588
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
589
|
+
return fixes.filter(fix => {
|
|
590
|
+
const changes = (0, transform_1.transformFileTextChanges)(language, fix.changes, false, language_core_1.isCodeActionsEnabled, true);
|
|
591
|
+
if (changes) {
|
|
592
|
+
fix.changes = changes;
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
return false;
|
|
588
596
|
});
|
|
589
|
-
return fixes;
|
|
590
597
|
};
|
|
591
598
|
}
|
|
592
599
|
function getEncodedSemanticClassifications(language, getEncodedSemanticClassifications) {
|
package/lib/node/transform.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ export declare function transformCallHierarchyItem(language: Language<string>, i
|
|
|
12
12
|
export declare function transformDiagnostic<T extends ts.Diagnostic>(language: Language<string>, diagnostic: T, program: ts.Program | undefined, isTsc: boolean): T | undefined;
|
|
13
13
|
export declare function fillSourceFileText(language: Language<string>, sourceFile: ts.SourceFile): void;
|
|
14
14
|
export declare function transformFileTextChanges(language: Language<string>, changes: readonly ts.FileTextChanges[], fallbackToAnyMatch: boolean, filter: (data: CodeInformation) => boolean): ts.FileTextChanges[];
|
|
15
|
+
/**
|
|
16
|
+
* With `allOrNothing`, returns `undefined` when any change cannot be mapped to a source file.
|
|
17
|
+
*/
|
|
18
|
+
export declare function transformFileTextChanges(language: Language<string>, changes: readonly ts.FileTextChanges[], fallbackToAnyMatch: boolean, filter: (data: CodeInformation) => boolean, allOrNothing: true): ts.FileTextChanges[] | undefined;
|
|
15
19
|
export declare function transformDocumentSpan<T extends ts.DocumentSpan>(language: Language<string>, documentSpan: T, fallbackToAnyMatch: boolean, filter: (data: CodeInformation) => boolean, shouldFallback?: boolean): T | undefined;
|
|
16
20
|
export declare function transformSpan(language: Language<string>, fileName: string | undefined, textSpan: ts.TextSpan | undefined, fallbackToAnyMatch: boolean, filter: (data: CodeInformation) => boolean): {
|
|
17
21
|
fileName: string;
|
package/lib/node/transform.js
CHANGED
|
@@ -94,19 +94,22 @@ function fillSourceFileText(language, sourceFile) {
|
|
|
94
94
|
+ sourceFile.text.substring(sourceScript.snapshot.getLength());
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
function transformFileTextChanges(language, changes, fallbackToAnyMatch, filter) {
|
|
97
|
+
function transformFileTextChanges(language, changes, fallbackToAnyMatch, filter, allOrNothing = false) {
|
|
98
98
|
const changesPerFile = {};
|
|
99
99
|
const newFiles = new Set();
|
|
100
100
|
for (const fileChanges of changes) {
|
|
101
101
|
const [_, source] = (0, utils_1.getServiceScript)(language, fileChanges.fileName);
|
|
102
102
|
if (source) {
|
|
103
|
-
fileChanges.textChanges
|
|
103
|
+
for (const c of fileChanges.textChanges) {
|
|
104
104
|
const { fileName, textSpan } = transformSpan(language, fileChanges.fileName, c.span, fallbackToAnyMatch, filter)
|
|
105
105
|
?? {};
|
|
106
106
|
if (fileName && textSpan) {
|
|
107
107
|
(changesPerFile[fileName] ?? (changesPerFile[fileName] = [])).push({ ...c, span: textSpan });
|
|
108
108
|
}
|
|
109
|
-
|
|
109
|
+
else if (allOrNothing) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
110
113
|
}
|
|
111
114
|
else {
|
|
112
115
|
const list = changesPerFile[fileChanges.fileName] ?? (changesPerFile[fileChanges.fileName] = []);
|
|
@@ -7,7 +7,7 @@ export declare let getLanguagePlugins: (ts: typeof import('typescript'), options
|
|
|
7
7
|
export declare function runTsc(tscPath: string, options: string[] | {
|
|
8
8
|
extraSupportedExtensions: string[];
|
|
9
9
|
extraExtensionsToRemove: string[];
|
|
10
|
-
}, _getLanguagePlugins: typeof getLanguagePlugins, typescriptObject?: string):
|
|
10
|
+
}, _getLanguagePlugins: typeof getLanguagePlugins, typescriptObject?: string): void;
|
|
11
11
|
/**
|
|
12
12
|
* Replaces the code of typescript to add support for additional extensions and language plugins.
|
|
13
13
|
*
|
package/lib/quickstart/runTsc.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.getLanguagePlugins = void 0;
|
|
|
4
4
|
exports.runTsc = runTsc;
|
|
5
5
|
exports.transformTscContent = transformTscContent;
|
|
6
6
|
const fs = require("fs");
|
|
7
|
+
const module_1 = require("module");
|
|
7
8
|
const path = require("path");
|
|
8
9
|
let getLanguagePlugins = () => [];
|
|
9
10
|
exports.getLanguagePlugins = getLanguagePlugins;
|
|
@@ -20,35 +21,39 @@ function runTsc(tscPath, options, _getLanguagePlugins, typescriptObject) {
|
|
|
20
21
|
extraExtensionsToRemove = options.extraExtensionsToRemove;
|
|
21
22
|
}
|
|
22
23
|
const proxyApiPath = require.resolve('../node/proxyCreateProgram');
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
let tsc = readFileSync(...args);
|
|
27
|
-
try {
|
|
28
|
-
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
|
|
29
|
-
}
|
|
30
|
-
catch {
|
|
31
|
-
// Support the tsc shim used in Typescript v5.7 and up
|
|
32
|
-
const requireRegex = /module\.exports\s*=\s*require\((?:"|')(?<path>\.\/\w+\.js)(?:"|')\)/;
|
|
33
|
-
const requirePath = requireRegex.exec(tsc)?.groups?.path;
|
|
34
|
-
if (requirePath) {
|
|
35
|
-
tsc = readFileSync(path.join(path.dirname(tscPath), requirePath), 'utf8');
|
|
36
|
-
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
throw new Error('Failed to locate tsc module path from shim');
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return readFileSync(...args);
|
|
44
|
-
};
|
|
24
|
+
// Mirror the TS v5.7+ tsc shim behavior: enable the V8/JSC bytecode cache
|
|
25
|
+
// so the (huge) transformed tsc source is compiled once and reused on
|
|
26
|
+
// subsequent invocations. Supported by Node 22+; no-op on Bun.
|
|
45
27
|
try {
|
|
46
|
-
|
|
28
|
+
module_1.Module.enableCompileCache?.();
|
|
47
29
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
30
|
+
catch { }
|
|
31
|
+
// Detect the tsc shim used in TypeScript v5.7+: a tiny preamble that
|
|
32
|
+
// re-exports the actual compiler from a sibling file. Resolve through it
|
|
33
|
+
// up front so transformTscContent operates on the real source.
|
|
34
|
+
const shimRegex = /module\.exports\s*=\s*require\(["'](?<path>\.\/\w+\.js)["']\)/;
|
|
35
|
+
let compilePath = tscPath;
|
|
36
|
+
let tsc = fs.readFileSync(tscPath, 'utf8');
|
|
37
|
+
const shimMatch = shimRegex.exec(tsc);
|
|
38
|
+
if (shimMatch?.groups?.path) {
|
|
39
|
+
compilePath = path.join(path.dirname(tscPath), shimMatch.groups.path);
|
|
40
|
+
tsc = fs.readFileSync(compilePath, 'utf8');
|
|
51
41
|
}
|
|
42
|
+
let transformed = transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
|
|
43
|
+
// Strip UTF-8 BOM — Node's Module._extensions['.js'] does this before
|
|
44
|
+
// compiling; we bypass that wrapper so we replicate it here.
|
|
45
|
+
if (transformed.charCodeAt(0) === 0xFEFF) {
|
|
46
|
+
transformed = transformed.slice(1);
|
|
47
|
+
}
|
|
48
|
+
// Avoid monkey-patching `fs.readFileSync` to inject the transformed source:
|
|
49
|
+
// Bun's module loader does not read source through the JS `fs` API, so the
|
|
50
|
+
// patch is silently ignored. Compile the transformed source directly via
|
|
51
|
+
// the (private but widely-used) Module#_compile, which is honored by both
|
|
52
|
+
// Node and Bun. See https://github.com/oven-sh/bun/issues/4754.
|
|
53
|
+
const m = new module_1.Module(compilePath);
|
|
54
|
+
m.filename = compilePath;
|
|
55
|
+
m.paths = module_1.Module._nodeModulePaths(path.dirname(compilePath));
|
|
56
|
+
m._compile(transformed, compilePath);
|
|
52
57
|
}
|
|
53
58
|
/**
|
|
54
59
|
* Replaces the code of typescript to add support for additional extensions and language plugins.
|
|
@@ -74,6 +79,16 @@ function transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraE
|
|
|
74
79
|
if (extraExtensionsToRemove.length) {
|
|
75
80
|
const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', ');
|
|
76
81
|
tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`);
|
|
82
|
+
// These extensions are stripped on emit, so (like .ts -> .js) the JS file
|
|
83
|
+
// they produce ends in .js. Teach tryGetJSExtensionForFile that mapping,
|
|
84
|
+
// otherwise build mode (tsc -b) declaration emit calls getJSExtensionForFile
|
|
85
|
+
// and Debug.fail()s with "Extension .xxx is unsupported" when it computes a
|
|
86
|
+
// module specifier to one of these files with a "js" ending preference.
|
|
87
|
+
tsc = replace(tsc, /function tryGetJSExtensionForFile\(/, s => `function tryGetJSExtensionForFile(fileName, options) {
|
|
88
|
+
return [${extsText}].some(ext => fileName.endsWith(ext))
|
|
89
|
+
? ".js"
|
|
90
|
+
: _tryGetJSExtensionForFile(fileName, options)
|
|
91
|
+
}\n` + s.replace('tryGetJSExtensionForFile', '_tryGetJSExtensionForFile'));
|
|
77
92
|
}
|
|
78
93
|
// Support for basename.xxx to basename.xxx.d.ts
|
|
79
94
|
if (neededPatchExtenstions.length) {
|
|
@@ -93,6 +108,10 @@ function transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraE
|
|
|
93
108
|
].join(', ')
|
|
94
109
|
+ `);\n`
|
|
95
110
|
+ s.replace('createProgram', '_createProgram'));
|
|
111
|
+
// Release per-project state when a project leaves the solution builder's build order.
|
|
112
|
+
// Not applying this only costs memory, so a missing match is not an error.
|
|
113
|
+
tsc = tsc.replace(/mutateMapSkippingNewValues\(state\.builderPrograms, currentProjects, noopOnDelete\);/, s => s
|
|
114
|
+
+ `\nrequire(${JSON.stringify(proxyApiPath)}).releaseRemovedProjects?.(createProgram, configFilePath => currentProjects.has(configFilePath));`);
|
|
96
115
|
return tsc;
|
|
97
116
|
}
|
|
98
117
|
function replace(text, search, replace) {
|
package/lib/resolveModuleName.js
CHANGED
|
@@ -72,16 +72,36 @@ function createResolveModuleName(ts, getFileSize, host, languagePlugins, getSour
|
|
|
72
72
|
};
|
|
73
73
|
return (moduleName, containingFile, compilerOptions, cache, redirectedReference, resolutionMode) => {
|
|
74
74
|
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, cache, redirectedReference, resolutionMode);
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
const resolvedModule = result.resolvedModule;
|
|
76
|
+
if (resolvedModule) {
|
|
77
|
+
const sourceFileInfo = toSourceFileInfo.get(resolvedModule.resolvedFileName);
|
|
77
78
|
if (sourceFileInfo) {
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
resolvedModule.resolvedFileName = sourceFileInfo.sourceFileName;
|
|
80
|
+
resolvedModule.extension = sourceFileInfo.extension;
|
|
81
|
+
// TypeScript could not realpath the virtual file name, so do it for the source file
|
|
82
|
+
// under the same conditions TypeScript applies to node_modules lookups.
|
|
83
|
+
if (!compilerOptions.preserveSymlinks
|
|
84
|
+
&& host.realpath
|
|
85
|
+
&& resolvedModule.isExternalLibraryImport
|
|
86
|
+
&& !resolvedModule.originalPath
|
|
87
|
+
&& !ts.isExternalModuleNameRelative(moduleName)) {
|
|
88
|
+
const realPath = host.realpath(sourceFileInfo.sourceFileName).replace(/\\/g, '/');
|
|
89
|
+
if (!isSamePath(realPath, sourceFileInfo.sourceFileName)) {
|
|
90
|
+
resolvedModule.originalPath = sourceFileInfo.sourceFileName;
|
|
91
|
+
resolvedModule.resolvedFileName = realPath;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
80
94
|
}
|
|
81
95
|
}
|
|
82
96
|
toSourceFileInfo.clear();
|
|
83
97
|
return result;
|
|
84
98
|
};
|
|
99
|
+
function isSamePath(a, b) {
|
|
100
|
+
const useCaseSensitiveFileNames = typeof host.useCaseSensitiveFileNames === 'function'
|
|
101
|
+
? host.useCaseSensitiveFileNames()
|
|
102
|
+
: host.useCaseSensitiveFileNames;
|
|
103
|
+
return useCaseSensitiveFileNames ? a === b : a.toLowerCase() === b.toLowerCase();
|
|
104
|
+
}
|
|
85
105
|
// fix https://github.com/vuejs/language-tools/issues/3332
|
|
86
106
|
function fileExists(fileName) {
|
|
87
107
|
if (host.fileExists(fileName)) {
|
package/package.json
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
"name": "@volar/typescript",
|
|
3
|
+
"version": "2.4.29",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"**/*.js",
|
|
7
|
+
"**/*.d.ts"
|
|
8
|
+
],
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/volarjs/volar.js.git",
|
|
12
|
+
"directory": "packages/typescript"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@volar/language-core": "2.4.29",
|
|
16
|
+
"@volar/language-service": "2.4.29",
|
|
17
|
+
"path-browserify": "^1.0.1",
|
|
18
|
+
"vscode-uri": "^3.0.8"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/path-browserify": "~1.0.3"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"typescript": "*"
|
|
25
|
+
},
|
|
26
|
+
"peerDependenciesMeta": {
|
|
27
|
+
"typescript": {
|
|
28
|
+
"optional": true
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|