Svelte or ESTree AST node
printing options
Stringified Svelte AST node
TODO: Ask Svelte maintainers if Script
and SvelteOptions
were omittted from SvelteNode
intentionally - possibly forgotten to include
import fs from "node:fs";
import { print } from "svelte-ast-print";
import { parse } from "svelte/compiler";
const originalSvelteCode = fs.readFileSync("src/App.svelte", "utf-8");
let svelteAST = parse(originalSvelteCode, { modern: true });
// 👆 For now, only modern is supported.
// By default is 'false'.
// Is it planned to be 'true' from Svelte v6+
// ...
// Do some modifications on this AST...
// e.g. transform `<slot />` to `{@render children()}`
// ...
const output = print(svelteAST); // AST is now a stringified code output! 🎉
fs.writeFileSync("src/App.svelte", output, { encoding: " utf-8" });
Print AST SvelteAST.BaseNode as a string. Aka parse in reverse.
How does it work under the hood?
esrap
print_es to print ESTree specification-complaint AST nodeHow to use it?