Skip to content

Commit 5e593ac

Browse files
author
Andy
authored
Avoid cast by providing type predicate to isExternalModuleAugmentation (#22119)
* Avoid cast by providing type predicate to isExternalModuleAugmentation * Break into isExternalModuleAugmentation and isModuleAugmentationExternal
1 parent bb6bd45 commit 5e593ac

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ namespace ts {
16021602
if (hasModifier(node, ModifierFlags.Export)) {
16031603
errorOnFirstToken(node, Diagnostics.export_modifier_cannot_be_applied_to_ambient_modules_and_module_augmentations_since_they_are_always_visible);
16041604
}
1605-
if (isExternalModuleAugmentation(node)) {
1605+
if (isModuleAugmentationExternal(node)) {
16061606
declareModuleSymbol(node);
16071607
}
16081608
else {
@@ -1618,10 +1618,7 @@ namespace ts {
16181618
}
16191619

16201620
const symbol = declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
1621-
1622-
if (pattern) {
1623-
(file.patternAmbientModules || (file.patternAmbientModules = [])).push({ pattern, symbol });
1624-
}
1621+
file.patternAmbientModules = append(file.patternAmbientModules, pattern && { pattern, symbol });
16251622
}
16261623
}
16271624
else {

src/compiler/checker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23771,8 +23771,7 @@ namespace ts {
2377123771
// - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module).
2377223772
const checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & SymbolFlags.Transient);
2377323773
if (checkBody && node.body) {
23774-
// body of ambient external module is always a module block
23775-
for (const statement of (<ModuleBlock>node.body).statements) {
23774+
for (const statement of node.body.statements) {
2377623775
checkModuleAugmentationElement(statement, isGlobalAugmentation);
2377723776
}
2377823777
}

src/compiler/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,9 @@ namespace ts {
20932093

20942094
export type ModuleBody = NamespaceBody | JSDocNamespaceBody;
20952095

2096+
/* @internal */
2097+
export interface AmbientModuleDeclaration extends ModuleDeclaration { body?: ModuleBlock; }
2098+
20962099
export interface ModuleDeclaration extends DeclarationStatement, JSDocContainer {
20972100
kind: SyntaxKind.ModuleDeclaration;
20982101
parent?: ModuleBody | SourceFile;

src/compiler/utilities.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,8 @@ namespace ts {
424424
return node.kind === SyntaxKind.VariableDeclaration && node.parent.kind === SyntaxKind.CatchClause;
425425
}
426426

427-
export function isAmbientModule(node: Node): boolean {
428-
return node && isModuleDeclaration(node) &&
429-
(node.name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
427+
export function isAmbientModule(node: Node): node is AmbientModuleDeclaration {
428+
return isModuleDeclaration(node) && (node.name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
430429
}
431430

432431
export function isModuleWithStringLiteralName(node: Node): node is ModuleDeclaration {
@@ -457,18 +456,19 @@ namespace ts {
457456
return !!(module.flags & NodeFlags.GlobalAugmentation);
458457
}
459458

460-
export function isExternalModuleAugmentation(node: Node): boolean {
459+
export function isExternalModuleAugmentation(node: Node): node is AmbientModuleDeclaration {
460+
return isAmbientModule(node) && isModuleAugmentationExternal(node);
461+
}
462+
463+
export function isModuleAugmentationExternal(node: AmbientModuleDeclaration) {
461464
// external module augmentation is a ambient module declaration that is either:
462465
// - defined in the top level scope and source file is an external module
463466
// - defined inside ambient module declaration located in the top level scope and source file not an external module
464-
if (!node || !isAmbientModule(node)) {
465-
return false;
466-
}
467467
switch (node.parent.kind) {
468468
case SyntaxKind.SourceFile:
469-
return isExternalModule(<SourceFile>node.parent);
469+
return isExternalModule(node.parent);
470470
case SyntaxKind.ModuleBlock:
471-
return isAmbientModule(node.parent.parent) && !isExternalModule(<SourceFile>node.parent.parent.parent);
471+
return isAmbientModule(node.parent.parent) && isSourceFile(node.parent.parent.parent) && !isExternalModule(node.parent.parent.parent);
472472
}
473473
return false;
474474
}

src/services/importTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace ts.FindAllReferences {
6363
// Module augmentations may use this module's exports without importing it.
6464
for (const decl of exportingModuleSymbol.declarations) {
6565
if (isExternalModuleAugmentation(decl)) {
66-
addIndirectUser(decl as SourceFileLike);
66+
addIndirectUser(decl);
6767
}
6868
}
6969

0 commit comments

Comments
 (0)