Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: programatic api for setting compiler #43

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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