diff --git a/changelog_unreleased/scss/13931.md b/changelog_unreleased/scss/13931.md new file mode 100644 index 000000000000..8936ca49bbf7 --- /dev/null +++ b/changelog_unreleased/scss/13931.md @@ -0,0 +1,36 @@ +#### fix: preserve empty lines between scss nested 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,