1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | export type LiteralUnion<T extends U, U = string> = T | (Pick<U, never> & { _?: never });
|
21 |
|
22 | export type AST = any;
|
23 | export type Doc = doc.builders.Doc;
|
24 |
|
25 |
|
26 | export interface FastPath<T = any> {
|
27 | stack: T[];
|
28 | getName(): null | PropertyKey;
|
29 | getValue(): T;
|
30 | getNode(count?: number): null | T;
|
31 | getParentNode(count?: number): null | T;
|
32 | call<U>(callback: (path: this) => U, ...names: PropertyKey[]): U;
|
33 | each(callback: (path: this) => void, ...names: PropertyKey[]): void;
|
34 | map<U>(callback: (path: this, index: number) => U, ...names: PropertyKey[]): U[];
|
35 | }
|
36 |
|
37 | export type BuiltInParser = (text: string, options?: any) => AST;
|
38 | export type BuiltInParserName =
|
39 | | 'angular'
|
40 | | 'babel-flow'
|
41 | | 'babel-ts'
|
42 | | 'babel'
|
43 | | 'css'
|
44 | | 'espree'
|
45 | | 'flow'
|
46 | | 'graphql'
|
47 | | 'html'
|
48 | | 'json-stringify'
|
49 | | 'json'
|
50 | | 'json5'
|
51 | | 'less'
|
52 | | 'lwc'
|
53 | | 'markdown'
|
54 | | 'mdx'
|
55 | | 'meriyah'
|
56 | | 'scss'
|
57 | | 'typescript'
|
58 | | 'vue'
|
59 | | 'yaml';
|
60 | export type BuiltInParsers = Record<BuiltInParserName, BuiltInParser>;
|
61 |
|
62 | export type CustomParser = (text: string, parsers: BuiltInParsers, options: Options) => AST;
|
63 |
|
64 | export interface Options extends Partial<RequiredOptions> {}
|
65 | export interface RequiredOptions extends doc.printer.Options {
|
66 | |
67 |
|
68 |
|
69 |
|
70 | semi: boolean;
|
71 | |
72 |
|
73 |
|
74 |
|
75 | singleQuote: boolean;
|
76 | |
77 |
|
78 |
|
79 |
|
80 | jsxSingleQuote: boolean;
|
81 | |
82 |
|
83 |
|
84 |
|
85 | trailingComma: 'none' | 'es5' | 'all';
|
86 | |
87 |
|
88 |
|
89 |
|
90 | bracketSpacing: boolean;
|
91 | |
92 |
|
93 |
|
94 |
|
95 | jsxBracketSameLine: boolean;
|
96 | |
97 |
|
98 |
|
99 |
|
100 | rangeStart: number;
|
101 | |
102 |
|
103 |
|
104 |
|
105 | rangeEnd: number;
|
106 | |
107 |
|
108 |
|
109 | parser: LiteralUnion<BuiltInParserName> | CustomParser;
|
110 | |
111 |
|
112 |
|
113 | filepath: string;
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 | requirePragma: boolean;
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | insertPragma: boolean;
|
128 | |
129 |
|
130 |
|
131 |
|
132 |
|
133 | proseWrap: 'always' | 'never' | 'preserve';
|
134 | |
135 |
|
136 |
|
137 |
|
138 | arrowParens: 'avoid' | 'always';
|
139 | |
140 |
|
141 |
|
142 | plugins: Array<string | Plugin>;
|
143 | |
144 |
|
145 |
|
146 |
|
147 | htmlWhitespaceSensitivity: 'css' | 'strict' | 'ignore';
|
148 | |
149 |
|
150 |
|
151 |
|
152 | endOfLine: 'auto' | 'lf' | 'crlf' | 'cr';
|
153 | |
154 |
|
155 |
|
156 |
|
157 | quoteProps: 'as-needed' | 'consistent' | 'preserve';
|
158 | |
159 |
|
160 |
|
161 |
|
162 | vueIndentScriptAndStyle: boolean;
|
163 | |
164 |
|
165 |
|
166 |
|
167 | embeddedLanguageFormatting: 'auto' | 'off';
|
168 | }
|
169 |
|
170 | export interface ParserOptions<T = any> extends RequiredOptions {
|
171 | locStart: (node: T) => number;
|
172 | locEnd: (node: T) => number;
|
173 | originalText: string;
|
174 | }
|
175 |
|
176 | export interface Plugin<T = any> {
|
177 | languages?: SupportLanguage[];
|
178 | parsers?: { [parserName: string]: Parser<T> };
|
179 | printers?: { [astFormat: string]: Printer<T> };
|
180 | options?: SupportOptions;
|
181 | defaultOptions?: Partial<RequiredOptions>;
|
182 | }
|
183 |
|
184 | export interface Parser<T = any> {
|
185 | parse: (text: string, parsers: { [parserName: string]: Parser }, options: ParserOptions<T>) => T;
|
186 | astFormat: string;
|
187 | hasPragma?: (text: string) => boolean;
|
188 | locStart: (node: T) => number;
|
189 | locEnd: (node: T) => number;
|
190 | preprocess?: (text: string, options: ParserOptions<T>) => string;
|
191 | }
|
192 |
|
193 | export interface Printer<T = any> {
|
194 | print(path: FastPath<T>, options: ParserOptions<T>, print: (path: FastPath<T>) => Doc): Doc;
|
195 | embed?: (
|
196 | path: FastPath<T>,
|
197 | print: (path: FastPath<T>) => Doc,
|
198 | textToDoc: (text: string, options: Options) => Doc,
|
199 | options: ParserOptions<T>,
|
200 | ) => Doc | null;
|
201 | insertPragma?: (text: string) => string;
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 | massageAstNode?: (node: any, newNode: any, parent: any) => any;
|
208 | hasPrettierIgnore?: (path: FastPath<T>) => boolean;
|
209 | canAttachComment?: (node: T) => boolean;
|
210 | willPrintOwnComments?: (path: FastPath<T>) => boolean;
|
211 | printComment?: (commentPath: FastPath<T>, options: ParserOptions<T>) => Doc;
|
212 | handleComments?: {
|
213 | ownLine?: (
|
214 | commentNode: any,
|
215 | text: string,
|
216 | options: ParserOptions<T>,
|
217 | ast: T,
|
218 | isLastComment: boolean,
|
219 | ) => boolean;
|
220 | endOfLine?: (
|
221 | commentNode: any,
|
222 | text: string,
|
223 | options: ParserOptions<T>,
|
224 | ast: T,
|
225 | isLastComment: boolean,
|
226 | ) => boolean;
|
227 | remaining?: (
|
228 | commentNode: any,
|
229 | text: string,
|
230 | options: ParserOptions<T>,
|
231 | ast: T,
|
232 | isLastComment: boolean,
|
233 | ) => boolean;
|
234 | };
|
235 | }
|
236 |
|
237 | export interface CursorOptions extends Options {
|
238 | |
239 |
|
240 |
|
241 | cursorOffset: number;
|
242 | rangeStart?: never;
|
243 | rangeEnd?: never;
|
244 | }
|
245 |
|
246 | export interface CursorResult {
|
247 | formatted: string;
|
248 | cursorOffset: number;
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | export function format(source: string, options?: Options): string;
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 | export function check(source: string, options?: Options): boolean;
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | export function formatWithCursor(source: string, options: CursorOptions): CursorResult;
|
269 |
|
270 | export interface ResolveConfigOptions {
|
271 | |
272 |
|
273 |
|
274 | useCache?: boolean;
|
275 | |
276 |
|
277 |
|
278 | config?: string;
|
279 | |
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | editorconfig?: boolean;
|
289 | }
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | export function resolveConfig(filePath: string, options?: ResolveConfigOptions): Promise<null | Options>;
|
305 | export namespace resolveConfig {
|
306 | function sync(filePath: string, options?: ResolveConfigOptions): null | Options;
|
307 | }
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 | export function resolveConfigFile(filePath?: string): Promise<null | string>;
|
321 | export namespace resolveConfigFile {
|
322 | function sync(filePath?: string): null | string;
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | export function clearConfigCache(): void;
|
330 |
|
331 | export interface SupportLanguage {
|
332 | name: string;
|
333 | since?: string;
|
334 | parsers: BuiltInParserName[] | string[];
|
335 | group?: string;
|
336 | tmScope?: string;
|
337 | aceMode?: string;
|
338 | codemirrorMode?: string;
|
339 | codemirrorMimeType?: string;
|
340 | aliases?: string[];
|
341 | extensions?: string[];
|
342 | filenames?: string[];
|
343 | linguistLanguageId?: number;
|
344 | vscodeLanguageIds?: string[];
|
345 | }
|
346 |
|
347 | export interface SupportOptionRange {
|
348 | start: number;
|
349 | end: number;
|
350 | step: number;
|
351 | }
|
352 |
|
353 | export type SupportOptionType = 'int' | 'boolean' | 'choice' | 'path';
|
354 |
|
355 | export interface BaseSupportOption<Type extends SupportOptionType> {
|
356 | readonly name?: string;
|
357 | since: string;
|
358 | category: string;
|
359 | |
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 | type: Type;
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | deprecated?: true | string;
|
374 | |
375 |
|
376 |
|
377 |
|
378 | description?: string;
|
379 | }
|
380 |
|
381 | export interface IntSupportOption extends BaseSupportOption<'int'> {
|
382 | default: number;
|
383 | array?: false;
|
384 | range?: SupportOptionRange;
|
385 | }
|
386 |
|
387 | export interface IntArraySupportOption extends BaseSupportOption<'int'> {
|
388 | default: Array<{ value: number[] }>;
|
389 | array: true;
|
390 | }
|
391 |
|
392 | export interface BooleanSupportOption extends BaseSupportOption<'boolean'> {
|
393 | default: boolean;
|
394 | array?: false;
|
395 | description: string;
|
396 | oppositeDescription?: string;
|
397 | }
|
398 |
|
399 | export interface BooleanArraySupportOption extends BaseSupportOption<'boolean'> {
|
400 | default: Array<{ value: boolean[] }>;
|
401 | array: true;
|
402 | }
|
403 |
|
404 | export interface ChoiceSupportOption<Value = any> extends BaseSupportOption<'choice'> {
|
405 | default: Value | Array<{ since: string; value: Value }>;
|
406 | description: string;
|
407 | choices: Array<{
|
408 | since?: string;
|
409 | value: Value;
|
410 | description: string;
|
411 | }>;
|
412 | }
|
413 |
|
414 | export interface PathSupportOption extends BaseSupportOption<'path'> {
|
415 | default: string;
|
416 | array?: false;
|
417 | }
|
418 |
|
419 | export interface PathArraySupportOption extends BaseSupportOption<'path'> {
|
420 | default: Array<{ value: string[] }>;
|
421 | array: true;
|
422 | }
|
423 |
|
424 | export type SupportOption =
|
425 | | IntSupportOption
|
426 | | IntArraySupportOption
|
427 | | BooleanSupportOption
|
428 | | BooleanArraySupportOption
|
429 | | ChoiceSupportOption
|
430 | | PathSupportOption
|
431 | | PathArraySupportOption;
|
432 |
|
433 | export interface SupportOptions extends Record<string, SupportOption> {}
|
434 |
|
435 | export interface SupportInfo {
|
436 | languages: SupportLanguage[];
|
437 | options: SupportOption[];
|
438 | }
|
439 |
|
440 | export interface FileInfoOptions {
|
441 | ignorePath?: string;
|
442 | withNodeModules?: boolean;
|
443 | plugins?: string[];
|
444 | resolveConfig?: boolean;
|
445 | }
|
446 |
|
447 | export interface FileInfoResult {
|
448 | ignored: boolean;
|
449 | inferredParser: string | null;
|
450 | }
|
451 |
|
452 | export function getFileInfo(filePath: string, options?: FileInfoOptions): Promise<FileInfoResult>;
|
453 |
|
454 | export namespace getFileInfo {
|
455 | function sync(filePath: string, options?: FileInfoOptions): FileInfoResult;
|
456 | }
|
457 |
|
458 |
|
459 |
|
460 |
|
461 | export function getSupportInfo(): SupportInfo;
|
462 |
|
463 |
|
464 |
|
465 |
|
466 | export const version: string;
|
467 |
|
468 |
|
469 | export namespace util {
|
470 | function isNextLineEmpty(text: string, node: any, locEnd: (node: any) => number): boolean;
|
471 | function isNextLineEmptyAfterIndex(text: string, index: number): boolean;
|
472 | function isPreviousLineEmpty(text: string, node: any, locStart: (node: any) => number): boolean;
|
473 | function getNextNonSpaceNonCommentCharacterIndex(text: string, node: any, options: ParserOptions): number;
|
474 | function makeString(rawContent: string, enclosingQuote: "'" | '"', unescapeUnnecessaryEscapes: boolean): string;
|
475 | function addLeadingComment(node: any, commentNode: any): void;
|
476 | function addDanglingComment(node: any, commentNode: any): void;
|
477 | function addTrailingComment(node: any, commentNode: any): void;
|
478 | }
|
479 |
|
480 |
|
481 | export namespace doc {
|
482 | namespace builders {
|
483 | type Doc =
|
484 | | string
|
485 | | Align
|
486 | | BreakParent
|
487 | | Concat
|
488 | | Fill
|
489 | | Group
|
490 | | IfBreak
|
491 | | Indent
|
492 | | Line
|
493 | | LineSuffix
|
494 | | LineSuffixBoundary
|
495 | | Trim
|
496 | | Cursor;
|
497 |
|
498 | interface Align {
|
499 | type: 'align';
|
500 | contents: Doc;
|
501 | n: number | string | { type: 'root' };
|
502 | }
|
503 |
|
504 | interface BreakParent {
|
505 | type: 'break-parent';
|
506 | }
|
507 |
|
508 | interface Concat {
|
509 | type: 'concat';
|
510 | parts: Doc[];
|
511 | }
|
512 |
|
513 | interface Fill {
|
514 | type: 'fill';
|
515 | parts: Doc[];
|
516 | }
|
517 |
|
518 | interface Group {
|
519 | type: 'group';
|
520 | contents: Doc;
|
521 | break: boolean;
|
522 | expandedStates: Doc[];
|
523 | }
|
524 |
|
525 | interface IfBreak {
|
526 | type: 'if-break';
|
527 | breakContents: Doc;
|
528 | flatContents: Doc;
|
529 | }
|
530 |
|
531 | interface Indent {
|
532 | type: 'indent';
|
533 | contents: Doc;
|
534 | }
|
535 |
|
536 | interface Line {
|
537 | type: 'line';
|
538 | soft?: boolean;
|
539 | hard?: boolean;
|
540 | literal?: boolean;
|
541 | }
|
542 |
|
543 | interface LineSuffix {
|
544 | type: 'line-suffix';
|
545 | contents: Doc;
|
546 | }
|
547 |
|
548 | interface LineSuffixBoundary {
|
549 | type: 'line-suffix-boundary';
|
550 | }
|
551 |
|
552 | interface Trim {
|
553 | type: 'trim';
|
554 | }
|
555 |
|
556 | interface Cursor {
|
557 | type: 'cursor';
|
558 | placeholder: symbol;
|
559 | }
|
560 |
|
561 | function addAlignmentToDoc(doc: Doc, size: number, tabWidth: number): Doc;
|
562 | function align(n: Align['n'], contents: Doc): Align;
|
563 | const breakParent: BreakParent;
|
564 | function concat(contents: Doc[]): Concat;
|
565 | function conditionalGroup(states: Doc[], opts?: { shouldBreak: boolean }): Group;
|
566 | function dedent(contents: Doc): Align;
|
567 | function dedentToRoot(contents: Doc): Align;
|
568 | function fill(parts: Doc[]): Fill;
|
569 | function group(contents: Doc, opts?: { shouldBreak: boolean }): Group;
|
570 | const hardline: Concat;
|
571 | function ifBreak(breakContents: Doc, flatContents: Doc): IfBreak;
|
572 | function indent(contents: Doc): Indent;
|
573 | function join(separator: Doc, parts: Doc[]): Concat;
|
574 | const line: Line;
|
575 | function lineSuffix(contents: Doc): LineSuffix;
|
576 | const lineSuffixBoundary: LineSuffixBoundary;
|
577 | const literalline: Concat;
|
578 | function markAsRoot(contents: Doc): Align;
|
579 | const softline: Line;
|
580 | const trim: Trim;
|
581 | const cursor: Cursor;
|
582 | }
|
583 | namespace debug {
|
584 | function printDocToDebug(doc: Doc): string;
|
585 | }
|
586 | namespace printer {
|
587 | function printDocToString(
|
588 | doc: Doc,
|
589 | options: Options,
|
590 | ): {
|
591 | formatted: string;
|
592 | cursorNodeStart?: number;
|
593 | cursorNodeText?: string;
|
594 | };
|
595 | interface Options {
|
596 | |
597 |
|
598 |
|
599 |
|
600 | printWidth: number;
|
601 | |
602 |
|
603 |
|
604 |
|
605 | tabWidth: number;
|
606 | |
607 |
|
608 |
|
609 |
|
610 | useTabs: boolean;
|
611 | parentParser?: string;
|
612 | embeddedInHtml: boolean;
|
613 | }
|
614 | }
|
615 | namespace utils {
|
616 | function isEmpty(doc: Doc): boolean;
|
617 | function isLineNext(doc: Doc): boolean;
|
618 | function willBreak(doc: Doc): boolean;
|
619 | function traverseDoc(
|
620 | doc: Doc,
|
621 | onEnter?: (doc: Doc) => void | boolean,
|
622 | onExit?: (doc: Doc) => void,
|
623 | shouldTraverseConditionalGroups?: boolean,
|
624 | ): void;
|
625 | function mapDoc<T>(doc: Doc, callback: (doc: Doc) => T): T;
|
626 | function propagateBreaks(doc: Doc): void;
|
627 | function removeLines(doc: Doc): Doc;
|
628 | function stripTrailingHardline(doc: Doc): Doc;
|
629 | }
|
630 | }
|