MCPcopy Create free account
hub / github.com/microsoft/typescript-go / collectModuleReferences

Function collectModuleReferences

internal/parser/references.go:24–71  ·  view source on GitHub ↗
(file *ast.SourceFile, node *ast.Statement, inAmbientModule bool)

Source from the content-addressed store, hash-verified

22}
23
24func collectModuleReferences(file *ast.SourceFile, node *ast.Statement, inAmbientModule bool) {
25 if ast.IsAnyImportOrReExport(node) {
26 moduleNameExpr := ast.GetExternalModuleName(node)
27 // TypeScript 1.0 spec (April 2014): 12.1.6
28 // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference other external modules
29 // only through top - level external module names. Relative external module names are not permitted.
30 if moduleNameExpr != nil && ast.IsStringLiteral(moduleNameExpr) {
31 moduleName := moduleNameExpr.Text()
32 if moduleName != "" && (!inAmbientModule || !tspath.IsExternalModuleNameRelative(moduleName)) {
33 ast.SetImportsOfSourceFile(file, append(file.Imports(), moduleNameExpr))
34 // !!! removed `&& p.currentNodeModulesDepth == 0`
35 if file.UsesUriStyleNodeCoreModules != core.TSTrue && !file.IsDeclarationFile {
36 if strings.HasPrefix(moduleName, "node:") && !core.ExclusivelyPrefixedNodeCoreModules[moduleName] {
37 // Presence of `node:` prefix takes precedence over unprefixed node core modules
38 file.UsesUriStyleNodeCoreModules = core.TSTrue
39 } else if file.UsesUriStyleNodeCoreModules == core.TSUnknown && core.UnprefixedNodeCoreModules[moduleName] {
40 // Avoid `unprefixedNodeCoreModules.has` for every import
41 file.UsesUriStyleNodeCoreModules = core.TSFalse
42 }
43 }
44 }
45 }
46 return
47 }
48 if ast.IsModuleDeclaration(node) && ast.IsAmbientModule(node) && (inAmbientModule || ast.HasSyntacticModifier(node, ast.ModifierFlagsAmbient) || file.IsDeclarationFile) {
49 nameText := node.AsModuleDeclaration().Name().Text()
50 // Ambient module declarations can be interpreted as augmentations for some existing external modules.
51 // This will happen in two cases:
52 // - if current file is external module then module augmentation is a ambient module declaration defined in the top level scope
53 // - if current file is not external module then module augmentation is an ambient module declaration with non-relative module name
54 // immediately nested in top level ambient module declaration .
55 if ast.IsExternalModule(file) || (inAmbientModule && !tspath.IsExternalModuleNameRelative(nameText)) {
56 file.ModuleAugmentations = append(file.ModuleAugmentations, node.AsModuleDeclaration().Name())
57 } else if !inAmbientModule {
58 file.AmbientModuleNames = append(file.AmbientModuleNames, nameText)
59 // An AmbientExternalModuleDeclaration declares an external module.
60 // This type of declaration is permitted only in the global module.
61 // The StringLiteral must specify a top - level external module name.
62 // Relative external module names are not permitted
63 // NOTE: body of ambient module is always a module block, if it exists
64 if node.Body() != nil {
65 for _, statement := range node.Body().Statements() {
66 collectModuleReferences(file, statement, true /*inAmbientModule*/)
67 }
68 }
69 }
70 }
71}

Callers 1

Calls 15

IsAnyImportOrReExportFunction · 0.92
GetExternalModuleNameFunction · 0.92
IsStringLiteralFunction · 0.92
SetImportsOfSourceFileFunction · 0.92
IsModuleDeclarationFunction · 0.92
IsAmbientModuleFunction · 0.92
HasSyntacticModifierFunction · 0.92
IsExternalModuleFunction · 0.92
AsModuleDeclarationMethod · 0.80
BodyMethod · 0.80
StatementsMethod · 0.80

Tested by

no test coverage detected