Skip to content

Commit 3aa92f5

Browse files
committed
addressed PR feedback
1 parent 1e11a55 commit 3aa92f5

5 files changed

Lines changed: 42 additions & 30 deletions

File tree

src/compiler/checker.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14246,7 +14246,7 @@ namespace ts {
1424614246
if (checkBody) {
1424714247
// body of ambient external module is always a module block
1424814248
for (const statement of (<ModuleBlock>node.body).statements) {
14249-
checkBodyOfModuleAugmentation(statement, isGlobalAugmentation);
14249+
checkModuleAugmentationElement(statement, isGlobalAugmentation);
1425014250
}
1425114251
}
1425214252
}
@@ -14273,20 +14273,12 @@ namespace ts {
1427314273
checkSourceElement(node.body);
1427414274
}
1427514275

14276-
function checkBodyOfModuleAugmentation(node: Node, isGlobalAugmentation: boolean): void {
14276+
function checkModuleAugmentationElement(node: Node, isGlobalAugmentation: boolean): void {
1427714277
switch (node.kind) {
1427814278
case SyntaxKind.VariableStatement:
1427914279
// error each individual name in variable statement instead of marking the entire variable statement
1428014280
for (const decl of (<VariableStatement>node).declarationList.declarations) {
14281-
if (isBindingPattern(decl.name)) {
14282-
for (const el of (<BindingPattern>decl.name).elements) {
14283-
// mark individual names in binding pattern
14284-
checkBodyOfModuleAugmentation(el, isGlobalAugmentation);
14285-
}
14286-
}
14287-
else {
14288-
checkBodyOfModuleAugmentation(decl, isGlobalAugmentation);
14289-
}
14281+
checkModuleAugmentationElement(decl, isGlobalAugmentation);
1429014282
}
1429114283
break;
1429214284
case SyntaxKind.ExportAssignment:
@@ -14302,7 +14294,23 @@ namespace ts {
1430214294
case SyntaxKind.ImportDeclaration:
1430314295
grammarErrorOnFirstToken(node, Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module);
1430414296
break;
14305-
default:
14297+
case SyntaxKind.BindingElement:
14298+
case SyntaxKind.VariableDeclaration:
14299+
const name = (<VariableDeclaration | BindingElement>node).name;
14300+
if (isBindingPattern(name)) {
14301+
for (const el of name.elements) {
14302+
// mark individual names in binding pattern
14303+
checkModuleAugmentationElement(el, isGlobalAugmentation);
14304+
}
14305+
break;
14306+
}
14307+
// fallthrough
14308+
case SyntaxKind.ClassDeclaration:
14309+
case SyntaxKind.EnumDeclaration:
14310+
case SyntaxKind.FunctionDeclaration:
14311+
case SyntaxKind.InterfaceDeclaration:
14312+
case SyntaxKind.ModuleDeclaration:
14313+
case SyntaxKind.TypeAliasDeclaration:
1430614314
const symbol = getSymbolOfNode(node);
1430714315
if (symbol) {
1430814316
// module augmentations cannot introduce new names on the top level scope of the module

src/compiler/program.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,7 @@ namespace ts {
527527
}
528528

529529
if (resolveModuleNamesWorker) {
530-
const moduleNames: string[] = [];
531-
for (const moduleName of newSourceFile.imports) {
532-
moduleNames.push(moduleName.text);
533-
}
534-
for (const moduleName of newSourceFile.moduleAugmentations) {
535-
moduleNames.push(moduleName.text);
536-
}
530+
const moduleNames = map(concatenate(newSourceFile.imports, newSourceFile.moduleAugmentations), getTextOfLiteral);
537531
const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(newSourceFile.fileName, currentDirectory));
538532
// ensure that module resolution results are still correct
539533
for (let i = 0; i < moduleNames.length; i++) {
@@ -922,6 +916,10 @@ namespace ts {
922916
return a.text === b.text;
923917
}
924918

919+
function getTextOfLiteral(literal: LiteralExpression): string {
920+
return literal.text;
921+
}
922+
925923
function collectExternalModuleReferences(file: SourceFile): void {
926924
if (file.imports) {
927925
return;
@@ -1128,13 +1126,7 @@ namespace ts {
11281126
collectExternalModuleReferences(file);
11291127
if (file.imports.length || file.moduleAugmentations.length) {
11301128
file.resolvedModules = {};
1131-
const moduleNames: string[] = [];
1132-
for (const name of file.imports) {
1133-
moduleNames.push(name.text);
1134-
}
1135-
for (const name of file.moduleAugmentations) {
1136-
moduleNames.push(name.text);
1137-
}
1129+
const moduleNames = map(concatenate(file.imports, file.moduleAugmentations), getTextOfLiteral);
11381130
const resolutions = resolveModuleNamesWorker(moduleNames, getNormalizedAbsolutePath(file.fileName, currentDirectory));
11391131
for (let i = 0; i < moduleNames.length; i++) {
11401132
const resolution = resolutions[i];

tests/baselines/reference/moduleAugmentationDisallowedExtensions.errors.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ tests/cases/compiler/x.ts(8,9): error TS2663: Module augmentation cannot introdu
33
tests/cases/compiler/x.ts(9,11): error TS2663: Module augmentation cannot introduce new names in the top level scope.
44
tests/cases/compiler/x.ts(10,10): error TS2663: Module augmentation cannot introduce new names in the top level scope.
55
tests/cases/compiler/x.ts(10,14): error TS2663: Module augmentation cannot introduce new names in the top level scope.
6+
tests/cases/compiler/x.ts(10,23): error TS2663: Module augmentation cannot introduce new names in the top level scope.
7+
tests/cases/compiler/x.ts(10,38): error TS2663: Module augmentation cannot introduce new names in the top level scope.
8+
tests/cases/compiler/x.ts(10,43): error TS2663: Module augmentation cannot introduce new names in the top level scope.
9+
tests/cases/compiler/x.ts(10,48): error TS2663: Module augmentation cannot introduce new names in the top level scope.
610
tests/cases/compiler/x.ts(11,15): error TS2663: Module augmentation cannot introduce new names in the top level scope.
711
tests/cases/compiler/x.ts(12,15): error TS2663: Module augmentation cannot introduce new names in the top level scope.
812
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
2327

2428
export let a = 1;
2529

26-
==== tests/cases/compiler/x.ts (19 errors) ====
30+
==== tests/cases/compiler/x.ts (23 errors) ====
2731

2832
namespace N1 {
2933
export let x = 1;
@@ -39,10 +43,18 @@ tests/cases/compiler/x.ts(25,5): error TS2664: Exports and export assignments ar
3943
const z: number;
4044
~
4145
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
42-
let {x1, y1}: {x1: number, y1: string}
46+
let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} }
4347
~~
4448
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
4549
~~
50+
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
51+
~
52+
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
53+
~~~
54+
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
55+
~~~
56+
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
57+
~~~
4658
!!! error TS2663: Module augmentation cannot introduce new names in the top level scope.
4759
interface A { x }
4860
~

tests/baselines/reference/moduleAugmentationDisallowedExtensions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare module "./observable" {
1414
var x: number;
1515
let y: number;
1616
const z: number;
17-
let {x1, y1}: {x1: number, y1: string}
17+
let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} }
1818
interface A { x }
1919
namespace N {
2020
export class C {}

tests/cases/compiler/moduleAugmentationDisallowedExtensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare module "./observable" {
1313
var x: number;
1414
let y: number;
1515
const z: number;
16-
let {x1, y1}: {x1: number, y1: string}
16+
let {x1, y1, z0: {n}, z1: {arr: [el1, el2, el3]}}: {x1: number, y1: string, z0: {n: number}, z1: {arr: number[]} }
1717
interface A { x }
1818
namespace N {
1919
export class C {}

0 commit comments

Comments
 (0)