solc-typed-ast
solc-typed-ast copied to clipboard
Filtering by node type during parsing by `ASTReader`
Description
For my task I want to remove all the StructuredDocumentation nodes, it seems like it's possible without traversing the graph by simply ignoring this particular node type in ASTNodeProcessor.
Context
This is useful for stripping comments for smart contracts compression. Though, I can't think of any other node type that developers might want to blacklist.
Potential bug
There might be an issue with removing documentation from EventDefinition, FunctionDefinition and potentially other node types. Since they aren't inherited from ASTNodeWithChildren, it's impossible to invoke .removeChild on the StructuredDocumentation children.
The work-around I adopted was setting doc_node.text = '', however, when using ASTWriter it still outputs empty comment line:
///
event Transfer(address indexed from, address indexed to, uint256 value);
If there is an easier way to remove all StructuredDocumentation-nodes from AST, I'd be grateful if you provide an example. Thank you.
I was able to ignore StructuredDocumentation-node with custom writer mapping like this:
class NoopWriter extends ASTNodeWriter {
writeInner(node: ASTNode, writer: ASTWriter): SrcDesc {
return [''];
}
}
let ASTWriterMapping = DefaultASTWriterMapping;
let dummyWriter = new NoopWriter();
ASTWriterMapping.set(StructuredDocumentation, dummyWriter);
const formatter = new PrettyFormatter(4, 0);
const writer = new ASTWriter(ASTWriterMapping, formatter, '0.8.0');
for (const sourceUnit of sourceUnits) {
console.log(writer.write(sourceUnit));
}
Filtering during reading is not well defined, and may result in unexpected failures. I think the solution you have found with the printer is more elegant, so closing for now.