0.2.6 • Published 9 months ago
@sap-ux/cds-annotation-parser v0.2.6
@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) {
// Expected pathToLabel: "/value/items/0/properties/2/value"
const pathToLabel = findAnnotationNode(ast, {
position: { line: 5, character: 15 },
includeDelimiterCharacters: true
});
// An array of nodes matching each segment of the path.
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
);
/* Expected serializedNodes:
[
"Node of type 'collection'",
"<array of child elements>",
"Node of type 'record'",
"<array of child elements>",
"Node of type 'record-property'",
"Node of type 'string'",
]
*/
const termNode = getNode(ast, '/term');
if (termNode.type === 'path') {
// expected termName: "UI.LineItem"
const termName = termNode.value;
}
const qualifierNode = getNode(ast, '/qualifier');
if (qualifierNode.type === 'qualifier') {
// expected qualifier: "table1"
const qualifier = qualifierNode.value;
}
const propertyValueNode = getNode(ast, '/value/items/0/properties/1/value');
if (propertyValueNode.type === 'path') {
// expected value: "some.path"
const value = propertyValueNode.value;
}
}Keywords
OData annotations CAP CDS
0.2.12
5 months ago
0.2.11
6 months ago
0.2.10
6 months ago
0.2.7
9 months ago
0.2.6
9 months ago
0.2.9
7 months ago
0.2.8
8 months ago
0.2.5
10 months ago
0.2.4
1 year ago
0.2.3
1 year ago
0.1.12
1 year ago
0.1.13
1 year ago
0.2.1
1 year ago
0.2.0
1 year ago
0.2.2
1 year ago
0.1.11
2 years ago
0.1.10
2 years ago
0.1.9
2 years ago
0.1.8
2 years ago
0.1.7
2 years ago
0.1.6
2 years ago
0.1.4
2 years ago
0.1.5
2 years ago
0.1.3
2 years ago
0.1.2
2 years ago
0.1.1
2 years ago