Skip to content

allow augmentation for entities exported via 'export=' #6742

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
merged 2 commits into from
Jan 31, 2016
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
29 changes: 18 additions & 11 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,17 @@ namespace ts {
if (!mainModule) {
return;
}
// if module symbol has already been merged - it is safe to use it.
// otherwise clone it
mainModule = mainModule.flags & SymbolFlags.Merged ? mainModule : cloneSymbol(mainModule);
mergeSymbol(mainModule, moduleAugmentation.symbol);
// obtain item referenced by 'export='
mainModule = resolveExternalModuleSymbol(mainModule);
if (mainModule.flags & SymbolFlags.Namespace) {
// if module symbol has already been merged - it is safe to use it.
// otherwise clone it
mainModule = mainModule.flags & SymbolFlags.Merged ? mainModule : cloneSymbol(mainModule);
mergeSymbol(mainModule, moduleAugmentation.symbol);
}
else {
error(moduleName, Diagnostics.Cannot_augment_module_0_because_it_resolves_to_a_non_module_entity, moduleName.text);
}
}
}

Expand Down Expand Up @@ -891,7 +898,7 @@ namespace ts {
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
}
else if (!exportDefaultSymbol && allowSyntheticDefaultImports) {
return resolveSymbol(moduleSymbol.exports["export="]) || resolveSymbol(moduleSymbol);
return resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol);
}
return exportDefaultSymbol;
}
Expand Down Expand Up @@ -1182,7 +1189,7 @@ namespace ts {
// An external module with an 'export =' declaration resolves to the target of the 'export =' declaration,
// and an external module with no 'export =' declaration resolves to the module itself.
function resolveExternalModuleSymbol(moduleSymbol: Symbol): Symbol {
return moduleSymbol && resolveSymbol(moduleSymbol.exports["export="]) || moduleSymbol;
return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports["export="])) || moduleSymbol;
}

// An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export ='
Expand All @@ -1197,8 +1204,8 @@ namespace ts {
return symbol;
}

function getExportAssignmentSymbol(moduleSymbol: Symbol): Symbol {
return moduleSymbol.exports["export="];
function hasExportAssignmentSymbol(moduleSymbol: Symbol): boolean {
return moduleSymbol.exports["export="] !== undefined;
}

function getExportsOfModuleAsArray(moduleSymbol: Symbol): Symbol[] {
Expand Down Expand Up @@ -14845,7 +14852,7 @@ namespace ts {
else {
// export * from "foo"
const moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier);
if (moduleSymbol && moduleSymbol.exports["export="]) {
if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) {
error(node.moduleSpecifier, Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
}
}
Expand Down Expand Up @@ -15683,7 +15690,7 @@ namespace ts {
return true;
}

const hasExportAssignment = getExportAssignmentSymbol(moduleSymbol) !== undefined;
const hasExportAssignment = hasExportAssignmentSymbol(moduleSymbol);
// if module has export assignment then 'resolveExternalModuleSymbol' will return resolved symbol for export assignment
// otherwise it will return moduleSymbol itself
moduleSymbol = resolveExternalModuleSymbol(moduleSymbol);
Expand Down Expand Up @@ -16042,7 +16049,7 @@ namespace ts {
if (!isExternalOrCommonJsModule(file)) {
mergeSymbolTable(globals, file.locals);
}
if (file.moduleAugmentations) {
if (file.moduleAugmentations.length) {
(augmentations || (augmentations = [])).push(file.moduleAugmentations);
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1819,6 +1819,10 @@
"category": "Error",
"code": 2670
},
"Cannot augment module '{0}' because it resolves to a non-module entity.": {
"category": "Error",
"code": 2671
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/augmentExportEquals1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
tests/cases/compiler/file2.ts(6,16): error TS2671: Cannot augment module './file1' because it resolves to a non-module entity.
tests/cases/compiler/file3.ts(3,8): error TS2503: Cannot find namespace 'x'.


==== tests/cases/compiler/file3.ts (1 errors) ====
import x = require("./file1");
import "./file2";
let a: x.A; // should not work
~
!!! error TS2503: Cannot find namespace 'x'.
==== tests/cases/compiler/file1.ts (0 errors) ====
var x = 1;
export = x;

==== tests/cases/compiler/file2.ts (1 errors) ====

import x = require("./file1");

// augmentation for './file1'
// should error since './file1' does not have namespace meaning
declare module "./file1" {
~~~~~~~~~
!!! error TS2671: Cannot augment module './file1' because it resolves to a non-module entity.
interface A { a }
}

36 changes: 36 additions & 0 deletions tests/baselines/reference/augmentExportEquals1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/compiler/augmentExportEquals1.ts] ////

//// [file1.ts]
var x = 1;
export = x;

//// [file2.ts]

import x = require("./file1");

// augmentation for './file1'
// should error since './file1' does not have namespace meaning
declare module "./file1" {
interface A { a }
}

//// [file3.ts]
import x = require("./file1");
import "./file2";
let a: x.A; // should not work

//// [file1.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var x = 1;
return x;
});
//// [file2.js]
define(["require", "exports"], function (require, exports) {
"use strict";
});
//// [file3.js]
define(["require", "exports", "./file2"], function (require, exports) {
"use strict";
var a; // should not work
});
29 changes: 29 additions & 0 deletions tests/baselines/reference/augmentExportEquals1_1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
tests/cases/compiler/file2.ts(6,16): error TS2671: Cannot augment module 'file1' because it resolves to a non-module entity.
tests/cases/compiler/file3.ts(3,8): error TS2503: Cannot find namespace 'x'.


==== tests/cases/compiler/file3.ts (1 errors) ====
import x = require("file1");
import "file2";
let a: x.A; // should not work
~
!!! error TS2503: Cannot find namespace 'x'.
==== tests/cases/compiler/file1.d.ts (0 errors) ====

declare module "file1" {
var x: number;
export = x;
}

==== tests/cases/compiler/file2.ts (1 errors) ====
/// <reference path="file1.d.ts"/>
import x = require("file1");

// augmentation for 'file1'
// should error since 'file1' does not have namespace meaning
declare module "file1" {
~~~~~~~
!!! error TS2671: Cannot augment module 'file1' because it resolves to a non-module entity.
interface A { a }
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/augmentExportEquals1_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/augmentExportEquals1_1.ts] ////

//// [file1.d.ts]

declare module "file1" {
var x: number;
export = x;
}

//// [file2.ts]
/// <reference path="file1.d.ts"/>
import x = require("file1");

// augmentation for 'file1'
// should error since 'file1' does not have namespace meaning
declare module "file1" {
interface A { a }
}

//// [file3.ts]
import x = require("file1");
import "file2";
let a: x.A; // should not work

//// [file2.js]
define(["require", "exports"], function (require, exports) {
"use strict";
});
//// [file3.js]
define(["require", "exports", "file2"], function (require, exports) {
"use strict";
var a; // should not work
});
25 changes: 25 additions & 0 deletions tests/baselines/reference/augmentExportEquals2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/file2.ts(4,16): error TS2671: Cannot augment module './file1' because it resolves to a non-module entity.
tests/cases/compiler/file3.ts(3,8): error TS2503: Cannot find namespace 'x'.


==== tests/cases/compiler/file3.ts (1 errors) ====
import x = require("./file1");
import "./file2";
let a: x.A; // should not work
~
!!! error TS2503: Cannot find namespace 'x'.
==== tests/cases/compiler/file1.ts (0 errors) ====

function foo() {}
export = foo;

==== tests/cases/compiler/file2.ts (1 errors) ====
import x = require("./file1");

// should error since './file1' does not have namespace meaning
declare module "./file1" {
~~~~~~~~~
!!! error TS2671: Cannot augment module './file1' because it resolves to a non-module entity.
interface A { a }
}

35 changes: 35 additions & 0 deletions tests/baselines/reference/augmentExportEquals2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/augmentExportEquals2.ts] ////

//// [file1.ts]

function foo() {}
export = foo;

//// [file2.ts]
import x = require("./file1");

// should error since './file1' does not have namespace meaning
declare module "./file1" {
interface A { a }
}

//// [file3.ts]
import x = require("./file1");
import "./file2";
let a: x.A; // should not work

//// [file1.js]
define(["require", "exports"], function (require, exports) {
"use strict";
function foo() { }
return foo;
});
//// [file2.js]
define(["require", "exports"], function (require, exports) {
"use strict";
});
//// [file3.js]
define(["require", "exports", "./file2"], function (require, exports) {
"use strict";
var a; // should not work
});
29 changes: 29 additions & 0 deletions tests/baselines/reference/augmentExportEquals2_1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
tests/cases/compiler/file2.ts(6,16): error TS2671: Cannot augment module 'file1' because it resolves to a non-module entity.
tests/cases/compiler/file3.ts(3,8): error TS2503: Cannot find namespace 'x'.


==== tests/cases/compiler/file3.ts (1 errors) ====
import x = require("file1");
import "file2";
let a: x.A; // should not work
~
!!! error TS2503: Cannot find namespace 'x'.
==== tests/cases/compiler/file1.d.ts (0 errors) ====

declare module "file1" {
function foo(): void;
export = foo;
}

==== tests/cases/compiler/file2.ts (1 errors) ====

/// <reference path="file1.d.ts"/>
import x = require("file1");

// should error since './file1' does not have namespace meaning
declare module "file1" {
~~~~~~~
!!! error TS2671: Cannot augment module 'file1' because it resolves to a non-module entity.
interface A { a }
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/augmentExportEquals2_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/augmentExportEquals2_1.ts] ////

//// [file1.d.ts]

declare module "file1" {
function foo(): void;
export = foo;
}

//// [file2.ts]

/// <reference path="file1.d.ts"/>
import x = require("file1");

// should error since './file1' does not have namespace meaning
declare module "file1" {
interface A { a }
}

//// [file3.ts]
import x = require("file1");
import "file2";
let a: x.A; // should not work

//// [file2.js]
define(["require", "exports"], function (require, exports) {
"use strict";
});
//// [file3.js]
define(["require", "exports", "file2"], function (require, exports) {
"use strict";
var a; // should not work
});
31 changes: 31 additions & 0 deletions tests/baselines/reference/augmentExportEquals3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/compiler/file2.ts(6,15): error TS2665: Module augmentation cannot introduce new names in the top level scope.
tests/cases/compiler/file2.ts(7,9): error TS2665: Module augmentation cannot introduce new names in the top level scope.


==== tests/cases/compiler/file1.ts (0 errors) ====

function foo() {}
namespace foo {
export var v = 1;
}
export = foo;

==== tests/cases/compiler/file2.ts (2 errors) ====
import x = require("./file1");
x.b = 1;

// OK - './file1' is a namespace
declare module "./file1" {
interface A { a }
~
!!! error TS2665: Module augmentation cannot introduce new names in the top level scope.
let b: number;
~
!!! error TS2665: Module augmentation cannot introduce new names in the top level scope.
}

==== tests/cases/compiler/file3.ts (0 errors) ====
import * as x from "./file1";
import "./file2";
let a: x.A;
let b = x.b;
Loading