From 784bd1e82d2d96ac9a2db7610b215cedf8209391 Mon Sep 17 00:00:00 2001 From: Hammad Date: Wed, 6 Apr 2022 22:31:51 -0230 Subject: [PATCH 1/6] Fixing issues with spacing when an object literal lives inside a mixin call --- js/src/css/beautifier.js | 13 +++++++- python/cssbeautifier/css/beautifier.py | 10 ++++++- test/data/css/tests.js | 41 ++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/js/src/css/beautifier.js b/js/src/css/beautifier.js index 62cab3a11..f2d50fbd1 100644 --- a/js/src/css/beautifier.js +++ b/js/src/css/beautifier.js @@ -314,7 +314,12 @@ Beautifier.prototype.beautify = function() { this.indent(); this._output.set_indent(this._indentLevel); } else { - this.indent(); + // inside mixin and first param is object + if (previous_ch === '(') { + this._output.space_before_token = false; + } else if (previous_ch !== ',') { + this.indent(); + } this.print_string(this._ch); } @@ -346,6 +351,12 @@ Beautifier.prototype.beautify = function() { this._output.add_new_line(true); } } + if (this._input.peek() === ')') { + this._output.trim(true); + if (this._options.brace_style === "expand") { + this._output.add_new_line(true); + } + } } else if (this._ch === ":") { if ((insideRule || enteringConditionalGroup) && !(this._input.lookBack("&") || this.foundNestedPseudoClass()) && !this._input.lookBack("(") && !insideAtExtend && parenLevel === 0) { // 'property: value' delimiter diff --git a/python/cssbeautifier/css/beautifier.py b/python/cssbeautifier/css/beautifier.py index 7a5941c3d..550efc094 100644 --- a/python/cssbeautifier/css/beautifier.py +++ b/python/cssbeautifier/css/beautifier.py @@ -343,7 +343,11 @@ def beautify(self): self.indent() self._output.set_indent(self._indentLevel) else: - self.indent() + # inside mixin and first param is object + if previous_ch == "(": + self._output.space_before_token = False + elif previous_ch != ",": + self.indent() self.print_string(self._ch) self.eatWhitespace(True) @@ -372,6 +376,10 @@ def beautify(self): ): if self._input.peek() != "}": self._output.add_new_line(True) + if self._input.peek() == ")": + self._output.trim(True) + if self._options.brace_style == "expand": + self._output.add_new_line(True) elif self._ch == ":": if ( (insideRule or enteringConditionalGroup) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index dada3b705..700d96aae 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1360,9 +1360,8 @@ exports.test_data = { '}', '.set {', ' each(@set, {', - ' @{key}-@{index}: @value;', - ' }', - ' );', + ' @{key}-@{index}: @value;', + ' });', '}' ] }, @@ -1732,6 +1731,20 @@ exports.test_data = { '{{empty_line_indent}} width: auto;', '}' ] + }, { + comment: 'mixins call with object notation, and brace_style="expand"', + input: [ + '.example({', + ' color:red;', + '});' + ], + output: [ + '.example(', + ' {', + ' color:red;', + ' }', + ');' + ] }, { comment: 'integration test of newline_between_rules, imports, and brace_style="expand"', input: '.a{} @import "custom.css";.rule{}', @@ -1758,6 +1771,28 @@ exports.test_data = { ' &:extend(a:hover);', '}' ] + }, { + unchanged: [ + '.test {', + ' .example({', + ' color:red;', + ' });', + '}' + ] + }, { + unchanged: [ + '.example2({', + ' display:none;', + '});' + ] + }, { + unchanged: [ + '.aa {', + ' .mq-medium(a, {', + ' background: red;', + ' });', + '}' + ] }, { comment: 'Ensure simple closing parens do not break behavior', unchanged: [ From 15e0bbfa6c16fa4b96b0300108c2308fb860379b Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 6 Apr 2022 21:34:52 -0700 Subject: [PATCH 2/6] Update test/data/css/tests.js --- test/data/css/tests.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index 700d96aae..349b185fb 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1793,6 +1793,16 @@ exports.test_data = { ' });', '}' ] + }, { + unchanged: [ + '@selectors: blue, green, red;', + '', + 'each(@selectors, {', + ' .sel-@{value} {', + ' a: b;', + ' }', + '});' + ] }, { comment: 'Ensure simple closing parens do not break behavior', unchanged: [ From a3fc4b633e242d528d9fac25af0dcc409aaeada3 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 6 Apr 2022 21:36:37 -0700 Subject: [PATCH 3/6] Update test/data/css/tests.js --- test/data/css/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index 349b185fb..23422b3ae 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1793,7 +1793,7 @@ exports.test_data = { ' });', '}' ] - }, { + }, { unchanged: [ '@selectors: blue, green, red;', '', From d768b06d8c2ced40aeb0fb32edd35c3fa5102b44 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 6 Apr 2022 21:41:28 -0700 Subject: [PATCH 4/6] Update test/data/css/tests.js --- test/data/css/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index 23422b3ae..d28742f70 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1799,7 +1799,7 @@ exports.test_data = { '', 'each(@selectors, {', ' .sel-@{value} {', - ' a: b;', + ' a: b;', ' }', '});' ] From f6e0936d9fc96401fa51e21935b9e9f76187bb81 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 6 Apr 2022 21:44:28 -0700 Subject: [PATCH 5/6] Update test/data/css/tests.js --- test/data/css/tests.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index d28742f70..df06ac6ba 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1796,7 +1796,6 @@ exports.test_data = { }, { unchanged: [ '@selectors: blue, green, red;', - '', 'each(@selectors, {', ' .sel-@{value} {', ' a: b;', From 395b77394eace1e4f24f788afae56061805c9f3b Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 6 Apr 2022 22:09:06 -0700 Subject: [PATCH 6/6] Update test/data/css/tests.js --- test/data/css/tests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/data/css/tests.js b/test/data/css/tests.js index df06ac6ba..ae64dd1f5 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1801,7 +1801,7 @@ exports.test_data = { ' a: b;', ' }', '});' - ] + ] }, { comment: 'Ensure simple closing parens do not break behavior', unchanged: [