-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathprinters.ts
More file actions
145 lines (122 loc) · 3.68 KB
/
printers.ts
File metadata and controls
145 lines (122 loc) · 3.68 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { type AstPath, doc as AST, type Printer } from 'prettier';
import { printers as prettierPrinters } from 'prettier/plugins/estree';
import type { PluginOptions } from './options.js';
import {
checkPrettierIgnore,
docMatchesString,
fixPreviousPrint,
printRawText,
printTemplateContent,
printTemplateTag,
saveCurrentPrintOnSiblingNode,
trimPrinted,
} from './printers/index.js';
import { isGlimmerTemplate, isGlimmerTemplateParent } from './types/glimmer.js';
import { assert } from './utils/assert.js';
import { type NodeType, PRINTER_NAME } from './utils/index.js';
const printer = prettierPrinters['estree'] as Printer<NodeType>;
function embed(path: AstPath<NodeType>, options: PluginOptions<NodeType>) {
const { node } = path;
return async (
textToDoc: (
text: string,
options: PluginOptions<NodeType>,
) => Promise<AST.builders.Doc>,
) => {
if (node && isGlimmerTemplate(node)) {
if (checkPrettierIgnore(path)) {
return printRawText(path, options);
}
try {
const content = await printTemplateContent(
node.extra.template.contents,
textToDoc,
options,
);
const printed = printTemplateTag(content);
if (options.templateExportDefault && path.parent?.type === 'Program') {
printed.unshift('export ', 'default ');
}
saveCurrentPrintOnSiblingNode(path, printed);
return printed;
} catch (error) {
console.error(error);
const printed = [printRawText(path, options)];
saveCurrentPrintOnSiblingNode(path, printed);
return printed;
}
}
// Nothing to embed, so move on to the regular printer.
return;
};
}
function getVisitorKeys(node: NodeType, nonTraversableKeys: Set<string>) {
if (node && isGlimmerTemplate(node)) {
return [];
}
return printer.getVisitorKeys?.(node, nonTraversableKeys) || [];
}
function print(
path: AstPath<NodeType>,
options: PluginOptions<NodeType>,
print: (path: AstPath<NodeType>) => AST.builders.Doc,
args?: unknown,
) {
const { node } = path;
if (isGlimmerTemplateParent(node)) {
if (checkPrettierIgnore(path)) {
return printRawText(path, options);
} else {
let printed = printer.print(path, options, print, args);
assert('Expected Glimmer doc to be an array', Array.isArray(printed));
trimPrinted(printed);
// Remove semicolons so we can manage them ourselves
if (docMatchesString(printed[0], ';')) {
printed.shift();
}
if (docMatchesString(printed.at(-1), ';')) {
printed.pop();
}
trimPrinted(printed);
// Always remove export default so we start with a blank slate
if (
docMatchesString(printed[0], 'export') &&
docMatchesString(printed[1], 'default')
) {
printed = printed.slice(2);
trimPrinted(printed);
}
if (options.templateExportDefault) {
printed.unshift('export ', 'default ');
}
saveCurrentPrintOnSiblingNode(path, printed);
return printed;
}
}
if (options.semi && node?.extra?.['prevTemplatePrinted']) {
fixPreviousPrint(
node.extra['prevTemplatePrinted'] as AST.builders.Doc[],
path,
options,
print,
args,
);
}
return printer.print(path, options, print, args);
}
function printPrettierIgnored(
path: AstPath<NodeType>,
options: PluginOptions<NodeType>,
) {
return printRawText(path, options);
}
export const printers: Record<string, Printer<NodeType>> = {
[PRINTER_NAME]: {
...printer,
// @ts-expect-error: Type <...> is not assignable to <...>
embed,
getVisitorKeys,
print,
printPrettierIgnored,
},
};