Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created February 9, 2021 08:20
Show Gist options
  • Save mizchi/d9be0ec969203f32f66e5f6eda9decb9 to your computer and use it in GitHub Desktop.
Save mizchi/d9be0ec969203f32f66e5f6eda9decb9 to your computer and use it in GitHub Desktop.
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);
@MoJiayong
Copy link

Hi,Do you have Java code to implement Telegram authorization login?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment