Preserve trailing commas in single-item SCSS lists (#19928)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
ffb4f9e7bb
commit
d4126e8c97
4 files changed
+166
-13
No files matched your search
@@ -0,0 +1,13 @@
|
||||
#### Preserve trailing commas in single-item lists (#19928 by @kovsu)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```scss
|
||||
// Input
|
||||
$list: ("a",);
|
||||
|
||||
// Prettier stable
|
||||
$list: ("a");
|
||||
|
||||
// Prettier main
|
||||
$list: ("a",);
|
||||
```
|
||||
@@ -15,9 +15,10 @@ import {
|
||||
lineSuffixBoundary,
|
||||
softline,
|
||||
} from "../../document/index.js";
|
||||
import getNextNonSpaceNonCommentCharacter from "../../utilities/get-next-non-space-non-comment-character.js";
|
||||
import isNextLineEmpty from "../../utilities/is-next-line-empty.js";
|
||||
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
|
||||
import { locEnd, locStart } from "../loc.js";
|
||||
import { locEnd } from "../loc.js";
|
||||
import {
|
||||
isConfigurationNode,
|
||||
isKeyInValuePairNode,
|
||||
@@ -28,18 +29,34 @@ import {
|
||||
} from "../utilities/index.js";
|
||||
import { shouldPrintTrailingComma } from "./misc.js";
|
||||
|
||||
function hasComma({ node, parent }, options) {
|
||||
return Boolean(
|
||||
node.source &&
|
||||
options.originalText
|
||||
.slice(locStart(node), locStart(parent.close))
|
||||
.trimEnd()
|
||||
.endsWith(","),
|
||||
function hasTrailingComma({ node, parent }, options) {
|
||||
if (parent.type !== "value-paren_group") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const nodes = node.type === "value-comma_group" ? node.groups : [node];
|
||||
const lastValue = nodes.findLast((child) => child.type !== "value-comment");
|
||||
|
||||
if (!lastValue) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const nextCharacter = getNextNonSpaceNonCommentCharacter(
|
||||
options.originalText,
|
||||
locEnd(lastValue),
|
||||
);
|
||||
return nextCharacter === ",";
|
||||
}
|
||||
|
||||
function printTrailingComma(path, options) {
|
||||
if (isVarFunctionNode(path.grandparent) && hasComma(path, options)) {
|
||||
if (
|
||||
(isVarFunctionNode(path.grandparent) ||
|
||||
(options.parser === "scss" &&
|
||||
path.grandparent?.type !== "value-func" &&
|
||||
path.parent.groups.length === 1 &&
|
||||
!isKeyValuePairNode(path.parent.groups[0]))) &&
|
||||
hasTrailingComma(path, options)
|
||||
) {
|
||||
return ",";
|
||||
}
|
||||
|
||||
|
||||
@@ -179,7 +179,7 @@ $z-indexes: (
|
||||
=====================================output=====================================
|
||||
.simplification {
|
||||
foo: (
|
||||
calc() // It is a comment
|
||||
calc(), // It is a comment
|
||||
);
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ $z-indexes: (
|
||||
=====================================output=====================================
|
||||
.simplification {
|
||||
foo: (
|
||||
calc() // It is a comment
|
||||
calc(), // It is a comment
|
||||
);
|
||||
}
|
||||
|
||||
@@ -288,6 +288,110 @@ a {
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`issue-6356.scss - {"trailingComma":"es5"} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["scss"]
|
||||
trailingComma: "es5"
|
||||
printWidth: 80 (default) |
|
||||
=====================================input======================================
|
||||
$single-string: ("a",);
|
||||
$single-space-list: (1 2 3,);
|
||||
$single-nested-list: ((1, 2),);
|
||||
@include assert-equal(daff-string-split(".a", "."), ("a",));
|
||||
|
||||
$multiple-items: (1, 2,);
|
||||
$single-argument: foo(1,);
|
||||
|
||||
$single-entry-map: (a: 1,);
|
||||
|
||||
$comment-after-comma: ("a",
|
||||
// ,
|
||||
);
|
||||
$comment-without-comma: ("a"
|
||||
// ,
|
||||
);
|
||||
$empty-comment-after-comma: ("a",
|
||||
//
|
||||
);
|
||||
|
||||
=====================================output=====================================
|
||||
$single-string: ("a",);
|
||||
$single-space-list: (1 2 3,);
|
||||
$single-nested-list: ((1, 2),);
|
||||
@include assert-equal(daff-string-split(".a", "."), ("a",));
|
||||
|
||||
$multiple-items: (1, 2);
|
||||
$single-argument: foo(1);
|
||||
|
||||
$single-entry-map: (
|
||||
a: 1,
|
||||
);
|
||||
|
||||
$comment-after-comma: (
|
||||
"a", // ,
|
||||
);
|
||||
$comment-without-comma: (
|
||||
"a" // ,
|
||||
);
|
||||
$empty-comment-after-comma: (
|
||||
"a", //
|
||||
);
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`issue-6356.scss - {"trailingComma":"none"} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["scss"]
|
||||
trailingComma: "none"
|
||||
printWidth: 80 (default) |
|
||||
=====================================input======================================
|
||||
$single-string: ("a",);
|
||||
$single-space-list: (1 2 3,);
|
||||
$single-nested-list: ((1, 2),);
|
||||
@include assert-equal(daff-string-split(".a", "."), ("a",));
|
||||
|
||||
$multiple-items: (1, 2,);
|
||||
$single-argument: foo(1,);
|
||||
|
||||
$single-entry-map: (a: 1,);
|
||||
|
||||
$comment-after-comma: ("a",
|
||||
// ,
|
||||
);
|
||||
$comment-without-comma: ("a"
|
||||
// ,
|
||||
);
|
||||
$empty-comment-after-comma: ("a",
|
||||
//
|
||||
);
|
||||
|
||||
=====================================output=====================================
|
||||
$single-string: ("a",);
|
||||
$single-space-list: (1 2 3,);
|
||||
$single-nested-list: ((1, 2),);
|
||||
@include assert-equal(daff-string-split(".a", "."), ("a",));
|
||||
|
||||
$multiple-items: (1, 2);
|
||||
$single-argument: foo(1);
|
||||
|
||||
$single-entry-map: (
|
||||
a: 1
|
||||
);
|
||||
|
||||
$comment-after-comma: (
|
||||
"a", // ,
|
||||
);
|
||||
$comment-without-comma: (
|
||||
"a" // ,
|
||||
);
|
||||
$empty-comment-after-comma: (
|
||||
"a", //
|
||||
);
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`issue-6920.scss - {"trailingComma":"es5"} format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["scss"]
|
||||
@@ -387,7 +491,7 @@ $breakpoint-map: (
|
||||
|
||||
=====================================output=====================================
|
||||
$colors: hotpink deepskyblue firebrick;
|
||||
$list: (a);
|
||||
$list: (a,);
|
||||
$list: ("Helvetica", "Arial", sans-serif);
|
||||
$colors: ("red", "blue");
|
||||
$config: (
|
||||
@@ -492,7 +596,7 @@ $breakpoint-map: (
|
||||
|
||||
=====================================output=====================================
|
||||
$colors: hotpink deepskyblue firebrick;
|
||||
$list: (a);
|
||||
$list: (a,);
|
||||
$list: ("Helvetica", "Arial", sans-serif);
|
||||
$colors: ("red", "blue");
|
||||
$config: (
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
$single-string: ("a",);
|
||||
$single-space-list: (1 2 3,);
|
||||
$single-nested-list: ((1, 2),);
|
||||
@include assert-equal(daff-string-split(".a", "."), ("a",));
|
||||
|
||||
$multiple-items: (1, 2,);
|
||||
$single-argument: foo(1,);
|
||||
|
||||
$single-entry-map: (a: 1,);
|
||||
|
||||
$comment-after-comma: ("a",
|
||||
// ,
|
||||
);
|
||||
$comment-without-comma: ("a"
|
||||
// ,
|
||||
);
|
||||
$empty-comment-after-comma: ("a",
|
||||
//
|
||||
);
|
||||
Reference in new issue
Block a user