diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index de3d8fde307..f94afb3fc57 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -14246,7 +14246,7 @@ namespace ts { if (checkBody) { // body of ambient external module is always a module block for (const statement of (node.body).statements) { - checkBodyOfModuleAugmentation(statement, isGlobalAugmentation); + checkModuleAugmentationElement(statement, isGlobalAugmentation); } } } @@ -14273,20 +14273,12 @@ namespace ts { checkSourceElement(node.body); } - function checkBodyOfModuleAugmentation(node: Node, isGlobalAugmentation: boolean): void { + function checkModuleAugmentationElement(node: Node, isGlobalAugmentation: boolean): void { switch (node.kind) { case SyntaxKind.VariableStatement: // error each individual name in variable statement instead of marking the entire variable statement for (const decl of (node).declarationList.declarations) { - if (isBindingPattern(decl.name)) { - for (const el of (decl.name).elements) { - // mark individual names in binding pattern - checkBodyOfModuleAugmentation(el, isGlobalAugmentation); - } - } - else { - checkBodyOfModuleAugmentation(decl, isGlobalAugmentation); - } + checkModuleAugmentationElement(decl, isGlobalAugmentation); } break; case SyntaxKind.ExportAssignment: @@ -14302,7 +14294,23 @@ namespace ts { case SyntaxKind.ImportDeclaration: grammarErrorOnFirstToken(node, Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module); break; - default: + case SyntaxKind.BindingElement: + case SyntaxKind.VariableDeclaration: + const name = (node).name; + if (isBindingPattern(name)) { + for (const el of name.elements) { + // mark individual names in binding pattern + checkModuleAugmentationElement(el, isGlobalAugmentation); + } + break; + } + // fallthrough + case SyntaxKind.ClassDeclaration: + case SyntaxKind.EnumDeclaration: + case SyntaxKind.FunctionDeclaration: + case SyntaxKind.InterfaceDeclaration: + case SyntaxKind.ModuleDeclaration: + case SyntaxKind.TypeAliasDeclaration: const symbol = getSymbolOfNode(node); if (symbol) { // module augmentations cannot introduce new names on the top level scope of the module diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 80a2a1f342d..52c9fa4ef84 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -527,13 +527,7 @@ namespace ts { } if (resolveModuleNamesWorker) { - const moduleNames: string[] = []; - for (const moduleName of newSourceFile.imports) { - moduleNames.push(moduleName.text); - } - for (const moduleName of newSourceFile.moduleAugmentations) { - moduleNames.push(moduleName.text); - } + const moduleNames = map(concatenate(newSourceFile.imports, newSourceFile.moduleAugmentations), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(newSourceFile.fileName, currentDirectory)); // ensure that module resolution results are still correct for (let i = 0; i < moduleNames.length; i++) { @@ -922,6 +916,10 @@ namespace ts { return a.text === b.text; } + function getTextOfLiteral(literal: LiteralExpression): string { + return literal.text; + } + function collectExternalModuleReferences(file: SourceFile): void { if (file.imports) { return; @@ -1128,13 +1126,7 @@ namespace ts { collectExternalModuleReferences(file); if (file.imports.length || file.moduleAugmentations.length) { file.resolvedModules = {}; - const moduleNames: string[] = []; - for (const name of file.imports) { - moduleNames.push(name.text); - } - for (const name of file.moduleAugmentations) { - moduleNames.push(name.text); - } + const moduleNames = map(concatenate(file.imports, file.moduleAugmentations), getTextOfLiteral); const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory)); for (let i = 0; i < moduleNames.length; i++) { const resolution = resolutions[i]; diff --git a/tests/baselines/reference/moduleAugmentationDisallowedExtensions.errors.txt b/tests/baselines/reference/moduleAugmentationDisallowedExtensions.errors.txt index f90b07dd815..974d20515b6 100644 --- a/tests/baselines/reference/moduleAugmentationDisallowedExtensions.errors.txt +++ b/tests/baselines/reference/moduleAugmentationDisallowedExtensions.errors.txt @@ -3,6 +3,10 @@ tests/cases/compiler/x.ts(8,9): error TS2663: Module augmentation cannot introdu tests/cases/compiler/x.ts(9,11): error TS2663: Module augmentation cannot introduce new names in the top level scope. tests/cases/compiler/x.ts(10,10): error TS2663: Module augmentation cannot introduce new names in the top level scope. tests/cases/compiler/x.ts(10,14): error TS2663: Module augmentation cannot introduce new names in the top level scope. +tests/cases/compiler/x.ts(10,23): error TS2663: Module augmentation cannot introduce new names in the top level scope. +tests/cases/compiler/x.ts(10,38): error TS2663: Module augmentation cannot introduce new names in the top level scope. +tests/cases/compiler/x.ts(10,43): error TS2663: Module augmentation cannot introduce new names in the top level scope. +tests/cases/compiler/x.ts(10,48): error TS2663: Module augmentation cannot introduce new names in the top level scope. tests/cases/compiler/x.ts(11,15): error TS2663: Module augmentation cannot introduce new names in the top level scope. tests/cases/compiler/x.ts(12,15): error TS2663: Module augmentation cannot introduce new names in the top level scope. tests/cases/compiler/x.ts(15,11): error TS2663: Module augmentation cannot introduce new names in the top level scope. @@ -23,7 +27,7 @@ tests/cases/compiler/x.ts(25,5): error TS2664: Exports and export assignments ar export let a = 1; -==== tests/cases/compiler/x.ts (19 errors) ==== +==== tests/cases/compiler/x.ts (23 errors) ==== namespace N1 { export let x = 1; @@ -39,10 +43,18 @@ tests/cases/compiler/x.ts(25,5): error TS2664: Exports and export assignments ar const z: number; ~ !!! error TS2663: Module augmentation cannot introduce new names in the top level scope. - let {x1, y1}: {x1: number, y1: string} + let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} } ~~ !!! error TS2663: Module augmentation cannot introduce new names in the top level scope. ~~ +!!! error TS2663: Module augmentation cannot introduce new names in the top level scope. + ~ +!!! error TS2663: Module augmentation cannot introduce new names in the top level scope. + ~~~ +!!! error TS2663: Module augmentation cannot introduce new names in the top level scope. + ~~~ +!!! error TS2663: Module augmentation cannot introduce new names in the top level scope. + ~~~ !!! error TS2663: Module augmentation cannot introduce new names in the top level scope. interface A { x } ~ diff --git a/tests/baselines/reference/moduleAugmentationDisallowedExtensions.js b/tests/baselines/reference/moduleAugmentationDisallowedExtensions.js index 62b14d71277..8c497d63c42 100644 --- a/tests/baselines/reference/moduleAugmentationDisallowedExtensions.js +++ b/tests/baselines/reference/moduleAugmentationDisallowedExtensions.js @@ -14,7 +14,7 @@ declare module "./observable" { var x: number; let y: number; const z: number; - let {x1, y1}: {x1: number, y1: string} + let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} } interface A { x } namespace N { export class C {} diff --git a/tests/cases/compiler/moduleAugmentationDisallowedExtensions.ts b/tests/cases/compiler/moduleAugmentationDisallowedExtensions.ts index 116c5cb7820..b4b286db502 100644 --- a/tests/cases/compiler/moduleAugmentationDisallowedExtensions.ts +++ b/tests/cases/compiler/moduleAugmentationDisallowedExtensions.ts @@ -13,7 +13,7 @@ declare module "./observable" { var x: number; let y: number; const z: number; - let {x1, y1}: {x1: number, y1: string} + let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} } interface A { x } namespace N { export class C {}