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

fix: getVisitorKeys throwing when called before parse #51

Merged
Merged
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
33 changes: 18 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
let currentCompiler: typeof Compiler;
let currentConfig: Config;

function getCurrentCompiler(): [typeof Compiler, Config] {
try {
return [
(currentCompiler ||= rootRequire("@marko/compiler")),
(currentConfig ||= rootRequire("@marko/compiler/config").default),
];
} catch (cause) {
throw new Error(

Check warning on line 55 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L55

Added line #L55 was not covered by tests
"You must have @marko/compiler installed to use prettier-plugin-marko.",
{ cause },
);
}
}

export const languages: SupportLanguage[] = [
{
name: "marko",
Expand Down Expand Up @@ -113,19 +127,7 @@
async parse(text, opts) {
const { filepath = defaultFilePath } = opts;

const [{ compile, types: t }, config] = (() => {
try {
return [
(currentCompiler ||= rootRequire("@marko/compiler")),
(currentConfig ||= rootRequire("@marko/compiler/config").default),
];
} catch (cause) {
throw new Error(
"You must have @marko/compiler installed to use prettier-plugin-marko.",
{ cause },
);
}
})() as [typeof Compiler, Config];
const [{ compile, types: t }, config] = getCurrentCompiler();

const translator = (() => {
try {
Expand Down Expand Up @@ -693,7 +695,7 @@
embed(path, opts) {
const node = path.getNode() as types.Node;
const type = node?.type;
const { types: t } = currentCompiler;
const [{ types: t }] = getCurrentCompiler();

switch (type) {
case "File":
Expand Down Expand Up @@ -1092,7 +1094,8 @@
};
},
getVisitorKeys(node) {
return (currentCompiler.types as any).VISITOR_KEYS[node.type] || emptyArr;
const [compiler] = getCurrentCompiler();
return (compiler.types as any).VISITOR_KEYS[node.type] || emptyArr;
},
},
};
Expand Down