UNPKG

yaml-language-server

Version:
73 lines 2.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCustomTags = getCustomTags; const yaml_1 = require("yaml"); const customTags_1 = require("../utils/customTags"); class CommonTagImpl { constructor(tag, type, returnType) { this.tag = tag; this.type = type; this.returnType = returnType; } get collection() { if (this.type === 'mapping') { return 'map'; } if (this.type === 'sequence') { return 'seq'; } return undefined; } resolve(value) { if ((0, yaml_1.isMap)(value) && this.type === 'mapping') { return this.addReturnTypeMetadata(value); } if ((0, yaml_1.isSeq)(value) && this.type === 'sequence') { return this.addReturnTypeMetadata(value); } if (typeof value === 'string' && this.type === 'scalar') { return this.addReturnTypeMetadata(value); } } addReturnTypeMetadata(value) { if (!this.returnType) { return value; } if (typeof value === 'string') { const scalar = new yaml_1.Scalar(value); (0, customTags_1.setCustomTagReturnType)(scalar, this.returnType); return scalar; } (0, customTags_1.setCustomTagReturnType)(value, this.returnType); return value; } } class IncludeTag { constructor() { this.tag = '!include'; this.type = 'scalar'; } resolve(value, onError) { if (value && value.length > 0 && value.trim()) { return value; } onError('!include without value'); } } /** * Converts the tags from settings and adds known tags such as !include * and returns Tags that can be used by the parser. * @param customTags Tags for parser */ function getCustomTags(customTags) { const tags = []; for (const tag of customTags ?? []) { const parsedTag = (0, customTags_1.parseCustomTag)(tag); if (parsedTag) { tags.push(new CommonTagImpl(parsedTag.tag, parsedTag.inputType, parsedTag.returnType)); } } tags.push(new IncludeTag()); return tags; } //# sourceMappingURL=custom-tag-provider.js.map