Support tsConfig paths aliases during move operations#1495
Open
tryggvigy wants to merge 1 commit intodsherret:latestfrom
Open
Support tsConfig paths aliases during move operations#1495tryggvigy wants to merge 1 commit intodsherret:latestfrom
tryggvigy wants to merge 1 commit intodsherret:latestfrom
Conversation
tryggvigy
commented
Jan 28, 2024
Comment on lines
+1028
to
+1044
| const paths = compilerOptions?.paths; | ||
| // See https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths | ||
| if (paths) { | ||
| // Check if the string literal is a path alias specifier | ||
| for (const [path, mappings] of Object.entries(paths)) { | ||
| const hasWildCard = path.endsWith('*'); | ||
| const [alias] = path.split('*'); | ||
| if (literalText.startsWith(alias) && hasWildCard) { | ||
| const pathAfterAlias = FileUtils.pathJoin( | ||
| stringLiteral._sourceFile.getDirectoryPath(), | ||
| stringLiteral._sourceFile.getRelativePathAsModuleSpecifierTo(sourceFile) | ||
| ); | ||
| stringLiteral.setLiteralValue(FileUtils.pathJoin(alias, pathAfterAlias)); | ||
| break; | ||
| } | ||
| } | ||
| } |
Author
There was a problem hiding this comment.
This is very naive right now and only works with wildcard paths (most common use-case afaik). If we want to productionize this approach we should create new helpers in ModuleUtils and/or SourceFile to more deeply support paths aliases, instead of reaching directly for FileUtils here.
tryggvigy
commented
Jan 28, 2024
| compilerOptions: { | ||
| baseUrl: '.', | ||
| paths: { | ||
| 'Root/*': ['./*'] |
Author
There was a problem hiding this comment.
If we want to move ahead with this PR we should add a lot more tests for paths.
- With and without wildcard value
- More nested mappings
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Showcases fix for #1494.
Naive support for
tsconfig.compilerOptions.pathsmodule specifier aliases during move operations.