Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
indexPositionAndPath(path, parent, node, elementIndex) {
path = path || '.'
if (node) {
if (node.endPosition < node.startPosition) {
console.error(`unexpected node start/end combination: ${JSON.stringify(node)}`)
}
} else if (parent.kind === YAML.Kind.SEQ) {
let prevItem = null
if (elementIndex > 0) {
for (let i=elementIndex-1; i >= 0 && prevItem === null; --i) {
prevItem = parent.items[i]
}
}
let startPos = (prevItem ? prevItem.endPosition : parent.startPosition) + 1
let endPos = elementIndex + 1 < parent.items.length ? parent.items[elementIndex+1].startPosition : parent.endPosition
node = {startPosition: startPos, endPosition: endPos, parent: parent}
} else if (parent.kind === YAML.Kind.MAPPING) {
let map = parent.parent
let startPos, endPos
if (elementIndex > 0) {
startPos = map.mappings[elementIndex-1].endPosition
} else {
startPos = parent.endPosition
let currentNodeInSchema = this.searchSchema(rootNode, currentNode);
let parentNodeSearch = null;
let parentNodeType = null;
//When a parent node exists we need to check if the child types are invalid
if (currentNodePath.length >= 2) {
parentNodeSearch = this.searchSchema(rootNode, currentNodePath[currentNodePath.length - 2]);
//Error: Check if this is the right child type of parent
if (this.isInvalidParentType(parentNodeSearch, currentNodePath[currentNodePath.length - 2].value, currentNode.key.value)) {
this.errorHandler.addErrorResult(currentNode, "Node \'" + currentNode.key.value + "\' has an invalid type. Valid type(s) of key node are: " + this.collectTypesForParent(parentNodeSearch, currentNode.key.value).toString(), main_1.DiagnosticSeverity.Error);
}
}
if (!(currentNodeInSchema.length > 0)) {
this.errorHandler.addErrorResult(currentNode.key, "Node \'" + currentNode.key.value + "\' is not found", main_1.DiagnosticSeverity.Error);
}
//Error: If type is mapping then we need to check the scalar type
if (currentNode.kind === yaml_ast_parser_1.Kind.MAPPING && currentNode.value !== null && this.isInvalidType(currentNode, currentNodeInSchema)) {
this.errorHandler.addErrorResult(currentNode.value, "Node \'" + currentNode.key.value + "\' has an invalid type. Valid type(s) are: " + this.collectTypes(currentNodeInSchema).toString(), main_1.DiagnosticSeverity.Error);
}
let childrenNodes = astServices_1.generateChildren(currentNode.value);
childrenNodes.forEach(child => {
//We are getting back a bunch of nodes which all have a key and we adding them
let newNodePath = currentNodePath.concat(child);
let searchThroughSchema = this.searchSchema(rootNode, child);
let isValidInSchema = searchThroughSchema.length > 0;
if (!isValidInSchema) {
if (this.hasAdditionalProperties(currentNodeInSchema)) {
this.errorHandler.addErrorResult(child, "\'" + child.key.value + "\' is an additional property of " + currentNode.key.value, main_1.DiagnosticSeverity.Warning);
}
else {
this.errorHandler.addErrorResult(child, "\'" + child.key.value + "\' is not a valid child node of " + currentNode.key.value, main_1.DiagnosticSeverity.Error);
}
}
this,
parent,
name,
false,
node.startPosition,
node.endPosition,
instance.customTag
)
result.value = node.value
return result
}
}
break
}
case Yaml.Kind.ANCHOR_REF: {
const instance = (node as Yaml.YAMLAnchorReference).value
return (
this,
this.recursivelyBuildAst(parent, instance) ||
new NullASTNode(
this,
parent,
null,
node.startPosition,
node.endPosition
)
)
}
case Yaml.Kind.INCLUDE_REF: {
const result = new StringASTNode(
export function buildAstRecursively(parent: ASTNode | undefined, node: Yaml.YAMLNode): ASTNode {
if (!node) {
return new NullASTNode(parent, 0, 0);
}
switch (node.kind) {
case Yaml.Kind.MAP: {
const instance = node as Yaml.YamlMap;
const result = new ObjectASTNode(
parent,
instance.startPosition,
instance.endPosition,
);
for (const mapping of instance.mappings) {
result.addProperty(
buildAstRecursively(result, mapping) as PropertyASTNode
);
}
return result;
}
case Yaml.Kind.MAPPING: {
const instance = node as Yaml.YAMLMapping;
const key = instance.key;
return searchServiceTraverser.traverseKubernetesSchema(parentNodes, node, false, function (possibleChildren) {
let possibleChildrenNoDuplicates = arrUtils_1.removeDuplicates(possibleChildren, "description");
let hoverNode = possibleChildrenNoDuplicates[0];
if (hoverNode) {
let startPos = node.startPosition;
let endPos = node.endPosition;
//Use the keys start position when you are hovering over a scalar item
if (node.kind === yaml_ast_parser_1.Kind.SCALAR) {
startPos = node.parent.key.startPosition ? node.parent.key.startPosition : startPos;
}
let hoverRange = vscode_languageserver_types_1.Range.create(document.positionAt(startPos), document.positionAt(endPos));
let hoverItem = {
contents: hoverNode.description,
range: hoverRange
};
return hoverItem;
}
return null;
});
}
import { IndexToPosition } from '../parsing/text-utility';
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as yamlAst from "yaml-ast-parser";
import { JsonPath } from "./jsonpath";
import { Message, SourceLocation, Channel } from '../message';
import { NewEmptyObject } from "../parsing/stable-object";
/**
* reexport required elements
*/
export { newScalar } from "yaml-ast-parser";
export const Kind: { SCALAR: number, MAPPING: number, MAP: number, SEQ: number, ANCHOR_REF: number, INCLUDE_REF: number } = yamlAst.Kind;
export type YAMLNode = yamlAst.YAMLNode;
export type YAMLScalar = yamlAst.YAMLScalar;
export type YAMLMapping = yamlAst.YAMLMapping;
export type YAMLMap = yamlAst.YamlMap;
export type YAMLSequence = yamlAst.YAMLSequence;
export type YAMLAnchorReference = yamlAst.YAMLAnchorReference;
export const CreateYAMLMapping: (key: YAMLScalar, value: YAMLNode) => YAMLMapping = yamlAst.newMapping;
export const CreateYAMLScalar: (value: string) => YAMLScalar = yamlAst.newScalar;
export interface YAMLNodeWithPath {
path: JsonPath;
node: YAMLNode;
}
/**
testForParentType(parentNodeType, itemsList, currentNode, parentNode) {
if (itemsList.indexOf(currentNode) !== -1) {
if (parentNodeType === 'array') {
return parentNode.kind !== yaml_ast_parser_1.Kind.SEQ;
}
else if (parentNodeType === undefined) {
return parentNode.kind !== yaml_ast_parser_1.Kind.MAP;
}
else if (parentNodeType === "object") {
return parentNode.kind !== yaml_ast_parser_1.Kind.MAP;
}
}
return false;
}
isInvalidType(node, searchSchema) {
private recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
if (!node) {
return
}
switch (node.kind) {
case Yaml.Kind.MAP: {
const instance = node as Yaml.YamlMap
const result = new ObjectASTNode(
this,
parent,
null,
node.startPosition,
node.endPosition
)
for (const mapping of instance.mappings) {
result.addProperty(this.recursivelyBuildAst(
result,
mapping
) as PropertyASTNode)
}
function pathForNode(node) {
let path = ''
if (node && node.parent) {
path = pathForNode(node.parent)
if (node.parent.kind === YAML.Kind.SEQ) {
let index = node.parent.items.indexOf(node)
path += `[${index}]`
} else if (node.parent.kind === YAML.Kind.MAPPING) {
let key = node.parent.key.value
path += `.${key}`
}
}
return path
}
function isSeq(node) {
return node.kind === yaml_ast_parser_1.Kind.SEQ;
}
function isMapping(node) {