diff --git a/internal/binder/binder.go b/internal/binder/binder.go index e767cb20d9..5f18fa716a 100644 --- a/internal/binder/binder.go +++ b/internal/binder/binder.go @@ -797,18 +797,18 @@ func (b *Binder) bindModuleDeclaration(node *ast.Node) { if ast.IsModuleAugmentationExternal(node) { b.declareModuleSymbol(node) } else { - var pattern core.Pattern name := node.AsModuleDeclaration().Name() + symbol := b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsValueModule, ast.SymbolFlagsValueModuleExcludes) + if ast.IsStringLiteral(name) { - pattern = core.TryParsePattern(name.AsStringLiteral().Text) + pattern := core.TryParsePattern(name.AsStringLiteral().Text) if !pattern.IsValid() { + // An invalid pattern - must have multiple wildcards. b.errorOnFirstToken(name, diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, name.AsStringLiteral().Text) + } else if pattern.StarIndex >= 0 { + b.file.PatternAmbientModules = append(b.file.PatternAmbientModules, &ast.PatternAmbientModule{Pattern: pattern, Symbol: symbol}) } } - symbol := b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsValueModule, ast.SymbolFlagsValueModuleExcludes) - if pattern.StarIndex >= 0 { - b.file.PatternAmbientModules = append(b.file.PatternAmbientModules, &ast.PatternAmbientModule{Pattern: pattern, Symbol: symbol}) - } } } else { state := b.declareModuleSymbol(node) diff --git a/testdata/baselines/reference/compiler/invalidGlobalAugmentation.errors.txt b/testdata/baselines/reference/compiler/invalidGlobalAugmentation.errors.txt new file mode 100644 index 0000000000..6b5c497a6e --- /dev/null +++ b/testdata/baselines/reference/compiler/invalidGlobalAugmentation.errors.txt @@ -0,0 +1,19 @@ +globals.ts(1,9): error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations. +react-native.ts(2,16): error TS2664: Invalid module name in augmentation, module 'react-native' cannot be found. + + +==== globals.ts (1 errors) ==== + declare global { + ~~~~~~ +!!! error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations. + const __FOO__: any; + } + +==== react-native.ts (1 errors) ==== + export {} + declare module "react-native" { + ~~~~~~~~~~~~~~ +!!! error TS2664: Invalid module name in augmentation, module 'react-native' cannot be found. + const __FOO__: any; + } + \ No newline at end of file diff --git a/testdata/tests/cases/compiler/invalidGlobalAugmentation.ts b/testdata/tests/cases/compiler/invalidGlobalAugmentation.ts new file mode 100644 index 0000000000..c12d229740 --- /dev/null +++ b/testdata/tests/cases/compiler/invalidGlobalAugmentation.ts @@ -0,0 +1,13 @@ +// @noTypesAndSymbols: true +// @noEmit: true + +// @Filename: globals.ts +declare global { + const __FOO__: any; +} + +// @Filename: react-native.ts +export {} +declare module "react-native" { + const __FOO__: any; +}