Skip to content

Commit

Permalink
Avoid having small right operand on its own line (prettier-solidity#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Sep 23, 2019
1 parent 62d4d9f commit 4d9629f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 47 deletions.
10 changes: 7 additions & 3 deletions src/binary-operator-printers/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ module.exports = {
const groupIfNecessary = groupIfNecessaryBuilder(path);
const indentIfNecessary = indentIfNecessaryBuilder(path);

const right = concat([node.operator, line, path.call(print, 'right')]);
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const shouldGroup =
node.left.type !== 'BinaryOperation' &&
path.getParentNode().type !== 'BinaryOperation';
return groupIfNecessary(
concat([
path.call(print, 'left'),
' ',
indentIfNecessary(
concat([node.operator, line, path.call(print, 'right')])
)
indentIfNecessary(shouldGroup ? group(right) : right)
])
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/binary-operator-printers/comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ module.exports = {
print: (node, path, print) => {
const indentIfNecessary = indentIfNecessaryBuilder(path);

const right = concat([node.operator, line, path.call(print, 'right')]);
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const shouldGroup =
node.left.type !== 'BinaryOperation' &&
path.getParentNode().type !== 'BinaryOperation';
return group(
concat([
path.call(print, 'left'),
' ',
indentIfNecessary(
concat([node.operator, line, path.call(print, 'right')])
)
indentIfNecessary(shouldGroup ? group(right) : right)
])
);
}
Expand Down
14 changes: 12 additions & 2 deletions src/binary-operator-printers/exponentiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ const {
module.exports = {
match: op => op === '**',
print: (node, path, print) => {
const right = concat([
ifBreak(' ', ''),
node.operator,
softline,
path.call(print, 'right')
]);
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const shouldGroup =
node.left.type !== 'BinaryOperation' &&
path.getParentNode().type !== 'BinaryOperation';
return group(
concat([
path.call(print, 'left'),
ifBreak(' ', ''),
indent(concat([node.operator, softline, path.call(print, 'right')]))
indent(shouldGroup ? group(right) : right)
])
);
}
Expand Down
10 changes: 7 additions & 3 deletions src/binary-operator-printers/logical.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ module.exports = {
const groupIfNecessary = groupIfNecessaryBuilder(path);
const indentIfNecessary = indentIfNecessaryBuilder(path);

const right = concat([node.operator, line, path.call(print, 'right')]);
// If it's a single binary operation, avoid having a small right
// operand like - 1 on its own line
const shouldGroup =
node.left.type !== 'BinaryOperation' &&
path.getParentNode().type !== 'BinaryOperation';
return groupIfNecessary(
concat([
path.call(print, 'left'),
' ',
indentIfNecessary(
concat([node.operator, line, path.call(print, 'right')])
)
indentIfNecessary(shouldGroup ? group(right) : right)
])
);
}
Expand Down
54 changes: 18 additions & 36 deletions tests/BinaryOperators/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ contract ArithmeticOperators {
veryVeryVeryVeryVeryLongVariableCalledB;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
c;
) + c;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
Expand All @@ -264,20 +263,17 @@ contract ArithmeticOperators {
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
c
) + c
) {}
a(
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
c
) + c
);
return
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) +
c;
) + c;
}
}
Expand Down Expand Up @@ -330,8 +326,7 @@ contract ArithmeticOperators {
veryVeryVeryVeryVeryLongVariableCalledB;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
c;
)**c;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
Expand All @@ -348,20 +343,17 @@ contract ArithmeticOperators {
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
c
)**c
) {}
a(
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
c
)**c
);
return
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) **
c;
)**c;
}
}
Expand Down Expand Up @@ -490,8 +482,7 @@ contract ComparisonOperators {
veryVeryVeryVeryVeryLongVariableCalledB;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ==
c;
) == c;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) <
Expand All @@ -508,20 +499,17 @@ contract ComparisonOperators {
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ==
c
) == c
) {}
a(
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ==
c
) == c
);
return
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ==
c;
) == c;
}
}
Expand Down Expand Up @@ -605,8 +593,7 @@ contract LogicalOperators {
) {}
if (
(veryVeryVeryVeryVeryLongVariableCalledA &&
veryVeryVeryVeryVeryLongVariableCalledB) ||
c
veryVeryVeryVeryVeryLongVariableCalledB) || c
) {}
if (
veryVeryVeryVeryVeryLongVariableCalledA ||
Expand All @@ -626,8 +613,7 @@ contract LogicalOperators {
) {}
while (
(veryVeryVeryVeryVeryLongVariableCalledA &&
veryVeryVeryVeryVeryLongVariableCalledB) ||
c
veryVeryVeryVeryVeryLongVariableCalledB) || c
) {}
while (
veryVeryVeryVeryVeryLongVariableCalledA ||
Expand All @@ -650,8 +636,7 @@ contract LogicalOperators {
veryVeryVeryVeryVeryLongVariableCalledB;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
c;
) || c;
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
Expand All @@ -668,20 +653,17 @@ contract LogicalOperators {
if (
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
c
) || c
) {}
a(
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
c
) || c
);
return
veryVeryVeryVeryVeryLongFunctionCalledA(
veryVeryVeryVeryVeryLongVariableCalledB
) ||
c;
) || c;
}
}
Expand Down

0 comments on commit 4d9629f

Please sign in to comment.