yaml-unist-parser
Version:
A YAML parser that produces output compatible with unist
158 lines (157 loc) • 5.13 kB
text/typescript
import * as YAML from "yaml";
//#region src/types.d.ts
interface ParseOptions {
uniqueKeys?: boolean;
}
interface Node {
type: string;
position: Position;
/** @internal non-enumerable */
_parent?: YamlUnistNode | null;
}
interface Position {
start: Point;
end: Point;
}
type Range = [number, number] | YAML.Range;
interface Point {
/** 1-based */
line: number;
/** 1-based */
column: number;
/** 0-based */
offset: number;
}
interface Parent extends Node {
children: Node[];
}
interface Literal extends Node {
value: string;
}
interface Content {
anchor: null | Anchor;
tag: null | Tag;
/** comments between the node and its tag/anchor */
middleComments: Comment[];
}
interface CommentAttachable extends LeadingCommentAttachable, TrailingCommentAttachable {}
interface LeadingCommentAttachable {
/** comments in front of the node */
leadingComments: Comment[];
}
interface TrailingCommentAttachable {
/** comment on the same line of the node */
trailingComment: null | Comment;
}
interface EndCommentAttachable {
/** comments after the node with greater column */
endComments: Comment[];
}
type YamlUnistNode = Comment | Tag | Anchor | Root | Document | DocumentHead | DocumentBody | Directive | Alias | BlockLiteral | BlockFolded | Plain | QuoteSingle | QuoteDouble | Mapping | MappingItem | MappingKey | MappingValue | Sequence | SequenceItem | FlowMapping | FlowMappingItem | FlowSequence | FlowSequenceItem;
type ContentNode = Extract<YamlUnistNode, Content>;
interface Comment extends Literal {
type: "comment";
}
interface Anchor extends Literal {
type: "anchor";
}
interface Tag extends Literal {
type: "tag";
}
interface Root extends Parent {
type: "root";
children: Document[];
comments: Comment[];
}
interface Document extends Parent, TrailingCommentAttachable {
type: "document";
directivesEndMarker: boolean;
documentEndMarker: boolean;
children: [DocumentHead, DocumentBody];
}
interface DocumentHead extends Parent, EndCommentAttachable, TrailingCommentAttachable {
type: "documentHead";
children: Directive[];
}
interface DocumentBody extends Parent, EndCommentAttachable {
type: "documentBody";
children: [] | [ContentNode];
}
interface Directive extends Node, CommentAttachable {
type: "directive";
name: string;
parameters: string[];
}
interface Alias extends Literal, Content, CommentAttachable {
type: "alias";
}
interface BlockValue extends Literal, Content, LeadingCommentAttachable {
chomping: "clip" | "keep" | "strip";
indent: null | number;
/** comment between indicator and the value */
indicatorComment: null | Comment;
}
interface BlockLiteral extends BlockValue {
type: "blockLiteral";
}
interface BlockFolded extends BlockValue {
type: "blockFolded";
}
interface Plain extends Literal, Content, CommentAttachable {
type: "plain";
}
interface QuoteValue extends Literal, Content, CommentAttachable {}
interface QuoteSingle extends QuoteValue {
type: "quoteSingle";
}
interface QuoteDouble extends QuoteValue {
type: "quoteDouble";
}
interface Mapping extends Parent, Content, LeadingCommentAttachable {
type: "mapping";
children: MappingItem[];
}
interface MappingItemBase extends Parent, LeadingCommentAttachable {
/** key-value pair */
children: [MappingKey, MappingValue];
}
interface MappingItem extends MappingItemBase {
type: "mappingItem";
}
interface MappingKey extends Parent, TrailingCommentAttachable, EndCommentAttachable {
type: "mappingKey";
children: [] | [ContentNode];
}
interface MappingValue extends Parent, CommentAttachable, EndCommentAttachable {
type: "mappingValue";
children: [] | [ContentNode];
}
interface Sequence extends Parent, Content, LeadingCommentAttachable, EndCommentAttachable {
type: "sequence";
children: SequenceItem[];
}
interface SequenceItemBase extends Parent {
children: [] | [ContentNode];
}
interface SequenceItem extends SequenceItemBase, CommentAttachable, EndCommentAttachable {
type: "sequenceItem";
}
interface FlowCollection extends Parent, Content, CommentAttachable, EndCommentAttachable {
children: Array<FlowMappingItem | FlowSequenceItem>;
}
interface FlowMapping extends FlowCollection {
type: "flowMapping";
children: FlowMappingItem[];
}
interface FlowMappingItem extends MappingItemBase {
type: "flowMappingItem";
}
interface FlowSequence extends FlowCollection {
type: "flowSequence";
children: Array<FlowMappingItem | FlowSequenceItem>;
}
interface FlowSequenceItem extends SequenceItemBase {
type: "flowSequenceItem";
}
//#endregion
export { Alias, Anchor, BlockFolded, BlockLiteral, BlockValue, Comment, CommentAttachable, Content, ContentNode, Directive, Document, DocumentBody, DocumentHead, EndCommentAttachable, FlowCollection, FlowMapping, FlowMappingItem, FlowSequence, FlowSequenceItem, LeadingCommentAttachable, Literal, Mapping, MappingItem, MappingItemBase, MappingKey, MappingValue, Node, Parent, ParseOptions, Plain, Point, Position, QuoteDouble, QuoteSingle, QuoteValue, Range, Root, Sequence, SequenceItem, SequenceItemBase, Tag, TrailingCommentAttachable, YamlUnistNode };