-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.ts
More file actions
175 lines (147 loc) · 4.65 KB
/
index.ts
File metadata and controls
175 lines (147 loc) · 4.65 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { Node } from '@babel/types';
import type {
AstPath,
doc,
Options as PrettierOptions,
Printer,
} from 'prettier';
import { printers as estreePrinters } from 'prettier/plugins/estree.js';
import { TEMPLATE_TAG_CLOSE, TEMPLATE_TAG_OPEN } from '../config.js';
import type { Options } from '../options.js';
import {
isGlimmerTemplate,
isGlimmerTemplateParent,
} from '../types/glimmer.js';
import { assert } from '../utils/assert.js';
import {
fixPreviousPrint,
saveCurrentPrintOnSiblingNode,
} from './ambiguity.js';
import { printTemplateContent, printTemplateTag } from './template.js';
const estreePrinter = estreePrinters['estree'] as Printer<Node | undefined>;
export const printer: Printer<Node | undefined> = {
...estreePrinter,
getVisitorKeys(node, nonTraversableKeys) {
if (node && isGlimmerTemplate(node)) {
return [];
}
return estreePrinter.getVisitorKeys?.(node, nonTraversableKeys) || [];
},
print(
path: AstPath<Node | undefined>,
options: Options,
print: (path: AstPath<Node | undefined>) => doc.builders.Doc,
args: unknown,
) {
const { node } = path;
if (isGlimmerTemplateParent(node)) {
if (checkPrettierIgnore(path)) {
return printRawText(path, options);
} else {
let printed = estreePrinter.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 doc.builders.Doc[],
path,
options,
print,
args,
);
}
return estreePrinter.print(path, options, print, args);
},
/** Prints embedded GlimmerExpressions/GlimmerTemplates. */
embed(path: AstPath<Node | undefined>, embedOptions: PrettierOptions) {
const { node } = path;
return async (textToDoc) => {
if (node && isGlimmerTemplate(node)) {
if (checkPrettierIgnore(path)) {
return printRawText(path, embedOptions as Options);
}
try {
const content = await printTemplateContent(
node.extra.template.contents,
textToDoc,
embedOptions as Options,
);
const printed = printTemplateTag(content);
saveCurrentPrintOnSiblingNode(path, printed);
return printed;
} catch (error) {
console.error(error);
const printed = [printRawText(path, embedOptions as Options)];
saveCurrentPrintOnSiblingNode(path, printed);
return printed;
}
}
// Nothing to embed, so move on to the regular printer.
return;
};
},
};
/** Remove the empty strings that Prettier added so we can manage them. */
function trimPrinted(printed: doc.builders.Doc[]): void {
while (docMatchesString(printed[0], '')) {
printed.shift();
}
while (docMatchesString(printed.at(-1), '')) {
printed.pop();
}
}
function printRawText(
{ node }: AstPath<Node | undefined>,
options: Options,
): string {
if (!node) {
return '';
}
if (isGlimmerTemplate(node)) {
return (
TEMPLATE_TAG_OPEN + node.extra.template.contents + TEMPLATE_TAG_CLOSE
);
}
assert('expected start', typeof node.start == 'number');
assert('expected end', typeof node.end == 'number');
return options.originalText.slice(node.start, node.end);
}
function hasPrettierIgnore(path: AstPath<Node | undefined>): boolean {
return path.node?.leadingComments?.at(-1)?.value.trim() === 'prettier-ignore';
}
function checkPrettierIgnore(path: AstPath<Node | undefined>): boolean {
return (
hasPrettierIgnore(path) ||
(!!path.getParentNode() &&
path.callParent((parent) => checkPrettierIgnore(parent)))
);
}
function docMatchesString(
doc: doc.builders.Doc | undefined,
string: string,
): doc is string {
return typeof doc === 'string' && doc.trim() === string;
}