MCPcopy Index your code
hub / github.com/microsoft/typescript-go / resolveExternalModule

Method resolveExternalModule

internal/checker/checker.go:15067–15350  ·  view source on GitHub ↗
(location *ast.Node, moduleReference string, moduleNotFoundError *diagnostics.Message, errorNode *ast.Node, isForAugmentation bool)

Source from the content-addressed store, hash-verified

15065}
15066
15067func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference string, moduleNotFoundError *diagnostics.Message, errorNode *ast.Node, isForAugmentation bool) *ast.Symbol {
15068 if errorNode != nil && strings.HasPrefix(moduleReference, "@types/") {
15069 withoutAtTypePrefix := moduleReference[len("@types/"):]
15070 c.error(errorNode, diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1, withoutAtTypePrefix, moduleReference)
15071 }
15072 ambientModule := c.tryFindAmbientModule(moduleReference, true /*withAugmentations*/)
15073 if ambientModule != nil {
15074 return ambientModule
15075 }
15076
15077 importingSourceFile := ast.GetSourceFileOfNode(location)
15078 var (
15079 contextSpecifier *ast.Node
15080 mode core.ResolutionMode
15081 )
15082
15083 if ast.IsStringLiteralLike(location) || location.Parent != nil && ast.IsModuleDeclaration(location.Parent) && location.Parent.AsModuleDeclaration().Name() == location {
15084 contextSpecifier = location
15085 } else if ast.IsModuleDeclaration(location) {
15086 contextSpecifier = location.AsModuleDeclaration().Name()
15087 } else if ast.IsLiteralImportTypeNode(location) {
15088 contextSpecifier = location.AsImportTypeNode().Argument.AsLiteralTypeNode().Literal
15089 } else if ast.IsVariableDeclarationInitializedToBareOrAccessedRequire(location) {
15090 contextSpecifier = ast.GetModuleSpecifierOfBareOrAccessedRequire(location)
15091 } else {
15092 ancestor := ast.FindAncestor(location, ast.IsImportCall)
15093 if ancestor != nil {
15094 contextSpecifier = ancestor.Arguments()[0]
15095 }
15096
15097 if ancestor == nil {
15098 ancestor = ast.FindAncestor(location, ast.IsImportDeclarationOrJSImportDeclaration)
15099 if ancestor != nil {
15100 contextSpecifier = ancestor.ModuleSpecifier()
15101 }
15102 }
15103 if ancestor == nil {
15104 ancestor = ast.FindAncestor(location, ast.IsExportDeclaration)
15105 if ancestor != nil {
15106 contextSpecifier = ancestor.ModuleSpecifier()
15107 }
15108 }
15109 if ancestor == nil {
15110 ancestor = ast.FindAncestor(location, ast.IsImportEqualsDeclaration)
15111 if ancestor != nil {
15112 if moduleRefrence := ancestor.AsImportEqualsDeclaration().ModuleReference; moduleRefrence.Kind == ast.KindExternalModuleReference {
15113 contextSpecifier = moduleRefrence.Expression()
15114 }
15115 }
15116 }
15117 }
15118
15119 if contextSpecifier != nil && ast.IsStringLiteralLike(contextSpecifier) {
15120 mode = c.program.GetModeForUsageLocation(importingSourceFile, contextSpecifier)
15121 } else {
15122 mode = c.program.GetDefaultResolutionModeForFile(importingSourceFile)
15123 }
15124

Calls 15

errorMethod · 0.95
tryFindAmbientModuleMethod · 0.95
addDiagnosticMethod · 0.95
getMergedSymbolMethod · 0.95
GetSourceFileOfNodeFunction · 0.92
IsStringLiteralLikeFunction · 0.92
IsModuleDeclarationFunction · 0.92

Tested by

no test coverage detected