Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
refactor: typescript-deno-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 1, 2020
1 parent eae5fb6 commit 6170e21
Showing 1 changed file with 52 additions and 53 deletions.
105 changes: 52 additions & 53 deletions typescript-deno-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ export class DenoPlugin implements ts_module.server.PluginModule {
create(info: ts_module.server.PluginCreateInfo): ts_module.LanguageService {
const projectDirectory = info.project.getCurrentDirectory();

// TypeScript plugins have a `cwd` of `/`, which causes issues with import resolution.
process.chdir(projectDirectory);
this.logger.info(
`Create typescript-deno-plugin for project ${projectDirectory}`
);

this.configurationManager.onUpdatedConfig(() => {
info.project.refreshDiagnostics();
Expand All @@ -69,7 +70,6 @@ export class DenoPlugin implements ts_module.server.PluginModule {
this.configurationManager.update(vscodeSettings);
}

this.logger.info(`Create typescript-deno-plugin`);
const getCompilationSettings = info.languageServiceHost.getCompilationSettings.bind(
info.languageServiceHost
);
Expand Down Expand Up @@ -196,11 +196,6 @@ export class DenoPlugin implements ts_module.server.PluginModule {
});
};

if (!resolveModuleNames) {
this.logger.info("resolveModuleNames is undefined.");
return info.languageService;
}

info.languageService.getCompletionEntryDetails = (
fileName: string,
position: number,
Expand Down Expand Up @@ -241,57 +236,61 @@ export class DenoPlugin implements ts_module.server.PluginModule {
return details;
};

info.languageServiceHost.resolveModuleNames = (
moduleNames: string[],
containingFile: string,
...rest
): (ts_module.ResolvedModule | undefined)[] => {
if (!this.configurationManager.config.enable) {
return resolveModuleNames(moduleNames, containingFile, ...rest);
}
if (resolveModuleNames) {
info.languageServiceHost.resolveModuleNames = (
moduleNames: string[],
containingFile: string,
...rest
): (ts_module.ResolvedModule | undefined)[] => {
if (!this.configurationManager.config.enable) {
return resolveModuleNames(moduleNames, containingFile, ...rest);
}

const importMapsFilepath = this.configurationManager.config.import_map
? path.isAbsolute(this.configurationManager.config.import_map)
? this.configurationManager.config.import_map
: path.resolve(
info.project.getCurrentDirectory(),
this.configurationManager.config.import_map
)
: undefined;

const resolver = ModuleResolver.create(
containingFile,
importMapsFilepath
);
const importMapsFilepath = this.configurationManager.config.import_map
? path.isAbsolute(this.configurationManager.config.import_map)
? this.configurationManager.config.import_map
: path.resolve(
info.project.getCurrentDirectory(),
this.configurationManager.config.import_map
)
: undefined;

const resolver = ModuleResolver.create(
containingFile,
importMapsFilepath
);

const resolvedModules = resolver.resolveModules(moduleNames);
const resolvedModules = resolver.resolveModules(moduleNames);

return resolveModuleNames(
resolvedModules.map((v, index) => (v ? v.module : moduleNames[index])),
containingFile,
...rest
).map((v, index) => {
if (!v) {
const cacheModule = resolvedModules[index];
if (cacheModule) {
const moduleFilepath = cacheModule.filepath;
// import * as React from 'https://dev.jspm.io/react'
if (
path.isAbsolute(moduleFilepath) &&
pathExistsSync(moduleFilepath)
) {
return {
extension: this.typescript.Extension.Js,
isExternalLibraryImport: false,
resolvedFileName: moduleFilepath
} as ts_module.ResolvedModuleFull;
return resolveModuleNames(
resolvedModules.map((v, index) =>
v ? v.module : moduleNames[index]
),
containingFile,
...rest
).map((v, index) => {
if (!v) {
const cacheModule = resolvedModules[index];
if (cacheModule) {
const moduleFilepath = cacheModule.filepath;
// import * as React from 'https://dev.jspm.io/react'
if (
path.isAbsolute(moduleFilepath) &&
pathExistsSync(moduleFilepath)
) {
return {
extension: this.typescript.Extension.Js,
isExternalLibraryImport: false,
resolvedFileName: moduleFilepath
} as ts_module.ResolvedModuleFull;
}
}
}
}

return v;
});
};
return v;
});
};
}

return info.languageService;
}
Expand Down

0 comments on commit 6170e21

Please sign in to comment.