Skip to content

Commit 5c0668a

Browse files
Check patterns for validity before putting them into pattern ambient modules (#875)
Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 63a2365 commit 5c0668a

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

internal/binder/binder.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -797,18 +797,18 @@ func (b *Binder) bindModuleDeclaration(node *ast.Node) {
797797
if ast.IsModuleAugmentationExternal(node) {
798798
b.declareModuleSymbol(node)
799799
} else {
800-
var pattern core.Pattern
801800
name := node.AsModuleDeclaration().Name()
801+
symbol := b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsValueModule, ast.SymbolFlagsValueModuleExcludes)
802+
802803
if ast.IsStringLiteral(name) {
803-
pattern = core.TryParsePattern(name.AsStringLiteral().Text)
804+
pattern := core.TryParsePattern(name.AsStringLiteral().Text)
804805
if !pattern.IsValid() {
806+
// An invalid pattern - must have multiple wildcards.
805807
b.errorOnFirstToken(name, diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, name.AsStringLiteral().Text)
808+
} else if pattern.StarIndex >= 0 {
809+
b.file.PatternAmbientModules = append(b.file.PatternAmbientModules, &ast.PatternAmbientModule{Pattern: pattern, Symbol: symbol})
806810
}
807811
}
808-
symbol := b.declareSymbolAndAddToSymbolTable(node, ast.SymbolFlagsValueModule, ast.SymbolFlagsValueModuleExcludes)
809-
if pattern.StarIndex >= 0 {
810-
b.file.PatternAmbientModules = append(b.file.PatternAmbientModules, &ast.PatternAmbientModule{Pattern: pattern, Symbol: symbol})
811-
}
812812
}
813813
} else {
814814
state := b.declareModuleSymbol(node)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
globals.ts(1,9): error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
2+
react-native.ts(2,16): error TS2664: Invalid module name in augmentation, module 'react-native' cannot be found.
3+
4+
5+
==== globals.ts (1 errors) ====
6+
declare global {
7+
~~~~~~
8+
!!! error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
9+
const __FOO__: any;
10+
}
11+
12+
==== react-native.ts (1 errors) ====
13+
export {}
14+
declare module "react-native" {
15+
~~~~~~~~~~~~~~
16+
!!! error TS2664: Invalid module name in augmentation, module 'react-native' cannot be found.
17+
const __FOO__: any;
18+
}
19+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @noTypesAndSymbols: true
2+
// @noEmit: true
3+
4+
// @Filename: globals.ts
5+
declare global {
6+
const __FOO__: any;
7+
}
8+
9+
// @Filename: react-native.ts
10+
export {}
11+
declare module "react-native" {
12+
const __FOO__: any;
13+
}

0 commit comments

Comments
 (0)