Skip to content

Avoid cast by providing type predicate to isExternalModuleAugmentation #22119

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
3 commits merged into from
Mar 6, 2018
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
7 changes: 2 additions & 5 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ namespace ts {
if (hasModifier(node, ModifierFlags.Export)) {
errorOnFirstToken(node, Diagnostics.export_modifier_cannot_be_applied_to_ambient_modules_and_module_augmentations_since_they_are_always_visible);
}
if (isExternalModuleAugmentation(node)) {
if (isModuleAugmentationExternal(node)) {
declareModuleSymbol(node);
}
else {
Expand All @@ -1618,10 +1618,7 @@ namespace ts {
}

const symbol = declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);

if (pattern) {
(file.patternAmbientModules || (file.patternAmbientModules = [])).push({ pattern, symbol });
}
file.patternAmbientModules = append(file.patternAmbientModules, pattern && { pattern, symbol });
}
}
else {
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23740,8 +23740,7 @@ namespace ts {
// - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module).
const checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & SymbolFlags.Transient);
if (checkBody && node.body) {
// body of ambient external module is always a module block
for (const statement of (<ModuleBlock>node.body).statements) {
for (const statement of node.body.statements) {
checkModuleAugmentationElement(statement, isGlobalAugmentation);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2093,6 +2093,9 @@ namespace ts {

export type ModuleBody = NamespaceBody | JSDocNamespaceBody;

/* @internal */
export interface AmbientModuleDeclaration extends ModuleDeclaration { body?: ModuleBlock; }

export interface ModuleDeclaration extends DeclarationStatement, JSDocContainer {
kind: SyntaxKind.ModuleDeclaration;
parent?: ModuleBody | SourceFile;
Expand Down
18 changes: 9 additions & 9 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ namespace ts {
return node.kind === SyntaxKind.VariableDeclaration && node.parent.kind === SyntaxKind.CatchClause;
}

export function isAmbientModule(node: Node): boolean {
return node && isModuleDeclaration(node) &&
(node.name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
export function isAmbientModule(node: Node): node is AmbientModuleDeclaration {
return isModuleDeclaration(node) && (node.name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node));
}

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

export function isExternalModuleAugmentation(node: Node): boolean {
export function isExternalModuleAugmentation(node: Node): node is AmbientModuleDeclaration {
return isAmbientModule(node) && isModuleAugmentationExternal(node);
}

export function isModuleAugmentationExternal(node: AmbientModuleDeclaration) {
// external module augmentation is a ambient module declaration that is either:
// - defined in the top level scope and source file is an external module
// - defined inside ambient module declaration located in the top level scope and source file not an external module
if (!node || !isAmbientModule(node)) {
return false;
}
switch (node.parent.kind) {
case SyntaxKind.SourceFile:
return isExternalModule(<SourceFile>node.parent);
return isExternalModule(node.parent);
case SyntaxKind.ModuleBlock:
return isAmbientModule(node.parent.parent) && !isExternalModule(<SourceFile>node.parent.parent.parent);
return isAmbientModule(node.parent.parent) && isSourceFile(node.parent.parent.parent) && !isExternalModule(node.parent.parent.parent);
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/importTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace ts.FindAllReferences {
// Module augmentations may use this module's exports without importing it.
for (const decl of exportingModuleSymbol.declarations) {
if (isExternalModuleAugmentation(decl)) {
addIndirectUser(decl as SourceFileLike);
addIndirectUser(decl);
}
}

Expand Down