Node.d.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { Document } from '../doc/Document.js';
  2. import type { ToJSOptions } from '../options.js';
  3. import { Token } from '../parse/cst.js';
  4. import type { StringifyContext } from '../stringify/stringify.js';
  5. import type { Alias } from './Alias.js';
  6. import { NODE_TYPE } from './identity.js';
  7. import type { Scalar } from './Scalar.js';
  8. import { ToJSContext } from './toJS.js';
  9. import type { MapLike, YAMLMap } from './YAMLMap.js';
  10. import type { YAMLSeq } from './YAMLSeq.js';
  11. export type Node<T = unknown> = Alias | Scalar<T> | YAMLMap<unknown, T> | YAMLSeq<T>;
  12. /** Utility type mapper */
  13. export type NodeType<T> = T extends string | number | bigint | boolean | null | undefined ? Scalar<T> : T extends Date ? Scalar<string | Date> : T extends Array<any> ? YAMLSeq<NodeType<T[number]>> : T extends {
  14. [key: string]: any;
  15. } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : T extends {
  16. [key: number]: any;
  17. } ? YAMLMap<NodeType<keyof T>, NodeType<T[keyof T]>> : Node;
  18. export type ParsedNode = Alias.Parsed | Scalar.Parsed | YAMLMap.Parsed | YAMLSeq.Parsed;
  19. /** `[start, value-end, node-end]` */
  20. export type Range = [number, number, number];
  21. export declare abstract class NodeBase {
  22. readonly [NODE_TYPE]: symbol;
  23. /** A comment on or immediately after this */
  24. comment?: string | null;
  25. /** A comment before this */
  26. commentBefore?: string | null;
  27. /**
  28. * The `[start, value-end, node-end]` character offsets for the part of the
  29. * source parsed into this node (undefined if not parsed). The `value-end`
  30. * and `node-end` positions are themselves not included in their respective
  31. * ranges.
  32. */
  33. range?: Range | null;
  34. /** A blank line before this node and its commentBefore */
  35. spaceBefore?: boolean;
  36. /** The CST token that was composed into this node. */
  37. srcToken?: Token;
  38. /** A fully qualified tag, if required */
  39. tag?: string;
  40. /**
  41. * Customize the way that a key-value pair is resolved.
  42. * Used for YAML 1.1 !!merge << handling.
  43. */
  44. addToJSMap?: (ctx: ToJSContext | undefined, map: MapLike, value: unknown) => void;
  45. /** A plain JS representation of this node */
  46. abstract toJSON(): any;
  47. abstract toString(ctx?: StringifyContext, onComment?: () => void, onChompKeep?: () => void): string;
  48. constructor(type: symbol);
  49. /** Create a copy of this node. */
  50. clone(): NodeBase;
  51. /** A plain JavaScript representation of this node. */
  52. toJS(doc: Document<Node, boolean>, { mapAsMap, maxAliasCount, onAnchor, reviver }?: ToJSOptions): any;
  53. }