Skip to content

Commit

Permalink
fix(tsc): improve regexp performance for global type removal
Browse files Browse the repository at this point in the history
The issue with the removal of global types was the RegExp, for
certain files written like `.tsbuildinfo` which is a single line
long contents file it would scan the whole line scanning for the
characters.

By specifying `m` flag it causes ^ and $ to match the begin/end
of each line (not only begin/end of string).

This removes the need for the check of the filename to do the
replacement check.
  • Loading branch information
blake-newman committed Apr 12, 2024
1 parent 591d019 commit 7a3d615
Showing 1 changed file with 2 additions and 9 deletions.
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;

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

0 comments on commit 7a3d615

Please sign in to comment.