Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11474x 11474x 11474x 11474x 11474x 45156x 45156x 45156x 45156x 45156x 45156x 45156x 45156x 45156x 45156x 11474x 11474x 10927x 10927x 11474x 11474x 247x 247x 11474x 11474x 270x 270x 270x 11474x 11474x 353x 328x 328x 25x 25x 25x 353x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 11474x 11474x 24x 24x 24x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 24x 24x 24x 11474x 11474x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 11474x 11474x 24x 20x 20x 5x 5x 15x 15x 20x 1x 1x 1x 20x 24x 11474x 11474x 24x 24x 24x 22x 22x 2x 2x 2x 24x 2x 2x 2x 2x 24x 1x 1x 1x 24x 11474x 11474x 24x 24x 13x 13x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 24x 150x 93x 19x 19x 19x 93x 150x 8x 57x 30x 11x 11x 30x 19x 19x 49x 19x 19x 19x 19x 19x 19x 19x 150x 11x 11x 24x 19x 10x 10x 9x 9x 19x 19x 19x 19x 5x 5x 5x 5x 5x 5x 4x 4x 19x 1x 1x 1x 1x 1x 1x 3x 3x 3x 19x 1x 1x 1x 1x 1x 19x 11x 11474x 11474x 19x 19x 19x 3x 19x 2x 2x 14x 14x 19x 4x 4x 4x 4x 4x 10x 10x 11474x 11474x 9x 9x 9x 9x 4x 4x 4x 4x 4x 5x 5x 11474x 11474x 3x 1x 1x 3x 2x 2x 2x 3x 11474x 11474x 24x 34x 34x 27x 27x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 34x 24x 11474x 11474x 24x 24x 13x 13x 11x 24x 23x 23x 23x 23x 23x 22x 22x 1x 1x 1x 1x 23x 11x 11474x 11474x 24x 47x 47x 47x 47x 23x 23x 23x 47x 47x 47x 47x 1x 1x 1x 47x 24x 11474x 11474x 24x 24x 24x 1x 1x 1x 1x 1x 24x 24x 47x 47x 1x 1x 46x 46x 47x 46x 46x 47x 46x 47x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 44x 44x 47x 17x 17x 27x 27x 27x 47x 24x 24x 47x 1x 1x 1x 47x 24x 11474x 11474x 175x 175x 11474x | import {ExpressionNode, TokenNode} from "../abap/nodes";
import {StructureType, UnknownType, VoidType} from "../abap/types/basic";
import {
CDSAnnotation, CDSAs, CDSDefineAbstract, CDSDefineProjection, CDSDefineView, CDSElement, CDSName, CDSPrefixedName,
CDSRelation, CDSSource, CDSType,
} from "../cds/expressions";
import {DDIC} from "../ddic";
import {IFile} from "../files/_ifile";
import {Issue} from "../issue";
import {DataDefinition, DataElement} from "../objects";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
export class CDSCheckSyntaxConf extends BasicRuleConfig {
}
type Source = {
entity: string,
names: string[],
type: StructureType | UnknownType | VoidType,
};
const KNOWN_QUAN_DATA_ELEMENTS = ["MENGE_D"];
const KNOWN_CURR_DATA_ELEMENTS = ["BWERT", "DZWERT"];
// Reserved names, cannot be used as element names, see DDIC table TRESE
const RESERVED_ELEMENT_NAMES = ["BEGIN", "NUMBER", "POSITION"];
const MAX_LABEL_LENGTH = 40;
// Annotations which are not supported in CDS view entities
const VIEW_ENTITY_FORBIDDEN_ANNOTATIONS = ["Semantics.unitOfMeasure"];
export class CDSCheckSyntax implements IRule {
private reg: IRegistry;
private conf = new CDSCheckSyntaxConf();
public getMetadata(): IRuleMetadata {
return {
key: "cds_check_syntax",
title: "CDS Check Syntax",
shortDescription: "Checks CDS source, field, type, and semantic annotation references",
extendedInformation: `Performs semantic checks which require other DDIC objects to be available.
Missing objects are only reported when their names match the configured errorNamespace.`,
tags: [RuleTag.Syntax],
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: CDSCheckSyntaxConf) {
this.conf = conf;
}
public initialize(reg: IRegistry): IRule {
this.reg = reg;
return this;
}
public run(object: IObject): Issue[] {
if (!(object instanceof DataDefinition)) {
return [];
}
const tree = object.getTree();
const file = object.findSourceFile();
if (tree === undefined || file === undefined) {
return [];
}
const issues: Issue[] = [];
const ddic = new DDIC(this.reg);
const sources = this.findSources(tree, ddic, issues, file);
this.checkRelations(tree, ddic, issues, file);
this.checkTypes(tree, ddic, issues, file);
this.checkRootProjection(tree, ddic, issues, file);
this.checkFieldAnnotations(tree, ddic, issues, file);
this.checkLabels(tree, issues, file);
this.checkViewEntityAnnotations(tree, issues, file);
this.checkReservedNames(tree, issues, file);
this.checkFields(tree, sources, object, issues, file);
return issues;
}
private findSources(tree: ExpressionNode, ddic: DDIC, issues: Issue[], file: IFile): Source[] {
const sources: Source[] = [];
for (const node of tree.findAllExpressionsRecursive(CDSSource)) {
const prefixed = node.findDirectExpression(CDSPrefixedName);
const entityNode = prefixed?.findDirectExpression(CDSName);
if (entityNode === undefined) {
continue;
}
const entity = this.name(entityNode);
const asName = node.findDirectExpression(CDSAs)?.findDirectExpression(CDSName);
const bareAlias = node.findDirectExpression(CDSName);
const alias = asName === undefined ? bareAlias : asName;
const lookup = ddic.lookupTableOrView(entity);
if (lookup.type instanceof UnknownType && lookup.object === undefined && this.reg.inErrorNamespace(entity)) {
issues.push(Issue.atToken(file, entityNode.getFirstToken(), `CDS source "${entity}" not found`,
this.getMetadata().key, this.conf.severity));
}
if (lookup.type instanceof StructureType || lookup.type instanceof UnknownType || lookup.type instanceof VoidType) {
const names = [entity.toUpperCase()];
if (alias) {
names.push(this.name(alias).toUpperCase());
}
sources.push({entity, names, type: lookup.type});
}
}
return sources;
}
private checkRelations(tree: ExpressionNode, ddic: DDIC, issues: Issue[], file: IFile) {
for (const relation of tree.findAllExpressionsRecursive(CDSRelation)) {
const directTokens = relation.getDirectTokens();
if (directTokens.length === 0) {
continue;
}
const entity = directTokens.map(t => t.getStr()).join("");
const lookup = ddic.lookupTableOrView(entity);
if (lookup.type instanceof UnknownType && lookup.object === undefined && this.reg.inErrorNamespace(entity)) {
issues.push(Issue.atToken(file, directTokens[0], `CDS source "${entity}" not found`,
this.getMetadata().key, this.conf.severity));
}
}
}
private checkTypes(tree: ExpressionNode, ddic: DDIC, issues: Issue[], file: IFile) {
for (const typeNode of tree.findAllExpressionsRecursive(CDSType)) {
const typeName = typeNode.findDirectExpressions(CDSName).map(n => this.name(n)).join(".");
if (typeName === "" || typeName.toUpperCase().startsWith("ABAP.")) {
continue;
}
const lookup = ddic.lookup(typeName);
if (lookup.type instanceof UnknownType && lookup.object === undefined && this.reg.inErrorNamespace(typeName)) {
issues.push(Issue.atToken(file, typeNode.getFirstToken(), `CDS type "${typeName}" not found`,
this.getMetadata().key, this.conf.severity));
}
}
}
private checkRootProjection(tree: ExpressionNode, ddic: DDIC, issues: Issue[], file: IFile) {
const projection = tree.findFirstExpression(CDSDefineProjection);
const rootToken = projection?.findDirectTokenByText("ROOT");
if (projection === undefined || rootToken === undefined) {
return;
}
const names = projection.findDirectExpressions(CDSName);
const projectedEntityNode = names[1];
if (projectedEntityNode === undefined) {
return;
}
const projectedEntityName = this.name(projectedEntityNode);
const projectedEntity = ddic.lookupTableOrView(projectedEntityName).object;
if (projectedEntity instanceof DataDefinition
&& projectedEntity.getTree()?.findDirectTokenByText("ROOT") === undefined) {
const message = `ROOT keyword not valid since ${projectedEntityName} is not a root property`;
issues.push(Issue.atToken(file, rootToken, message, this.getMetadata().key, this.conf.severity));
}
}
private checkFieldAnnotations(tree: ExpressionNode, ddic: DDIC, issues: Issue[], file: IFile) {
const definition = tree.findFirstExpression(CDSDefineAbstract);
if (definition === undefined) {
return;
}
let definitionNameSeen = false;
let fieldName: ExpressionNode | undefined;
let annotations: ExpressionNode[] = [];
const fields: {
name: string,
node: ExpressionNode,
annotations: string[],
semanticType: "QUAN" | "CURR" | undefined,
}[] = [];
for (const child of definition.getChildren()) {
if (child instanceof TokenNode) {
if (child.get().getStr() === ";") {
fieldName = undefined;
annotations = [];
}
continue;
} else if (child.get() instanceof CDSAnnotation) {
annotations.push(child);
} else if (child.get() instanceof CDSName) {
if (definitionNameSeen === false) {
definitionNameSeen = true;
annotations = [];
} else {
fieldName = child;
}
} else if (child.get() instanceof CDSType && fieldName !== undefined) {
const semanticType = this.determineSemanticType(child, ddic);
const normalizedAnnotations = annotations.map(a => a.concatTokens().replace(/\s/g, ""));
fields.push({name: this.name(fieldName), node: fieldName, annotations: normalizedAnnotations, semanticType});
fieldName = undefined;
annotations = [];
}
}
const reportedCompanionFields = new Set<string>();
for (const field of fields) {
if (field.semanticType === undefined) {
continue;
}
const quantity = field.semanticType === "QUAN";
const referenceAnnotation = quantity ? "@Semantics.quantity.unitOfMeasure" : "@Semantics.amount.currencyCode";
const companionAnnotation = quantity ? "@Semantics.unitOfMeasure: true" : "@Semantics.currencyCode: true";
const reference = this.findSemanticReference(field.annotations, field.semanticType);
if (reference === undefined) {
const kind = quantity ? "quantity" : "amount";
const message = `CDS ${kind} field "${field.name}" requires ${referenceAnnotation}`;
issues.push(Issue.atToken(file, field.node.getFirstToken(), message,
this.getMetadata().key, this.conf.severity));
continue;
}
const companion = fields.find(f => f.name.toUpperCase() === reference.toUpperCase());
if (companion === undefined) {
const kind = quantity ? "unit" : "currency";
const message = `CDS field "${field.name}" references missing ${kind} field "${reference}"`;
issues.push(Issue.atToken(file, field.node.getFirstToken(), message,
this.getMetadata().key, this.conf.severity));
continue;
}
const companionKey = field.semanticType + ":" + companion.name.toUpperCase();
if (reportedCompanionFields.has(companionKey) === false
&& this.hasCompanionAnnotation(companion.annotations, field.semanticType) === false) {
const message = `CDS field "${companion.name}" requires ${companionAnnotation}`;
issues.push(Issue.atToken(file, companion.node.getFirstToken(), message,
this.getMetadata().key, this.conf.severity));
reportedCompanionFields.add(companionKey);
}
}
}
private determineSemanticType(typeNode: ExpressionNode, ddic: DDIC): "QUAN" | "CURR" | undefined {
const typeName = typeNode.findDirectExpressions(CDSName).map(n => this.name(n)).join(".");
const upper = typeName.toUpperCase();
if (upper === "ABAP.QUAN" || KNOWN_QUAN_DATA_ELEMENTS.includes(upper)) {
return "QUAN";
} else if (upper === "ABAP.CURR" || KNOWN_CURR_DATA_ELEMENTS.includes(upper)) {
return "CURR";
}
const object = ddic.lookup(typeName).object;
if (object instanceof DataElement) {
const dataType = object.getDataType(this.reg)?.toUpperCase();
if (dataType === "QUAN" || dataType === "CURR") {
return dataType;
}
}
return undefined;
}
private findSemanticReference(annotations: string[], type: "QUAN" | "CURR"): string | undefined {
const expression = type === "QUAN"
? /(?:SEMANTICS\.QUANTITY|SEMANTICS:\{QUANTITY:\{)\.?UNITOFMEASURE:'([^']+)'/i
: /(?:SEMANTICS\.AMOUNT|SEMANTICS:\{AMOUNT:\{)\.?CURRENCYCODE:'([^']+)'/i;
for (const annotation of annotations) {
const match = annotation.match(expression);
if (match) {
return match[1];
}
}
return undefined;
}
private hasCompanionAnnotation(annotations: string[], type: "QUAN" | "CURR"): boolean {
if (type === "QUAN") {
return annotations.some(a => a.toUpperCase().includes("@SEMANTICS.UNITOFMEASURE:TRUE")
|| a.toUpperCase().includes("@SEMANTICS:{UNITOFMEASURE:TRUE"));
} else {
return annotations.some(a => a.toUpperCase().includes("@SEMANTICS.CURRENCYCODE:TRUE")
|| a.toUpperCase().includes("@SEMANTICS:{CURRENCYCODE:TRUE"));
}
}
private checkLabels(tree: ExpressionNode, issues: Issue[], file: IFile) {
for (const annotation of tree.findAllExpressionsRecursive(CDSAnnotation)) {
const concatenated = annotation.concatTokens();
if (/@\s*<?\s*EndUserText/i.test(concatenated) === false) {
continue;
}
const expression = /label\s*:\s*'((?:[^']|'')*)'/gi;
let match = expression.exec(concatenated);
while (match !== null) {
const label = match[1].replace(/''/g, "'");
if (label.length > MAX_LABEL_LENGTH) {
const message = `CDS label is ${label.length} characters, maximum is ${MAX_LABEL_LENGTH}`;
issues.push(Issue.atToken(file, annotation.getFirstToken(), message,
this.getMetadata().key, this.conf.severity));
}
match = expression.exec(concatenated);
}
}
}
private checkViewEntityAnnotations(tree: ExpressionNode, issues: Issue[], file: IFile) {
const view = tree.findFirstExpression(CDSDefineView);
if (view === undefined || view.findDirectTokenByText("ENTITY") === undefined) {
return;
}
for (const annotation of tree.findAllExpressionsRecursive(CDSAnnotation)) {
const normalized = annotation.concatTokens().replace(/\s/g, "").toUpperCase().replace(/^@</, "@");
for (const forbidden of VIEW_ENTITY_FORBIDDEN_ANNOTATIONS) {
const upper = forbidden.toUpperCase();
if (normalized.startsWith("@" + upper) === false
&& normalized.startsWith("@" + upper.replace(".", ":{")) === false) {
continue;
}
const message = `Annotation ${forbidden} is not allowed in view entities`;
issues.push(Issue.atToken(file, annotation.getFirstToken(), message,
this.getMetadata().key, this.conf.severity));
}
}
}
private checkReservedNames(tree: ExpressionNode, issues: Issue[], file: IFile) {
for (const element of tree.findAllExpressionsRecursive(CDSElement)) {
if (element.findDirectTokenByText("INCLUDE") !== undefined) {
continue;
}
let nameNode = element.findDirectExpression(CDSAs)?.findDirectExpression(CDSName);
if (nameNode === undefined) {
const names = element.findDirectExpression(CDSPrefixedName)?.findDirectExpressions(CDSName) || [];
nameNode = names[names.length - 1];
}
if (nameNode === undefined) {
continue;
}
const name = this.name(nameNode);
if (RESERVED_ELEMENT_NAMES.includes(name.toUpperCase())) {
issues.push(Issue.atToken(file, nameNode.getFirstToken(), `CDS element name "${name}" is reserved`,
this.getMetadata().key, this.conf.severity));
}
}
}
private checkFields(tree: ExpressionNode, sources: Source[], object: DataDefinition,
issues: Issue[], file: IFile) {
const associationNames = new Set<string>();
for (const association of object.getParsedData()?.associations || []) {
associationNames.add(association.name.toUpperCase());
if (association.as) {
associationNames.add(association.as.toUpperCase());
}
}
for (const element of tree.findAllExpressionsRecursive(CDSElement)) {
const prefixed = element.findDirectExpression(CDSPrefixedName);
if (prefixed === undefined) {
continue;
}
const names = prefixed.findDirectExpressions(CDSName);
if (names.length === 0) {
continue;
}
const first = this.name(names[0]);
if (first === "*" || first.startsWith("$") || first.startsWith("#") || associationNames.has(first.toUpperCase())) {
continue;
}
if (names.length > 1) {
const matchingSources = sources.filter(s => s.names.includes(first.toUpperCase()));
if (matchingSources.length !== 1) {
continue;
}
const source = matchingSources[0];
const sourceType = source.type;
if (!(sourceType instanceof StructureType)) {
continue;
}
const field = this.name(names[1]);
if (field !== "*" && sourceType.getComponentByName(field) === undefined) {
issues.push(Issue.atToken(file, names[1].getFirstToken(), `CDS field "${field}" not found in "${source.entity}"`,
this.getMetadata().key, this.conf.severity));
}
continue;
}
// Do not guess across joins or when a dependency is absent.
if (sources.length !== 1) {
continue;
}
const source = sources[0];
const sourceType = source.type;
if (!(sourceType instanceof StructureType)) {
continue;
}
if (sourceType.getComponentByName(first) === undefined) {
issues.push(Issue.atToken(file, names[0].getFirstToken(), `CDS field "${first}" not found in "${source.entity}"`,
this.getMetadata().key, this.conf.severity));
}
}
}
private name(node: ExpressionNode): string {
return node.concatTokens().replace(/ /g, "");
}
}
|