YAML AST Parser
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.
Common errors
-
TypeError: (0 , yaml_ast_parser_1.load) is not a function
cause This error often occurs when attempting to use CommonJS `require` with a library primarily designed for ES Modules, or when trying to destructure a default export as a named export.fixEnsure 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"`). -
Property 'value' does not exist on type 'YAMLNode'.
cause The `value` property is specific to `YAMLScalar` nodes, but you are trying to access it on the generic `YAMLNode` type without proper type narrowing or casting.fixBefore 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); }` -
Cannot read properties of undefined (reading 'items')
cause This typically happens when trying to access the `items` property, which belongs to `YAMLSequence` nodes, on a node that is not a sequence (e.g., a scalar or a map).fixEnsure 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(...); }`
Warnings
- gotcha The `yaml-ast-parser` project appears to be abandoned, with no significant updates or commits in several years. This means there's no ongoing maintenance, bug fixes, or security patches, which could pose risks for long-term projects.
- gotcha The library uses `0.0.x` versioning, which typically indicates a pre-1.0.0 state where the API is not yet stable and breaking changes can occur without major version bumps. Although the project is abandoned now, this was a common risk during its active development.
- gotcha `yaml-ast-parser` is a fork of `js-yaml`. While it adds AST functionality and `!include` support, it may not keep pace with `js-yaml`'s updates, bug fixes, or new features. This could lead to diverging behavior or missing compatibility with newer YAML specifications.
Install
-
npm install yaml-ast-parser -
yarn add yaml-ast-parser -
pnpm add yaml-ast-parser
Imports
- load
const { load } = require('yaml-ast-parser');import { load } from 'yaml-ast-parser'; - Kind
import { YAMLNodeKind } from 'yaml-ast-parser';import { Kind } from 'yaml-ast-parser'; - YAMLNode
import YAMLNode from 'yaml-ast-parser';
import { YAMLNode, YAMLScalar, YamlMap, YAMLSequence, YAMLMapping } from 'yaml-ast-parser'; - determineScalarType
import { determineType } from 'yaml-ast-parser';import { determineScalarType, parseYamlBoolean, parseYamlFloat, parseYamlInteger } from 'yaml-ast-parser';
Quickstart
import { load, Kind, YAMLScalar, YamlMap, YAMLSequence, YAMLMapping, YAMLNode } from 'yaml-ast-parser';
const yamlContent = `
name: John Doe
age: 30
address:
street: 123 Main St
city: Anytown
skills:
- TypeScript
- Node.js
- YAML
`;
function traverseAST(node: YAMLNode, level: number = 0) {
const indent = ' '.repeat(level);
console.log(`${indent}Kind: ${Kind[node.kind]} (Pos: ${node.startPosition}-${node.endPosition})`);
switch (node.kind) {
case Kind.SCALAR:
const scalarNode = node as YAMLScalar;
console.log(`${indent} Value: '${scalarNode.value}'`);
break;
case Kind.MAP:
const mapNode = node as YamlMap;
mapNode.mappings.forEach(mapping => {
console.log(`${indent} Key: '${(mapping.key as YAMLScalar).value}'`);
traverseAST(mapping.value, level + 1);
});
break;
case Kind.SEQ:
const sequenceNode = node as YAMLSequence;
sequenceNode.items.forEach(item => {
traverseAST(item, level + 1);
});
break;
case Kind.MAPPING:
const mappingNode = node as YAMLMapping;
// Key is a scalar, Value is a general YAMLNode
console.log(`${indent} Mapping Key: '${(mappingNode.key as YAMLScalar).value}'`);
traverseAST(mappingNode.value, level + 1);
break;
default:
// Handle other kinds if necessary
break;
}
}
const ast = load(yamlContent);
if (ast) {
console.log('YAML AST Root Node:');
traverseAST(ast);
} else {
console.error('Failed to parse YAML content.');
}