@abaplint/core
Version:
abaplint - Core API
113 lines • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrefixIsCurrentClass = exports.PrefixIsCurrentClassConf = void 0;
const issue_1 = require("../issue");
const _abap_rule_1 = require("./_abap_rule");
const Structures = require("../abap/3_structures/structures");
const _basic_rule_config_1 = require("./_basic_rule_config");
const expressions_1 = require("../abap/2_statements/expressions");
const position_1 = require("../position");
const edit_helper_1 = require("../edit_helper");
const _irule_1 = require("./_irule");
class PrefixIsCurrentClassConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
/**
* Checks usages of self references with 'me' when calling instance methods
*/
this.omitMeInstanceCalls = true;
}
}
exports.PrefixIsCurrentClassConf = PrefixIsCurrentClassConf;
class PrefixIsCurrentClass extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new PrefixIsCurrentClassConf();
}
getMetadata() {
return {
key: "prefix_is_current_class",
title: "Prefix is current class",
shortDescription: `Reports errors if the current class or interface references itself with "current_class=>"`,
// eslint-disable-next-line max-len
extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#omit-the-self-reference-me-when-calling-an-instance-attribute-or-method`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile],
};
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
return this.checkClasses(file).concat(this.checkInterfaces(file));
}
checkInterfaces(file) {
var _a;
const struc = file.getStructure();
if (struc === undefined) {
return [];
}
const issues = [];
for (const s of struc.findDirectStructures(Structures.Interface)) {
const name = (_a = s.findFirstExpression(expressions_1.InterfaceName)) === null || _a === void 0 ? void 0 : _a.getFirstToken().getStr().toUpperCase();
if (name === undefined) {
continue;
}
const staticAccess = name + "=>";
for (const e of s.findAllExpressions(expressions_1.TypeName)) {
const concat = e.concatTokens().toUpperCase();
if (concat.startsWith(staticAccess)) {
const stat = e.findDirectTokenByText("=>");
if (stat === undefined) {
continue;
}
const start = new position_1.Position(stat.getRow(), stat.getCol() - name.length);
const end = new position_1.Position(stat.getRow(), stat.getCol() + 2);
const fix = edit_helper_1.EditHelper.deleteRange(file, start, end);
issues.push(issue_1.Issue.atToken(file, e.getFirstToken(), "Reference to current interface can be omitted", this.getMetadata().key, this.conf.severity, fix));
}
}
}
return issues;
}
checkClasses(file) {
const struc = file.getStructure();
if (struc === undefined) {
return [];
}
const issues = [];
const classStructures = struc.findDirectStructures(Structures.ClassImplementation);
classStructures.push(...struc.findDirectStructures(Structures.ClassDefinition));
const meAccess = "ME->";
for (const c of classStructures) {
const className = c.findFirstExpression(expressions_1.ClassName).getFirstToken().getStr().toUpperCase();
const staticAccess = className + "=>";
for (const s of c.findAllStatementNodes()) {
const concat = s.concatTokensWithoutStringsAndComments().toUpperCase();
if (concat.includes(staticAccess)) {
const tokenPos = s.findTokenSequencePosition(className, "=>");
if (tokenPos) {
const end = new position_1.Position(tokenPos.getRow(), tokenPos.getCol() + className.length + 2);
const fix = edit_helper_1.EditHelper.deleteRange(file, tokenPos, end);
issues.push(issue_1.Issue.atRange(file, tokenPos, end, "Reference to current class can be omitted: \"" + staticAccess + "\"", this.getMetadata().key, this.conf.severity, fix));
}
}
else if (this.conf.omitMeInstanceCalls === true
&& concat.includes(meAccess)
&& s.findFirstExpression(expressions_1.MethodCall)) {
const tokenPos = s.findTokenSequencePosition("me", "->");
if (tokenPos) {
const end = new position_1.Position(tokenPos.getRow(), tokenPos.getCol() + 4);
const fix = edit_helper_1.EditHelper.deleteRange(file, tokenPos, end);
issues.push(issue_1.Issue.atRange(file, tokenPos, end, "Omit 'me->' in instance calls", this.getMetadata().key, this.conf.severity, fix));
}
}
}
}
return issues;
}
}
exports.PrefixIsCurrentClass = PrefixIsCurrentClass;
//# sourceMappingURL=prefix_is_current_class.js.map