-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.js
More file actions
42 lines (38 loc) · 1.19 KB
/
plugin.js
File metadata and controls
42 lines (38 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pluginTypescript from "prettier/plugins/typescript";
import pluginEstree from "prettier/plugins/estree";
import { magic, preProcess } from "./replace.js";
import { builders } from "prettier/doc";
// Get the estree printer from the estree plugin
const baseEstreePrinter = pluginEstree.printers.estree;
// Initialize the AssemblyScript estree printer at module load time
const as_estree = {
...baseEstreePrinter,
printComment(commentPath, options) {
let comment = commentPath.getValue().value;
if (comment.startsWith(magic) && comment.endsWith(magic)) {
let doc = [];
if (commentPath.stack[commentPath.stack.length - 2] == 0) {
doc.push(builders.hardline);
}
doc.push(comment.slice(magic.length, -magic.length));
return doc;
} else {
return baseEstreePrinter.printComment(commentPath, options);
}
},
};
function parse(text, options) {
let ast = pluginTypescript.parsers.typescript.parse(text, options);
return ast;
}
export default {
parsers: {
typescript: {
...pluginTypescript.parsers.typescript,
parse,
astFormat: "as-estree",
preprocess: preProcess,
},
},
printers: { "as-estree": as_estree },
};