Skip to content

Check patterns for validity before putting them into pattern ambient modules #875

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

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/binder/binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

13 changes: 13 additions & 0 deletions testdata/tests/cases/compiler/invalidGlobalAugmentation.ts
Original file line number Diff line number Diff line change
@@ -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;
}