-
-
Notifications
You must be signed in to change notification settings - Fork 367
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
feat(language-service): drag and drop import respects tsconfig path aliases #4184
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74c3e85
Pre-work for #4125
johnsoncodehk 1a591d6
Merge branch 'master' into tsconfig-for-drag-and-drop
johnsoncodehk 044b8b6
Supports tsconfg paths option
johnsoncodehk 7be7f53
Respect `importModuleSpecifier`
johnsoncodehk 4f4f0bf
Update getImportPathForFile.ts
johnsoncodehk File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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
66 changes: 66 additions & 0 deletions
66
packages/typescript-plugin/lib/requests/getImportPathForFile.ts
This file contains 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,66 @@ | ||
import type * as ts from 'typescript'; | ||
|
||
const enum ExportKind { | ||
Named, | ||
Default, | ||
ExportEquals, | ||
UMD, | ||
} | ||
|
||
interface SymbolExportInfo { | ||
readonly symbol: Symbol; | ||
readonly moduleSymbol: Symbol; | ||
/** Set if `moduleSymbol` is an external module, not an ambient module */ | ||
moduleFileName: string | undefined; | ||
exportKind: ExportKind; | ||
targetFlags: ts.SymbolFlags; | ||
/** True if export was only found via the package.json AutoImportProvider (for telemetry). */ | ||
isFromPackageJson: boolean; | ||
} | ||
|
||
export function getImportPathForFile( | ||
this: { | ||
typescript: typeof import('typescript'); | ||
languageService: ts.LanguageService; | ||
languageServiceHost: ts.LanguageServiceHost; | ||
}, | ||
fileName: string, | ||
incomingFileName: string, | ||
preferences: ts.UserPreferences, | ||
) { | ||
const { typescript: ts, languageService, languageServiceHost } = this; | ||
const { createImportSpecifierResolver }: { | ||
createImportSpecifierResolver( | ||
importingFile: ts.SourceFile, | ||
program: ts.Program, | ||
host: ts.LanguageServiceHost, | ||
preferences: ts.UserPreferences | ||
): { | ||
getModuleSpecifierForBestExportInfo( | ||
exportInfo: readonly SymbolExportInfo[], | ||
symbolName: string, | ||
position: number, | ||
isValidTypeOnlyUseSite: boolean, | ||
fromCacheOnly?: boolean | ||
): { exportInfo?: SymbolExportInfo, moduleSpecifier: string, computedWithoutCacheCount: number; } | undefined; | ||
}; | ||
} = (ts as any).codefix; | ||
const program = languageService.getProgram(); | ||
const sourceFile = program?.getSourceFile(fileName); | ||
const incomingFile = program?.getSourceFile(incomingFileName); | ||
if (!program || !sourceFile || !incomingFile) { | ||
return; | ||
} | ||
const importSpecifierResolver = createImportSpecifierResolver(sourceFile, program, languageServiceHost, preferences); | ||
const info = importSpecifierResolver.getModuleSpecifierForBestExportInfo([{ | ||
// @ts-expect-error | ||
symbol: incomingFile.symbol, | ||
// @ts-expect-error | ||
moduleSymbol: incomingFile.symbol, | ||
moduleFileName: incomingFileName, | ||
exportKind: ExportKind.Default, | ||
isFromPackageJson: false, | ||
targetFlags: 0, | ||
}], 'default', 0, false); | ||
return info?.moduleSpecifier; | ||
} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
How does it work? 🧐
Will
typescript
secretly export some internal services?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.
Yes, some APIs are not exposed in dts.