Closed maytinhdibo closed 2 months ago
Get your node, look for the range, then take your initial string and just do
const source = readFileSync("/path/to/your/file.sol").toString();
const ast = parse(source, {range: true});
// find node / subnode
const oneNode = ast.children[0];
const myCode = source.slice(oneNode.range[0], oneNode.range[1]);
console.log("MyCode", myCode);
And you're done. Make sure to pass range: true
in the parse options.
How to export code from AST?