@calystral/prettier-plugin-solidity
Version:
prettier plugin for solidity
55 lines (45 loc) • 1.16 kB
JavaScript
const {
doc: {
builders: { concat, hardline }
},
util: { isNextLineEmptyAfterIndex }
} = require('prettier/standalone');
function printPreservingEmptyLines(path, key, options, print) {
const parts = [];
path.each((childPath) => {
const node = childPath.getValue();
const nodeType = node.type;
if (parts.length !== 0) {
parts.push(hardline);
}
if (childPath.getName() > 0) {
if (nodeType === 'ContractDefinition') {
if (parts[parts.length - 2] !== hardline) {
parts.push(hardline);
}
parts.push(hardline);
}
if (
nodeType === 'FunctionDefinition' &&
parts[parts.length - 2] !== hardline
) {
parts.push(hardline);
}
}
parts.push(print(childPath));
if (
isNextLineEmptyAfterIndex(
options.originalText,
options.locEnd(node) + 1
) ||
nodeType === 'FunctionDefinition'
) {
parts.push(hardline);
}
}, key);
if (parts.length > 1 && parts[parts.length - 1] === hardline) {
parts.pop();
}
return concat(parts);
}
module.exports = printPreservingEmptyLines;