Skip to content

Commit

Permalink
chore: code consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Aug 30, 2024
1 parent 931444f commit ac1bec7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
19 changes: 8 additions & 11 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ export function baseCreate(
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost);
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost);
const globalTypesName = `${commandLine.vueOptions.lib}_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`;
const snapshots = new Map<string, ts.IScriptSnapshot>();
const globalTypesContents = vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
const globalTypesSnapshot: ts.IScriptSnapshot = {
getText: (start, end) => globalTypesContents.substring(start, end),
getLength: () => globalTypesContents.length,
getChangeRange: () => undefined,
};
if (directoryExists) {
languageServiceHost.directoryExists = path => {
if (path.endsWith('.vue-global-types')) {
Expand All @@ -149,22 +154,14 @@ export function baseCreate(
};
}
languageServiceHost.fileExists = path => {
if (path.endsWith(globalTypesName)) {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
return true;
}
return fileExists(path);
};
languageServiceHost.getScriptSnapshot = path => {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
if (!snapshots.has(path)) {
const contents = vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates);
snapshots.set(path, {
getText: (start, end) => contents.substring(start, end),
getLength: () => contents.length,
getChangeRange: () => undefined,
});
}
return snapshots.get(path)!;
return globalTypesSnapshot;
}
return getScriptSnapshot(path);
};
Expand Down
21 changes: 9 additions & 12 deletions packages/language-server/lib/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ export function initialize(
project.vue = { compilerOptions: vueCompilerOptions };

if (project.typescript) {
const globalTypesName = `${vueCompilerOptions.lib}_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
const directoryExists = project.typescript.languageServiceHost.directoryExists?.bind(project.typescript.languageServiceHost);
const fileExists = project.typescript.languageServiceHost.fileExists.bind(project.typescript.languageServiceHost);
const getScriptSnapshot = project.typescript.languageServiceHost.getScriptSnapshot.bind(project.typescript.languageServiceHost);
const snapshots = new Map<string, ts.IScriptSnapshot>();
const globalTypesName = `${vueCompilerOptions.lib}_${vueCompilerOptions.target}_${vueCompilerOptions.strictTemplates}.d.ts`;
const globalTypesContents = generateGlobalTypes(vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates);
const globalTypesSnapshot: ts.IScriptSnapshot = {
getText: (start, end) => globalTypesContents.substring(start, end),
getLength: () => globalTypesContents.length,
getChangeRange: () => undefined,
};
if (directoryExists) {
project.typescript.languageServiceHost.directoryExists = path => {
if (path.endsWith('.vue-global-types')) {
Expand All @@ -66,22 +71,14 @@ export function initialize(
};
}
project.typescript.languageServiceHost.fileExists = path => {
if (path.endsWith(globalTypesName)) {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
return true;
}
return fileExists(path);
};
project.typescript.languageServiceHost.getScriptSnapshot = path => {
if (path.endsWith(`.vue-global-types/${globalTypesName}`) || path.endsWith(`.vue-global-types\\${globalTypesName}`)) {
if (!snapshots.has(path)) {
const contents = generateGlobalTypes(vueCompilerOptions.lib, vueCompilerOptions.target, vueCompilerOptions.strictTemplates);
snapshots.set(path, {
getText: (start, end) => contents.substring(start, end),
getLength: () => contents.length,
getChangeRange: () => undefined,
});
}
return snapshots.get(path)!;
return globalTypesSnapshot;
}
return getScriptSnapshot(path);
};
Expand Down

0 comments on commit ac1bec7

Please sign in to comment.