• Print AST SvelteAST.BaseNode as a string. Aka parse in reverse.

    1. Firstly, it determines whether the provided AST node is unique node for Svelte SvelteAST.Node.
    2. Based on type check guard from above:
      • it uses either this package's Printer to print Svelte AST node,
      • otherwise it uses esrap print_es to print ESTree specification-complaint AST node

    Parameters

    Returns string

    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" });