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

Commit

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

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

this.configurationManager.onUpdatedConfig(() => {
info.project.refreshDiagnostics();
Expand All @@ -70,6 +69,7 @@ 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,6 +196,11 @@ 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 @@ -236,61 +241,57 @@ export class DenoPlugin implements ts_module.server.PluginModule {
return details;
};

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

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 453a9b0

Please sign in to comment.