From c7c0f8bd6bce2bb14c2766f17265ba790de89fea Mon Sep 17 00:00:00 2001 From: "Anantachai Saothong (Manta)" Date: Mon, 24 Sep 2018 18:07:08 +0700 Subject: [PATCH] Fixed removing parentheses around negation in an expression list See https://github.com/ThisIsManta/vscode-stylus-supremacy/issues/25 --- edge/format.js | 13 ++++++++----- .../input.styl | 3 ++- .../output.styl | 1 + .../input.styl | 3 ++- .../output.styl | 1 + 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/edge/format.js b/edge/format.js index 248de42..2ebe6de 100644 --- a/edge/format.js +++ b/edge/format.js @@ -625,12 +625,11 @@ function format(content, options = {}) { inputNode.parent.op !== '[]' && inputNode.parent.op !== '[]=' ) - const parentIsStringInterpolation = ( + const parentIsStringInterpolation = inputNode.parent instanceof Stylus.nodes.BinOp && inputNode.parent.op === '%' && inputNode.parent.right === inputNode && inputNode.nodes.length > 1 - ) const parentIsNestedExpression = inputNode.nodes.length === 1 && inputNode.parent instanceof Stylus.nodes.Expression && @@ -646,16 +645,19 @@ function format(content, options = {}) { inputNode.parent.parent instanceof Stylus.nodes.Object || inputNode.parent.parent instanceof Stylus.nodes.BinOp && inputNode.parent.parent.op === '[]' ) === false - const currentIsEmpty = ( + const currentIsEmpty = inputNode.nodes.length === 0 && inputNode.parent instanceof Stylus.nodes.Expression && !(inputNode.parent instanceof Stylus.nodes.Arguments) && // Note that `Arguments` type inherits `Expression` type inputNode.parent.nodes.length === 1 - ) const currentIsDivision = inputNode.nodes.length === 1 && inputNode.nodes[0] instanceof Stylus.nodes.BinOp && inputNode.nodes[0].op === '/' + const currentIsNegation = + inputNode.nodes.length === 1 && + inputNode.nodes[0] instanceof Stylus.nodes.UnaryOp && + inputNode.nodes[0].op === '-' const currentHasUnitSuffix = inputNode.nodes.length === 2 && inputNode.nodes[0] instanceof Stylus.nodes.BinOp && @@ -665,7 +667,8 @@ function format(content, options = {}) { parentIsStringInterpolation || parentIsNestedExpression || currentIsEmpty || - currentIsDivision + currentIsDivision || + currentIsNegation if (currentHasParenthesis || currentHasUnitSuffix) { outputBuffer.append(openParen) } diff --git a/spec/option-insert-parenthesis-after-negation-false/input.styl b/spec/option-insert-parenthesis-after-negation-false/input.styl index b3e55a1..8f6b88f 100644 --- a/spec/option-insert-parenthesis-after-negation-false/input.styl +++ b/spec/option-insert-parenthesis-after-negation-false/input.styl @@ -5,4 +5,5 @@ .class2 top -x left - x - right -(x) \ No newline at end of file + right -(x) + padding 1px (- x) \ No newline at end of file diff --git a/spec/option-insert-parenthesis-after-negation-false/output.styl b/spec/option-insert-parenthesis-after-negation-false/output.styl index 1e1f3e6..2bfad62 100644 --- a/spec/option-insert-parenthesis-after-negation-false/output.styl +++ b/spec/option-insert-parenthesis-after-negation-false/output.styl @@ -8,4 +8,5 @@ top: -x; left: - x; right: -(x); + padding: 1px (- x); } \ No newline at end of file diff --git a/spec/option-insert-parenthesis-after-negation-true/input.styl b/spec/option-insert-parenthesis-after-negation-true/input.styl index b3e55a1..8f6b88f 100644 --- a/spec/option-insert-parenthesis-after-negation-true/input.styl +++ b/spec/option-insert-parenthesis-after-negation-true/input.styl @@ -5,4 +5,5 @@ .class2 top -x left - x - right -(x) \ No newline at end of file + right -(x) + padding 1px (- x) \ No newline at end of file diff --git a/spec/option-insert-parenthesis-after-negation-true/output.styl b/spec/option-insert-parenthesis-after-negation-true/output.styl index 2be0050..82b4bfc 100644 --- a/spec/option-insert-parenthesis-after-negation-true/output.styl +++ b/spec/option-insert-parenthesis-after-negation-true/output.styl @@ -8,4 +8,5 @@ top: -x; left: -(x); right: -(x); + padding: 1px (-(x)); } \ No newline at end of file