index.d.ts 26 KB

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