@abaplint/core
Version:
abaplint - Core API
112 lines • 4.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClassAttributeNames = exports.ClassAttributeNamesConf = void 0;
const issue_1 = require("../issue");
const _naming_rule_config_1 = require("./_naming_rule_config");
const name_validator_1 = require("../utils/name_validator");
const _abap_rule_1 = require("./_abap_rule");
const _abap_file_information_1 = require("../abap/4_file_information/_abap_file_information");
const _irule_1 = require("./_irule");
const ddic_1 = require("../ddic");
class ClassAttributeNamesConf extends _naming_rule_config_1.NamingRuleConfig {
constructor() {
super(...arguments);
/** Ignore global exception classes */
this.ignoreExceptions = true;
/** Ignore local classes */
this.ignoreLocal = true;
/** Ignore interfaces */
this.ignoreInterfaces = false;
/** The pattern for static variable names */
this.statics = "^G._.+$";
/** The pattern for instance variable names */
this.instance = "^M._.+$";
/** The pattern for constant variable names */
this.constants = "";
}
}
exports.ClassAttributeNamesConf = ClassAttributeNamesConf;
class ClassAttributeNames extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new ClassAttributeNamesConf();
}
getMetadata() {
return {
key: "class_attribute_names",
title: "Class attributes naming",
shortDescription: `Allows you to enforce a pattern, such as a prefix, for class variable names.`,
tags: [_irule_1.RuleTag.Naming, _irule_1.RuleTag.SingleFile],
};
}
getDescription(actual, expected) {
return this.conf.patternKind === "required" ?
"Class attribute name \"" + actual + "\" does not match pattern " + expected :
"Class attribute name \"" + actual + "\" must not match pattern " + expected;
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file, obj) {
let issues = [];
if (this.conf.patternKind === undefined) {
this.conf.patternKind = "required";
}
let attributes = [];
const ddic = new ddic_1.DDIC(this.reg);
for (const classDef of file.getInfo().listClassDefinitions()) {
if ((classDef.isLocal && this.conf.ignoreLocal)
|| (ddic.isException(classDef, obj) && this.conf.ignoreExceptions)) {
continue;
}
attributes = attributes.concat(classDef.attributes);
}
if (this.conf.ignoreInterfaces === false) {
for (const intfDef of file.getInfo().listInterfaceDefinitions()) {
if (intfDef.isLocal && this.conf.ignoreLocal) {
continue;
}
attributes = attributes.concat(intfDef.attributes);
}
}
issues = this.checkAttributes(attributes);
return issues;
}
checkAttributes(attr) {
if (attr === undefined) {
return [];
}
let ret = [];
for (const a of attr) {
switch (a.level) {
case _abap_file_information_1.AttributeLevel.Instance:
ret = ret.concat(this.checkName(a.identifier, this.conf.instance));
break;
case _abap_file_information_1.AttributeLevel.Static:
ret = ret.concat(this.checkName(a.identifier, this.conf.statics));
break;
case _abap_file_information_1.AttributeLevel.Constant:
ret = ret.concat(this.checkName(a.identifier, this.conf.constants));
break;
default:
break;
}
}
return ret;
}
checkName(attr, expected) {
const ret = [];
const regex = new RegExp(expected, "i");
const name = attr.getName();
if (name_validator_1.NameValidator.violatesRule(name, regex, this.conf)) {
const issue = issue_1.Issue.atIdentifier(attr, this.getDescription(name, expected), this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
return ret;
}
}
exports.ClassAttributeNames = ClassAttributeNames;
//# sourceMappingURL=class_attribute_names.js.map