Skip to content

Commit

Permalink
Fix empty line check between array elements (prettier#14736)
Browse files Browse the repository at this point in the history
Co-authored-by: ☀️ <sol@solfox.online>
Co-authored-by: fisker Cheung <lionkay@gmail.com>
  • Loading branch information
3 people authored and medikoo committed Feb 14, 2024
1 parent 88ecc85 commit 9858454
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 5 deletions.
24 changes: 24 additions & 0 deletions changelog_unreleased/javascript/14736.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#### Fix empty line check between array elements (#14736 by @solarized-fox)

<!-- prettier-ignore -->
```jsx
// Input
[
(a = b),

c // comment
]

// Prettier stable
[
(a = b),
c, // comment
];

// Prettier main
[
(a = b),

c, // comment
];
```
24 changes: 19 additions & 5 deletions src/language-js/print/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ import {
fill,
} from "../../document/builders.js";
import hasNewline from "../../utils/has-newline.js";
import isNextLineEmptyAfterIndex from "../../utils/is-next-line-empty.js";
import skipInlineComment from "../../utils/skip-inline-comment.js";
import skipTrailingComment from "../../utils/skip-trailing-comment.js";
import {
shouldPrintComma,
hasComment,
CommentCheckFlags,
isNextLineEmpty,
isNumericLiteral,
isSignedNumericLiteral,
isArrayOrTupleExpression,
isObjectOrRecordExpression,
} from "../utils/index.js";
import { locStart } from "../loc.js";
import { locStart, locEnd } from "../loc.js";

import { printOptionalToken } from "./misc.js";
import { printTypeAnnotationProperty } from "./type-annotation.js";
Expand Down Expand Up @@ -170,6 +172,18 @@ function isConciselyPrintedArray(node, options) {
);
}

function isLineAfterElementEmpty({ node }, { originalText: text }) {
const skipComment = (idx) =>
skipInlineComment(text, skipTrailingComment(text, idx));

const skipToComma = (currentIdx) =>
text[currentIdx] === ","
? currentIdx
: skipToComma(skipComment(currentIdx + 1));

return isNextLineEmptyAfterIndex(text, skipToComma(locEnd(node)));
}

function printArrayElements(path, options, elementsProperty, print) {
const parts = [];

Expand All @@ -180,7 +194,7 @@ function printArrayElements(path, options, elementsProperty, print) {
parts.push([
",",
line,
node && isNextLineEmpty(node, options) ? softline : "",
node && isLineAfterElementEmpty(path, options) ? softline : "",
]);
}
}, elementsProperty);
Expand All @@ -193,12 +207,12 @@ function printArrayElements(path, options, elementsProperty, print) {
function printArrayElementsConcisely(path, options, print, trailingComma) {
const parts = [];

path.each(({ node, isLast, next }) => {
path.each(({ isLast, next }) => {
parts.push([print(), isLast ? trailingComma : ","]);

if (!isLast) {
parts.push(
isNextLineEmpty(node, options)
isLineAfterElementEmpty(path, options)
? [hardline, hardline]
: hasComment(next, CommentCheckFlags.Leading | CommentCheckFlags.Line)
? hardline
Expand Down
135 changes: 135 additions & 0 deletions tests/format/js/arrays/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,87 @@ a = [
]
b = [
100,
(200)
,
300
,
1
,
2, 3
]
c = [
"apple",
"banana",
"blueberry",
"red",
"blue"
,
"yellow",
"broccoli",
"celery",
"lettuce"
,
"green"
,
"green",
"green",
//an egg
"egg",
//a bigger egg
"big egg"
//the biggest egg
,
"huge egg"
,
//not an egg
"lasagna"
]
_ = [
a,
b //
]
_ = [
(a),
b, //
];
_ = [
(((((
a = b/* comment */))/* comment */))),
c //
]
_ = [
(((((
(a = b)/* comment */))/* comment */))),
c //
]
_ = [
(a=b
),
b, //
];
=====================================output=====================================
a = [
1, 2,
Expand All @@ -787,6 +868,60 @@ a = [
4,
];
b = [
100,
200,
300,
1, 2, 3,
];
c = [
"apple", "banana", "blueberry", "red", "blue", "yellow", "broccoli", "celery",
"lettuce", "green", "green", "green",
//an egg
"egg",
//a bigger egg
"big egg",
//the biggest egg
"huge egg",
//not an egg
"lasagna",
];
_ = [
a,
b, //
];
_ = [
a,
b, //
];
_ = [
(a = b) /* comment */ /* comment */,
c, //
];
_ = [
(a = b) /* comment */ /* comment */,
c, //
];
_ = [
(a = b),
b, //
];
================================================================================
`;
Expand Down
81 changes: 81 additions & 0 deletions tests/format/js/arrays/preserve_empty_lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,84 @@ a = [


]

b = [
100,

(200)
,

300
,

1
,
2, 3
]

c = [
"apple",
"banana",
"blueberry",

"red",
"blue"
,
"yellow",

"broccoli",
"celery",
"lettuce"
,

"green"
,
"green",
"green",

//an egg
"egg",
//a bigger egg
"big egg"
//the biggest egg
,
"huge egg"
,

//not an egg
"lasagna"

]

_ = [
a,

b //
]

_ = [
(a),

b, //
];

_ = [
(((((
a = b/* comment */))/* comment */))),

c //
]

_ = [
(((((
(a = b)/* comment */))/* comment */))),

c //
]

_ = [
(a=b

),
b, //
];

0 comments on commit 9858454

Please sign in to comment.