Skip to content

Commit

Permalink
Fixed missing parentheses on a unary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Anantachai Saothong (Manta) committed Jun 22, 2017
1 parent 87e943e commit a6062d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
14 changes: 11 additions & 3 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ function format(content, options = {}) {
outputBuffer.append('{')
}

const parentIsArithmeticOperator = inputNode.parent instanceof Stylus.nodes.UnaryOp || inputNode.parent instanceof Stylus.nodes.BinOp && inputNode.parent.left === inputNode
if (parentIsArithmeticOperator) {
outputBuffer.append(openParen)
}

const currentIsPartOfPropertyNames = !!findParentNode(inputNode, node => node instanceof Stylus.nodes.Property && node.segments.includes(inputNode))

const currentIsPartOfKeyframes = !!findParentNode(inputNode, node => node instanceof Stylus.nodes.Keyframes && node.segments.includes(inputNode))
Expand Down Expand Up @@ -558,6 +563,10 @@ function format(content, options = {}) {
outputBuffer.append('}')
}

if (parentIsArithmeticOperator) {
outputBuffer.append(closeParen)
}

if (insideExpression === false) {
if (options.insertSemicolons) {
outputBuffer.append(';')
Expand Down Expand Up @@ -601,9 +610,8 @@ function format(content, options = {}) {
outputBuffer.append(travel(inputNode, inputNode.val, indentLevel, true))

} else {
const insideUnaryOp = inputNode.parent instanceof Stylus.nodes.Expression && (inputNode.parent.parent instanceof Stylus.nodes.UnaryOp || inputNode.parent.parent instanceof Stylus.nodes.BinOp)
const escapeDivider = inputNode.op === '/'
if (insideUnaryOp || escapeDivider) {
if (escapeDivider) {
outputBuffer.append(openParen)
}

Expand All @@ -613,7 +621,7 @@ function format(content, options = {}) {
outputBuffer.append(' ' + travel(inputNode, inputNode.right, indentLevel, true))
}

if (insideUnaryOp || escapeDivider) {
if (escapeDivider) {
outputBuffer.append(closeParen)
}
}
Expand Down
9 changes: 6 additions & 3 deletions spec/operator/input.styl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
body
display !0
visibility not true is false
left -5px
margin 1+2px
margin-top -5px
margin-bottom -(6px)
margin-left value
margin-right -(value)
padding 1+2px
list = 1 2 3
color 1 in list
padding list[-1]
item = list[-1]
range = 1...5
math = 1+2- 3*(4/5) %6**7
color = color is defined ? unit(num,'px') : white
Expand Down
9 changes: 6 additions & 3 deletions spec/operator/output.styl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
body {
display: !0;
visibility: !true == false;
left: -5px;
margin: 1 + 2px;
margin-top: -5px;
margin-bottom: -(6px);
margin-left: value;
margin-right: -(value);
padding: 1 + 2px;
list = 1 2 3;
color: 1 in list;
padding: list[-1];
item = list[-1];
range = 1...5;
math = 1 + 2 - 3 * (4 / 5) % 6 ** 7;
color = color is defined ? unit(num, 'px') : white;
Expand Down

0 comments on commit a6062d0

Please sign in to comment.