Skip to content

Commit

Permalink
Fix less/scss format error (prettier#12536)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored and medikoo committed Jan 2, 2024
1 parent 16017fc commit 89fb33a
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
29 changes: 29 additions & 0 deletions changelog_unreleased/less/12536.md
@@ -0,0 +1,29 @@
#### Fix LESS/SCSS format error (#12536 by @fisker)

<!-- prettier-ignore -->
```less
// Input
.background-gradient(@cut) {
background: linear-gradient(
to right,
@white 0%,
@white (@cut - 0.01%),
@portal-background @cut,
@portal-background 100%
);
}

// Prettier stable
TypeError: Cannot read properties of undefined (reading 'endOffset')

// Prettier main
.background-gradient(@cut) {
background: linear-gradient(
to right,
@white 0%,
@white (@cut - 0.01%),
@portal-background @cut,
@portal-background 100%
);
}
```
17 changes: 9 additions & 8 deletions src/language-css/printer-postcss.js
Expand Up @@ -924,15 +924,16 @@ function genericPrint(path, options, print) {
if (
!isLast &&
child.type === "value-comma_group" &&
child.groups &&
child.groups[0].type !== "value-paren_group" &&
isNextLineEmpty(
options.originalText,
getLast(child.groups),
locEnd
)
isNonEmptyArray(child.groups)
) {
printed.push(hardline);
const last = getLast(child.groups);
if (
// `value-paren_group` missing location info
last.source &&
isNextLineEmpty(options.originalText, last, locEnd)
) {
printed.push(hardline);
}
}

return printed;
Expand Down
12 changes: 12 additions & 0 deletions tests/format/less/parens/2.less
@@ -0,0 +1,12 @@
.background-gradient(@cut) {
background: linear-gradient(
to right,


/* Should preserve empty lines */
@white 0%,
@white (@cut - 0.01%),
@portal-background @cut,
@portal-background 100%
);
}
34 changes: 34 additions & 0 deletions tests/format/less/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`2.less format 1`] = `
====================================options=====================================
parsers: ["less"]
printWidth: 80
| printWidth
=====================================input======================================
.background-gradient(@cut) {
background: linear-gradient(
to right,
/* Should preserve empty lines */
@white 0%,
@white (@cut - 0.01%),
@portal-background @cut,
@portal-background 100%
);
}
=====================================output=====================================
.background-gradient(@cut) {
background: linear-gradient(
to right,
/* Should preserve empty lines */ @white 0%,
@white (@cut - 0.01%),
@portal-background @cut,
@portal-background 100%
);
}
================================================================================
`;

exports[`parens.less format 1`] = `
====================================options=====================================
parsers: ["less"]
Expand Down
15 changes: 15 additions & 0 deletions tests/format/scss/parens/2.scss
@@ -0,0 +1,15 @@
$icons: (
cal-day-group:
(
left: 253,
top: 73,
),


/* Should preserve empty lines */
cal-week-group:
(
left: 1,
top: 169,
)
);
38 changes: 38 additions & 0 deletions tests/format/scss/parens/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`2.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
| printWidth
=====================================input======================================
$icons: (
cal-day-group:
(
left: 253,
top: 73,
),
/* Should preserve empty lines */
cal-week-group:
(
left: 1,
top: 169,
)
);
=====================================output=====================================
$icons: (
cal-day-group: (
left: 253,
top: 73,
),
/* Should preserve empty lines */ cal-week-group:
(
left: 1,
top: 169,
),
);
================================================================================
`;

exports[`parens.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
Expand Down

0 comments on commit 89fb33a

Please sign in to comment.