@abaplint/core
Version:
abaplint - Core API
94 lines • 3.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoPublicAttributes = exports.NoPublicAttributesConf = void 0;
const issue_1 = require("../issue");
const _basic_rule_config_1 = require("./_basic_rule_config");
const _abap_rule_1 = require("./_abap_rule");
const visibility_1 = require("../abap/4_file_information/visibility");
const _abap_file_information_1 = require("../abap/4_file_information/_abap_file_information");
const ddic_1 = require("../ddic");
const _irule_1 = require("./_irule");
class NoPublicAttributesConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
/** Allows public attributes, if they are declared as READ-ONLY. */
this.allowReadOnly = false;
/** Option to ignore test classes for this check. */
this.ignoreTestClasses = false;
}
}
exports.NoPublicAttributesConf = NoPublicAttributesConf;
class NoPublicAttributes extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new NoPublicAttributesConf();
}
getMetadata() {
return {
key: "no_public_attributes",
title: "No public attributes",
shortDescription: `Checks that classes and interfaces don't contain any public attributes.
Exceptions are excluded from this rule.`,
extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#members-private-by-default-protected-only-if-needed`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.SingleFile],
};
}
getDescription(name) {
return "Public attributes are not allowed, attribute \"" + name + "\"";
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file, obj) {
this.file = file;
const attributes = this.getAllPublicAttributes(obj);
return this.findAllIssues(attributes);
}
getAllPublicAttributes(obj) {
let attributes = [];
attributes = attributes.concat(this.getAllPublicClassAttributes(obj));
attributes = attributes.concat(this.getAllPublicInterfaceAttributes());
return attributes;
}
getAllPublicClassAttributes(obj) {
let attributes = [];
const ddic = new ddic_1.DDIC(this.reg);
for (const classDef of this.file.getInfo().listClassDefinitions()) {
if (ddic.isException(classDef, obj)) {
continue;
}
attributes = attributes.concat(classDef.attributes.filter(a => a.visibility === visibility_1.Visibility.Public));
}
return attributes;
}
getAllPublicInterfaceAttributes() {
let attributes = [];
for (const interfaceDef of this.file.getInfo().listInterfaceDefinitions()) {
attributes = attributes.concat(interfaceDef.attributes.filter(a => a.visibility === visibility_1.Visibility.Public));
}
return attributes;
}
findAllIssues(attributes) {
const issues = [];
for (const attr of attributes) {
if (this.conf.allowReadOnly === true && attr.readOnly) {
continue;
}
else if (attr.level === _abap_file_information_1.AttributeLevel.Constant) {
continue;
}
else if ((this.conf.ignoreTestClasses === true)
&& this.file.getFilename().includes(".testclasses.")) {
continue;
}
const issue = issue_1.Issue.atIdentifier(attr.identifier, this.getDescription(attr.name), this.getMetadata().key, this.conf.severity);
issues.push(issue);
}
return issues;
}
}
exports.NoPublicAttributes = NoPublicAttributes;
//# sourceMappingURL=no_public_attributes.js.map