UNPKG

34.7 kBTypeScriptView Raw
1// Type definitions for prettier 2.7
2// Project: https://prettier.io
3// https://github.com/prettier/prettier
4// Definitions by: Ika <https://github.com/ikatyang>
5// Ifiok Jr. <https://github.com/ifiokjr>
6// Florian Imdahl <https://github.com/ffflorian>
7// Sosuke Suzuki <https://github.com/sosukesuzuki>
8// Christopher Quadflieg <https://github.com/Shinigami92>
9// Georgii Dolzhykov <https://github.com/thorn0>
10// JounQin <https://github.com/JounQin>
11// Chuah Chee Shian <https://github.com/shian15810>
12// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
13// Minimum TypeScript Version: 4.2
14
15// Adding export {} here to shut off automatic exporting from index.d.ts. There
16// are quite a few utility types here that don't need to be shipped with the
17// exported module.
18export {};
19
20// This utility is here to handle the case where you have an explicit union
21// between string literals and the generic string type. It would normally
22// resolve out to just the string type, but this generic LiteralUnion maintains
23// the intellisense of the original union.
24//
25// It comes from this issue: microsoft/TypeScript#29729:
26// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-700527227
27export type LiteralUnion<T extends U, U = string> = T | (Pick<U, never> & { _?: never | undefined });
28
29export type AST = any;
30export type Doc = doc.builders.Doc;
31
32// The type of elements that make up the given array T.
33type ArrayElement<T> = T extends Array<infer E> ? E : never;
34
35// A union of the properties of the given object that are arrays.
36type ArrayProperties<T> = { [K in keyof T]: NonNullable<T[K]> extends any[] ? K : never }[keyof T];
37
38// A union of the properties of the given array T that can be used to index it.
39// If the array is a tuple, then that's going to be the explicit indices of the
40// array, otherwise it's going to just be number.
41type IndexProperties<T extends { length: number }> = IsTuple<T> extends true
42 ? Exclude<Partial<T>['length'], T['length']>
43 : number;
44
45// Effectively performing T[P], except that it's telling TypeScript that it's
46// safe to do this for tuples, arrays, or objects.
47type IndexValue<T, P> = T extends any[] ? (P extends number ? T[P] : never) : P extends keyof T ? T[P] : never;
48
49// Determines if an object T is an array like string[] (in which case this
50// evaluates to false) or a tuple like [string] (in which case this evaluates to
51// true).
52// eslint-disable-next-line @typescript-eslint/no-unused-vars
53type IsTuple<T> = T extends [] ? true : T extends [infer First, ...infer Remain] ? IsTuple<Remain> : false;
54
55type CallProperties<T> = T extends any[] ? IndexProperties<T> : keyof T;
56type IterProperties<T> = T extends any[] ? IndexProperties<T> : ArrayProperties<T>;
57
58type CallCallback<T, U> = (path: AstPath<T>, index: number, value: any) => U;
59type EachCallback<T> = (path: AstPath<ArrayElement<T>>, index: number, value: any) => void;
60type MapCallback<T, U> = (path: AstPath<ArrayElement<T>>, index: number, value: any) => U;
61
62// https://github.com/prettier/prettier/blob/main/src/common/ast-path.js
63export class AstPath<T = any> {
64 constructor(value: T);
65 stack: T[];
66 callParent<U>(callback: (path: this) => U, count?: number): U;
67 getName(): PropertyKey | null;
68 getValue(): T;
69 getNode(count?: number): T | null;
70 getParentNode(count?: number): T | null;
71 match(...predicates: Array<(node: any, name: string | null, number: number | null) => boolean>): boolean;
72
73 // For each of the tree walk functions (call, each, and map) this provides 5
74 // strict type signatures, along with a fallback at the end if you end up
75 // calling more than 5 properties deep. This helps a lot with typing because
76 // for the majority of cases you're calling fewer than 5 properties, so the
77 // tree walk functions have a clearer understanding of what you're doing.
78 //
79 // Note that resolving these types is somewhat complicated, and it wasn't
80 // even supported until TypeScript 4.2 (before it would just say that the
81 // type instantiation was excessively deep and possibly infinite).
82
83 call<U>(callback: CallCallback<T, U>): U;
84 call<U, P1 extends CallProperties<T>>(callback: CallCallback<IndexValue<T, P1>, U>, prop1: P1): U;
85 call<U, P1 extends keyof T, P2 extends CallProperties<T[P1]>>(
86 callback: CallCallback<IndexValue<IndexValue<T, P1>, P2>, U>,
87 prop1: P1,
88 prop2: P2,
89 ): U;
90 call<U, P1 extends keyof T, P2 extends CallProperties<T[P1]>, P3 extends CallProperties<IndexValue<T[P1], P2>>>(
91 callback: CallCallback<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>, U>,
92 prop1: P1,
93 prop2: P2,
94 prop3: P3,
95 ): U;
96 call<
97 U,
98 P1 extends keyof T,
99 P2 extends CallProperties<T[P1]>,
100 P3 extends CallProperties<IndexValue<T[P1], P2>>,
101 P4 extends CallProperties<IndexValue<IndexValue<T[P1], P2>, P3>>
102 >(
103 callback: CallCallback<IndexValue<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>, P4>, U>,
104 prop1: P1,
105 prop2: P2,
106 prop3: P3,
107 prop4: P4,
108 ): U;
109 call<U, P extends PropertyKey>(
110 callback: CallCallback<any, U>,
111 prop1: P,
112 prop2: P,
113 prop3: P,
114 prop4: P,
115 ...props: P[]
116 ): U;
117
118 each(callback: EachCallback<T>): void;
119 each<P1 extends IterProperties<T>>(callback: EachCallback<IndexValue<T, P1>>, prop1: P1): void;
120 each<P1 extends keyof T, P2 extends IterProperties<T[P1]>>(
121 callback: EachCallback<IndexValue<IndexValue<T, P1>, P2>>,
122 prop1: P1,
123 prop2: P2,
124 ): void;
125 each<P1 extends keyof T, P2 extends IterProperties<T[P1]>, P3 extends IterProperties<IndexValue<T[P1], P2>>>(
126 callback: EachCallback<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>>,
127 prop1: P1,
128 prop2: P2,
129 prop3: P3,
130 ): void;
131 each<
132 P1 extends keyof T,
133 P2 extends IterProperties<T[P1]>,
134 P3 extends IterProperties<IndexValue<T[P1], P2>>,
135 P4 extends IterProperties<IndexValue<IndexValue<T[P1], P2>, P3>>
136 >(
137 callback: EachCallback<IndexValue<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>, P4>>,
138 prop1: P1,
139 prop2: P2,
140 prop3: P3,
141 prop4: P4,
142 ): void;
143 each(
144 callback: EachCallback<any[]>,
145 prop1: PropertyKey,
146 prop2: PropertyKey,
147 prop3: PropertyKey,
148 prop4: PropertyKey,
149 ...props: PropertyKey[]
150 ): void;
151
152 map<U>(callback: MapCallback<T, U>): U[];
153 map<U, P1 extends IterProperties<T>>(callback: MapCallback<IndexValue<T, P1>, U>, prop1: P1): U[];
154 map<U, P1 extends keyof T, P2 extends IterProperties<T[P1]>>(
155 callback: MapCallback<IndexValue<IndexValue<T, P1>, P2>, U>,
156 prop1: P1,
157 prop2: P2,
158 ): U[];
159 map<U, P1 extends keyof T, P2 extends IterProperties<T[P1]>, P3 extends IterProperties<IndexValue<T[P1], P2>>>(
160 callback: MapCallback<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>, U>,
161 prop1: P1,
162 prop2: P2,
163 prop3: P3,
164 ): U[];
165 map<
166 U,
167 P1 extends keyof T,
168 P2 extends IterProperties<T[P1]>,
169 P3 extends IterProperties<IndexValue<T[P1], P2>>,
170 P4 extends IterProperties<IndexValue<IndexValue<T[P1], P2>, P3>>
171 >(
172 callback: MapCallback<IndexValue<IndexValue<IndexValue<IndexValue<T, P1>, P2>, P3>, P4>, U>,
173 prop1: P1,
174 prop2: P2,
175 prop3: P3,
176 prop4: P4,
177 ): U[];
178 map<U>(
179 callback: MapCallback<any[], U>,
180 prop1: PropertyKey,
181 prop2: PropertyKey,
182 prop3: PropertyKey,
183 prop4: PropertyKey,
184 ...props: PropertyKey[]
185 ): U[];
186}
187
188/** @deprecated `FastPath` was renamed to `AstPath` */
189export type FastPath<T = any> = AstPath<T>;
190
191export type BuiltInParser = (text: string, options?: any) => AST;
192export type BuiltInParserName =
193 | 'angular'
194 | 'babel-flow'
195 | 'babel-ts'
196 | 'babel'
197 | 'css'
198 | 'espree'
199 | 'flow'
200 | 'glimmer'
201 | 'graphql'
202 | 'html'
203 | 'json-stringify'
204 | 'json'
205 | 'json5'
206 | 'less'
207 | 'lwc'
208 | 'markdown'
209 | 'mdx'
210 | 'meriyah'
211 | 'scss'
212 | 'typescript'
213 | 'vue'
214 | 'yaml';
215export type BuiltInParsers = Record<BuiltInParserName, BuiltInParser>;
216
217export type CustomParser = (text: string, parsers: BuiltInParsers, options: Options) => AST;
218
219/**
220 * For use in `.prettierrc.js`, `.prettierrc.cjs`, `prettier.config.js` or `prettier.config.cjs`.
221 */
222export interface Config extends Options {
223 overrides?: Array<{
224 files: string | string[];
225 excludeFiles?: string | string[];
226 options?: Options;
227 }>;
228}
229
230export interface Options extends Partial<RequiredOptions> {}
231
232export interface RequiredOptions extends doc.printer.Options {
233 /**
234 * Print semicolons at the ends of statements.
235 * @default true
236 */
237 semi: boolean;
238 /**
239 * Use single quotes instead of double quotes.
240 * @default false
241 */
242 singleQuote: boolean;
243 /**
244 * Use single quotes in JSX.
245 * @default false
246 */
247 jsxSingleQuote: boolean;
248 /**
249 * Print trailing commas wherever possible.
250 * @default 'es5'
251 */
252 trailingComma: 'none' | 'es5' | 'all';
253 /**
254 * Print spaces between brackets in object literals.
255 * @default true
256 */
257 bracketSpacing: boolean;
258 /**
259 * Put the `>` of a multi-line HTML (HTML, JSX, Vue, Angular) element at the end of the last line instead of being
260 * alone on the next line (does not apply to self closing elements).
261 * @default false
262 */
263 bracketSameLine: boolean;
264 /**
265 * Put the `>` of a multi-line JSX element at the end of the last line instead of being alone on the next line.
266 * @default false
267 * @deprecated use bracketSameLine instead
268 */
269 jsxBracketSameLine: boolean;
270 /**
271 * Format only a segment of a file.
272 * @default 0
273 */
274 rangeStart: number;
275 /**
276 * Format only a segment of a file.
277 * @default Infinity
278 */
279 rangeEnd: number;
280 /**
281 * Specify which parser to use.
282 */
283 parser: LiteralUnion<BuiltInParserName> | CustomParser;
284 /**
285 * Specify the input filepath. This will be used to do parser inference.
286 */
287 filepath: string;
288 /**
289 * Prettier can restrict itself to only format files that contain a special comment, called a pragma, at the top of the file.
290 * This is very useful when gradually transitioning large, unformatted codebases to prettier.
291 * @default false
292 */
293 requirePragma: boolean;
294 /**
295 * Prettier can insert a special @format marker at the top of files specifying that
296 * the file has been formatted with prettier. This works well when used in tandem with
297 * the --require-pragma option. If there is already a docblock at the top of
298 * the file then this option will add a newline to it with the @format marker.
299 * @default false
300 */
301 insertPragma: boolean;
302 /**
303 * By default, Prettier will wrap markdown text as-is since some services use a linebreak-sensitive renderer.
304 * In some cases you may want to rely on editor/viewer soft wrapping instead, so this option allows you to opt out.
305 * @default 'preserve'
306 */
307 proseWrap: 'always' | 'never' | 'preserve';
308 /**
309 * Include parentheses around a sole arrow function parameter.
310 * @default 'always'
311 */
312 arrowParens: 'avoid' | 'always';
313 /**
314 * Provide ability to support new languages to prettier.
315 */
316 plugins: Array<string | Plugin>;
317 /**
318 * Specify plugin directory paths to search for plugins if not installed in the same `node_modules` where prettier is located.
319 */
320 pluginSearchDirs: string[] | false;
321 /**
322 * How to handle whitespaces in HTML.
323 * @default 'css'
324 */
325 htmlWhitespaceSensitivity: 'css' | 'strict' | 'ignore';
326 /**
327 * Which end of line characters to apply.
328 * @default 'lf'
329 */
330 endOfLine: 'auto' | 'lf' | 'crlf' | 'cr';
331 /**
332 * Change when properties in objects are quoted.
333 * @default 'as-needed'
334 */
335 quoteProps: 'as-needed' | 'consistent' | 'preserve';
336 /**
337 * Whether or not to indent the code inside <script> and <style> tags in Vue files.
338 * @default false
339 */
340 vueIndentScriptAndStyle: boolean;
341 /**
342 * Control whether Prettier formats quoted code embedded in the file.
343 * @default 'auto'
344 */
345 embeddedLanguageFormatting: 'auto' | 'off';
346 /**
347 * Enforce single attribute per line in HTML, Vue and JSX.
348 * @default false
349 */
350 singleAttributePerLine: boolean;
351}
352
353export interface ParserOptions<T = any> extends RequiredOptions {
354 locStart: (node: T) => number;
355 locEnd: (node: T) => number;
356 originalText: string;
357}
358
359export interface Plugin<T = any> {
360 languages?: SupportLanguage[] | undefined;
361 parsers?: { [parserName: string]: Parser<T> } | undefined;
362 printers?: { [astFormat: string]: Printer<T> } | undefined;
363 options?: SupportOptions | undefined;
364 defaultOptions?: Partial<RequiredOptions> | undefined;
365}
366
367export interface Parser<T = any> {
368 parse: (text: string, parsers: { [parserName: string]: Parser }, options: ParserOptions<T>) => T;
369 astFormat: string;
370 hasPragma?: ((text: string) => boolean) | undefined;
371 locStart: (node: T) => number;
372 locEnd: (node: T) => number;
373 preprocess?: ((text: string, options: ParserOptions<T>) => string) | undefined;
374}
375
376export interface Printer<T = any> {
377 print(path: AstPath<T>, options: ParserOptions<T>, print: (path: AstPath<T>) => Doc, args?: unknown): Doc;
378 embed?:
379 | ((
380 path: AstPath<T>,
381 print: (path: AstPath<T>) => Doc,
382 textToDoc: (text: string, options: Options) => Doc,
383 options: ParserOptions<T>,
384 ) => Doc | null)
385 | undefined;
386 insertPragma?: ((text: string) => string) | undefined;
387 /**
388 * @returns `null` if you want to remove this node
389 * @returns `void` if you want to use modified newNode
390 * @returns anything if you want to replace the node with it
391 */
392 massageAstNode?: ((node: any, newNode: any, parent: any) => any) | undefined;
393 hasPrettierIgnore?: ((path: AstPath<T>) => boolean) | undefined;
394 canAttachComment?: ((node: T) => boolean) | undefined;
395 isBlockComment?: ((node: T) => boolean) | undefined;
396 willPrintOwnComments?: ((path: AstPath<T>) => boolean) | undefined;
397 printComment?: ((commentPath: AstPath<T>, options: ParserOptions<T>) => Doc) | undefined;
398 handleComments?:
399 | {
400 ownLine?:
401 | ((
402 commentNode: any,
403 text: string,
404 options: ParserOptions<T>,
405 ast: T,
406 isLastComment: boolean,
407 ) => boolean)
408 | undefined;
409 endOfLine?:
410 | ((
411 commentNode: any,
412 text: string,
413 options: ParserOptions<T>,
414 ast: T,
415 isLastComment: boolean,
416 ) => boolean)
417 | undefined;
418 remaining?:
419 | ((
420 commentNode: any,
421 text: string,
422 options: ParserOptions<T>,
423 ast: T,
424 isLastComment: boolean,
425 ) => boolean)
426 | undefined;
427 }
428 | undefined;
429}
430
431export interface CursorOptions extends Options {
432 /**
433 * Specify where the cursor is.
434 */
435 cursorOffset: number;
436 rangeStart?: never;
437 rangeEnd?: never;
438}
439
440export interface CursorResult {
441 formatted: string;
442 cursorOffset: number;
443}
444
445/**
446 * `format` is used to format text using Prettier. [Options](https://prettier.io/docs/en/options.html) may be provided to override the defaults.
447 */
448export function format(source: string, options?: Options): string;
449
450/**
451 * `check` checks to see if the file has been formatted with Prettier given those options and returns a `Boolean`.
452 * This is similar to the `--list-different` parameter in the CLI and is useful for running Prettier in CI scenarios.
453 */
454export function check(source: string, options?: Options): boolean;
455
456/**
457 * `formatWithCursor` both formats the code, and translates a cursor position from unformatted code to formatted code.
458 * This is useful for editor integrations, to prevent the cursor from moving when code is formatted.
459 *
460 * The `cursorOffset` option should be provided, to specify where the cursor is. This option cannot be used with `rangeStart` and `rangeEnd`.
461 */
462export function formatWithCursor(source: string, options: CursorOptions): CursorResult;
463
464export interface ResolveConfigOptions {
465 /**
466 * If set to `false`, all caching will be bypassed.
467 */
468 useCache?: boolean | undefined;
469 /**
470 * Pass directly the path of the config file if you don't wish to search for it.
471 */
472 config?: string | undefined;
473 /**
474 * If set to `true` and an `.editorconfig` file is in your project,
475 * Prettier will parse it and convert its properties to the corresponding prettier configuration.
476 * This configuration will be overridden by `.prettierrc`, etc. Currently,
477 * the following EditorConfig properties are supported:
478 * - indent_style
479 * - indent_size/tab_width
480 * - max_line_length
481 */
482 editorconfig?: boolean | undefined;
483}
484
485/**
486 * `resolveConfig` can be used to resolve configuration for a given source file,
487 * passing its path as the first argument. The config search will start at the
488 * file path and continue to search up the directory.
489 * (You can use `process.cwd()` to start searching from the current directory).
490 *
491 * A promise is returned which will resolve to:
492 *
493 * - An options object, providing a [config file](https://prettier.io/docs/en/configuration.html) was found.
494 * - `null`, if no file was found.
495 *
496 * The promise will be rejected if there was an error parsing the configuration file.
497 */
498export function resolveConfig(filePath: string, options?: ResolveConfigOptions): Promise<Options | null>;
499export namespace resolveConfig {
500 function sync(filePath: string, options?: ResolveConfigOptions): Options | null;
501}
502
503/**
504 * `resolveConfigFile` can be used to find the path of the Prettier configuration file,
505 * that will be used when resolving the config (i.e. when calling `resolveConfig`).
506 *
507 * A promise is returned which will resolve to:
508 *
509 * - The path of the configuration file.
510 * - `null`, if no file was found.
511 *
512 * The promise will be rejected if there was an error parsing the configuration file.
513 */
514export function resolveConfigFile(filePath?: string): Promise<string | null>;
515export namespace resolveConfigFile {
516 function sync(filePath?: string): string | null;
517}
518
519/**
520 * As you repeatedly call `resolveConfig`, the file system structure will be cached for performance. This function will clear the cache.
521 * Generally this is only needed for editor integrations that know that the file system has changed since the last format took place.
522 */
523export function clearConfigCache(): void;
524
525export interface SupportLanguage {
526 name: string;
527 since?: string | undefined;
528 parsers: BuiltInParserName[] | string[];
529 group?: string | undefined;
530 tmScope?: string | undefined;
531 aceMode?: string | undefined;
532 codemirrorMode?: string | undefined;
533 codemirrorMimeType?: string | undefined;
534 aliases?: string[] | undefined;
535 extensions?: string[] | undefined;
536 filenames?: string[] | undefined;
537 linguistLanguageId?: number | undefined;
538 vscodeLanguageIds?: string[] | undefined;
539 interpreters?: string[] | undefined;
540}
541
542export interface SupportOptionRange {
543 start: number;
544 end: number;
545 step: number;
546}
547
548export type SupportOptionType = 'int' | 'string' | 'boolean' | 'choice' | 'path';
549
550export type CoreCategoryType = 'Config' | 'Editor' | 'Format' | 'Other' | 'Output' | 'Global' | 'Special';
551
552export interface BaseSupportOption<Type extends SupportOptionType> {
553 readonly name?: string | undefined;
554 since: string;
555 /**
556 * Usually you can use {@link CoreCategoryType}
557 */
558 category: string;
559 /**
560 * The type of the option.
561 *
562 * When passing a type other than the ones listed below, the option is
563 * treated as taking any string as argument, and `--option <${type}>` will
564 * be displayed in --help.
565 */
566 type: Type;
567 /**
568 * Indicate that the option is deprecated.
569 *
570 * Use a string to add an extra message to --help for the option,
571 * for example to suggest a replacement option.
572 */
573 deprecated?: true | string | undefined;
574 /**
575 * Description to be displayed in --help. If omitted, the option won't be
576 * shown at all in --help.
577 */
578 description?: string | undefined;
579}
580
581export interface IntSupportOption extends BaseSupportOption<'int'> {
582 default?: number | undefined;
583 array?: false | undefined;
584 range?: SupportOptionRange | undefined;
585}
586
587export interface IntArraySupportOption extends BaseSupportOption<'int'> {
588 default?: Array<{ value: number[] }> | undefined;
589 array: true;
590}
591
592export interface StringSupportOption extends BaseSupportOption<'string'> {
593 default?: string | undefined;
594 array?: false | undefined;
595}
596
597export interface StringArraySupportOption extends BaseSupportOption<'string'> {
598 default?: Array<{ value: string[] }> | undefined;
599 array: true;
600}
601
602export interface BooleanSupportOption extends BaseSupportOption<'boolean'> {
603 default?: boolean | undefined;
604 array?: false | undefined;
605 description: string;
606 oppositeDescription?: string | undefined;
607}
608
609export interface BooleanArraySupportOption extends BaseSupportOption<'boolean'> {
610 default?: Array<{ value: boolean[] }> | undefined;
611 array: true;
612}
613
614export interface ChoiceSupportOption<Value = any> extends BaseSupportOption<'choice'> {
615 default?: Value | Array<{ since: string; value: Value }> | undefined;
616 description: string;
617 choices: Array<{
618 since?: string | undefined;
619 value: Value;
620 description: string;
621 }>;
622}
623
624export interface PathSupportOption extends BaseSupportOption<'path'> {
625 default?: string | undefined;
626 array?: false | undefined;
627}
628
629export interface PathArraySupportOption extends BaseSupportOption<'path'> {
630 default?: Array<{ value: string[] }> | undefined;
631 array: true;
632}
633
634export type SupportOption =
635 | IntSupportOption
636 | IntArraySupportOption
637 | StringSupportOption
638 | StringArraySupportOption
639 | BooleanSupportOption
640 | BooleanArraySupportOption
641 | ChoiceSupportOption
642 | PathSupportOption
643 | PathArraySupportOption;
644
645export interface SupportOptions extends Record<string, SupportOption> {}
646
647export interface SupportInfo {
648 languages: SupportLanguage[];
649 options: SupportOption[];
650}
651
652export interface FileInfoOptions {
653 ignorePath?: string | undefined;
654 withNodeModules?: boolean | undefined;
655 plugins?: string[] | undefined;
656 resolveConfig?: boolean | undefined;
657}
658
659export interface FileInfoResult {
660 ignored: boolean;
661 inferredParser: string | null;
662}
663
664export function getFileInfo(filePath: string, options?: FileInfoOptions): Promise<FileInfoResult>;
665
666export namespace getFileInfo {
667 function sync(filePath: string, options?: FileInfoOptions): FileInfoResult;
668}
669
670/**
671 * Returns an object representing the parsers, languages and file types Prettier supports for the current version.
672 */
673export function getSupportInfo(): SupportInfo;
674
675/**
676 * `version` field in `package.json`
677 */
678export const version: string;
679
680// https://github.com/prettier/prettier/blob/main/src/common/util-shared.js
681export namespace util {
682 interface SkipOptions {
683 backwards?: boolean | undefined;
684 }
685
686 type Quote = "'" | '"';
687
688 function addDanglingComment(node: any, comment: any, marker: any): void;
689 function addLeadingComment(node: any, comment: any): void;
690 function addTrailingComment(node: any, comment: any): void;
691 function getAlignmentSize(value: string, tabWidth: number, startIndex?: number): number;
692 function getIndentSize(value: string, tabWidth: number): number;
693 function getMaxContinuousCount(str: string, target: string): number;
694 function getNextNonSpaceNonCommentCharacterIndex<N>(
695 text: string,
696 node: N,
697 locEnd: (node: N) => number,
698 ): number | false;
699 function getStringWidth(text: string): number;
700 function hasNewline(text: string, index: number, opts?: SkipOptions): boolean;
701 function hasNewlineInRange(text: string, start: number, end: number): boolean;
702 function hasSpaces(text: string, index: number, opts?: SkipOptions): boolean;
703 function isNextLineEmpty<N>(text: string, node: N, locEnd: (node: N) => number): boolean;
704 function isNextLineEmptyAfterIndex(text: string, index: number): boolean;
705 function isPreviousLineEmpty<N>(text: string, node: N, locStart: (node: N) => number): boolean;
706 function makeString(rawContent: string, enclosingQuote: Quote, unescapeUnnecessaryEscapes?: boolean): string;
707 function skip(chars: string | RegExp): (text: string, index: number | false, opts?: SkipOptions) => number | false;
708 function skipEverythingButNewLine(text: string, index: number | false, opts?: SkipOptions): number | false;
709 function skipInlineComment(text: string, index: number | false): number | false;
710 function skipNewline(text: string, index: number | false, opts?: SkipOptions): number | false;
711 function skipSpaces(text: string, index: number | false, opts?: SkipOptions): number | false;
712 function skipToLineEnd(text: string, index: number | false, opts?: SkipOptions): number | false;
713 function skipTrailingComment(text: string, index: number | false): number | false;
714 function skipWhitespace(text: string, index: number | false, opts?: SkipOptions): number | false;
715}
716
717// https://github.com/prettier/prettier/blob/main/src/document/index.js
718export namespace doc {
719 namespace builders {
720 type DocCommand =
721 | Align
722 | BreakParent
723 | Concat
724 | Cursor
725 | Fill
726 | Group
727 | IfBreak
728 | Indent
729 | IndentIfBreak
730 | Label
731 | Line
732 | LineSuffix
733 | LineSuffixBoundary
734 | Trim;
735 type Doc = string | Doc[] | DocCommand;
736
737 interface Align {
738 type: 'align';
739 contents: Doc;
740 n: number | string | { type: 'root' };
741 }
742
743 interface BreakParent {
744 type: 'break-parent';
745 }
746
747 interface Concat {
748 type: 'concat';
749 parts: Doc[];
750 }
751
752 interface Cursor {
753 type: 'cursor';
754 placeholder: symbol;
755 }
756
757 interface Fill {
758 type: 'fill';
759 parts: Doc[];
760 }
761
762 interface Group {
763 type: 'group';
764 contents: Doc;
765 break: boolean;
766 expandedStates: Doc[];
767 }
768
769 interface HardlineWithoutBreakParent extends Line {
770 hard: true;
771 }
772
773 interface IfBreak {
774 type: 'if-break';
775 breakContents: Doc;
776 flatContents: Doc;
777 }
778
779 interface Indent {
780 type: 'indent';
781 contents: Doc;
782 }
783
784 interface IndentIfBreak {
785 type: 'indent-if-break';
786 }
787
788 interface Label {
789 type: 'label';
790 }
791
792 interface Line {
793 type: 'line';
794 soft?: boolean | undefined;
795 hard?: boolean | undefined;
796 literal?: boolean | undefined;
797 }
798
799 interface LineSuffix {
800 type: 'line-suffix';
801 contents: Doc;
802 }
803
804 interface LineSuffixBoundary {
805 type: 'line-suffix-boundary';
806 }
807
808 interface LiterallineWithoutBreakParent extends Line {
809 hard: true;
810 literal: true;
811 }
812
813 interface Softline extends Line {
814 soft: true;
815 }
816
817 interface Trim {
818 type: 'trim';
819 }
820
821 interface GroupOptions {
822 shouldBreak?: boolean | undefined;
823 id?: symbol | undefined;
824 }
825
826 function addAlignmentToDoc(doc: Doc, size: number, tabWidth: number): Doc;
827 /** @see [align](https://github.com/prettier/prettier/blob/main/commands.md#align) */
828 function align(widthOrString: Align['n'], doc: Doc): Align;
829 /** @see [breakParent](https://github.com/prettier/prettier/blob/main/commands.md#breakparent) */
830 const breakParent: BreakParent;
831 /**
832 * @see [concat](https://github.com/prettier/prettier/blob/main/commands.md#deprecated-concat)
833 * @deprecated use `Doc[]` instead
834 */
835 function concat(docs: Doc[]): Concat;
836 /** @see [conditionalGroup](https://github.com/prettier/prettier/blob/main/commands.md#conditionalgroup) */
837 function conditionalGroup(alternatives: Doc[], options?: GroupOptions): Group;
838 /** @see [dedent](https://github.com/prettier/prettier/blob/main/commands.md#dedent) */
839 function dedent(doc: Doc): Align;
840 /** @see [dedentToRoot](https://github.com/prettier/prettier/blob/main/commands.md#dedenttoroot) */
841 function dedentToRoot(doc: Doc): Align;
842 /** @see [fill](https://github.com/prettier/prettier/blob/main/commands.md#fill) */
843 function fill(docs: Doc[]): Fill;
844 /** @see [group](https://github.com/prettier/prettier/blob/main/commands.md#group) */
845 function group(doc: Doc, opts?: GroupOptions): Group;
846 /** @see [hardline](https://github.com/prettier/prettier/blob/main/commands.md#hardline) */
847 const hardline: Concat;
848 /** @see [hardlineWithoutBreakParent](https://github.com/prettier/prettier/blob/main/commands.md#hardlinewithoutbreakparent-and-literallinewithoutbreakparent) */
849 const hardlineWithoutBreakParent: HardlineWithoutBreakParent;
850 /** @see [ifBreak](https://github.com/prettier/prettier/blob/main/commands.md#ifbreak) */
851 function ifBreak(ifBreak: Doc, noBreak?: Doc, options?: { groupId?: symbol | undefined }): IfBreak;
852 /** @see [indent](https://github.com/prettier/prettier/blob/main/commands.md#indent) */
853 function indent(doc: Doc): Indent;
854 /** @see [indentIfBreak](https://github.com/prettier/prettier/blob/main/commands.md#indentifbreak) */
855 function indentIfBreak(doc: Doc, opts: { groupId: symbol; negate?: boolean | undefined }): IndentIfBreak;
856 /** @see [join](https://github.com/prettier/prettier/blob/main/commands.md#join) */
857 function join(sep: Doc, docs: Doc[]): Concat;
858 /** @see [label](https://github.com/prettier/prettier/blob/main/commands.md#label) */
859 function label(label: string, doc: Doc): Label;
860 /** @see [line](https://github.com/prettier/prettier/blob/main/commands.md#line) */
861 const line: Line;
862 /** @see [lineSuffix](https://github.com/prettier/prettier/blob/main/commands.md#linesuffix) */
863 function lineSuffix(suffix: Doc): LineSuffix;
864 /** @see [lineSuffixBoundary](https://github.com/prettier/prettier/blob/main/commands.md#linesuffixboundary) */
865 const lineSuffixBoundary: LineSuffixBoundary;
866 /** @see [literalline](https://github.com/prettier/prettier/blob/main/commands.md#literalline) */
867 const literalline: Concat;
868 /** @see [literallineWithoutBreakParent](https://github.com/prettier/prettier/blob/main/commands.md#hardlinewithoutbreakparent-and-literallinewithoutbreakparent) */
869 const literallineWithoutBreakParent: LiterallineWithoutBreakParent;
870 /** @see [markAsRoot](https://github.com/prettier/prettier/blob/main/commands.md#markasroot) */
871 function markAsRoot(doc: Doc): Align;
872 /** @see [softline](https://github.com/prettier/prettier/blob/main/commands.md#softline) */
873 const softline: Softline;
874 /** @see [trim](https://github.com/prettier/prettier/blob/main/commands.md#trim) */
875 const trim: Trim;
876 /** @see [cursor](https://github.com/prettier/prettier/blob/main/commands.md#cursor) */
877 const cursor: Cursor;
878 }
879 namespace debug {
880 function printDocToDebug(doc: Doc): string;
881 }
882 namespace printer {
883 function printDocToString(
884 doc: Doc,
885 options: Options,
886 ): {
887 formatted: string;
888 cursorNodeStart?: number | undefined;
889 cursorNodeText?: string | undefined;
890 };
891 interface Options {
892 /**
893 * Specify the line length that the printer will wrap on.
894 * @default 80
895 */
896 printWidth: number;
897 /**
898 * Specify the number of spaces per indentation-level.
899 * @default 2
900 */
901 tabWidth: number;
902 /**
903 * Indent lines with tabs instead of spaces
904 * @default false
905 */
906 useTabs: boolean;
907 parentParser?: string | undefined;
908 __embeddedInHtml?: boolean | undefined;
909 }
910 }
911 namespace utils {
912 function cleanDoc(doc: Doc): Doc;
913 function findInDoc<T = Doc>(doc: Doc, callback: (doc: Doc) => T, defaultValue: T): T;
914 function getDocParts(doc: Doc): Doc;
915 function isConcat(doc: Doc): boolean;
916 function isEmpty(doc: Doc): boolean;
917 function isLineNext(doc: Doc): boolean;
918 function mapDoc<T = Doc>(doc: Doc, callback: (doc: Doc) => T): T;
919 function normalizeDoc(doc: Doc): Doc;
920 function normalizeParts(parts: Doc[]): Doc[];
921 function propagateBreaks(doc: Doc): void;
922 function removeLines(doc: Doc): Doc;
923 function replaceNewlinesWithLiterallines(doc: Doc): Doc;
924 function stripTrailingHardline(doc: Doc): Doc;
925 function traverseDoc(
926 doc: Doc,
927 onEnter?: (doc: Doc) => void | boolean,
928 onExit?: (doc: Doc) => void,
929 shouldTraverseConditionalGroups?: boolean,
930 ): void;
931 function willBreak(doc: Doc): boolean;
932 }
933}
934
\No newline at end of file