Skip to content

Commit

Permalink
Move module resolution to own package
Browse files Browse the repository at this point in the history
  • Loading branch information
Matsuuu committed Nov 12, 2023
1 parent 03110ff commit 961a960
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CODE_ACTIONS } from "../enum/code-actions.js";
import { getPathAsDtsFile, getPathAsJsFile, getPathAsTsFile } from "../../ts/filepath-transformers.js";
import { CustomElementsLanguageServiceRequest } from "../../request.js";
import { normalizePath } from "../../interop.js";
import { resolveModule } from "../../ts/modules.js";

export function getImportDiagnostics(request: CustomElementsLanguageServiceRequest): tss.Diagnostic[] {
const { filePath, document, htmlLanguageService, projectBasePath, project } = request;
Expand Down Expand Up @@ -48,7 +49,7 @@ export function getImportDiagnostics(request: CustomElementsLanguageServiceReque

const cemInstanceRef = definition.cem;
const fullImportPath = normalizePath(`${cemInstanceRef.cemSourcePath}/${definition.path}`);
const moduleResolution = ts.resolveModuleName(`${cemInstanceRef.cemSourceFolderPath}/${definition.path}`, filePath, project.getCompilerOptions(), project.projectService.host);
const moduleResolution = resolveModule(`${cemInstanceRef.cemSourceFolderPath}/${definition.path}`, filePath, project);
const resolvedModuleFileName = moduleResolution.resolvedModule?.resolvedFileName;

if (!resolvedModuleFileName || !associatedFiles.includes(resolvedModuleFileName)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/custom-elements-languageserver-core/src/ts/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import ts from "typescript";
import tss from "typescript/lib/tsserverlibrary";

export function resolveModule(modulePath: string, sourceFilePath: string, project: tss.server.Project) {
return ts.resolveModuleName(modulePath, sourceFilePath, project.getCompilerOptions(), project.projectService.host);
}
3 changes: 2 additions & 1 deletion lib/custom-elements-languageserver-core/src/ts/sourcefile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from "path";
import * as fs from "fs";
import { getPathAsDtsFile, getPathAsJsFile, getPathAsTsFile } from "./filepath-transformers.js";
import { normalizePath } from "../interop.js";
import { resolveModule } from "./modules.js";

export function getSourceFile(baseOrFullPath: string, classPath: string | undefined, project: tss.server.Project) {
const fullClassPath = classPath === undefined ?
Expand Down Expand Up @@ -43,7 +44,7 @@ export function getAllFilesAssociatedWithSourceFile(sourceFile: ts.SourceFile, b
const imports = fileInfo.importedFiles;

for (const importReference of imports) {
const moduleResolution = ts.resolveModuleName(importReference.fileName, currentSourceFile.fileName, project.getCompilerOptions(), project.projectService.host);
const moduleResolution = resolveModule(importReference.fileName, currentSourceFile.fileName, project);
// TODO: for node modules, cache the found connections
const importFilePath = moduleResolution.resolvedModule?.resolvedFileName ?? importReference.fileName
// No need for this anymore?
Expand Down

0 comments on commit 961a960

Please sign in to comment.