UNPKG

prettier-plugin-solidity

Version:

A Prettier Plugin for automatically formatting your Solidity code.

51 lines 2.32 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 handleIfStatementComments({ text, precedingNode, enclosingNode, followingNode, comment }) { if ((enclosingNode === null || enclosingNode === void 0 ? void 0 : enclosingNode.kind) !== NonterminalKind.IfStatement || !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: // if (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 === ')') { addTrailingComment(precedingNode, comment); return true; } // Comments before `else`: // - treat as leading comments of the elseBranch, if it's a BlockStatement // - treat as a dangling comment otherwise if (precedingNode === enclosingNode.body && followingNode === enclosingNode.elseBranch) { addTrailingComment(precedingNode.variant, comment); return true; } if (followingNode.kind === NonterminalKind.IfStatement) { if (followingNode.body.variant.kind === NonterminalKind.Block) { addCollectionNodeFirstComment(followingNode.body.variant.statements, comment); } else { addLeadingComment(followingNode.body.variant, comment); } return true; } // For comments positioned after the condition parenthesis in an if statement // before the consequent without brackets on, such as // if (a) /* comment */ 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-if-statement-comments.js.map