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

239 files changed

Lines changed: 1445 additions & 1217 deletions

File tree

Some content is hidden

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

src/compiler/checker.ts

Lines changed: 254 additions & 31 deletions
Large diffs are not rendered by default.

src/compiler/emitter.ts

Lines changed: 12 additions & 39 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)