index.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { CST } from './parse-cst'
  2. import {
  3. AST,
  4. Alias,
  5. Collection,
  6. Merge,
  7. Node,
  8. Pair,
  9. Scalar,
  10. Schema,
  11. YAMLMap,
  12. YAMLSeq
  13. } from './types'
  14. import { Type, YAMLError, YAMLWarning } from './util'
  15. export { AST, CST }
  16. export { default as parseCST } from './parse-cst'
  17. /**
  18. * `yaml` defines document-specific options in three places: as an argument of
  19. * parse, create and stringify calls, in the values of `YAML.defaultOptions`,
  20. * and in the version-dependent `YAML.Document.defaults` object. Values set in
  21. * `YAML.defaultOptions` override version-dependent defaults, and argument
  22. * options override both.
  23. */
  24. export const defaultOptions: Options
  25. type Replacer = any[] | ((key: any, value: any) => boolean)
  26. type Reviver = (key: any, value: any) => any
  27. export interface Options extends Schema.Options {
  28. /**
  29. * Default prefix for anchors.
  30. *
  31. * Default: `'a'`, resulting in anchors `a1`, `a2`, etc.
  32. */
  33. anchorPrefix?: string
  34. /**
  35. * The number of spaces to use when indenting code.
  36. *
  37. * Default: `2`
  38. */
  39. indent?: number
  40. /**
  41. * Whether block sequences should be indented.
  42. *
  43. * Default: `true`
  44. */
  45. indentSeq?: boolean
  46. /**
  47. * Include references in the AST to each node's corresponding CST node.
  48. *
  49. * Default: `false`
  50. */
  51. keepCstNodes?: boolean
  52. /**
  53. * Store the original node type when parsing documents.
  54. *
  55. * Default: `true`
  56. */
  57. keepNodeTypes?: boolean
  58. /**
  59. * Keep `undefined` object values when creating mappings and return a Scalar
  60. * node when calling `YAML.stringify(undefined)`, rather than `undefined`.
  61. *
  62. * Default: `false`
  63. */
  64. keepUndefined?: boolean
  65. /**
  66. * When outputting JS, use Map rather than Object to represent mappings.
  67. *
  68. * Default: `false`
  69. */
  70. mapAsMap?: boolean
  71. /**
  72. * Prevent exponential entity expansion attacks by limiting data aliasing count;
  73. * set to `-1` to disable checks; `0` disallows all alias nodes.
  74. *
  75. * Default: `100`
  76. */
  77. maxAliasCount?: number
  78. /**
  79. * Include line position & node type directly in errors; drop their verbose source and context.
  80. *
  81. * Default: `true`
  82. */
  83. prettyErrors?: boolean
  84. /**
  85. * When stringifying, require keys to be scalars and to use implicit rather than explicit notation.
  86. *
  87. * Default: `false`
  88. */
  89. simpleKeys?: boolean
  90. /**
  91. * The YAML version used by documents without a `%YAML` directive.
  92. *
  93. * Default: `"1.2"`
  94. */
  95. version?: '1.0' | '1.1' | '1.2'
  96. }
  97. /**
  98. * Some customization options are availabe to control the parsing and
  99. * stringification of scalars. Note that these values are used by all documents.
  100. */
  101. export const scalarOptions: {
  102. binary: scalarOptions.Binary
  103. bool: scalarOptions.Bool
  104. int: scalarOptions.Int
  105. null: scalarOptions.Null
  106. str: scalarOptions.Str
  107. }
  108. export namespace scalarOptions {
  109. interface Binary {
  110. /**
  111. * The type of string literal used to stringify `!!binary` values.
  112. *
  113. * Default: `'BLOCK_LITERAL'`
  114. */
  115. defaultType: Scalar.Type
  116. /**
  117. * Maximum line width for `!!binary`.
  118. *
  119. * Default: `76`
  120. */
  121. lineWidth: number
  122. }
  123. interface Bool {
  124. /**
  125. * String representation for `true`. With the core schema, use `'true' | 'True' | 'TRUE'`.
  126. *
  127. * Default: `'true'`
  128. */
  129. trueStr: string
  130. /**
  131. * String representation for `false`. With the core schema, use `'false' | 'False' | 'FALSE'`.
  132. *
  133. * Default: `'false'`
  134. */
  135. falseStr: string
  136. }
  137. interface Int {
  138. /**
  139. * Whether integers should be parsed into BigInt values.
  140. *
  141. * Default: `false`
  142. */
  143. asBigInt: boolean
  144. }
  145. interface Null {
  146. /**
  147. * String representation for `null`. With the core schema, use `'null' | 'Null' | 'NULL' | '~' | ''`.
  148. *
  149. * Default: `'null'`
  150. */
  151. nullStr: string
  152. }
  153. interface Str {
  154. /**
  155. * The default type of string literal used to stringify values in general
  156. *
  157. * Default: `'PLAIN'`
  158. */
  159. defaultType: Scalar.Type
  160. /**
  161. * The default type of string literal used to stringify implicit key values
  162. *
  163. * Default: `'PLAIN'`
  164. */
  165. defaultKeyType: Scalar.Type
  166. /**
  167. * Use 'single quote' rather than "double quote" by default
  168. *
  169. * Default: `false`
  170. */
  171. defaultQuoteSingle: boolean
  172. doubleQuoted: {
  173. /**
  174. * Whether to restrict double-quoted strings to use JSON-compatible syntax.
  175. *
  176. * Default: `false`
  177. */
  178. jsonEncoding: boolean
  179. /**
  180. * Minimum length to use multiple lines to represent the value.
  181. *
  182. * Default: `40`
  183. */
  184. minMultiLineLength: number
  185. }
  186. fold: {
  187. /**
  188. * Maximum line width (set to `0` to disable folding).
  189. *
  190. * Default: `80`
  191. */
  192. lineWidth: number
  193. /**
  194. * Minimum width for highly-indented content.
  195. *
  196. * Default: `20`
  197. */
  198. minContentWidth: number
  199. }
  200. }
  201. }
  202. export interface CreateNodeOptions {
  203. /**
  204. * Filter or modify values while creating a node.
  205. *
  206. * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter
  207. */
  208. replacer?: Replacer
  209. /**
  210. * Specify the collection type, e.g. `"!!omap"`. Note that this requires the
  211. * corresponding tag to be available in this document's schema.
  212. */
  213. tag?: string
  214. /**
  215. * Wrap plain values in `Scalar` objects.
  216. *
  217. * Default: `true`
  218. */
  219. wrapScalars?: boolean
  220. }
  221. export class Document extends Collection {
  222. cstNode?: CST.Document
  223. /**
  224. * @param value - The initial value for the document, which will be wrapped
  225. * in a Node container.
  226. */
  227. constructor(value?: any, options?: Options)
  228. constructor(value: any, replacer: null | Replacer, options?: Options)
  229. tag: never
  230. directivesEndMarker?: boolean
  231. type: Type.DOCUMENT
  232. /**
  233. * Anchors associated with the document's nodes;
  234. * also provides alias & merge node creators.
  235. */
  236. anchors: Document.Anchors
  237. /** The document contents. */
  238. contents: any
  239. /** Errors encountered during parsing. */
  240. errors: YAMLError[]
  241. /**
  242. * The schema used with the document. Use `setSchema()` to change or
  243. * initialise.
  244. */
  245. schema?: Schema
  246. /**
  247. * Array of prefixes; each will have a string `handle` that
  248. * starts and ends with `!` and a string `prefix` that the handle will be replaced by.
  249. */
  250. tagPrefixes: Document.TagPrefix[]
  251. /**
  252. * The parsed version of the source document;
  253. * if true-ish, stringified output will include a `%YAML` directive.
  254. */
  255. version?: string
  256. /** Warnings encountered during parsing. */
  257. warnings: YAMLWarning[]
  258. /**
  259. * Convert any value into a `Node` using the current schema, recursively
  260. * turning objects into collections.
  261. */
  262. createNode(
  263. value: any,
  264. { replacer, tag, wrapScalars }?: CreateNodeOptions
  265. ): Node
  266. /**
  267. * Convert a key and a value into a `Pair` using the current schema,
  268. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  269. *
  270. * @param options If `wrapScalars` is not `false`, wraps plain values in
  271. * `Scalar` objects.
  272. */
  273. createPair(key: any, value: any, options?: { wrapScalars?: boolean }): Pair
  274. /**
  275. * List the tags used in the document that are not in the default
  276. * `tag:yaml.org,2002:` namespace.
  277. */
  278. listNonDefaultTags(): string[]
  279. /** Parse a CST into this document */
  280. parse(cst: CST.Document): this
  281. /**
  282. * When a document is created with `new YAML.Document()`, the schema object is
  283. * not set as it may be influenced by parsed directives; call this with no
  284. * arguments to set it manually, or with arguments to change the schema used
  285. * by the document.
  286. */
  287. setSchema(
  288. id?: Options['version'] | Schema.Name,
  289. customTags?: (Schema.TagId | Schema.Tag)[]
  290. ): void
  291. /** Set `handle` as a shorthand string for the `prefix` tag namespace. */
  292. setTagPrefix(handle: string, prefix: string): void
  293. /**
  294. * A plain JavaScript representation of the document `contents`.
  295. *
  296. * @param mapAsMap - Use Map rather than Object to represent mappings.
  297. * Overrides values set in Document or global options.
  298. * @param onAnchor - If defined, called with the resolved `value` and
  299. * reference `count` for each anchor in the document.
  300. * @param reviver - A function that may filter or modify the output JS value
  301. */
  302. toJS(opt?: {
  303. mapAsMap?: boolean
  304. onAnchor?: (value: any, count: number) => void
  305. reviver?: Reviver
  306. }): any
  307. /**
  308. * A JSON representation of the document `contents`.
  309. *
  310. * @param arg Used by `JSON.stringify` to indicate the array index or property
  311. * name.
  312. */
  313. toJSON(arg?: string): any
  314. /** A YAML representation of the document. */
  315. toString(): string
  316. }
  317. export namespace Document {
  318. interface Parsed extends Document {
  319. contents: Node | null
  320. /** The schema used with the document. */
  321. schema: Schema
  322. }
  323. interface Anchors {
  324. /**
  325. * Create a new `Alias` node, adding the required anchor for `node`.
  326. * If `name` is empty, a new anchor name will be generated.
  327. */
  328. createAlias(node: Node, name?: string): Alias
  329. /**
  330. * Create a new `Merge` node with the given source nodes.
  331. * Non-`Alias` sources will be automatically wrapped.
  332. */
  333. createMergePair(...nodes: Node[]): Merge
  334. /** The anchor name associated with `node`, if set. */
  335. getName(node: Node): undefined | string
  336. /** List of all defined anchor names. */
  337. getNames(): string[]
  338. /** The node associated with the anchor `name`, if set. */
  339. getNode(name: string): undefined | Node
  340. /**
  341. * Find an available anchor name with the given `prefix` and a
  342. * numerical suffix.
  343. */
  344. newName(prefix: string): string
  345. /**
  346. * Associate an anchor with `node`. If `name` is empty, a new name will be generated.
  347. * To remove an anchor, use `setAnchor(null, name)`.
  348. */
  349. setAnchor(node: Node | null, name?: string): void | string
  350. }
  351. interface TagPrefix {
  352. handle: string
  353. prefix: string
  354. }
  355. }
  356. /**
  357. * Recursively turns objects into collections. Generic objects as well as `Map`
  358. * and its descendants become mappings, while arrays and other iterable objects
  359. * result in sequences.
  360. *
  361. * The primary purpose of this function is to enable attaching comments or other
  362. * metadata to a value, or to otherwise exert more fine-grained control over the
  363. * stringified output. To that end, you'll need to assign its return value to
  364. * the `contents` of a Document (or somewhere within said contents), as the
  365. * document's schema is required for YAML string output.
  366. *
  367. * @param wrapScalars If undefined or `true`, also wraps plain values in
  368. * `Scalar` objects; if `false` and `value` is not an object, it will be
  369. * returned directly.
  370. * @param tag Use to specify the collection type, e.g. `"!!omap"`. Note that
  371. * this requires the corresponding tag to be available based on the default
  372. * options. To use a specific document's schema, use `doc.schema.createNode`.
  373. */
  374. export function createNode(
  375. value: any,
  376. wrapScalars?: true,
  377. tag?: string
  378. ): YAMLMap | YAMLSeq | Scalar
  379. /**
  380. * YAML.createNode recursively turns objects into Map and arrays to Seq collections.
  381. * Its primary use is to enable attaching comments or other metadata to a value,
  382. * or to otherwise exert more fine-grained control over the stringified output.
  383. *
  384. * Doesn't wrap plain values in Scalar objects.
  385. */
  386. export function createNode(
  387. value: any,
  388. wrapScalars: false,
  389. tag?: string
  390. ): YAMLMap | YAMLSeq | string | number | boolean | null
  391. /**
  392. * Parse an input string into a single YAML.Document.
  393. */
  394. export function parseDocument(str: string, options?: Options): Document.Parsed
  395. /**
  396. * Parse the input as a stream of YAML documents.
  397. *
  398. * Documents should be separated from each other by `...` or `---` marker lines.
  399. */
  400. export function parseAllDocuments(
  401. str: string,
  402. options?: Options
  403. ): Document.Parsed[]
  404. /**
  405. * Parse an input string into JavaScript.
  406. *
  407. * Only supports input consisting of a single YAML document; for multi-document
  408. * support you should use `YAML.parseAllDocuments`. May throw on error, and may
  409. * log warnings using `console.warn`.
  410. *
  411. * @param str - A string with YAML formatting.
  412. * @param reviver - A reviver function, as in `JSON.parse()`
  413. * @returns The value will match the type of the root value of the parsed YAML
  414. * document, so Maps become objects, Sequences arrays, and scalars result in
  415. * nulls, booleans, numbers and strings.
  416. */
  417. export function parse(str: string, options?: Options): any
  418. export function parse(
  419. str: string,
  420. reviver: null | Reviver,
  421. options?: Options
  422. ): any
  423. /**
  424. * Stringify a value as a YAML document.
  425. *
  426. * @param replacer - A replacer array or function, as in `JSON.stringify()`
  427. * @returns Will always include `\n` as the last character, as is expected of YAML documents.
  428. */
  429. export function stringify(value: any, options?: Options): string
  430. export function stringify(
  431. value: any,
  432. replacer: null | Replacer,
  433. options?: number | string | Options
  434. ): string