yaml-ast-parser is a JavaScript/TypeScript library designed to parse YAML documents into an Abstract Syntax Tree (AST). It is a fork of `js-yaml` but specifically focuses on providing AST representation rather than direct deserialization into JavaScript objects. Key features include the ability to restore parsing after errors, integrate error reporting directly into AST nodes, and built-in support for the `!include` tag commonly used in RAML specifications. The library is currently at version `0.0.43` and appears to be in an abandoned state, with the last GitHub activity several years ago. Its primary differentiator is the granular AST access and specialized `!include` handling, making it suitable for tools that need to analyze or transform YAML structure rather than just consume its data. Developers should be aware of its inactive maintenance status when considering its use in new projects.
npm install yaml-ast-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a YAML string into an AST and then recursively traverse the resulting `YAMLNode` structure, printing the kind and value of each node. It highlights the use of `load`, `Kind` enum, and type casting for specific node types like `YAMLScalar`, `YamlMap`, and `YAMLSequence` to access their distinct properties.
For new projects, consider actively maintained YAML parsing libraries or forks that provide similar AST capabilities. For existing projects, pin the exact version to mitigate unexpected behavior, and be aware of potential vulnerabilities or unaddressed issues.
Always pin exact versions (`npm install yaml-ast-parser@0.0.43`) and thoroughly test any updates, even minor ones, if a newer version were to be released. This is less critical now due to abandonment but important to note historically.
Understand the specific differences between `yaml-ast-parser` and `js-yaml` relevant to your use case. If you rely on the fork's unique features, ensure they meet your requirements, and be prepared for potential incompatibilities with standard YAML parsers for complex documents.
Ensure you are using ES Module syntax: `import { load } from 'yaml-ast-parser';`. If using TypeScript, check your `tsconfig.json` for `module` and `moduleResolution` settings (e.g., `"module": "commonjs"` or `"esnext"`, `"moduleResolution": "node"` or `"bundler"`).Before accessing specific properties, check the `kind` of the `YAMLNode` using the `Kind` enum and cast it to the appropriate specific type. For example: `if (node.kind === Kind.SCALAR) { const scalarNode = node as YAMLScalar; console.log(scalarNode.value); }`Ensure that you verify the `kind` of the `YAMLNode` before attempting to access type-specific properties. For sequences: `if (node.kind === Kind.SEQ) { const sequenceNode = node as YAMLSequence; sequenceNode.items.forEach(...); }`No dependency data recorded yet.