yaml-language-server
Version:
70 lines • 2.1 kB
JavaScript
import { isSeq, isMap, Scalar } from 'yaml';
import { parseCustomTag, setCustomTagReturnType } from '../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 (isMap(value) && this.type === 'mapping') {
return this.addReturnTypeMetadata(value);
}
if (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 Scalar(value);
setCustomTagReturnType(scalar, this.returnType);
return scalar;
}
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
*/
export function getCustomTags(customTags) {
const tags = [];
for (const tag of customTags ?? []) {
const parsedTag = 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