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): improve accuracy of runTsc extensions parameter behavior #222

Merged
merged 3 commits into from
Jul 4, 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
45 changes: 32 additions & 13 deletions packages/typescript/lib/quickstart/runTsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export let getLanguagePlugins: (ts: typeof import('typescript'), options: ts.Cre

export function runTsc(
tscPath: string,
extensionsOrOptions: string[] | {
supportedExtensions: string[];
extensionsToRemove: string[];
options: string[] | {
extraSupportedExtensions: string[];
extraExtensionsToRemove: string[];
},
_getLanguagePlugins: typeof getLanguagePlugins
) {
Expand All @@ -25,20 +25,39 @@ export function runTsc(
if (args[0] === tscPath) {
let tsc = (readFileSync as any)(...args) as string;

const supportedExtensions = Array.isArray(extensionsOrOptions) ? extensionsOrOptions : extensionsOrOptions.supportedExtensions;
const extensionsToRemove = Array.isArray(extensionsOrOptions) ? [] : extensionsOrOptions.extensionsToRemove;
let extraSupportedExtensions: string[];
let extraExtensionsToRemove: string[];
if (Array.isArray(options)) {
extraSupportedExtensions = options;
extraExtensionsToRemove = [];
}
else {
extraSupportedExtensions = options.extraSupportedExtensions;
extraExtensionsToRemove = options.extraExtensionsToRemove;
}
const needPatchExtenstions = extraSupportedExtensions.filter(ext => !extraExtensionsToRemove.includes(ext));

// add allow extensions
if (supportedExtensions.length) {
const extsText = supportedExtensions.map(ext => `"${ext}"`).join(', ');
tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`);
tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`);
tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.concat([[${extsText}]])`);
// Add allow extensions
if (extraSupportedExtensions.length) {
const extsText = extraSupportedExtensions.map(ext => `"${ext}"`).join(', ');
tsc = replace(tsc, /supportedTSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`);
tsc = replace(tsc, /supportedJSExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`);
tsc = replace(tsc, /allSupportedExtensions = .*(?=;)/, s => s + `.map((group, i) => i === 0 ? group.splice(0, 0, ${extsText}) && group : group)`);
}
if (extensionsToRemove.length) {
const extsText = extensionsToRemove.map(ext => `"${ext}"`).join(', ');
// Use to emit basename.xxx to basename.d.ts instead of basename.xxx.d.ts
if (extraExtensionsToRemove.length) {
const extsText = extraExtensionsToRemove.map(ext => `"${ext}"`).join(', ');
tsc = replace(tsc, /extensionsToRemove = .*(?=;)/, s => s + `.concat([${extsText}])`);
}
// Support for basename.xxx to basename.xxx.d.ts
if (needPatchExtenstions.length) {
const extsText = needPatchExtenstions.map(ext => `"${ext}"`).join(', ');
tsc = replace(tsc, /function changeExtension\(/, s => `function changeExtension(path, newExtension) {
return [${extsText}].some(ext => path.endsWith(ext))
? path + newExtension
: _changeExtension(path, newExtension)
}\n` + s.replace('changeExtension', '_changeExtension'));
}

// proxy createProgram
tsc = replace(tsc, /function createProgram\(.+\) {/, s =>
Expand Down