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): resolve the shim used for tsc in Typescript v5.7 and up #252

Merged
merged 6 commits into from
Dec 13, 2024
Merged
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
39 changes: 26 additions & 13 deletions packages/typescript/lib/quickstart/runTsc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import type * as ts from 'typescript';
import type { Language, LanguagePlugin } from '@volar/language-core';

Expand All @@ -18,25 +19,37 @@ export function runTsc(
) {
getLanguagePlugins = _getLanguagePlugins;

let extraSupportedExtensions: string[];
let extraExtensionsToRemove: string[];

if (Array.isArray(options)) {
extraSupportedExtensions = options;
extraExtensionsToRemove = [];
}
else {
extraSupportedExtensions = options.extraSupportedExtensions;
extraExtensionsToRemove = options.extraExtensionsToRemove;
}

const proxyApiPath = require.resolve('../node/proxyCreateProgram');
const readFileSync = fs.readFileSync;

(fs as any).readFileSync = (...args: any[]) => {
if (args[0] === tscPath) {
let tsc = (readFileSync as any)(...args) as string;

let extraSupportedExtensions: string[];
let extraExtensionsToRemove: string[];
if (Array.isArray(options)) {
extraSupportedExtensions = options;
extraExtensionsToRemove = [];
}
else {
extraSupportedExtensions = options.extraSupportedExtensions;
extraExtensionsToRemove = options.extraExtensionsToRemove;
try {
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
} catch {
// Support the tsc shim used in Typescript v5.7 and up
const requireRegex = /module\.exports\s*=\s*require\((?:"|')(?<path>\.\/\w+\.js)(?:"|')\)/;
const requirePath = requireRegex.exec(tsc)?.groups?.path;
if (requirePath) {
tsc = readFileSync(path.join(path.dirname(tscPath), requirePath), 'utf8');
return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
} else {
throw new Error('Failed to locate tsc module path from shim');
}
}

return transformTscContent(tsc, proxyApiPath, extraSupportedExtensions, extraExtensionsToRemove, __filename, typescriptObject);
}
return (readFileSync as any)(...args);
};
Expand Down Expand Up @@ -112,7 +125,7 @@ function replace(text: string, ...[search, replace]: Parameters<String['replace'
text = text.replace(search, replace);
const after = text;
if (after === before) {
throw 'Search string not found: ' + JSON.stringify(search.toString());
throw new Error('Failed to replace: ' + search);
}
return after;
}
Loading