Sha256: 933252df2a80368a4cf88ad1a6fdc184eac27700ee5e1092b83f51f53966a65a

Contents?: true

Size: 1.35 KB

Versions: 10

Compression:

Stored size: 1.35 KB

Contents

const { concat, group, hardline, indent, join, line } = require("../prettier");

// Empty collections are array or hash literals that do not contain any
// contents. They can, however, have comments inside the body. You can solve
// this by having a child node inside the array that gets the comments attached
// to it, but that requires modifying the parser. Instead, we can just manually
// print out the non-leading comments here.
function printEmptyCollection(path, opts, startToken, endToken) {
  const node = path.getValue();

  // If there are no comments or only leading comments, then we can just print
  // out the start and end token and be done, as there are no comments inside
  // the body of this node.
  if (!node.comments || !node.comments.some((comment) => !comment.leading)) {
    return `${startToken}${endToken}`;
  }

  const comments = [];

  // For each comment, go through its path and print it out manually.
  const printComment = (commentPath) => {
    const comment = commentPath.getValue();

    if (!comment.leading) {
      comment.printed = true;
      comments.push(opts.printer.printComment(commentPath));
    }
  };

  path.each(printComment, "comments");

  return group(
    concat([
      startToken,
      indent(concat([hardline, join(hardline, comments)])),
      line,
      endToken
    ])
  );
}

module.exports = printEmptyCollection;

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
prettier-1.4.0 src/utils/printEmptyCollection.js
prettier-1.3.0 src/utils/printEmptyCollection.js
prettier-1.2.5 src/utils/printEmptyCollection.js
prettier-1.2.4 src/utils/printEmptyCollection.js
prettier-1.2.3 src/utils/printEmptyCollection.js
prettier-1.2.2 src/utils/printEmptyCollection.js
prettier-1.2.1 src/utils/printEmptyCollection.js
prettier-1.2.0 src/utils/printEmptyCollection.js
prettier-1.1.0 src/utils/printEmptyCollection.js
prettier-1.0.1 src/utils/printEmptyCollection.js