From 710f30666c72ff3fa0177866ab7c82e5aa5edd3e Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Thu, 27 Jul 2023 09:18:43 -0700 Subject: [PATCH] feat: programatic api for setting compiler (#43) --- src/constants.ts | 4 ---- src/index.ts | 33 +++++++++++++-------------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 0139cc9..40b46c5 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -4,15 +4,11 @@ declare module "prettier" { interface Options { markoSyntax?: "auto" | "html" | "concise"; markoAttrParen?: boolean; - markoCompiler?: typeof Compiler; - markoCompilerConfig?: Compiler.Config; } interface ParserOptions { markoSyntax?: "auto" | "html" | "concise"; markoAttrParen?: boolean; - markoCompiler?: typeof Compiler; - markoCompilerConfig?: Compiler.Config; // @internal markoLinePositions: number[]; // @internal diff --git a/src/index.ts b/src/index.ts index 821f93a..2997d47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,6 +54,9 @@ const expressionParser: CustomParser = (code, parsers, options) => { }; }; +let currentCompiler: typeof Compiler; +let currentConfig: Config; + export const languages: SupportLanguage[] = [ { name: "marko", @@ -127,10 +130,8 @@ export const parsers: Record> = { const [{ compileSync, types: t }, config] = (() => { try { return [ - (opts.markoCompiler ||= rootRequire("@marko/compiler")), - (opts.markoCompilerConfig ||= rootRequire( - "@marko/compiler/config" - ).default), + (currentCompiler ||= rootRequire("@marko/compiler")), + (currentConfig ||= rootRequire("@marko/compiler/config").default), ]; } catch (cause) { throw new Error( @@ -140,27 +141,14 @@ export const parsers: Record> = { } })() as [typeof Compiler, Config]; - const translator = (() => { - try { - return rootRequire(config.translator); - } catch (cause) { - throw new Error( - `Unable to load Marko translator at ${JSON.stringify( - config.translator - )}. Please install the Marko runtime.`, - { cause } - ); - } - })(); - const { ast } = compileSync(`${text}\n`, filepath, { + ...config, ast: true, code: false, optimize: false, output: "source", sourceMaps: false, writeVersionComment: false, - translator, babelConfig: { caller: { name: "@marko/prettier" }, babelrc: false, @@ -226,7 +214,7 @@ export const printers: Record> = { "marko-ast": { print(path, opts, print) { const node = path.getValue(); - const t = opts.markoCompiler!.types; + const t = currentCompiler.types; switch (node.type) { case "File": @@ -722,7 +710,7 @@ export const printers: Record> = { }, embed(path, print, toDoc, opts) { const node = path.getValue(); - const t = opts.markoCompiler!.types; + const t = currentCompiler.types; switch (node.type) { case "_MarkoEmbed": @@ -838,6 +826,11 @@ export const printers: Record> = { }, }; +export function setCompiler(compiler: typeof Compiler, config: Config) { + currentCompiler = compiler; + currentConfig = config; +} + function printSpecialDeclaration( path: AstPath, prefix: string,