Skip to content

Commit

Permalink
Bugfix/ Don't inline pipeline operator with leading ownline comment (#…
Browse files Browse the repository at this point in the history
…5015)

* don't inline pipeline operator with leading ownline comment

* Add tests for comments surrounded with binary expressions

* Add babylon test for ownlinecomment with pipeline operator
  • Loading branch information
Felix Wu authored and j-f1 committed Sep 1, 2018
1 parent db2bc36 commit e86f085
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/language-js/printer-estree.js
Expand Up @@ -5204,7 +5204,9 @@ function printBinaryishExpressions(
}

const shouldInline = shouldInlineLogicalExpression(node);
const lineBeforeOperator = node.operator === "|>";
const lineBeforeOperator =
node.operator === "|>" &&
!hasLeadingOwnLineComment(options.originalText, node.right, options);

const right = shouldInline
? concat([node.operator, " ", path.call(print, "right")])
Expand Down
147 changes: 147 additions & 0 deletions tests/comments/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -35,6 +35,153 @@ const foo = {
`;

exports[`binary-expressions.js - flow-verify 1`] = `
function addition() {
0
// Comment
+ x
}
function multiplication() {
0
// Comment
* x
}
function division() {
0
// Comment
/ x
}
function substraction() {
0
// Comment
- x
}
function remainder() {
0
// Comment
% x
}
function exponentiation() {
0
// Comment
** x
}
function leftShift() {
0
// Comment
<< x
}
function rightShift() {
0
// Comment
>> x
}
function unsignedRightShift() {
0
// Comment
>>> x
}
function bitwiseAnd() {
0
// Comment
& x
}
function bitwiseOr() {
0
// Comment
| x
}
function bitwiseXor() {
0
// Comment
^ x
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function addition() {
0 +
// Comment
x;
}
function multiplication() {
0 *
// Comment
x;
}
function division() {
0 /
// Comment
x;
}
function substraction() {
0 -
// Comment
x;
}
function remainder() {
0 %
// Comment
x;
}
function exponentiation() {
0 **
// Comment
x;
}
function leftShift() {
0 <<
// Comment
x;
}
function rightShift() {
0 >>
// Comment
x;
}
function unsignedRightShift() {
0 >>>
// Comment
x;
}
function bitwiseAnd() {
0 &
// Comment
x;
}
function bitwiseOr() {
0 |
// Comment
x;
}
function bitwiseXor() {
0 ^
// Comment
x;
}
`;

exports[`blank.js - flow-verify 1`] = `
// This file only
// has comments. This comment
Expand Down
71 changes: 71 additions & 0 deletions tests/comments/binary-expressions.js
@@ -0,0 +1,71 @@
function addition() {
0
// Comment
+ x
}

function multiplication() {
0
// Comment
* x
}

function division() {
0
// Comment
/ x
}

function substraction() {
0
// Comment
- x
}

function remainder() {
0
// Comment
% x
}

function exponentiation() {
0
// Comment
** x
}

function leftShift() {
0
// Comment
<< x
}

function rightShift() {
0
// Comment
>> x
}

function unsignedRightShift() {
0
// Comment
>>> x
}

function bitwiseAnd() {
0
// Comment
& x
}

function bitwiseOr() {
0
// Comment
| x
}

function bitwiseXor() {
0
// Comment
^ x
}
16 changes: 16 additions & 0 deletions tests/comments_pipeline_own_line/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pipeline_own_line.js - babylon-verify 1`] = `
function pipeline() {
0
// Comment
|> x
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function pipeline() {
0 |>
// Comment
x;
}
`;
1 change: 1 addition & 0 deletions tests/comments_pipeline_own_line/jsfmt.spec.js
@@ -0,0 +1 @@
run_spec(__dirname, ["babylon"]);
5 changes: 5 additions & 0 deletions tests/comments_pipeline_own_line/pipeline_own_line.js
@@ -0,0 +1,5 @@
function pipeline() {
0
// Comment
|> x
}

0 comments on commit e86f085

Please sign in to comment.