Skip to content

Commit bb7f7fb

Browse files
committed
Correct the condition for variable statement emit in the declaraiton file
1 parent cd14e36 commit bb7f7fb

7 files changed

Lines changed: 99 additions & 36 deletions

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5707,7 +5707,7 @@ module ts {
57075707
}
57085708

57095709
function isReferencedInExportAssignment(node: Declaration): boolean {
5710-
var exportAssignedSymbol = getExportAssignmentSymbol(getSymbolOfNode(node.parent));
5710+
var exportAssignedSymbol = getExportAssignmentSymbol(getSymbolOfNode(getContainerOfModuleElementDeclaration(node)));
57115711
if (exportAssignedSymbol) {
57125712
var symbol = getSymbolOfNode(node);
57135713
if (exportAssignedSymbol === symbol) {

src/compiler/emitter.ts

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,28 +1857,17 @@ module ts {
18571857
}
18581858
}
18591859

1860-
function emitCommaList(nodes: Node[]) {
1860+
function emitCommaList(nodes: Node[], eachNodeEmitFn: (node: Node) => void) {
1861+
var currentWriterPos = writer.getTextPos();
18611862
for (var i = 0, n = nodes.length; i < n; i++) {
1862-
if (i) {
1863+
if (currentWriterPos !== writer.getTextPos()) {
18631864
write(", ");
18641865
}
1865-
emitNode(nodes[i]);
1866+
currentWriterPos = writer.getTextPos();
1867+
eachNodeEmitFn(nodes[i]);
18661868
}
18671869
}
18681870

1869-
function emitCommaSeparatedSymbolToString(nodes: Node[]) {
1870-
if (nodes) {
1871-
for (var i = 0, n = nodes.length; i < n; i++) {
1872-
if (i) {
1873-
write(", ");
1874-
}
1875-
// TODO(shkamat): get the symbol name in the scope for this node
1876-
emitSourceTextOfNode(nodes[i]);
1877-
}
1878-
}
1879-
}
1880-
1881-
18821871
function emitSourceTextOfNode(node: Node) {
18831872
write(getSourceTextOfLocalNode(node));
18841873
}
@@ -1902,7 +1891,7 @@ module ts {
19021891
}
19031892

19041893
// If this node is in external module, check if this is export assigned
1905-
if (node.parent.flags & NodeFlags.ExternalModule) {
1894+
if (getContainerOfModuleElementDeclaration(node).flags & NodeFlags.ExternalModule) {
19061895
return resolver.isReferencedInExportAssignment(node);
19071896
}
19081897

@@ -1915,8 +1904,9 @@ module ts {
19151904
return true;
19161905
}
19171906

1918-
// emit the declaration if this is global source file
1919-
return node.parent.kind === SyntaxKind.SourceFile && !(node.parent.flags & NodeFlags.ExternalModule);
1907+
// emit the declaration if this is in global scope source file
1908+
var moduleDeclaration = getContainerOfModuleElementDeclaration(node);
1909+
return moduleDeclaration.kind === SyntaxKind.SourceFile && !(moduleDeclaration.flags & NodeFlags.ExternalModule);
19201910
}
19211911

19221912
function emitDeclarationFlags(node: Declaration) {
@@ -2013,17 +2003,27 @@ module ts {
20132003
}
20142004

20152005
function emitTypeParameters(typeParameters: TypeParameterDeclaration[]) {
2006+
function emitTypeParameter(node: TypeParameterDeclaration) {
2007+
emitSourceTextOfNode(node.name);
2008+
if (node.constraint) {
2009+
write(" extends ");
2010+
// TODO(shkamat): emit constraint using type
2011+
emitSourceTextOfNode(node.constraint);
2012+
}
2013+
}
2014+
20162015
if (typeParameters) {
20172016
write("<");
2018-
emitCommaSeparatedSymbolToString(typeParameters);
2017+
emitCommaList(typeParameters, emitTypeParameter);
20192018
write(">");
20202019
}
20212020
}
20222021

20232022
function emitHeritageClause(typeReferences: TypeReferenceNode[], isImplementsList: boolean) {
20242023
if (typeReferences) {
20252024
write(isImplementsList ? " implments " : " extends ");
2026-
emitCommaSeparatedSymbolToString(typeReferences);
2025+
// TODO(shkamat): get the symbol name in the scope for this node
2026+
emitCommaList(typeReferences, emitSourceTextOfNode);
20272027
}
20282028
}
20292029

@@ -2083,21 +2083,24 @@ module ts {
20832083
}
20842084

20852085
function emitVariableDeclaration(node: VariableDeclaration) {
2086-
emitSourceTextOfNode(node.name);
2087-
// If optional property emit ?
2088-
if (node.kind === SyntaxKind.Property && (node.flags & NodeFlags.QuestionMark)) {
2089-
write("?");
2090-
}
2091-
if (!(node.flags & NodeFlags.Private)) {
2092-
// TODO(shkamat): emit type of the node in given scope
2086+
if (node.kind !== SyntaxKind.VariableDeclaration || canEmitModuleElementDeclaration(node)) {
2087+
emitSourceTextOfNode(node.name);
2088+
// If optional property emit ?
2089+
if (node.kind === SyntaxKind.Property && (node.flags & NodeFlags.QuestionMark)) {
2090+
write("?");
2091+
}
2092+
if (!(node.flags & NodeFlags.Private)) {
2093+
// TODO(shkamat): emit type of the node in given scope
2094+
}
20932095
}
20942096
}
20952097

20962098
function emitVariableStatement(node: VariableStatement) {
2097-
if (canEmitModuleElementDeclaration(node)) {
2099+
var hasDeclarationWithEmit = forEach(node.declarations, varDeclaration => canEmitModuleElementDeclaration(varDeclaration));
2100+
if (hasDeclarationWithEmit) {
20982101
emitDeclarationFlags(node);
20992102
write("var ");
2100-
emitCommaList(node.declarations);
2103+
emitCommaList(node.declarations, emitVariableDeclaration);
21012104
write(";");
21022105
writeLine();
21032106
}
@@ -2154,7 +2157,7 @@ module ts {
21542157
}
21552158

21562159
// Parameters
2157-
emitCommaList(node.parameters);
2160+
emitCommaList(node.parameters, emitParameterDeclaration);
21582161

21592162
if (node.kind === SyntaxKind.IndexSignature) {
21602163
write("]");
@@ -2186,8 +2189,6 @@ module ts {
21862189

21872190
function emitNode(node: Node) {
21882191
switch (node.kind) {
2189-
case SyntaxKind.Parameter:
2190-
return emitParameterDeclaration(<ParameterDeclaration>node);
21912192
case SyntaxKind.Constructor:
21922193
case SyntaxKind.FunctionDeclaration:
21932194
case SyntaxKind.Method:
@@ -2202,8 +2203,6 @@ module ts {
22022203
return emitAccessorDeclaration(<AccessorDeclaration>node);
22032204
case SyntaxKind.VariableStatement:
22042205
return emitVariableStatement(<VariableStatement>node);
2205-
case SyntaxKind.VariableDeclaration:
2206-
return emitVariableDeclaration(<VariableDeclaration>node);
22072206
case SyntaxKind.Property:
22082207
return emitPropertyDeclaration(<PropertyDeclaration>node);
22092208
case SyntaxKind.InterfaceDeclaration:

src/compiler/parser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ 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+
return node.kind === SyntaxKind.VariableDeclaration ? node.parent.parent : node.parent;
279+
}
280+
276281
enum ParsingContext {
277282
SourceElements, // Elements in source file
278283
ModuleElements, // Elements in module declaration

tests/baselines/reference/declFileImportModuleWithExportAssignment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ declare module m2 {
4545
listen;
4646
}
4747
}
48+
declare var m2;
4849
export = m2;
4950
//// [declFileImportModuleWithExportAssignment_1.d.ts]
5051
export declare var a;

tests/baselines/reference/declareFileExportAssignment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ declare module m2 {
3333
listen;
3434
}
3535
}
36+
declare var m2;
3637
export = m2;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [declareFileExportAssignmentWithVarFromVariableStatement.ts]
2+
module m2 {
3+
export interface connectModule {
4+
(res, req, next): void;
5+
}
6+
export interface connectExport {
7+
use: (mod: connectModule) => connectExport;
8+
listen: (port: number) => void;
9+
}
10+
11+
}
12+
13+
var x = 10, m2: {
14+
(): m2.connectExport;
15+
test1: m2.connectModule;
16+
test2(): m2.connectModule;
17+
};
18+
19+
export = m2;
20+
21+
//// [declareFileExportAssignmentWithVarFromVariableStatement.js]
22+
var x = 10, m2;
23+
module.exports = m2;
24+
25+
26+
//// [declareFileExportAssignmentWithVarFromVariableStatement.d.ts]
27+
declare module m2 {
28+
interface connectModule {
29+
(res, req, next);
30+
}
31+
interface connectExport {
32+
use;
33+
listen;
34+
}
35+
}
36+
declare var m2;
37+
export = m2;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@module: commonjs
2+
// @declaration: true
3+
module m2 {
4+
export interface connectModule {
5+
(res, req, next): void;
6+
}
7+
export interface connectExport {
8+
use: (mod: connectModule) => connectExport;
9+
listen: (port: number) => void;
10+
}
11+
12+
}
13+
14+
var x = 10, m2: {
15+
(): m2.connectExport;
16+
test1: m2.connectModule;
17+
test2(): m2.connectModule;
18+
};
19+
20+
export = m2;

0 commit comments

Comments
 (0)