Skip to content

Commit fe1fea3

Browse files
committed
Merge pull request microsoft#30 from Microsoft/declarations
Changes to determine when to qualify the symbol in given enclosing declaration
2 parents 5ae265b + 1bb219a commit fe1fea3

File tree

239 files changed

+1445
-1217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+1445
-1217
lines changed

src/compiler/checker.ts

+254-31
Large diffs are not rendered by default.

src/compiler/emitter.ts

+12-39
Original file line numberDiff line numberDiff line change
@@ -1898,34 +1898,6 @@ module ts {
18981898
writeLine();
18991899
}
19001900

1901-
function isModuleElementExternallyVisible(node: Declaration) {
1902-
if (node.flags & NodeFlags.Export) {
1903-
// Exported member - emit this declaration
1904-
return true;
1905-
}
1906-
1907-
// If this node is in external module, check if this is export assigned
1908-
var moduleDeclaration = getContainerOfModuleElementDeclaration(node);
1909-
if ((moduleDeclaration.flags & NodeFlags.ExternalModule) || // Source file with external module flag
1910-
// Ambient external module declaration
1911-
(moduleDeclaration.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>moduleDeclaration).name.kind === SyntaxKind.StringLiteral)) {
1912-
return resolver.isReferencedInExportAssignment(node);
1913-
}
1914-
1915-
return false;
1916-
}
1917-
1918-
function canEmitModuleElementDeclaration(node: Declaration) {
1919-
if (isModuleElementExternallyVisible(node)) {
1920-
// Either exported module element or is referenced in export assignment
1921-
return true;
1922-
}
1923-
1924-
// emit the declaration if this is in global scope source file
1925-
var moduleDeclaration = getContainerOfModuleElementDeclaration(node);
1926-
return moduleDeclaration.kind === SyntaxKind.SourceFile && !(moduleDeclaration.flags & NodeFlags.ExternalModule);
1927-
}
1928-
19291901
function emitDeclarationFlags(node: Declaration) {
19301902
if (node.flags & NodeFlags.Static) {
19311903
if (node.flags & NodeFlags.Private) {
@@ -1952,8 +1924,7 @@ module ts {
19521924
}
19531925

19541926
function emitImportDeclaration(node: ImportDeclaration) {
1955-
// TODO(shkamat): Emit if import decl is used to declare type in this context
1956-
if (isModuleElementExternallyVisible(node)) {
1927+
if (resolver.isDeclarationVisible(node)) {
19571928
if (node.flags & NodeFlags.Export) {
19581929
write("export ");
19591930
}
@@ -1974,7 +1945,7 @@ module ts {
19741945
}
19751946

19761947
function emitModuleDeclaration(node: ModuleDeclaration) {
1977-
if (canEmitModuleElementDeclaration(node)) {
1948+
if (resolver.isDeclarationVisible(node)) {
19781949
emitDeclarationFlags(node);
19791950
write("module ");
19801951
emitSourceTextOfNode(node.name);
@@ -1997,7 +1968,7 @@ module ts {
19971968
}
19981969

19991970
function emitEnumDeclaration(node: EnumDeclaration) {
2000-
if (canEmitModuleElementDeclaration(node)) {
1971+
if (resolver.isDeclarationVisible(node)) {
20011972
emitDeclarationFlags(node);
20021973
write("enum ");
20031974
emitSourceTextOfNode(node.name);
@@ -2060,7 +2031,7 @@ module ts {
20602031
}
20612032
}
20622033

2063-
if (canEmitModuleElementDeclaration(node)) {
2034+
if (resolver.isDeclarationVisible(node)) {
20642035
emitDeclarationFlags(node);
20652036
write("class ");
20662037
emitSourceTextOfNode(node.name);
@@ -2084,7 +2055,7 @@ module ts {
20842055
}
20852056

20862057
function emitInterfaceDeclaration(node: InterfaceDeclaration) {
2087-
if (canEmitModuleElementDeclaration(node)) {
2058+
if (resolver.isDeclarationVisible(node)) {
20882059
emitDeclarationFlags(node);
20892060
write("interface ");
20902061
emitSourceTextOfNode(node.name);
@@ -2111,8 +2082,9 @@ module ts {
21112082
}
21122083

21132084
function emitVariableDeclaration(node: VariableDeclaration) {
2114-
// If we are emitting property it isnt moduleElement and doesnt need canEmitModuleElement check
2115-
if (node.kind !== SyntaxKind.VariableDeclaration || canEmitModuleElementDeclaration(node)) {
2085+
// If we are emitting property it isnt moduleElement and hence we already know it needs to be emitted
2086+
// so there is no check needed to see if declaration is visible
2087+
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
21162088
emitSourceTextOfNode(node.name);
21172089
// If optional property emit ?
21182090
if (node.kind === SyntaxKind.Property && (node.flags & NodeFlags.QuestionMark)) {
@@ -2126,7 +2098,7 @@ module ts {
21262098
}
21272099

21282100
function emitVariableStatement(node: VariableStatement) {
2129-
var hasDeclarationWithEmit = forEach(node.declarations, varDeclaration => canEmitModuleElementDeclaration(varDeclaration));
2101+
var hasDeclarationWithEmit = forEach(node.declarations, varDeclaration => resolver.isDeclarationVisible(varDeclaration));
21302102
if (hasDeclarationWithEmit) {
21312103
emitDeclarationFlags(node);
21322104
write("var ");
@@ -2151,8 +2123,9 @@ module ts {
21512123
}
21522124

21532125
function emitFunctionDeclaration(node: FunctionDeclaration) {
2154-
// If we are emitting Method/Constructor it isnt moduleElement and doesnt need canEmitModuleElement check
2155-
if ((node.kind !== SyntaxKind.FunctionDeclaration || canEmitModuleElementDeclaration(node)) &&
2126+
// If we are emitting Method/Constructor it isnt moduleElement and hence already determined to be emitting
2127+
// so no need to verify if the declaration is visible
2128+
if ((node.kind !== SyntaxKind.FunctionDeclaration || resolver.isDeclarationVisible(node)) &&
21562129
!resolver.isImplementationOfOverload(node)) {
21572130
emitDeclarationFlags(node);
21582131
if (node.kind === SyntaxKind.FunctionDeclaration) {

src/compiler/parser.ts

-6
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,6 @@ module ts {
273273
return s.parameters.length > 0 && (s.parameters[s.parameters.length - 1].flags & NodeFlags.Rest) !== 0;
274274
}
275275

276-
export function getContainerOfModuleElementDeclaration(node: Declaration) {
277-
// If the declaration is var declaration, then the parent is variable statement but we actually want the module
278-
var container = node.kind === SyntaxKind.VariableDeclaration ? node.parent.parent : node.parent;
279-
return container.kind == SyntaxKind.ModuleBlock ? container.parent : container;
280-
}
281-
282276
enum ParsingContext {
283277
SourceElements, // Elements in source file
284278
ModuleElements, // Elements in module declaration

src/compiler/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ module ts {
621621
getNodeCheckFlags(node: Node): NodeCheckFlags;
622622
getEnumMemberValue(node: EnumMember): number;
623623
shouldEmitDeclarations(): boolean;
624-
isReferencedInExportAssignment(node: Declaration): boolean;
624+
isDeclarationVisible(node: Declaration): boolean;
625625
isImplementationOfOverload(node: FunctionDeclaration): boolean;
626626
writeTypeAtLocation(location: Node, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter): void;
627627
writeReturnTypeOfSignatureDeclaration(signatureDeclaration: SignatureDeclaration, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: TextWriter): void;
@@ -740,6 +740,7 @@ module ts {
740740
flags?: NodeCheckFlags; // Set of flags specific to Node
741741
enumMemberValue?: number; // Constant value of enum member
742742
isIllegalTypeReferenceInConstraint?: boolean; // Is type reference in constraint refers to the type parameter from the same list
743+
isVisible?: boolean; // Is this node visible
743744
}
744745

745746
export enum TypeFlags {

tests/baselines/reference/commentsModules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ declare module m1 {
254254
function foo2Export(a: string): void;
255255
function foo3Export(): void;
256256
}
257-
declare var myvar: c;
257+
declare var myvar: m1.m2.c;
258258
declare module m2.m3 {
259259
class c {
260260
}

tests/baselines/reference/declFileAmbientExternalModuleWithSingleExportedModule.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ declare module "SubModule" {
3232
}
3333
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts]
3434
/// <reference path='declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts' />
35-
export declare var x: c;
35+
export declare var x: SubModule.m.m3.c;

tests/baselines/reference/declFileExportAssignmentImportInternalModule.js

+16
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,21 @@ module.exports = m;
3131

3232

3333
//// [declFileExportAssignmentImportInternalModule.d.ts]
34+
declare module m3 {
35+
module m2 {
36+
interface connectModule {
37+
(res: any, req: any, next: any): void;
38+
}
39+
interface connectExport {
40+
use: (mod: connectModule) => connectExport;
41+
listen: (port: number) => void;
42+
}
43+
}
44+
var server: {
45+
(): m2.connectExport;
46+
test1: m2.connectModule;
47+
test2(): m2.connectModule;
48+
};
49+
}
3450
import m = m3;
3551
export = m;

tests/baselines/reference/declFileExportAssignmentOfGenericInterface.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ interface Foo<T> {
2828
}
2929
export = Foo;
3030
//// [declFileExportAssignmentOfGenericInterface_1.d.ts]
31-
export declare var x: Foo<Foo<string>>;
31+
export declare var x: a<a<string>>;

tests/baselines/reference/declFileExportImportChain.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ export = b;
7474
//// [declFileExportImportChain_c.d.ts]
7575
export import b1 = require("declFileExportImportChain_b1");
7676
//// [declFileExportImportChain_d.d.ts]
77-
export declare var x: c1;
77+
export declare var x: m1.m2.c1;

tests/baselines/reference/declFileExportImportChain2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ export = a;
6565
//// [declFileExportImportChain2_c.d.ts]
6666
export import b = require("declFileExportImportChain2_b");
6767
//// [declFileExportImportChain2_d.d.ts]
68-
export declare var x: c1;
68+
export declare var x: m1.m2.c1;

tests/baselines/reference/declFileGenericType.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ export declare module C {
130130
constructor(val: T);
131131
}
132132
}
133-
export declare var a: A<B>;
134-
export declare var b: <T>(x: T) => A<B>;
135-
export declare var c: <T>(x: T) => A<B>;
136-
export declare var d: <T>(x: T) => A<B>[];
137-
export declare var e: <T extends A<B>>(x: T) => A<B>[];
138-
export declare var x: A<B>;
139-
export declare function f<T extends A<B>>(): void;
140-
export declare var g: A<B>;
141-
export declare class h extends A<B> {
133+
export declare var a: C.A<C.B>;
134+
export declare var b: <T>(x: T) => C.A<C.B>;
135+
export declare var c: <T>(x: T) => C.A<C.B>;
136+
export declare var d: <T>(x: T) => C.A<C.B>[];
137+
export declare var e: <T extends C.A<C.B>>(x: T) => C.A<C.B>[];
138+
export declare var x: C.A<C.B>;
139+
export declare function f<T extends C.A<C.B>>(): void;
140+
export declare var g: C.A<C.B>;
141+
export declare class h extends C.A<C.B> {
142142
}
143-
export interface i extends A<B> {
143+
export interface i extends C.A<C.B> {
144144
}
145-
export declare var j: <T extends A<B>>(x: T) => T;
145+
export declare var j: <T extends C.A<C.B>>(x: T) => T;

tests/baselines/reference/declFileGenericType2.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ declare module templa.mvc {
9999
declare module templa.mvc.composite {
100100
}
101101
declare module templa.dom.mvc {
102-
interface IElementController<ModelType extends IModel> extends IController<ModelType> {
102+
interface IElementController<ModelType extends templa.mvc.IModel> extends templa.mvc.IController<ModelType> {
103103
}
104104
}
105105
declare module templa.dom.mvc {
106-
class AbstractElementController<ModelType extends IModel> extends AbstractController<ModelType> implments IElementController<ModelType> {
106+
class AbstractElementController<ModelType extends templa.mvc.IModel> extends templa.mvc.AbstractController<ModelType> implments IElementController<ModelType> {
107107
constructor();
108108
}
109109
}
110110
declare module templa.dom.mvc.composite {
111-
class AbstractCompositeElementController<ModelType extends ICompositeControllerModel> extends AbstractElementController<ModelType> {
112-
_controllers: IController<IModel>[];
111+
class AbstractCompositeElementController<ModelType extends templa.mvc.composite.ICompositeControllerModel> extends AbstractElementController<ModelType> {
112+
_controllers: templa.mvc.IController<templa.mvc.IModel>[];
113113
constructor();
114114
}
115115
}

tests/baselines/reference/declFileImportModuleWithExportAssignment.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ declare module m2 {
4646
}
4747
}
4848
declare var m2: {
49-
(): connectExport;
50-
test1: connectModule;
51-
test2(): connectModule;
49+
(): m2.connectExport;
50+
test1: m2.connectModule;
51+
test2(): m2.connectModule;
5252
};
5353
export = m2;
5454
//// [declFileImportModuleWithExportAssignment_1.d.ts]
5555
export declare var a: {
56-
(): connectExport;
57-
test1: connectModule;
58-
test2(): connectModule;
56+
(): a1.connectExport;
57+
test1: a1.connectModule;
58+
test2(): a1.connectModule;
5959
};

tests/baselines/reference/declFileImportedTypeUseInTypeArgPosition.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ declare class List<T> {
2727
declare module 'mod1' {
2828
}
2929
declare module 'moo' {
30-
var p: List<Foo>;
30+
var p: List<x.Foo>;
3131
}

tests/baselines/reference/declFileInternalAliases.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ declare module m {
4040
}
4141
}
4242
declare module m1 {
43-
var d: c;
43+
var d: x;
4444
}
4545
declare module m2 {
4646
export import x = m.c;
47-
var d: c;
47+
var d: x;
4848
}

tests/baselines/reference/declFileModuleAssignmentInObjectLiteralProperty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ declare var d: {
3535
m: typeof m1;
3636
};
3737
m2: {
38-
c: typeof c;
38+
c: typeof m1.c;
3939
};
4040
};

tests/baselines/reference/declFileModuleContinuation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ declare module A.C {
3333
}
3434
}
3535
declare module A.B.C {
36-
class W implments Z {
36+
class W implments A.C.Z {
3737
}
3838
}

tests/baselines/reference/declFileTypeofInAnonymousType.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ declare module m1 {
6363
}
6464
}
6565
declare var a: {
66-
c: c;
66+
c: m1.c;
6767
};
6868
declare var b: {
69-
c: typeof c;
69+
c: typeof m1.c;
7070
m1: typeof m1;
7171
};
7272
declare var c: {
@@ -77,10 +77,10 @@ declare var d: {
7777
mod: typeof m1;
7878
};
7979
mc: {
80-
cl: typeof c;
80+
cl: typeof m1.c;
8181
};
8282
me: {
83-
en: typeof e;
83+
en: typeof m1.e;
8484
};
85-
mh: e;
85+
mh: m1.e;
8686
};

tests/baselines/reference/declFileWithClassNameConflictingWithClassReferredByExtendsClause.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ declare module A.B.Base {
7373
}
7474
}
7575
declare module X.Y.base {
76-
class W extends W {
76+
class W extends A.B.Base.W {
7777
name: string;
7878
}
7979
}
8080
declare module X.Y.base.Z {
81-
class W<TValue> extends W {
81+
class W<TValue> extends base.W {
8282
value: boolean;
8383
}
8484
}

tests/baselines/reference/declFileWithInternalModuleNameConflictsInExtendsClause1.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ declare module X.A.C {
3838
}
3939
}
4040
declare module X.A.B.C {
41-
class W implments Z {
41+
class W implments X.A.C.Z {
4242
}
4343
}

tests/baselines/reference/declFileWithInternalModuleNameConflictsInExtendsClause2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare module X.A.C {
4141
}
4242
}
4343
declare module X.A.B.C {
44-
class W implments Z {
44+
class W implments A.C.Z {
4545
}
4646
}
4747
declare module X.A.B.C {

tests/baselines/reference/declFileWithInternalModuleNameConflictsInExtendsClause3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare module X.A.C {
4141
}
4242
}
4343
declare module X.A.B.C {
44-
class W implments Z {
44+
class W implments X.A.C.Z {
4545
}
4646
}
4747
declare module X.A.B.C {

0 commit comments

Comments
 (0)