@sap-ux/cds-annotation-parser
Installation
Npm
npm install --save @sap-ux/cds-annotation-parser
Yarn
yarn add @sap-ux/cds-annotation-parser
Pnpm
pnpm add @sap-ux/cds-annotation-parser
Usage
Examples of how to use CDS annotation parsing and abstract syntax tree (AST) node search functions.
import { parse, findAnnotationNode, getAstNodes, getNode } from '@sap-ux/cds-annotation-parser';
const ast = parse(`
UI.LineItem #table1 : [
{
$type: 'UI.DataField',
value: some.path,
Label: 'Sample column'
}
]';
`);
if (ast !== undefined) {
const pathToLabel = findAnnotationNode(ast, {
position: { line: 5, character: 15 },
includeDelimiterCharacters: true
});
const nodes = getAstNodes(ast, pathToLabel);
const serializedNodes = nodes.map((n) =>
Array.isArray(n) ? '<array of child elements>' : typeof n === 'object' ? `Node of type '${n.type}'` : n
);
const termNode = getNode(ast, '/term');
if (termNode.type === 'path') {
const termName = termNode.value;
}
const qualifierNode = getNode(ast, '/qualifier');
if (qualifierNode.type === 'qualifier') {
const qualifier = qualifierNode.value;
}
const propertyValueNode = getNode(ast, '/value/items/0/properties/1/value');
if (propertyValueNode.type === 'path') {
const value = propertyValueNode.value;
}
}
Keywords
OData annotations CAP CDS