index.d.ts 25 KB

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