diff --git a/changelog_unreleased/scss/16607.md b/changelog_unreleased/scss/16607.md new file mode 100644 index 000000000..b67ab6ba3 --- /dev/null +++ b/changelog_unreleased/scss/16607.md @@ -0,0 +1,22 @@ +#### Fix error thrown when formatting SCSS file (#16607 by @fisker) + + +```scss +// Input +@if true { + $newKey: ($key: ( $theme-name: $value )) +} + +// Prettier stable +Error + +// Prettier main +@if true { + $newKey: ( + $key: + ( + $theme-name: $value, + ), + ); +} +``` diff --git a/src/language-css/print/parenthesized-value-group.js b/src/language-css/print/parenthesized-value-group.js index ecb937f19..427b96968 100644 --- a/src/language-css/print/parenthesized-value-group.js +++ b/src/language-css/print/parenthesized-value-group.js @@ -9,6 +9,12 @@ import { line, softline, } from "../../document/builders.js"; +import { + DOC_TYPE_FILL, + DOC_TYPE_GROUP, + DOC_TYPE_INDENT, +} from "../../document/constants.js"; +import { getDocType } from "../../document/utils.js"; import isNextLineEmpty from "../../utils/is-next-line-empty.js"; import isNonEmptyArray from "../../utils/is-non-empty-array.js"; import { locEnd, locStart } from "../loc.js"; @@ -91,7 +97,10 @@ function printParenthesizedValueGroup(path, options, print) { child.type === "value-comma_group" && child.groups && child.groups[0].type !== "value-paren_group" && - child.groups[2]?.type === "value-paren_group" + child.groups[2]?.type === "value-paren_group" && + getDocType(doc) === DOC_TYPE_GROUP && + getDocType(doc.contents) === DOC_TYPE_INDENT && + getDocType(doc.contents.contents) === DOC_TYPE_FILL ) { const { parts } = doc.contents.contents; parts[1] = group(parts[1]); diff --git a/tests/format/scss/parens/__snapshots__/format.test.js.snap b/tests/format/scss/parens/__snapshots__/format.test.js.snap index e920d1c99..0ece1acf4 100644 --- a/tests/format/scss/parens/__snapshots__/format.test.js.snap +++ b/tests/format/scss/parens/__snapshots__/format.test.js.snap @@ -39,6 +39,29 @@ $icons: ( ================================================================================ `; +exports[`issue-16594.scss format 1`] = ` +====================================options===================================== +parsers: ["scss"] +printWidth: 80 + | printWidth +=====================================input====================================== + @if true { + $newKey: ($key: ( $theme-name: $value )) + } + +=====================================output===================================== +@if true { + $newKey: ( + $key: + ( + $theme-name: $value, + ), + ); +} + +================================================================================ +`; + exports[`parens.scss format 1`] = ` ====================================options===================================== parsers: ["scss"] diff --git a/tests/format/scss/parens/issue-16594.scss b/tests/format/scss/parens/issue-16594.scss new file mode 100644 index 000000000..03e821e87 --- /dev/null +++ b/tests/format/scss/parens/issue-16594.scss @@ -0,0 +1,3 @@ + @if true { + $newKey: ($key: ( $theme-name: $value )) + }