UNPKG

prettier-plugin-solidity

Version:

A Prettier Plugin for automatically formatting your Solidity code.

32 lines 1.51 kB
import { NonterminalKind } from '@nomicfoundation/slang/cst'; import { util } from 'prettier'; import { locEnd } from '../../slang-utils/loc.js'; import addCollectionNodeFirstComment from './add-collection-node-first-comment.js'; const { addLeadingComment, addTrailingComment } = util; export default function handleWhileStatementComments({ text, precedingNode, enclosingNode, followingNode, comment }) { if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.kind) !== NonterminalKind.WhileStatement || !followingNode) { return false; } // We unfortunately have no way using the AST or location of nodes to know // if the comment is positioned before the condition parenthesis: // while (a /* comment */) {} // The only workaround I found is to look at the next character to see if // it is a ). const nextCharacter = util.getNextNonSpaceNonCommentCharacter(text, locEnd(comment)); if (nextCharacter === ')' || enclosingNode.condition === precedingNode) { addTrailingComment(precedingNode, comment); return true; } if (enclosingNode.body === followingNode) { if (followingNode.variant.kind === NonterminalKind.Block) { addCollectionNodeFirstComment(followingNode.variant.statements, comment); } else { addLeadingComment(followingNode, comment); } return true; } return false; } //# sourceMappingURL=handle-while-statement-comments.js.map