[Master] fix 12985 emit leading and trailing comment around binary operator (#16584)

* Emit leading and trailing on binary operator

* Add tests and baselines

* Update baselines
This commit is contained in:
Yui 2017-08-04 19:12:13 -07:00 committed by Wesley Wigham
parent 8f7a582fc5
commit d7fff8ebe9
16 changed files with 183 additions and 9 deletions

View file

@ -1346,7 +1346,9 @@ namespace ts {
emitExpression(node.left);
increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined);
emitLeadingCommentsOfPosition(node.operatorToken.pos);
writeTokenNode(node.operatorToken);
emitTrailingCommentsOfPosition(node.operatorToken.end);
increaseIndentIf(indentAfterOperator, " ");
emitExpression(node.right);
decreaseIndentIf(indentBeforeOperator, indentAfterOperator);

View file

@ -0,0 +1,25 @@
//// [commentOnBinaryOperator1.ts]
var a = 'some'
// comment
+ 'text';
var b = 'some'
/* comment */
+ 'text';
var c = 'some'
/* comment */
+ /*comment1*/
'text';
//// [commentOnBinaryOperator1.js]
var a = 'some'
// comment
+ 'text';
var b = 'some'
/* comment */
+ 'text';
var c = 'some'
/* comment */
+/*comment1*/
'text';

View file

@ -0,0 +1,19 @@
=== tests/cases/compiler/commentOnBinaryOperator1.ts ===
var a = 'some'
>a : Symbol(a, Decl(commentOnBinaryOperator1.ts, 0, 3))
// comment
+ 'text';
var b = 'some'
>b : Symbol(b, Decl(commentOnBinaryOperator1.ts, 4, 3))
/* comment */
+ 'text';
var c = 'some'
>c : Symbol(c, Decl(commentOnBinaryOperator1.ts, 8, 3))
/* comment */
+ /*comment1*/
'text';

View file

@ -0,0 +1,29 @@
=== tests/cases/compiler/commentOnBinaryOperator1.ts ===
var a = 'some'
>a : string
>'some' // comment + 'text' : string
>'some' : "some"
// comment
+ 'text';
>'text' : "text"
var b = 'some'
>b : string
>'some' /* comment */ + 'text' : string
>'some' : "some"
/* comment */
+ 'text';
>'text' : "text"
var c = 'some'
>c : string
>'some' /* comment */ + /*comment1*/ 'text' : string
>'some' : "some"
/* comment */
+ /*comment1*/
'text';
>'text' : "text"

View file

@ -0,0 +1,22 @@
//// [commentOnBinaryOperator2.ts]
var a = 'some'
// comment
+ 'text';
var b = 'some'
/* comment */
+ 'text';
var c = 'some'
/* comment */
+ /*comment1*/
'text';
//// [commentOnBinaryOperator2.js]
var a = 'some'
+ 'text';
var b = 'some'
+ 'text';
var c = 'some'
+
'text';

View file

@ -0,0 +1,19 @@
=== tests/cases/compiler/commentOnBinaryOperator2.ts ===
var a = 'some'
>a : Symbol(a, Decl(commentOnBinaryOperator2.ts, 0, 3))
// comment
+ 'text';
var b = 'some'
>b : Symbol(b, Decl(commentOnBinaryOperator2.ts, 4, 3))
/* comment */
+ 'text';
var c = 'some'
>c : Symbol(c, Decl(commentOnBinaryOperator2.ts, 8, 3))
/* comment */
+ /*comment1*/
'text';

View file

@ -0,0 +1,29 @@
=== tests/cases/compiler/commentOnBinaryOperator2.ts ===
var a = 'some'
>a : string
>'some' // comment + 'text' : string
>'some' : "some"
// comment
+ 'text';
>'text' : "text"
var b = 'some'
>b : string
>'some' /* comment */ + 'text' : string
>'some' : "some"
/* comment */
+ 'text';
>'text' : "text"
var c = 'some'
>c : string
>'some' /* comment */ + /*comment1*/ 'text' : string
>'some' : "some"
/* comment */
+ /*comment1*/
'text';
>'text' : "text"

View file

@ -14,7 +14,7 @@ foo(
function foo(/*c1*/ x, /*d1*/ y, /*e1*/ w) { }
var a, b;
foo(/*c2*/ 1, /*d2*/ 1 + 2, /*e1*/ a + b);
foo(/*c3*/ function () { }, /*d2*/ function () { }, /*e2*/ a + b);
foo(/*c3*/ function () { }, /*d2*/ function () { }, /*e2*/ a +/*e3*/ b);
foo(/*c3*/ function () { }, /*d3*/ function () { }, /*e3*/ (a + b));
foo(
/*c4*/ function () { },

View file

@ -41,9 +41,9 @@ function testcase() {
var one = 1;
var _float = -(4 / 3);
var a = new Array(false, undefined, null, "0", obj, -1.3333333333333, "str", -0, true, +0, one, 1, 0, false, _float, -(4 / 3));
if (a.indexOf(-(4 / 3)) === 14 &&
a.indexOf(0) === 7 &&
a.indexOf(-0) === 7 &&
if (a.indexOf(-(4 / 3)) === 14 &&// a[14]=_float===-(4/3)
a.indexOf(0) === 7 &&// a[7] = +0, 0===+0
a.indexOf(-0) === 7 &&// a[7] = +0, -0===+0
a.indexOf(1) === 10) {
return true;
}

View file

@ -6,5 +6,6 @@
//// [parserGreaterThanTokenAmbiguity10.js]
1
>>>
// before
>>>// after
2;

View file

@ -6,5 +6,6 @@
//// [parserGreaterThanTokenAmbiguity15.js]
1
>>=
// before
>>=// after
2;

View file

@ -6,5 +6,6 @@
//// [parserGreaterThanTokenAmbiguity20.js]
1
>>>=
// Before
>>>=// after
2;

View file

@ -6,5 +6,6 @@
//// [parserGreaterThanTokenAmbiguity5.js]
1
>>
// before
>>// after
2;

View file

@ -138,7 +138,7 @@ function foo8(x) {
var b;
return typeof x === "string"
? x === "hello"
: ((b = x) &&
: ((b = x) &&// number | boolean
(typeof x === "boolean"
? x // boolean
: x == 10)); // boolean

View file

@ -0,0 +1,12 @@
var a = 'some'
// comment
+ 'text';
var b = 'some'
/* comment */
+ 'text';
var c = 'some'
/* comment */
+ /*comment1*/
'text';

View file

@ -0,0 +1,13 @@
// @removeComments: true
var a = 'some'
// comment
+ 'text';
var b = 'some'
/* comment */
+ 'text';
var c = 'some'
/* comment */
+ /*comment1*/
'text';