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

Method checkModuleDeclaration

internal/checker/checker.go:5113–5201  ·  view source on GitHub ↗
(node *ast.Node)

Source from the content-addressed store, hash-verified

5111}
5112
5113func (c *Checker) checkModuleDeclaration(node *ast.Node) {
5114 if body := node.Body(); body != nil {
5115 c.checkSourceElement(body)
5116 if !ast.IsGlobalScopeAugmentation(node) {
5117 c.registerForUnusedIdentifiersCheck(node)
5118 }
5119 }
5120 isGlobalAugmentation := ast.IsGlobalScopeAugmentation(node)
5121 inAmbientContext := node.Flags&ast.NodeFlagsAmbient != 0
5122 if isGlobalAugmentation && !inAmbientContext {
5123 c.error(node.Name(), diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context)
5124 }
5125 isAmbientExternalModule := ast.IsAmbientModule(node)
5126 contextErrorMessage := core.IfElse(isAmbientExternalModule,
5127 diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file,
5128 diagnostics.A_namespace_declaration_is_only_allowed_at_the_top_level_of_a_namespace_or_module)
5129 if c.checkGrammarModuleElementContext(node, contextErrorMessage) {
5130 // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors.
5131 return
5132 }
5133 if !c.checkGrammarModifiers(node) {
5134 if !inAmbientContext && ast.IsStringLiteral(node.Name()) {
5135 c.grammarErrorOnNode(node.Name(), diagnostics.Only_ambient_modules_can_use_quoted_names)
5136 }
5137 }
5138 if ast.IsIdentifier(node.Name()) {
5139 c.checkCollisionsForDeclarationName(node, node.Name())
5140 if node.AsModuleDeclaration().Keyword == ast.KindModuleKeyword {
5141 c.error(node.Name(), diagnostics.A_namespace_declaration_should_not_be_declared_using_the_module_keyword_Please_use_the_namespace_keyword_instead)
5142 }
5143 }
5144 c.checkExportsOnMergedDeclarations(node)
5145 symbol := c.getSymbolOfDeclaration(node)
5146 // The following checks only apply on a non-ambient instantiated module declaration.
5147 if symbol.Flags&ast.SymbolFlagsValueModule != 0 && !inAmbientContext && isInstantiatedModule(node, c.compilerOptions.ShouldPreserveConstEnums()) {
5148 if c.shouldCheckErasableSyntax(node) {
5149 c.error(node, diagnostics.This_syntax_is_not_allowed_when_erasableSyntaxOnly_is_enabled)
5150 }
5151 if c.compilerOptions.GetIsolatedModules() && ast.GetSourceFileOfNode(node).ExternalModuleIndicator == nil {
5152 // This could be loosened a little if needed. The only problem we are trying to avoid is unqualified
5153 // references to namespace members declared in other files. But use of namespaces is discouraged anyway,
5154 // so for now we will just not allow them in scripts, which is the only place they can merge cross-file.
5155 c.error(node.Name(), diagnostics.Namespaces_are_not_allowed_in_global_script_files_when_0_is_enabled_If_this_file_is_not_intended_to_be_a_global_script_set_moduleDetection_to_force_or_add_an_empty_export_statement, c.getIsolatedModulesLikeFlagName())
5156 }
5157 if len(symbol.Declarations) > 1 {
5158 firstNonAmbientClassOrFunc := getFirstNonAmbientClassOrFunctionDeclaration(symbol)
5159 if firstNonAmbientClassOrFunc != nil {
5160 if ast.GetSourceFileOfNode(node) != ast.GetSourceFileOfNode(firstNonAmbientClassOrFunc) {
5161 c.error(node.Name(), diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged)
5162 } else if node.Pos() < firstNonAmbientClassOrFunc.Pos() {
5163 c.error(node.Name(), diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged)
5164 }
5165 }
5166 }
5167 if c.compilerOptions.VerbatimModuleSyntax.IsTrue() && ast.IsSourceFile(node.Parent) && node.ModifierFlags()&ast.ModifierFlagsExport != 0 && c.program.GetEmitModuleFormatOfFile(node.Parent.AsSourceFile()) == core.ModuleKindCommonJS {
5168 exportModifier := core.Find(node.ModifierNodes(), func(m *ast.Node) bool { return m.Kind == ast.KindExportKeyword })
5169 c.error(exportModifier, diagnostics.A_top_level_export_modifier_cannot_be_used_on_value_declarations_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled)
5170 }

Callers 1

Tested by

no test coverage detected