There are two ways to use this package.

If you know which specific AST node from Svelte syntax you want to print.
There are 4 categories of AST nodes available and their respective submodules:

  • CSS - "svelte-ast-print/css"
  • HTML - "svelte-ast-print/html"
  • JavaScript & TypeScript - "svelte-ast-print/js"
  • Svelte template - "svelte-ast-print/template"

Let's take for example SV.SnippetBlock which is a Svelte template-related node.

import type { AST } from "svelte/compiler";
import { printSvelteSnippet } from "svelte-ast-print/template";

// How you obtain the node is up to you.
// Either by building programmatically or from parsing
let node: AST.SnippetBlock;

const stringified = printSvelteSnippet(node);

If you don't know which AST node from Svelte syntax you want to print. It could be either JavaScript/TypeScript (ESTree specification complaint) or Svelte. Under the hood it uses esrap#print.

import type * as JS from "estree";
import type { AST as SV } from "svelte/compiler";
import { print } from "svelte-ast-print";

// How you obtain the node is up to you.
// Either by building programmatically or from parsing
let node: JS.Node | SV.BaseNode;

const stringified = print(node);

If you don't know which AST node from Svelte syntax you want to print, but you know that it's Svelte.

import type { AST } from "svelte/compiler";
import { printSvelte } from "svelte-ast-print";

// How you obtain the node is up to you.
// Either by building programmatically or from parsing
let node: AST.BaseNode;

const stringified = printSvelte(node);

Every print* function accepts a second argument for options. Is optional and has some sensible defaults.

Modules

<internal>

Functions

print
printSvelte