From f34492d7e515d6af6fecdc588241977f54471572 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 29 May 2017 12:49:25 -0700 Subject: [PATCH] Strip leading space for first combinators (#1807) --- src/printer.js | 4 +++- .../__snapshots__/jsfmt.spec.js.snap | 21 +++++++++++++++++++ tests/css_combinator/leading.css | 8 +++++++ .../nested-2/__snapshots__/jsfmt.spec.js.snap | 6 +++--- .../nested/__snapshots__/jsfmt.spec.js.snap | 4 ++-- 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 tests/css_combinator/leading.css diff --git a/src/printer.js b/src/printer.js index 5da9b179..f68160b0 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2690,7 +2690,9 @@ function genericPrintNoParens(path, options, print, args) { } case "selector-combinator": { if (n.value === "+" || n.value === ">" || n.value === "~") { - return concat([" ", n.value, " "]); + const parent = path.getParentNode(); + const leading = parent.type === "selector-selector" && parent.nodes[0] === n ? "" : " "; + return concat([leading, n.value, " "]); } return n.value; } diff --git a/tests/css_combinator/__snapshots__/jsfmt.spec.js.snap b/tests/css_combinator/__snapshots__/jsfmt.spec.js.snap index 4f4c6a4f..d42b86db 100644 --- a/tests/css_combinator/__snapshots__/jsfmt.spec.js.snap +++ b/tests/css_combinator/__snapshots__/jsfmt.spec.js.snap @@ -8,3 +8,24 @@ exports[`combinator.css 1`] = ` } `; + +exports[`leading.css 1`] = ` +a { + > * { + } + &, > * { + } + + * { + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +a { + > * { + } + &, > * { + } + + * { + } +} + +`; diff --git a/tests/css_combinator/leading.css b/tests/css_combinator/leading.css new file mode 100644 index 00000000..7030864e --- /dev/null +++ b/tests/css_combinator/leading.css @@ -0,0 +1,8 @@ +a { + > * { + } + &, > * { + } + + * { + } +} diff --git a/tests/stylefmt/nested-2/__snapshots__/jsfmt.spec.js.snap b/tests/stylefmt/nested-2/__snapshots__/jsfmt.spec.js.snap index 6593a22f..34d8b974 100644 --- a/tests/stylefmt/nested-2/__snapshots__/jsfmt.spec.js.snap +++ b/tests/stylefmt/nested-2/__snapshots__/jsfmt.spec.js.snap @@ -29,13 +29,13 @@ color:blue;} &:nth-child(5n+1) { &:nth-child(-n + 3) { color: green; } - > li > a { + > li > a { color: red; } - > li > li { + > li > li { color: blue; } - > p + p { + > p + p { color: green; } } diff --git a/tests/stylefmt/nested/__snapshots__/jsfmt.spec.js.snap b/tests/stylefmt/nested/__snapshots__/jsfmt.spec.js.snap index e7e20437..baae75f9 100644 --- a/tests/stylefmt/nested/__snapshots__/jsfmt.spec.js.snap +++ b/tests/stylefmt/nested/__snapshots__/jsfmt.spec.js.snap @@ -20,12 +20,12 @@ padding: 10px; .nested-2-1{ .foo { font-size: 12px; - > .nested-1 { + > .nested-1 { padding: 10px; .nested-2-1 { color: red; } - + .nested-2-2 { + + .nested-2-2 { color: blue; } }