-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
296 lines (268 loc) · 8.33 KB
/
utils.ts
File metadata and controls
296 lines (268 loc) · 8.33 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import type { Comment, Expression, TemplateLiteral } from "estree";
import type { AstPath, Doc, Options } from "prettier";
import { builders, utils } from "prettier/doc.js";
import type {
LiteralUnion,
OmitIndexSignature,
UnionToIntersection,
} from "type-fest";
import type { InternalPrintFun } from "../types.js";
const {
group,
indent,
softline,
lineSuffixBoundary,
addAlignmentToDoc,
align,
hardline,
} = builders;
const { mapDoc, replaceEndOfLine } = utils;
/**
* Get the alignment size for a string (counting tabs as tabWidth spaces).
*/
function getAlignmentSize(text: string, tabWidth: number): number {
let size = 0;
for (const char of text) {
if (char === "\t") {
// Tabs behave in a way that they are aligned to the nearest
// multiple of tabWidth:
// 0 -> 4, 1 -> 4, 2 -> 4, 3 -> 4
// 4 -> 8, 5 -> 8, 6 -> 8, 7 -> 8 ...
size = size + tabWidth - (size % tabWidth);
} else {
size++;
}
}
return size;
}
/**
* Get the indentation size for a template literal expression.
* This calculates the indentation based on the position of ${} in the template literal.
*/
function getIndentSize(value: string, tabWidth: number): number {
const lastNewlineIndex = value.lastIndexOf("\n");
if (lastNewlineIndex === -1) {
return 0;
}
// Get all the leading whitespaces after the last newline
const lastLine = value.slice(lastNewlineIndex + 1);
const leadingWhitespace = lastLine.match(/^[\t ]*/)?.[0] ?? "";
return getAlignmentSize(leadingWhitespace, tabWidth);
}
/**
* Compute the indent size for the expression at `index` inside `templateLiteral`.
*
* Strategy (on-demand): look at the quasi immediately preceding the expression;
* if it contains a newline, use the indentation after the last newline. If not,
* walk backwards to find the most recent newline in earlier quasis. This avoids
* keeping a global cache and keeps the logic local and simple.
*/
function getTemplateLiteralExpressionIndent(
templateLiteral: TemplateLiteral,
index: number,
tabWidth: number,
): { indentSize: number; previousQuasiText: string } {
let i = index;
const precedingQuasi = templateLiteral.quasis[i];
const previousQuasiText = precedingQuasi?.value.raw ?? "";
if (previousQuasiText.includes("\n")) {
return {
indentSize: getIndentSize(previousQuasiText, tabWidth),
previousQuasiText,
};
}
while (i-- > 0) {
const q = templateLiteral.quasis[i];
if (!q) break;
const text = q.value.raw;
if (text.includes("\n")) {
return { indentSize: getIndentSize(text, tabWidth), previousQuasiText };
}
}
return { indentSize: 0, previousQuasiText };
}
export function printTemplateExpression(
path: AstPath<Expression & { comments?: Comment[] }>,
print: InternalPrintFun,
templateLiteral: TemplateLiteral,
index: number,
tabWidth: number,
) {
const { node } = path;
let printed = print();
if (node?.comments?.length) {
printed = group([indent([softline, printed]), softline]);
}
// Calculate the indentation based on the position of ${} in the template literal
const { indentSize, previousQuasiText } = getTemplateLiteralExpressionIndent(
templateLiteral,
index,
tabWidth,
);
// Apply alignment based on the indentation
let expressionDoc: Doc = printed;
expressionDoc =
indentSize === 0 && previousQuasiText.endsWith("\n")
? align(Number.NEGATIVE_INFINITY, expressionDoc)
: addAlignmentToDoc(expressionDoc, indentSize, tabWidth);
return group(["${", expressionDoc, lineSuffixBoundary, "}"]);
}
export function printTemplateExpressions(
path: AstPath<TemplateLiteral>,
print: InternalPrintFun,
options?: Options,
) {
const tabWidth = options?.tabWidth ?? 2;
const templateLiteral = path.node;
return path.map(
(exprPath, index) =>
printTemplateExpression(
exprPath,
print,
templateLiteral,
index,
tabWidth,
),
"expressions",
);
}
export function simpleRehydrateDoc(
doc: Doc,
placeholderRegex: RegExp,
expressionDocs: Doc[],
/**
* How to handle newlines in the formatted content:
* - false: keep newlines as-is (default)
* - "hardline": replace newlines with hardline (adds indentation)
* - "literalline": replace newlines with literalline via replaceEndOfLine (preserves original indentation)
*/
newlineHandling: false | "hardline" | "literalline" = false,
) {
const contentDoc = mapDoc(doc, (doc) => {
if (typeof doc !== "string") {
return doc;
}
const parts = [];
const components = doc.split(placeholderRegex);
for (let i = 0; i < components.length; i++) {
let component = components[i]!;
if (i % 2 === 0) {
if (!component) {
continue;
}
component = component.replaceAll(/([\\`]|\${)/g, "\\$1");
if (newlineHandling === "literalline") {
// Use replaceEndOfLine which uses literalline (doesn't add indentation)
// This is useful for languages like CSS where the formatter already
// produces correctly indented output (e.g., in multi-line comments)
parts.push(replaceEndOfLine(component));
} else if (newlineHandling === "hardline") {
// Use hardline (adds indentation based on current indent level)
// This is useful for languages like SQL where the formatter produces
// output starting from column 0 and needs re-indentation
for (const c of component.split(/(\n)/)) {
c === "\n" ? parts.push(hardline) : parts.push(c);
}
} else {
parts.push(component);
}
} else {
const placeholderIndex = Number(component);
parts.push(expressionDocs[placeholderIndex]!);
}
}
return parts;
});
return contentDoc;
}
export function insertLanguage(
languages: string[],
language: string,
firstLanguage: string,
) {
if (language === firstLanguage) {
languages.unshift(language);
return;
}
let low = 0;
let high = languages.length;
while (low < high) {
const mid = (low + high) >>> 1;
if (languages[mid] === firstLanguage) {
languages.push(language);
return;
}
if (languages[mid]! < language) {
low = mid + 1;
} else {
high = mid;
}
}
languages.splice(low, 0, language);
}
export const randomUUID = (() => {
const dict = [...Array(26).keys()]
.map((key) => String.fromCharCode(key + 97))
.concat([...Array(10).keys()].map((key) => `${key}`));
return () => {
const uuidLength = 16;
let id = "";
for (let i = 0; i < uuidLength; ++i) {
id +=
dict[
Number.parseInt((Math.random() * dict.length).toFixed(0), 10) %
dict.length
];
}
return id;
};
})();
export function preparePlaceholder(leading = "p", trailing = "") {
const uuid1 = randomUUID();
const uuid2 = randomUUID();
const escapedLeading = escapeRegExp(leading);
const escapedTrailing = escapeRegExp(trailing);
const createPlaceholder = (index: number) => {
return `${leading}${uuid1}${index}${uuid2}${trailing}`;
};
const placeholderRegex = new RegExp(
`${escapedLeading}${uuid1}(\\d+)${uuid2}${escapedTrailing}`,
"ig",
);
return {
createPlaceholder,
placeholderRegex,
};
}
export function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
export function makeIdentifiersOptionName<T extends string>(language: T) {
return `${language}Identifiers` as const;
}
export function makeCommentsOptionName<T extends string>(language: T) {
return `${language}Comments` as const;
}
export function makeTagsOptionName<T extends string>(language: T) {
return `${language}Tags` as const;
}
export function makePluginOptionName<T extends string>(language: T) {
return `${language}Plugin` as const;
}
export function makeParserOptionName<T extends string>(language: T) {
return `${language}Parser` as const;
}
export type AutocompleteStringList<T extends string> = LiteralUnion<
T,
string
>[];
export type StringListToInterfaceKey<T extends readonly string[]> = {
[key in T[number]]: undefined;
};
export type Satisfies<U, T extends U> = T;
export type NormalizeOptions<T> = OmitIndexSignature<{
[k in keyof UnionToIntersection<T>]?: k extends keyof T
? T[k]
: UnionToIntersection<T>[k];
}>;
export const fallbackIndicator = "9ff2b366e8ca4c97b9aed1a29b5b94ed";