Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mhegazy opened this issue Jan 24, 2015 · 5 comments
Closed

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

mhegazy opened this issue Jan 24, 2015 · 5 comments
Assignees
Labels
Bug A bug in TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Jan 24, 2015

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;
    }
@mhegazy mhegazy added the Bug A bug in TypeScript label Jan 24, 2015
@mhegazy mhegazy self-assigned this Jan 24, 2015
@mhegazy mhegazy added this to the TypeScript 1.5 milestone Jan 24, 2015
@mhegazy
Copy link
Contributor Author

mhegazy commented Feb 3, 2015

This should be handled by #1735

@mhegazy mhegazy closed this as completed Feb 3, 2015
@mhegazy mhegazy added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Feb 3, 2015
@spadgos
Copy link

spadgos commented Apr 25, 2017

The wiki links to this page.

Something that is currently missing, and should be added in next release, is a built-in way to resolve imports. ts.preProcessFile returns import text, and the caller needs to resolve that to a .ts file path. Issue #1793 tracks fixing it.

Should that be updated?

@mhegazy
Copy link
Contributor Author

mhegazy commented Apr 27, 2017

Should that be updated?

i think so.

@vivin
Copy link

vivin commented Sep 19, 2017

@mhegazy Is there updated documentation that shows how this can be done? I'm looking to implement some custom module-resolution logic. I have it working with tsc, but I'd like the IDE to be able to resolve these modules as well without them showing up as errors.

@mhegazy
Copy link
Contributor Author

mhegazy commented Sep 19, 2017

@mhegazy Is there updated documentation that shows how this can be done? I'm looking to implement some custom module-resolution logic. I have it working with tsc, but I'd like the IDE to be able to resolve these modules as well without them showing up as errors.

IDE's use tsserver. the only way you can change the behavior there is if you create a language service plugin. you can find more docs about LS plugins in https://github.com/Microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants