Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate code from svelte ast
import prettier from "prettier/standalone";
import produce from "immer";
import { parse } from "svelte/compiler";
import type { Ast } from "svelte/types/compiler/interfaces";
// @ts-ignore
import * as sveltePlugin from "prettier-plugin-svelte";
function printTemplate(ast: Ast) {
const overridePlugin = produce(sveltePlugin, (d: any) => {
d.parsers.svelte.parse = () => {
return { ...ast, __isRoot: true };
};
});
return prettier.format(" dummy ", {
parser: "svelte",
plugins: [overridePlugin as any],
});
}
const code = "<div {id}>text</div>";
const parsed = parse(code);
// override ast
// @ts-ignore
parsed.html.children[0].name = "span";
const out = printTemplate(parsed);
console.log(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment