Skip to content

Commit e808ce1

Browse files
CopilotHerrCai0907
andcommitted
Fix TypeError: initialize printer at module load time instead of during parse
Co-authored-by: HerrCai0907 <77575210+HerrCai0907@users.noreply.github.com>
1 parent ba2b1c9 commit e808ce1

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

src/plugin.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
import pluginTypescript from "prettier/plugins/typescript";
2+
import pluginEstree from "prettier/plugins/estree";
23
import { magic, preProcess } from "./replace.js";
34
import { builders } from "prettier/doc";
45

5-
let as_estree = {};
6+
// Get the estree printer from the estree plugin
7+
const baseEstreePrinter = pluginEstree.printers.estree;
68

7-
async function initPrinter(jsPlugin) {
8-
let estree = jsPlugin.printers.estree;
9-
estree = typeof estree == "function" ? await estree() : estree;
10-
Object.assign(as_estree, {
11-
...estree,
12-
printComment(commentPath, options) {
13-
let comment = commentPath.getValue().value;
14-
if (comment.startsWith(magic) && comment.endsWith(magic)) {
15-
let doc = [];
16-
if (commentPath.stack[commentPath.stack.length - 2] == 0) {
17-
doc.push(builders.hardline);
18-
}
19-
doc.push(comment.slice(magic.length, -magic.length));
20-
return doc;
21-
} else {
22-
return jsPlugin.printers.estree.printComment(commentPath, options);
9+
// Initialize the AssemblyScript estree printer at module load time
10+
const as_estree = {
11+
...baseEstreePrinter,
12+
printComment(commentPath, options) {
13+
let comment = commentPath.getValue().value;
14+
if (comment.startsWith(magic) && comment.endsWith(magic)) {
15+
let doc = [];
16+
if (commentPath.stack[commentPath.stack.length - 2] == 0) {
17+
doc.push(builders.hardline);
2318
}
24-
},
25-
});
26-
}
19+
doc.push(comment.slice(magic.length, -magic.length));
20+
return doc;
21+
} else {
22+
return baseEstreePrinter.printComment(commentPath, options);
23+
}
24+
},
25+
};
2726

28-
async function parse(text, options) {
29-
await initPrinter(options.plugins.find((plugin) => plugin.printers && plugin.printers.estree));
27+
function parse(text, options) {
3028
let ast = pluginTypescript.parsers.typescript.parse(text, options);
3129
return ast;
3230
}

0 commit comments

Comments
 (0)