From d59803d1866a8f7be85afa950b4af43040232203 Mon Sep 17 00:00:00 2001 From: Jeremy Neander Date: Thu, 15 Dec 2022 00:54:47 -0600 Subject: [PATCH] fix: preserve empty lines between nested SCSS maps (#13931) Co-authored-by: Jeremy Neander Co-authored-by: fisker Cheung --- changelog_unreleased/scss/13931.md | 36 +++++++++++++++++++ src/language-css/printer-postcss.js | 14 +++++--- .../parens/__snapshots__/jsfmt.spec.js.snap | 1 + 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 changelog_unreleased/scss/13931.md diff --git a/changelog_unreleased/scss/13931.md b/changelog_unreleased/scss/13931.md new file mode 100644 index 000000000000..426d0c712c99 --- /dev/null +++ b/changelog_unreleased/scss/13931.md @@ -0,0 +1,36 @@ +#### Preserve empty lines between nested SCSS maps (#13931 by @jneander) + + +```scss +/* Input */ +$map: ( + 'one': ( + 'key': 'value', + ), + + 'two': ( + 'key': 'value', + ), +) + +/* Prettier stable */ +$map: ( + 'one': ( + 'key': 'value', + ), + 'two': ( + 'key': 'value', + ), +) + +/* Prettier main */ +$map: ( + 'one': ( + 'key': 'value', + ), + + 'two': ( + 'key': 'value', + ), +) +``` diff --git a/src/language-css/printer-postcss.js b/src/language-css/printer-postcss.js index f8bbd02966e2..4ba444c8d77d 100644 --- a/src/language-css/printer-postcss.js +++ b/src/language-css/printer-postcss.js @@ -918,7 +918,8 @@ function genericPrint(path, options, print) { path.map((childPath, index) => { const child = childPath.getValue(); const isLast = index === node.groups.length - 1; - const printed = [print(), isLast ? "" : ","]; + + let printed = [print(), isLast ? "" : ","]; // Key/Value pair in open paren already indented if ( @@ -931,7 +932,7 @@ function genericPrint(path, options, print) { ) { const parts = getDocParts(printed[0].contents.contents); parts[1] = group(parts[1]); - return group(dedent(printed)); + printed = [group(dedent(printed))]; } if ( @@ -939,9 +940,14 @@ function genericPrint(path, options, print) { child.type === "value-comma_group" && isNonEmptyArray(child.groups) ) { - const last = getLast(child.groups); + let last = getLast(child.groups); + + // `value-paren_group` does not have location info, but its closing parenthesis does. + if (!last.source && last.close) { + last = last.close; + } + if ( - // `value-paren_group` missing location info last.source && isNextLineEmpty(options.originalText, last, locEnd) ) { diff --git a/tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap b/tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap index 5a8c9e105192..d817793377d2 100644 --- a/tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap @@ -28,6 +28,7 @@ $icons: ( left: 253, top: 73, ), + /* Should preserve empty lines */ cal-week-group: ( left: 1,