Skip to content

Fix for dropped/extra comments #7943

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

Closed
wants to merge 4 commits into from
Closed
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
37 changes: 37 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2845,6 +2845,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}
}

function emitEmptyBlockComments(node: Node) {
// special handling for empty blocks
let pos = node.pos;

// skipping leading comments
const comments = getLeadingCommentRanges(currentText, node.pos);

if (comments) {
const lastComment = lastOrUndefined(comments);
if (lastComment) {
pos = lastComment.end;
}
}

pos = currentText.indexOf("{", pos) + 1;
// emitting all comments after '{'
emitTrailingCommentsOfPosition(pos);
emitLeadingCommentsOfPosition(pos);
}

function emitBlock(node: Block) {
if (isSingleLineEmptyBlock(node)) {
emitToken(SyntaxKind.OpenBraceToken, node.pos);
Expand All @@ -2855,6 +2875,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge

emitToken(SyntaxKind.OpenBraceToken, node.pos);
increaseIndent();

if (!!node.statements.length) {
const firstStatementNode = node.statements[0];
emitTrailingCommentsOfPosition(firstStatementNode.pos);
}
else {
emitEmptyBlockComments(node);
}

if (node.kind === SyntaxKind.ModuleBlock) {
Debug.assert(node.parent.kind === SyntaxKind.ModuleDeclaration);
emitCaptureThisForNodeIfNecessary(node.parent);
Expand All @@ -2863,6 +2892,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
if (node.kind === SyntaxKind.ModuleBlock) {
emitTempDeclarations(/*newLine*/ true);
}

if (!!node.statements.length) {
const lastStatementNode = lastOrUndefined(node.statements);
emitLeadingCommentsOfPosition(lastStatementNode.end);
}

decreaseIndent();
writeLine();
emitToken(SyntaxKind.CloseBraceToken, node.statements.end);
Expand Down Expand Up @@ -2894,6 +2929,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
emitToken(SyntaxKind.CloseParenToken, node.expression.end);
emitEmbeddedStatement(node.thenStatement);
if (node.elseStatement) {
emitLeadingCommentsOfPosition(node.thenStatement.end);
writeLine();
emitToken(SyntaxKind.ElseKeyword, node.thenStatement.end);
if (node.elseStatement.kind === SyntaxKind.IfStatement) {
Expand Down Expand Up @@ -4558,6 +4594,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
const { kind, parent } = node;
if (kind !== SyntaxKind.MethodDeclaration &&
kind !== SyntaxKind.MethodSignature &&
kind !== SyntaxKind.ArrowFunction &&
parent &&
parent.kind !== SyntaxKind.PropertyAssignment &&
parent.kind !== SyntaxKind.CallExpression &&
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/amdImportAsPrimaryExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ define(["require", "exports"], function (require, exports) {
define(["require", "exports", "./foo_0"], function (require, exports, foo) {
"use strict";
if (foo.E1.A === 0) {
// Should cause runtime import - interesting optimization possibility, as gets inlined to 0.
}
});
1 change: 1 addition & 0 deletions tests/baselines/reference/argsInScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var C = (function () {
}
C.prototype.P = function (ii, j, k) {
for (var i = 0; i < arguments.length; i++) {
// WScript.Echo("param: " + arguments[i]);
}
};
return C;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/augmentedTypesModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var m1b;
var m1b = 1; // error
var m1c = 1; // Should be allowed
var m1d;
(function (m1d) {
(function (m1d) {// error
var I = (function () {
function I() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var M;
}());
})(M || (M = {}));
var M;
(function (M) {
(function (M) {// Shouldnt be _M
var e = (function () {
function e() {
}
Expand Down Expand Up @@ -110,7 +110,7 @@ var M;
}());
})(M || (M = {}));
var M;
(function (M) {
(function (M) {// Shouldnt be _M
var e = (function () {
function e() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var M;
}());
})(M || (M = {}));
var M;
(function (M) {
(function (M) {// Shouldnt bn _M
var f = (function () {
function f() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var M;
})(m3 || (m3 = {}));
})(M || (M = {}));
var M;
(function (M) {
(function (M) {// shouldnt be _M
var m3;
(function (m3) {
var p = M.x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var f = () => this;
//// [collisionThisExpressionAndModuleInGlobal.js]
var _this = this;
var _this;
(function (_this) {
(function (_this) {//Error
var c = (function () {
function c() {
}
Expand Down
24 changes: 24 additions & 0 deletions tests/baselines/reference/commentOnIfElseStatement1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [commentOnIfElseStatement1.ts]

// 1
if (false) {
}
// 2
else if (!true) {

}
// 3
else {

}

//// [commentOnIfElseStatement1.js]
// 1
if (false) {
}
// 2
else if (!true) {
}
// 3
else {
}
14 changes: 14 additions & 0 deletions tests/baselines/reference/commentOnIfElseStatement1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/commentOnIfElseStatement1.ts ===

No type information for this code.// 1
No type information for this code.if (false) {
No type information for this code.}
No type information for this code.// 2
No type information for this code.else if (!true) {
No type information for this code.
No type information for this code.}
No type information for this code.// 3
No type information for this code.else {
No type information for this code.
No type information for this code.}
No type information for this code.
16 changes: 16 additions & 0 deletions tests/baselines/reference/commentOnIfElseStatement1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/commentOnIfElseStatement1.ts ===

// 1
if (false) {
>false : boolean
}
// 2
else if (!true) {
>!true : boolean
>true : boolean

}
// 3
else {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
var v = {
f: /**own f*/ (a) => 0
}
var w =
/* 1 */ (a) => 0;


//// [commentsBeforeFunctionExpression1.js]
var v = {
f: /**own f*/ function (a) { return 0; }
};
var w =
/* 1 */ function (a) { return 0; };
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ var v = {
>f : Symbol(f, Decl(commentsBeforeFunctionExpression1.ts, 0, 9))
>a : Symbol(a, Decl(commentsBeforeFunctionExpression1.ts, 1, 19))
}
var w =
>w : Symbol(w, Decl(commentsBeforeFunctionExpression1.ts, 3, 3))

/* 1 */ (a) => 0;
>a : Symbol(a, Decl(commentsBeforeFunctionExpression1.ts, 4, 9))

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ var v = {
>a : any
>0 : number
}
var w =
>w : (a: any) => number

/* 1 */ (a) => 0;
>(a) => 0 : (a: any) => number
>a : any
>0 : number

Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ exports.C1 = C1;
"use strict";
var foo = require("./foo_0");
if (foo.C1.s1) {
// Should cause runtime import
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/duplicateAnonymousInners1.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ var Foo;
}
return Helper;
}());
// Inner should not show up in intellisense
// Outer should show up in intellisense
})(Foo || (Foo = {}));
3 changes: 2 additions & 1 deletion tests/baselines/reference/duplicateLocalVariable1.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ var TestRunner = (function () {
exception = true;
testResult = false;
if (typeof testcase.errorMessageRegEx === "string") {
if (testcase.errorMessageRegEx === "") {
if (testcase.errorMessageRegEx === "") {// Any error is fine
testResult = true;
}
else if (e.message) {
Expand All @@ -391,6 +391,7 @@ var TestRunner = (function () {
}
}
if (testResult === false) {
//console.log(e.message);
}
}
if ((testcase.errorMessageRegEx !== undefined) && !exception) {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/duplicateSymbolsExportMatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ define(["require", "exports"], function (require, exports) {
var t;
})(inst || (inst = {}));
var inst;
(function (inst) {
(function (inst) {// one error
var t;
})(inst = M.inst || (M.inst = {}));
})(M || (M = {}));
Expand All @@ -103,7 +103,7 @@ define(["require", "exports"], function (require, exports) {
return C;
}());
var C;
(function (C) {
(function (C) {// Two visibility errors (one for the clodule symbol, and one for the merged container symbol)
var t;
})(C = M.C || (M.C = {}));
})(M || (M = {}));
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/for.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ for () { // error
}

//// [for.js]
for (var i = 0; i < 10; i++) {
for (var i = 0; i < 10; i++) {// ok
var x1 = i;
}
for (var j = 0; j < 10; j++) {
for (var j = 0; j < 10; j++) {// ok
var x2 = j;
}
for (var k = 0; k < 10;) {
for (var k = 0; k < 10;) {// ok
k++;
}
for (; i < 10;) {
for (; i < 10;) {// ok
i++;
}
for (; i > 1; i--) {
for (; i > 1; i--) {// ok
}
for (var l = 0;; l++) {
for (var l = 0;; l++) {// ok
if (l > 10) {
break;
}
}
for (;;) {
for (;;) {// ok
}
for (;;) {
for (;;) {// error
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/forIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ for (var l in arr) {

//// [forIn.js]
var arr = null;
for (var i in arr) {
for (var i in arr) {// error
var x1 = arr[i];
var y1 = arr[i];
}
for (var j in arr) {
for (var j in arr) {// ok
var x2 = arr[j];
var y2 = arr[j];
}
var arr2 = [];
for (j in arr2) {
for (j in arr2) {// ok
var x3 = arr2[j];
var y3 = arr2[j];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/invalidTryStatements2.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function fn2() {
function fn() {
try {
}
catch () {
catch () {// syntax error, missing '(x)'
}
try {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ function foo(a) {
//// [out.js]
function foo(a) {
for (var a_1 = 0; a_1 < 10; a_1++) {
// do something
}
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/moduleDuplicateIdentifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var FooBar;
FooBar.member1 = 2;
})(FooBar = exports.FooBar || (exports.FooBar = {}));
var FooBar;
(function (FooBar) {
(function (FooBar) {// Shouldn't error
FooBar.member2 = 42;
})(FooBar = exports.FooBar || (exports.FooBar = {}));
var Kettle = (function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/nameCollisions.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var T;
(function (T) {
var x = 2;
var x;
(function (x) {
(function (x) {// error
var Bar = (function () {
function Bar() {
}
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/noCatchBlock.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/baselines/reference/noCatchBlock.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading