Skip to content

Commit e1aee28

Browse files
author
Andy Hanson
committed
Avoid cast by providing type predicate to isExternalModuleAugmentation
1 parent f3ba15b commit e1aee28

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

src/compiler/binder.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,32 +1597,30 @@ namespace ts {
15971597
if (hasModifier(node, ModifierFlags.Export)) {
15981598
errorOnFirstToken(node, Diagnostics.export_modifier_cannot_be_applied_to_ambient_modules_and_module_augmentations_since_they_are_always_visible);
15991599
}
1600+
const { name } = node;
16001601
if (isExternalModuleAugmentation(node)) {
16011602
declareModuleSymbol(node);
16021603
}
16031604
else {
16041605
let pattern: Pattern | undefined;
1605-
if (node.name.kind === SyntaxKind.StringLiteral) {
1606-
const { text } = node.name;
1606+
if (name.kind === SyntaxKind.StringLiteral) {
1607+
const { text } = name;
16071608
if (hasZeroOrOneAsteriskCharacter(text)) {
16081609
pattern = tryParsePattern(text);
16091610
}
16101611
else {
1611-
errorOnFirstToken(node.name, Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, text);
1612+
errorOnFirstToken(name, Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, text);
16121613
}
16131614
}
16141615

16151616
const symbol = declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
1616-
1617-
if (pattern) {
1618-
(file.patternAmbientModules || (file.patternAmbientModules = [])).push({ pattern, symbol });
1619-
}
1617+
file.patternAmbientModules = append(file.patternAmbientModules, pattern && { pattern, symbol });
16201618
}
16211619
}
16221620
else {
16231621
const state = declareModuleSymbol(node);
1622+
const { symbol } = node;
16241623
if (state !== ModuleInstanceState.NonInstantiated) {
1625-
const { symbol } = node;
16261624
// if module was already merged with some function, class or non-const enum, treat it as non-const-enum-only
16271625
symbol.constEnumOnlyModule = (!(symbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.RegularEnum)))
16281626
// Current must be `const enum` only

src/compiler/checker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23659,8 +23659,7 @@ namespace ts {
2365923659
// - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module).
2366023660
const checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & SymbolFlags.Transient);
2366123661
if (checkBody && node.body) {
23662-
// body of ambient external module is always a module block
23663-
for (const statement of (<ModuleBlock>node.body).statements) {
23662+
for (const statement of node.body.statements) {
2366423663
checkModuleAugmentationElement(statement, isGlobalAugmentation);
2366523664
}
2366623665
}

src/compiler/utilities.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@ 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 && node.kind === SyntaxKind.ModuleDeclaration &&
429-
((<ModuleDeclaration>node).name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(<ModuleDeclaration>node));
427+
export interface AmbientModuleDeclaration extends ModuleDeclaration { body?: ModuleBlock; }
428+
export function isAmbientModule(node: Node): node is AmbientModuleDeclaration {
429+
return isModuleDeclaration(node) && (node.name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
430430
}
431431

432432
export function isModuleWithStringLiteralName(node: Node): node is ModuleDeclaration {
@@ -457,18 +457,18 @@ namespace ts {
457457
return !!(module.flags & NodeFlags.GlobalAugmentation);
458458
}
459459

460-
export function isExternalModuleAugmentation(node: Node): boolean {
460+
export function isExternalModuleAugmentation(node: Node): node is AmbientModuleDeclaration {
461461
// external module augmentation is a ambient module declaration that is either:
462462
// - defined in the top level scope and source file is an external module
463463
// - defined inside ambient module declaration located in the top level scope and source file not an external module
464-
if (!node || !isAmbientModule(node)) {
464+
if (!isAmbientModule(node)) {
465465
return false;
466466
}
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)