prettier-plugin-solidity
Version:
A Prettier Plugin for automatically formatting your Solidity code.
28 lines • 1.26 kB
JavaScript
import { doc } from 'prettier';
import { NonterminalKind } from '@nomicfoundation/slang/cst';
import { getNodeMetadata, updateMetadata } from '../slang-utils/metadata.js';
import { SourceUnitMembers } from './SourceUnitMembers.js';
const { line } = doc.builders;
export class SourceUnit {
constructor(ast, options) {
this.kind = NonterminalKind.SourceUnit;
let metadata = getNodeMetadata(ast);
this.members = new SourceUnitMembers(ast.members, options);
metadata = updateMetadata(metadata, [this.members]);
// Because of comments being extracted like a russian doll, the order needs
// to be fixed at the end.
this.comments = metadata.comments.sort((a, b) => a.loc.start - b.loc.start);
this.loc = metadata.loc;
}
print(path, print, options) {
return [
path.call(print, 'members'),
// Prettier's Markdown formatter already appends a new line on code
// blocks, therefore we avoid trailing with a new line at the end of
// a file in this case.
// https://github.com/prettier-solidity/prettier-plugin-solidity/issues/764
options.parentParser ? '' : line
];
}
}
//# sourceMappingURL=SourceUnit.js.map