Skip to content

Add a helper function to resolve module import text to a file #1793

Closed
@mhegazy

Description

@mhegazy

currently resolving a context for the language service requires calling ts.preProcessFile on all files in the context, and following references. resolving triple-slash references is simple, resolving modules is a bit more complicated, as it involves searching in containing directories until a matching .ts file is found.

As a work around, here is the resolution logic:

function resolveImport(moduleName: string, souceFilename: string, fileExists: (filename: string) => boolean): string {
        var searchPath = ts.getDirectoryPath(souceFilename);

        while (true) {
            var filename = ts.normalizePath(ts.combinePaths(searchPath, moduleName));
            if (fileExists(filename + ".ts")) return filename + ".ts";
            if (fileExists(filename + ".d.ts")) return filename + ".d.ts";

            var parentPath = ts.getDirectoryPath(searchPath);
            if (parentPath === searchPath) break;

            searchPath = parentPath;
        }

        return undefined;
    }

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptBy DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions