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(tsc): improve regexp performance for global type removal #4260

Merged
merged 1 commit into from
Apr 22, 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
11 changes: 2 additions & 9 deletions packages/tsc/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { runTsc } from '@volar/typescript/lib/quickstart/runTsc';
import * as vue from '@vue/language-core';
import * as path from 'path';

const windowsPathReg = /\\/g;

Expand All @@ -23,13 +22,7 @@ export function run() {
) {
const writeFile = options.host!.writeFile.bind(options.host);
options.host!.writeFile = (fileName, contents, ...args) => {
if (!vueLanguagePlugin.pluginContext.globalTypesHolder) {
return writeFile(fileName, contents, ...args);
}

const writeFileName = path.basename(vueLanguagePlugin.getCanonicalFileName(fileName));
const globalTypesFileName = path.basename(vueLanguagePlugin.getCanonicalFileName(vueLanguagePlugin.pluginContext.globalTypesHolder));
return writeFile(fileName, writeFileName.startsWith(globalTypesFileName) ? removeEmitGlobalTypes(contents) : contents, ...args);
return writeFile(fileName, removeEmitGlobalTypes(contents), ...args);
};
const vueLanguagePlugin = vue.createVueLanguagePlugin(
ts,
Expand Down Expand Up @@ -65,7 +58,7 @@ export function run() {
}
}

const removeEmitGlobalTypesRegexp = /[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n/g;
const removeEmitGlobalTypesRegexp = /^[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n?$/mg;
Copy link
Collaborator

@rchl rchl Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those initial/trailing \n make no sense with m flag. Those should be ignored anyway when matching with m flag.

Suggested change
const removeEmitGlobalTypesRegexp = /^[^\n]*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd[^\n]*\n?$/mg;
const removeEmitGlobalTypesRegexp = /^.*__VLS_globalTypesStart[\w\W]*__VLS_globalTypesEnd.*$/mg;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


export function removeEmitGlobalTypes(dts: string) {
return dts.replace(removeEmitGlobalTypesRegexp, '');
Expand Down
Loading