{"id":16937,"library":"yaml-language-server-parser","title":"YAML AST Parser for Language Servers","description":"This package provides a robust YAML Abstract Syntax Tree (AST) parser, specifically a maintained fork of `YAML-AST-PARSER` tailored for use within the YAML Language Server ecosystem. It parses YAML documents into a detailed AST structure, offering features like error restoration during parsing, reporting errors directly as part of AST nodes, and built-in support for the non-standard `!include` tag commonly used in RAML specifications. The current stable version is 0.1.3, suggesting a focused, stable release cadence for its specific application. Its primary differentiators are its explicit maintenance for language server tooling, its robust error handling capabilities which are critical for IDE integration, and its direct lineage from `JS-YAML` for core parsing fidelity, combined with a focus on AST representation.","status":"active","version":"0.1.3","language":"javascript","source_language":"en","source_url":"https://github.com/redhat-developer/yaml-ast-parser","tags":["javascript","raml","ast","yaml","typescript"],"install":[{"cmd":"npm install yaml-language-server-parser","lang":"bash","label":"npm"},{"cmd":"yarn add yaml-language-server-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add yaml-language-server-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses named exports and is designed for ESM consumption, though transpilation might allow CJS. TypeScript is fully supported.","wrong":"const load = require('yaml-language-server-parser').load;","symbol":"load","correct":"import { load } from 'yaml-language-server-parser';"},{"note":"Core AST node types (YAMLNode and its descendants like YAMLScalar, YamlMap, etc., along with the Kind enum) are named exports from the root module.","wrong":"import YAMLNode from 'yaml-language-server-parser/YAMLNode';","symbol":"YAMLNode","correct":"import { YAMLNode, Kind, YAMLScalar, YamlMap, YAMLSequence } from 'yaml-language-server-parser';"},{"note":"Helper functions for scalar type inference and parsing specific primitive types are named exports from the main package entry point.","wrong":"import { determineScalarType } from 'yaml-language-server-parser/lib/parser';","symbol":"determineScalarType","correct":"import { determineScalarType, parseYamlInteger } from 'yaml-language-server-parser';"}],"quickstart":{"code":"import { load, Kind, YAMLScalar, YamlMap, YAMLMapping, determineScalarType, parseYamlInteger, parseYamlBoolean } from 'yaml-language-server-parser';\n\nconst yamlString = `\n# A simple YAML document\nname: My Application\nversion: 1.0.0\nconfig:\n  port: 8080\n  debug: true\n  features:\n    - user_auth\n    - logging\n`;\n\n// Load the YAML string into an AST\nconst yamlNode = load(yamlString);\n\nconsole.log('--- AST Analysis ---');\n\nif (yamlNode.kind === Kind.MAP) {\n  const rootMap = yamlNode as YamlMap;\n  console.log(`Root node is a Map with ${rootMap.mappings.length} mappings.`);\n\n  for (const mapping of rootMap.mappings) {\n    if (mapping.key.value === 'name' && mapping.value?.kind === Kind.SCALAR) {\n      const nameScalar = mapping.value as YAMLScalar;\n      console.log(`Application Name: ${nameScalar.value}`);\n    }\n\n    if (mapping.key.value === 'config' && mapping.value?.kind === Kind.MAP) {\n      const configMap = mapping.value as YamlMap;\n      console.log(`  Config map found with ${configMap.mappings.length} items.`);\n\n      for (const configMapping of configMap.mappings) {\n        if (configMapping.key.value === 'port' && configMapping.value?.kind === Kind.SCALAR) {\n          const portScalar = configMapping.value as YAMLScalar;\n          console.log(`    Port (raw): ${portScalar.value}`);\n          console.log(`    Port (type determined): ${determineScalarType(portScalar)}`);\n          console.log(`    Port (parsed int): ${parseYamlInteger(portScalar.value)}`);\n        }\n        if (configMapping.key.value === 'debug' && configMapping.value?.kind === Kind.SCALAR) {\n          const debugScalar = configMapping.value as YAMLScalar;\n          console.log(`    Debug (raw): ${debugScalar.value}`);\n          console.log(`    Debug (type determined): ${determineScalarType(debugScalar)}`);\n          console.log(`    Debug (parsed bool): ${parseYamlBoolean(debugScalar.value)}`);\n        }\n      }\n    }\n  }\n}\n\nconsole.log(`\\n'name' node starts at: ${yamlNode.mappings.find(m => m.key.value === 'name')?.key.startPosition}`);\nconsole.log(`'name' node ends at: ${yamlNode.mappings.find(m => m.key.value === 'name')?.key.endPosition}`);\n","lang":"typescript","description":"Parses a multi-line YAML string into an Abstract Syntax Tree (AST), demonstrating how to navigate map and scalar nodes, identify their kinds, and use helper functions to determine and parse scalar values like integers and booleans. It also showcases accessing position information for nodes."},"warnings":[{"fix":"Verify your `package.json` to ensure `yaml-language-server-parser` is the intended dependency for language server related YAML parsing.","message":"This package is a maintained fork specifically for the YAML Language Server. If you're looking for the original, less-maintained `YAML-AST-PARSER` or the general-purpose `JS-YAML`, ensure you're installing the correct package (`yaml-language-server-parser` for language server tooling).","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If `!include` tag behavior is undesired or causes conflicts, custom tag resolvers may be necessary, though the package is designed to support this specific extension.","message":"The parser includes built-in support for the `!include` tag, often used in RAML. This is a non-standard YAML extension. Users expecting strict YAML 1.2 parsing without custom tag handling might need to be aware of this behavior, as it deviates from standard YAML specifications.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Consult the GitHub repository for recent activity and issue trends to gauge ongoing maintenance and stability if using for critical, non-language-server applications.","message":"The package version (e.g., 0.1.3) might imply an early development stage to some users. While it is a 'maintained fork' and stable for its intended use case (language servers), broader or high-traffic general-purpose YAML parsing might require evaluating its maturity for your specific requirements.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Use ES module import syntax: `import { load } from 'yaml-language-server-parser';`. Ensure your environment supports ESM or use a bundler/transpiler like Webpack/Rollup/Babel.","cause":"Attempting to use CommonJS `require` syntax (`const { load } = require('...')`) or incorrect destructuring when the module is primarily designed for ES Module (ESM) imports.","error":"TypeError: (0 , yaml_language_server_parser_1.load) is not a function"},{"fix":"Run `npm install yaml-language-server-parser` or `yarn add yaml-language-server-parser`. For TypeScript, ensure `esModuleInterop` is enabled in `tsconfig.json` if encountering import issues with older module resolutions.","cause":"The package is not installed, installed incorrectly, or TypeScript cannot locate its type definitions.","error":"Cannot find module 'yaml-language-server-parser' or its corresponding type declarations."},{"fix":"Always check the `kind` property of a `YAMLNode` against the `Kind` enum (e.g., `if (node.kind === Kind.SCALAR)`) and then cast it to the appropriate derived type (e.g., `(node as YAMLScalar).value`) before accessing type-specific properties.","cause":"Attempting to access a property specific to a derived AST node type (e.g., `YAMLScalar.value`) directly on the base `YAMLNode` type without first checking its `kind` and casting.","error":"Property 'value' does not exist on type 'YAMLNode'."}],"ecosystem":"npm","meta_description":null}