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