Skip to content

Commit 0287d2f

Browse files
Merge pull request microsoft#3185 from Microsoft/variableStatementComments
Don't emit leading/trailing comments for omitted variable statements
2 parents 74f1dd6 + 3664f29 commit 0287d2f

26 files changed

+31
-42
lines changed

src/compiler/emitter.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,9 +2229,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
22292229
emitEmbeddedStatement(node.statement);
22302230
}
22312231

2232-
/* Returns true if start of variable declaration list was emitted.
2233-
* Return false if nothing was written - this can happen for source file level variable declarations
2234-
* in system modules - such variable declarations are hoisted.
2232+
/**
2233+
* Returns true if start of variable declaration list was emitted.
2234+
* Returns false if nothing was written - this can happen for source file level variable declarations
2235+
* in system modules where such variable declarations are hoisted.
22352236
*/
22362237
function tryEmitStartOfVariableDeclarationList(decl: VariableDeclarationList, startPos?: number): boolean {
22372238
if (shouldHoistVariable(decl, /*checkIfSourceFileLevelDecl*/ true)) {
@@ -3060,6 +3061,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
30603061

30613062
function emitVariableStatement(node: VariableStatement) {
30623063
let startIsEmitted = false;
3064+
30633065
if (node.flags & NodeFlags.Export) {
30643066
if (isES6ExportedDeclaration(node)) {
30653067
// Exported ES6 module member
@@ -3070,6 +3072,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
30703072
else {
30713073
startIsEmitted = tryEmitStartOfVariableDeclarationList(node.declarationList);
30723074
}
3075+
30733076
if (startIsEmitted) {
30743077
emitCommaList(node.declarationList.declarations);
30753078
write(";");
@@ -3085,6 +3088,28 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
30853088
}
30863089
}
30873090

3091+
function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node: VariableStatement) {
3092+
// If we're not exporting the variables, there's nothing special here.
3093+
// Always emit comments for these nodes.
3094+
if (!(node.flags & NodeFlags.Export)) {
3095+
return true;
3096+
}
3097+
3098+
// If we are exporting, but it's a top-level ES6 module exports,
3099+
// we'll emit the declaration list verbatim, so emit comments too.
3100+
if (isES6ExportedDeclaration(node)) {
3101+
return true;
3102+
}
3103+
3104+
// Otherwise, only emit if we have at least one initializer present.
3105+
for (let declaration of node.declarationList.declarations) {
3106+
if (declaration.initializer) {
3107+
return true;
3108+
}
3109+
}
3110+
return false;
3111+
}
3112+
30883113
function emitParameter(node: ParameterDeclaration) {
30893114
if (languageVersion < ScriptTarget.ES6) {
30903115
if (isBindingPattern(node.name)) {
@@ -5747,6 +5772,9 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
57475772
case SyntaxKind.ExportAssignment:
57485773
return false;
57495774

5775+
case SyntaxKind.VariableStatement:
5776+
return shouldEmitLeadingAndTrailingCommentsForVariableStatement(<VariableStatement>node);
5777+
57505778
case SyntaxKind.ModuleDeclaration:
57515779
// Only emit the leading/trailing comments for a module if we're actually
57525780
// emitting the module as well.

tests/baselines/reference/commentsBeforeVariableStatement1.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ export var b: number;
55

66
//// [commentsBeforeVariableStatement1.js]
77
define(["require", "exports"], function (require, exports) {
8-
/** b's comment*/
98
});

tests/baselines/reference/commentsExternalModules.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ define(["require", "exports"], function (require, exports) {
6666
/** Module comment*/
6767
var m1;
6868
(function (m1) {
69-
/** b's comment*/
7069
/** foo's comment*/
7170
function foo() {
7271
return m1.b;
@@ -96,7 +95,6 @@ define(["require", "exports"], function (require, exports) {
9695
/** Module comment */
9796
var m4;
9897
(function (m4) {
99-
/** b's comment */
10098
/** foo's comment
10199
*/
102100
function foo() {

tests/baselines/reference/commentsExternalModules2.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ define(["require", "exports"], function (require, exports) {
6666
/** Module comment*/
6767
var m1;
6868
(function (m1) {
69-
/** b's comment*/
7069
/** foo's comment*/
7170
function foo() {
7271
return m1.b;
@@ -96,7 +95,6 @@ define(["require", "exports"], function (require, exports) {
9695
/** Module comment */
9796
var m4;
9897
(function (m4) {
99-
/** b's comment */
10098
/** foo's comment
10199
*/
102100
function foo() {

tests/baselines/reference/commentsExternalModules3.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ export var newVar2 = new extMod.m4.m2.c();
6565
/** Module comment*/
6666
var m1;
6767
(function (m1) {
68-
/** b's comment*/
6968
/** foo's comment*/
7069
function foo() {
7170
return m1.b;
@@ -95,7 +94,6 @@ var myvar = new m1.m2.c();
9594
/** Module comment */
9695
var m4;
9796
(function (m4) {
98-
/** b's comment */
9997
/** foo's comment
10098
*/
10199
function foo() {

tests/baselines/reference/commentsModules.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ new m7.m8.m9.c();
101101
/** Module comment*/
102102
var m1;
103103
(function (m1) {
104-
/** b's comment*/
105104
/** foo's comment*/
106105
function foo() {
107106
return m1.b;

tests/baselines/reference/declFileTypeAnnotationVisibilityErrorTypeAlias.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ var M;
5252
return Window;
5353
})();
5454
N.Window = Window;
55-
// Should report error that W is private
5655
})(N = M.N || (M.N = {}));
5756
})(M || (M = {}));
5857
var M1;
@@ -65,7 +64,6 @@ var M1;
6564
return Window;
6665
})();
6766
N.Window = Window;
68-
// No error
6967
})(N = M1.N || (M1.N = {}));
7068
})(M1 || (M1 = {}));
7169
var M2;

tests/baselines/reference/declFileTypeAnnotationVisibilityErrorTypeLiteral.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ var m;
5959
}
6060
};
6161
m.x3 = m.x;
62-
// Function type
6362
m.y2 = m.y;
64-
// constructor type
6563
m.z2 = m.z;
6664
})(m || (m = {}));

tests/baselines/reference/declarationEmit_nameConflicts3.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ var M;
7474
D[D["f"] = 0] = "f";
7575
})(P.D || (P.D = {}));
7676
var D = P.D;
77-
// ok
7877
P.w = M.D.f; // error, should be typeof M.D.f
7978
P.x = M.C.f; // error, should be typeof M.C.f
8079
P.x = M.E.f; // error, should be typeof M.E.f

tests/baselines/reference/declarationEmit_nameConflictsWithAlias.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export module M {
99
//// [declarationEmit_nameConflictsWithAlias.js]
1010
var M;
1111
(function (M) {
12-
// Gets emitted as C.I, which is the wrong interface
1312
})(M = exports.M || (exports.M = {}));
1413

1514

0 commit comments

Comments
 (0)