@abaplint/core
Version:
abaplint - Core API
110 lines • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectionScreenNaming = exports.SelectionScreenNamingConf = void 0;
const _abap_rule_1 = require("./_abap_rule");
const issue_1 = require("../issue");
const _naming_rule_config_1 = require("./_naming_rule_config");
const statements_1 = require("../abap/2_statements/statements");
const name_validator_1 = require("../utils/name_validator");
const expressions_1 = require("../abap/2_statements/expressions");
const _irule_1 = require("./_irule");
class SelectionScreenNamingConf extends _naming_rule_config_1.NamingRuleConfig {
constructor() {
super(...arguments);
/** The pattern for selection-screen parameters */
this.parameter = "^P_.+$";
/** The pattern for selection-screen select-options */
this.selectOption = "^S_.+$";
/** The pattern for selection-screen screen elements */
this.screenElement = "^SC_.+$";
}
}
exports.SelectionScreenNamingConf = SelectionScreenNamingConf;
class SelectionScreenNaming extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new SelectionScreenNamingConf();
}
getMetadata() {
return {
key: "selection_screen_naming",
title: "Selection screen naming conventions",
shortDescription: `Allows you to enforce a pattern, such as a prefix, for selection-screen variable names.`,
tags: [_irule_1.RuleTag.Naming, _irule_1.RuleTag.SingleFile],
};
}
getDescription(expected, actual) {
return this.conf.patternKind === "required" ?
`Selection-Screen variable name does not match pattern ${expected}: ${actual}` :
`Selection-Screen variable name must not match pattern ${expected}: ${actual}`;
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
const issues = [];
if (this.conf.patternKind === undefined) {
this.conf.patternKind = "required";
}
let parameterCheckDisabled = false;
let selectOptionDisabled = false;
let screenElementDisabled = false;
if (this.conf.parameter === undefined || this.conf.parameter.length === 0) {
parameterCheckDisabled = true;
}
if (this.conf.selectOption === undefined || this.conf.selectOption.length === 0) {
selectOptionDisabled = true;
}
if (this.conf.screenElement === undefined || this.conf.screenElement.length === 0) {
screenElementDisabled = true;
}
for (const stat of file.getStatements()) {
if ((stat.get() instanceof statements_1.Parameter && !parameterCheckDisabled)
|| (stat.get() instanceof statements_1.SelectOption && !selectOptionDisabled)
|| (stat.get() instanceof statements_1.SelectionScreen && !screenElementDisabled)) {
const fieldNode = this.getFieldForStatementNode(stat);
const regex = new RegExp(this.getPatternForStatement(stat.get()), "i");
if (fieldNode && name_validator_1.NameValidator.violatesRule(fieldNode.getFirstToken().getStr(), regex, this.conf)) {
issues.push(issue_1.Issue.atToken(file, fieldNode.getFirstToken(), this.getDescription(this.getPatternForStatement(stat.get()), fieldNode.getFirstToken().getStr()), this.getMetadata().key, this.conf.severity));
}
}
}
return issues;
}
getPatternForStatement(statement) {
let pattern = "";
if (statement instanceof statements_1.Parameter) {
pattern = this.conf.parameter;
}
else if (statement instanceof statements_1.SelectOption) {
pattern = this.conf.selectOption;
}
else if (statement instanceof statements_1.SelectionScreen) {
pattern = this.conf.screenElement;
}
return pattern;
}
getFieldForStatementNode(statNode) {
if (statNode.get() instanceof statements_1.Parameter) {
return statNode.findFirstExpression(expressions_1.FieldSub);
}
else if (statNode.get() instanceof statements_1.SelectOption) {
return statNode.findFirstExpression(expressions_1.FieldSub);
}
else if (statNode.get() instanceof statements_1.SelectionScreen) {
let ret = statNode.findFirstExpression(expressions_1.InlineField);
if (ret === undefined && statNode.concatTokens().toUpperCase().includes(" BEGIN OF TABBED BLOCK")) {
ret = statNode.findFirstExpression(expressions_1.BlockName);
}
return ret;
}
else {
return undefined;
}
}
}
exports.SelectionScreenNaming = SelectionScreenNaming;
//# sourceMappingURL=selection_screen_naming.js.map