Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve doc print #1922

Merged
merged 11 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ extends:
- eslint:recommended
- plugin:prettier/recommended
- plugin:jest/recommended
- plugin:prettier-doc/recommended
plugins:
- import
- prettier-doc
root: true
env:
es6: true
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"eslint-plugin-import": "2.27.5",
"eslint-plugin-jest": "27.2.1",
"eslint-plugin-prettier": "4.2.1",
"eslint-plugin-prettier-doc": "^1.1.0",
"jest": "29.3.1",
"jest-environment-jsdom": "29.3.1",
"jest-runner-eslint": "1.1.0",
Expand Down
27 changes: 11 additions & 16 deletions src/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
hasNewline,
hasNewlineInRange,
} = require("prettier").util;
const { concat, join, indent, hardline, cursor, lineSuffix, breakParent } =
const { join, indent, hardline, cursor, lineSuffix, breakParent } =
require("prettier").doc.builders;
const {
getNextNonSpaceNonCommentCharacterIndex,
Expand Down Expand Up @@ -853,7 +853,7 @@ function printDanglingComments(path, options, sameIndent, filter) {
if (sameIndent) {
return join(hardline, parts);
}
return indent(concat([hardline, join(hardline, parts)]));
return indent([hardline, join(hardline, parts)]);
}

function hasLeadingComment(node) {
Expand Down Expand Up @@ -889,7 +889,7 @@ function printComments(comments, options) {
parts.push(hardline);
}
});
return concat(parts);
return parts;
}

function isBlockComment(comment) {
Expand Down Expand Up @@ -930,7 +930,7 @@ function canAttachComment(node) {
// TODO remove after https://github.com/prettier/prettier/issues/5087
function prependCursorPlaceholder(path, options, printed) {
if (path.getNode() === options.cursorNode && path.getValue()) {
return concat([cursor, printed, cursor]);
return [cursor, printed, cursor];
}

return printed;
Expand All @@ -950,15 +950,15 @@ function printLeadingComment(commentPath, print, options) {
// Leading block comments should see if they need to stay on the
// same line or not.
if (isBlock) {
return concat([
return [
contents,
hasNewline(options.originalText, options.locEnd(comment))
? hardline
: " ",
]);
];
}

return concat([contents, hardline]);
return [contents, hardline];
}

function printTrailingComment(commentPath, print, options) {
Expand Down Expand Up @@ -993,18 +993,13 @@ function printTrailingComment(commentPath, print, options) {
options
);

return lineSuffix(
concat([hardline, isLineBeforeEmpty ? hardline : "", contents])
);
return lineSuffix([hardline, isLineBeforeEmpty ? hardline : "", contents]);
} else if (isBlock) {
// Trailing block comments never need a newline
return concat([" ", contents]);
return [" ", contents];
}

return concat([
lineSuffix(concat([" ", contents])),
!isBlock ? breakParent : "",
]);
return [lineSuffix([" ", contents]), !isBlock ? breakParent : ""];
}

function printAllComments(path, print, options, needsSemi) {
Expand Down Expand Up @@ -1042,7 +1037,7 @@ function printAllComments(path, print, options, needsSemi) {
return prependCursorPlaceholder(
path,
options,
concat(leadingParts.concat(trailingParts))
leadingParts.concat(trailingParts)
);
}

Expand Down
Loading