UNPKG

svelte-ast-print

Version:

Serialize Svelte AST nodes into stringified syntax. A.k.a parse in reverse.

94 lines 2.92 kB
/** * ## Usage * * There are two ways to use this package. * * @example Recommended usage * * 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 {@link SV.SnippetBlock} which is a Svelte template-related node. * * ```ts * import type { AST } from "svelte/compiler"; * import { printSnippetBlock } from "svelte-ast-print/template/block"; * * // How you obtain the node is up to you. * // Either by building programmatically or from parsing * let node: AST.SnippetBlock; * * const stringified = printSnippetBlock(node); * ``` * * @example General usage * * 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 {@link esrap#print}. * * ```ts * 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); * ``` * * @example General usage - Svelte only * * If you _don't know_ which AST node from Svelte syntax you want to print, but you _know_ that it's Svelte. * * ```ts * 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); * ``` * * --- * * ## Options * * Every `print*` function accepts a second argument for options. Is optional and has some sensible defaults. * * @see {@link PrintOptions} * * @module svelte-ast-print */ import type { PrintOptions } from "./_internal/option.ts"; import { type Result } from "./_internal/shared.ts"; import type { SvelteOnlyNode } from "./_internal/type.ts"; import type { Node } from "./_internal/type.ts"; /** * @param n Svelte or JavaScript/TypeScript ESTree specification complaint AST node * @param opts printing options * @returns Stringified Svelte AST node * @since 1.0.0 * * @__NO_SIDE_EFFECTS__ */ export declare function print<N extends Node>(n: N, opts?: Partial<PrintOptions>): Result<N>; /** * @param n Svelte AST node * @param opts printing options * @returns Stringified Svelte AST node * @since 1.0.0 * * @__NO_SIDE_EFFECTS__ */ export declare function printSvelte<N extends SvelteOnlyNode>(n: N, opts?: Partial<PrintOptions>): Result<N>; //# sourceMappingURL=lib.d.ts.map