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

Support nestled JSDoc comments #13445

Merged
merged 8 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/language-js/parse/postprocess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { locStart, locEnd } from "../../loc.js";
import isTsKeywordType from "../../utils/is-ts-keyword-type.js";
import isTypeCastComment from "../../utils/is-type-cast-comment.js";
import getLast from "../../../utils/get-last.js";
import isNonEmptyArray from "../../../utils/is-non-empty-array.js";
import isBlockComment from "../../utils/is-block-comment.js";
import { isIndentableBlockComment } from "../../print/comment.js";
import visitNode from "./visit-node.js";
import { throwErrorForInvalidNodes } from "./typescript.js";
import throwSyntaxError from "./throw-ts-syntax-error.js";
Expand Down Expand Up @@ -154,6 +157,29 @@ function postprocess(ast, options) {
}
});

/* eslint-disable prettier-internal-rules/no-node-comments */
if (isNonEmptyArray(ast.comments)) {
for (let i = ast.comments.length - 2; i >= 0; i--) {
const comment = ast.comments[i];
const nextComment = ast.comments[i + 1];
if (
isBlockComment(comment) &&
isIndentableBlockComment(comment) &&
isBlockComment(nextComment) &&
isIndentableBlockComment(nextComment) &&
locEnd(comment) === locStart(nextComment)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loc compare should be faster than isIndentableBlockComment check, let's move this condition up

) {
ast.comments.splice(i + 1, 1);
ast.comments[i] = {
...comment,
value: comment.value + "*//*" + nextComment.value,
range: [locStart(comment), locEnd(nextComment)],
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already mutate the array, let's mutate the comment directly?

}
}
}
/* eslint-enable prettier-internal-rules/no-node-comments */

return ast;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/language-js/print/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ function printIndentableBlockComment(comment) {
];
}

export { printComment };
export { printComment, isIndentableBlockComment };
220 changes: 220 additions & 0 deletions tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3144,6 +3144,226 @@ function HelloWorld() {
================================================================================
`;

exports[`jsdoc-nestled.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
const issues = {
see: "#7724 and #12653"
/** Trailing comment 1 (not nestled as both comments should be multiline for that) *//**
* Trailing comment 2
*/
};

/**
* @template T
* @param {Type} type
* @param {T} value
* @return {Value}
*//**
* @param {Type} type
* @return {Value}
*/
function value(type, value) {
if (arguments.length === 2) {
return new ConcreteValue(type, value);
} else {
return new Value(type);
}
}

/** Trailing nestled comment 1
*//** Trailing nestled comment 2
*/

=====================================output=====================================
const issues = {
see: "#7724 and #12653",
/** Trailing comment 1 (not nestled as both comments should be multiline for that) */ /**
* Trailing comment 2
*/
}

/**
* @template T
* @param {Type} type
* @param {T} value
* @return {Value}
*//**
* @param {Type} type
* @return {Value}
*/
function value(type, value) {
if (arguments.length === 2) {
return new ConcreteValue(type, value)
} else {
return new Value(type)
}
}

/** Trailing nestled comment 1
*//** Trailing nestled comment 2
*/

================================================================================
`;

exports[`jsdoc-nestled.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
const issues = {
see: "#7724 and #12653"
/** Trailing comment 1 (not nestled as both comments should be multiline for that) *//**
* Trailing comment 2
*/
};

/**
* @template T
* @param {Type} type
* @param {T} value
* @return {Value}
*//**
* @param {Type} type
* @return {Value}
*/
function value(type, value) {
if (arguments.length === 2) {
return new ConcreteValue(type, value);
} else {
return new Value(type);
}
}

/** Trailing nestled comment 1
*//** Trailing nestled comment 2
*/

=====================================output=====================================
const issues = {
see: "#7724 and #12653",
/** Trailing comment 1 (not nestled as both comments should be multiline for that) */ /**
* Trailing comment 2
*/
};

/**
* @template T
* @param {Type} type
* @param {T} value
* @return {Value}
*//**
* @param {Type} type
* @return {Value}
*/
function value(type, value) {
if (arguments.length === 2) {
return new ConcreteValue(type, value);
} else {
return new Value(type);
}
}

/** Trailing nestled comment 1
*//** Trailing nestled comment 2
*/

================================================================================
`;

exports[`jsdoc-nestled-dangling.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
{{{{{{{
o={
/**
* A
*//**
* B
*/

}
}}}}}}}

=====================================output=====================================
{
{
{
{
{
{
{
o = {
/**
* A
*//**
* B
*/
}
}
}
}
}
}
}
}

================================================================================
`;

exports[`jsdoc-nestled-dangling.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
{{{{{{{
o={
/**
* A
*//**
* B
*/

}
}}}}}}}

=====================================output=====================================
{
{
{
{
{
{
{
o = {
/**
* A
*//**
* B
*/
};
}
}
}
}
}
}
}

================================================================================
`;

exports[`jsx.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
Expand Down