Ability to resolve import path to SourceFile
- Dominant language
- TypeScript
- Stars
- 6.2k
- Forks
- 238
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
Hi, I'd like to ask if there's an easy way to resolve an arbitrary string coming from outside a project's source code as if it were an import source specifier within the project. Specifically, let's say I have a project with the following structure:
```
src/
index.ts
foo.ts
bar/
baz.ts
node_modules/
@types/
express/
index.d.ts
tsconfig.json
```
Also let's say that the `tsconfig.json` file contains `moduleResolution: "node"` in the `compilerOptions`. The TypeScript compiler is then able to resolve, for example, `import { Baz } from './bar/baz';` in the `index.ts` file to the full path to the `src/bar/baz.ts` file; similarly it is able to resolve `import { Application } from 'express';` to the full path to `node_modules/@types/express/index.d.ts`.
What I'd like to do is create a `new Project({ tsConfigFilePath: '/path/to/tsconfig.json' })` and then resolve an arbitrary module to an actual `SourceFile` so that I can get declarations of a specific export within that file - so I want to be able to do something like `project.resolve('./bar/baz').getExportedDeclarations().get('Baz')`, or `project.resolve('express').getExportedDeclarations().get('Application')`. I know module resolution needs to know where the module is being resolved _from_ in order to resolve relative paths, so maybe it would have to be `project.resolve('./bar/baz', '.')`, but I can't find anything even remotely resembling this. The best I got was `project.getModuleResolutionHost().realpath()`, but that might just be a helper to get the _filesystem_ real path for a symlink and it relies on the current working directory for relative paths anyway, which just doesn't seem like it's the thing I'm looking for..
Is there anything like that?
Thanks for the amazing library by the way @dsherret, you rock!
Contributor guide
Research direction
Start with new Project({ tsConfigFilePath }) and the getModuleResolutionHost().realpath() path, then compare how TypeScript resolves './bar/baz' and 'express' under tsconfig.json's moduleResolution: "node". Done means an arbitrary specifier can resolve from an appropriate source location to a SourceFile whose getExportedDeclarations() exposes Baz or Application.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100