Skip to content

Commit

Permalink
feat: programatic api for setting compiler (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Jul 27, 2023
1 parent 17dc4d6 commit 710f306
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
4 changes: 0 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 13 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ const expressionParser: CustomParser = (code, parsers, options) => {
};
};

let currentCompiler: typeof Compiler;
let currentConfig: Config;

export const languages: SupportLanguage[] = [
{
name: "marko",
Expand Down Expand Up @@ -127,10 +130,8 @@ export const parsers: Record<string, Parser<Node>> = {
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(
Expand All @@ -140,27 +141,14 @@ export const parsers: Record<string, Parser<Node>> = {
}
})() 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,
Expand Down Expand Up @@ -226,7 +214,7 @@ export const printers: Record<string, Printer<Node>> = {
"marko-ast": {
print(path, opts, print) {
const node = path.getValue();
const t = opts.markoCompiler!.types;
const t = currentCompiler.types;

switch (node.type) {
case "File":
Expand Down Expand Up @@ -722,7 +710,7 @@ export const printers: Record<string, Printer<Node>> = {
},
embed(path, print, toDoc, opts) {
const node = path.getValue();
const t = opts.markoCompiler!.types;
const t = currentCompiler.types;

switch (node.type) {
case "_MarkoEmbed":
Expand Down Expand Up @@ -838,6 +826,11 @@ export const printers: Record<string, Printer<Node>> = {
},
};

export function setCompiler(compiler: typeof Compiler, config: Config) {
currentCompiler = compiler;
currentConfig = config;
}

function printSpecialDeclaration(
path: AstPath<Node>,
prefix: string,
Expand Down

0 comments on commit 710f306

Please sign in to comment.