Skip to content

Commit d7fff8e

Browse files
yuitweswigham
authored andcommitted
[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
1 parent 8f7a582 commit d7fff8e

16 files changed

+183
-9
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,9 @@ namespace ts {
13461346

13471347
emitExpression(node.left);
13481348
increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined);
1349+
emitLeadingCommentsOfPosition(node.operatorToken.pos);
13491350
writeTokenNode(node.operatorToken);
1351+
emitTrailingCommentsOfPosition(node.operatorToken.end);
13501352
increaseIndentIf(indentAfterOperator, " ");
13511353
emitExpression(node.right);
13521354
decreaseIndentIf(indentBeforeOperator, indentAfterOperator);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [commentOnBinaryOperator1.ts]
2+
var a = 'some'
3+
// comment
4+
+ 'text';
5+
6+
var b = 'some'
7+
/* comment */
8+
+ 'text';
9+
10+
var c = 'some'
11+
/* comment */
12+
+ /*comment1*/
13+
'text';
14+
15+
//// [commentOnBinaryOperator1.js]
16+
var a = 'some'
17+
// comment
18+
+ 'text';
19+
var b = 'some'
20+
/* comment */
21+
+ 'text';
22+
var c = 'some'
23+
/* comment */
24+
+/*comment1*/
25+
'text';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/commentOnBinaryOperator1.ts ===
2+
var a = 'some'
3+
>a : Symbol(a, Decl(commentOnBinaryOperator1.ts, 0, 3))
4+
5+
// comment
6+
+ 'text';
7+
8+
var b = 'some'
9+
>b : Symbol(b, Decl(commentOnBinaryOperator1.ts, 4, 3))
10+
11+
/* comment */
12+
+ 'text';
13+
14+
var c = 'some'
15+
>c : Symbol(c, Decl(commentOnBinaryOperator1.ts, 8, 3))
16+
17+
/* comment */
18+
+ /*comment1*/
19+
'text';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/commentOnBinaryOperator1.ts ===
2+
var a = 'some'
3+
>a : string
4+
>'some' // comment + 'text' : string
5+
>'some' : "some"
6+
7+
// comment
8+
+ 'text';
9+
>'text' : "text"
10+
11+
var b = 'some'
12+
>b : string
13+
>'some' /* comment */ + 'text' : string
14+
>'some' : "some"
15+
16+
/* comment */
17+
+ 'text';
18+
>'text' : "text"
19+
20+
var c = 'some'
21+
>c : string
22+
>'some' /* comment */ + /*comment1*/ 'text' : string
23+
>'some' : "some"
24+
25+
/* comment */
26+
+ /*comment1*/
27+
'text';
28+
>'text' : "text"
29+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [commentOnBinaryOperator2.ts]
2+
var a = 'some'
3+
// comment
4+
+ 'text';
5+
6+
var b = 'some'
7+
/* comment */
8+
+ 'text';
9+
10+
var c = 'some'
11+
/* comment */
12+
+ /*comment1*/
13+
'text';
14+
15+
//// [commentOnBinaryOperator2.js]
16+
var a = 'some'
17+
+ 'text';
18+
var b = 'some'
19+
+ 'text';
20+
var c = 'some'
21+
+
22+
'text';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/commentOnBinaryOperator2.ts ===
2+
var a = 'some'
3+
>a : Symbol(a, Decl(commentOnBinaryOperator2.ts, 0, 3))
4+
5+
// comment
6+
+ 'text';
7+
8+
var b = 'some'
9+
>b : Symbol(b, Decl(commentOnBinaryOperator2.ts, 4, 3))
10+
11+
/* comment */
12+
+ 'text';
13+
14+
var c = 'some'
15+
>c : Symbol(c, Decl(commentOnBinaryOperator2.ts, 8, 3))
16+
17+
/* comment */
18+
+ /*comment1*/
19+
'text';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/commentOnBinaryOperator2.ts ===
2+
var a = 'some'
3+
>a : string
4+
>'some' // comment + 'text' : string
5+
>'some' : "some"
6+
7+
// comment
8+
+ 'text';
9+
>'text' : "text"
10+
11+
var b = 'some'
12+
>b : string
13+
>'some' /* comment */ + 'text' : string
14+
>'some' : "some"
15+
16+
/* comment */
17+
+ 'text';
18+
>'text' : "text"
19+
20+
var c = 'some'
21+
>c : string
22+
>'some' /* comment */ + /*comment1*/ 'text' : string
23+
>'some' : "some"
24+
25+
/* comment */
26+
+ /*comment1*/
27+
'text';
28+
>'text' : "text"
29+

tests/baselines/reference/commentsArgumentsOfCallExpression2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ foo(
1414
function foo(/*c1*/ x, /*d1*/ y, /*e1*/ w) { }
1515
var a, b;
1616
foo(/*c2*/ 1, /*d2*/ 1 + 2, /*e1*/ a + b);
17-
foo(/*c3*/ function () { }, /*d2*/ function () { }, /*e2*/ a + b);
17+
foo(/*c3*/ function () { }, /*d2*/ function () { }, /*e2*/ a +/*e3*/ b);
1818
foo(/*c3*/ function () { }, /*d3*/ function () { }, /*e3*/ (a + b));
1919
foo(
2020
/*c4*/ function () { },

tests/baselines/reference/parser15.4.4.14-9-2.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function testcase() {
4141
var one = 1;
4242
var _float = -(4 / 3);
4343
var a = new Array(false, undefined, null, "0", obj, -1.3333333333333, "str", -0, true, +0, one, 1, 0, false, _float, -(4 / 3));
44-
if (a.indexOf(-(4 / 3)) === 14 &&
45-
a.indexOf(0) === 7 &&
46-
a.indexOf(-0) === 7 &&
44+
if (a.indexOf(-(4 / 3)) === 14 &&// a[14]=_float===-(4/3)
45+
a.indexOf(0) === 7 &&// a[7] = +0, 0===+0
46+
a.indexOf(-0) === 7 &&// a[7] = +0, -0===+0
4747
a.indexOf(1) === 10) {
4848
return true;
4949
}

tests/baselines/reference/parserGreaterThanTokenAmbiguity10.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
//// [parserGreaterThanTokenAmbiguity10.js]
88
1
9-
>>>
9+
// before
10+
>>>// after
1011
2;

0 commit comments

Comments
 (0)