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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42199x 42199x 42199x 42199x 1x 21100x 21100x 21100x 21100x 21100x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 63442x 21100x 21100x 20205x 20205x 21100x 21100x 375x 375x 21100x 21100x 404x 404x 13x 13x 391x 391x 391x 391x 391x 404x 21x 21x 21x 21x 6x 6x 15x 21x 14x 14x 7x 7x 14x 8x 8x 8x 391x 391x 391x 21100x 21100x 14x 14x 14x 3x 3x 11x 11x 11x 11x 11x 14x 14x 14x 21100x | import {Issue} from "../issue";
import {ABAPRule} from "./_abap_rule";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {StatementNode} from "../abap/nodes";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ISyntaxResult} from "../abap/5_syntax/_spaghetti_scope";
import {GenericObjectReferenceType, ObjectReferenceType, VoidType} from "../abap/types/basic";
import {nodeChainType} from "./_type_lookup";
export class UseMessageClassConf extends BasicRuleConfig {
/** Allow MESSAGE with an object reference, eg. an exception, like MESSAGE lx_error TYPE 'E' */
public allowExceptions: boolean = true;
}
export class UseMessageClass extends ABAPRule {
private conf = new UseMessageClassConf();
public getMetadata(): IRuleMetadata {
return {
key: "use_message_class",
title: "Use message class",
shortDescription: `Define messages in SE91 instead of using inline text or variables`,
extendedInformation: `If allowExceptions is enabled, MESSAGE with an object reference is allowed, eg. MESSAGE lx_error TYPE 'E'.
Void types are only allowed if the type name contains "CX_", as void class references cannot be distinguished from other void types.`,
tags: [RuleTag.Styleguide, RuleTag.SingleFile],
badExample: `MESSAGE 'Something went wrong' TYPE 'E'.`,
goodExample: `MESSAGE e001(bc_msg).`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: UseMessageClassConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
const structure = file.getStructure();
if (structure === undefined) {
return [];
}
const issues: Issue[] = [];
const key = this.getMetadata().key;
let syntax: ISyntaxResult | undefined = undefined;
for (const statement of structure.findAllStatements(Statements.Message)) {
// The parser populates MessageSource only for the class-based forms (e001(bc_msg) or ID/TYPE/NUMBER).
// The text form (MESSAGE <source> TYPE <source>) uses MessageSourceSource directly with no MessageSource child.
// Absence of MessageSource is therefore a reliable discriminator between the two forms.
if (statement.findDirectExpression(Expressions.MessageSource) !== undefined) {
continue;
}
if (this.conf.allowExceptions === true) {
syntax ??= new SyntaxLogic(this.reg, obj).run();
if (this.isObjectReference(statement, file, syntax)) {
continue;
}
}
issues.push(Issue.atStatement(file, statement, "Use a message class (SE91) instead of inline text or variable", key, this.conf.severity));
}
return issues;
}
private isObjectReference(statement: StatementNode, file: ABAPFile, syntax: ISyntaxResult): boolean {
const chain = statement.findDirectExpression(Expressions.MessageSourceSource)
?.findDirectExpression(Expressions.Source)?.findDirectExpression(Expressions.FieldChain);
if (chain === undefined) {
return false;
}
const type = nodeChainType(chain, syntax.spaghetti, file);
// void class references cannot be distinguished from other void types, guess based on the name
return type instanceof ObjectReferenceType
|| type instanceof GenericObjectReferenceType
|| (type instanceof VoidType && /CX_/i.test(type.getVoided() || ""));
}
}
|