This repository was archived by the owner on Mar 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
feat(typescript): resolve typescript module paths from compilerOptions #258
Open
dherges
wants to merge
1
commit into
angular:master
Choose a base branch
from
dherges:feat-resolve-typescript-module-path
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
typescript/src/services/TsParser/resolveModulePath.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { TsParser } from '.'; | ||
| import { resolveModulePath } from './resolveModulePath'; | ||
| const path = require('canonical-path'); | ||
|
|
||
| describe('resolveModulePath', () => { | ||
| let parser: TsParser; | ||
| let basePath: string; | ||
| beforeEach(() => { | ||
| parser = new TsParser(require('dgeni/lib/mocks/log')(false)); | ||
| basePath = path.resolve(__dirname, '../../mocks'); | ||
| }); | ||
|
|
||
| it('should return the resolved module path derived by compilerOptions.paths', () => { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I add another test w/o |
||
| parser.options.paths = { | ||
| '@foo/bar': ['./tsParser/getExportDocType.test.ts'] | ||
| }; | ||
|
|
||
| const parseInfo = parser.parse(['tsParser/getExportDocType.test.ts'], basePath); | ||
| const moduleSymbol = parseInfo.moduleSymbols[0]; | ||
|
|
||
| expect(resolveModulePath(moduleSymbol, parser.options)).toEqual('@foo/bar'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import * as path from 'path'; | ||
| import { CompilerOptions, Symbol } from 'typescript'; | ||
|
|
||
| /** | ||
| * Resolves the TypeScript module import path derived by the `paths` in `TypeScriptPackagesDef`. | ||
| * | ||
| * Implements a reverse lookup of the TypeScript `compilerOptions.paths` mapping. | ||
| * | ||
| * Example: `'@my/foo: ['./src/index.ts']` is the paths mapping. | ||
| * TODO: add test spec | ||
| */ | ||
| export function resolveModulePath(symbol: Symbol, options: CompilerOptions): string | undefined { | ||
| if (!options.paths) { | ||
| return; | ||
| } | ||
| const paths = options.paths || []; | ||
| const tsModules = Object.keys(paths); | ||
|
|
||
| // Relative file path of this document, e.g. 'a/b/public_api.ts' | ||
| const fileName = symbol.valueDeclaration!.getSourceFile().fileName; | ||
|
|
||
| // Find the TypeScript module path, e.g. '@my/foo' | ||
| const typeScriptModule = tsModules.find(tsModule => paths[tsModule] | ||
| .some(p => path.normalize(p) === fileName)); | ||
|
|
||
| return typeScriptModule; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is adding this property a good way to solve my use case?