@abaplint/core
Version:
abaplint - Core API
112 lines (111 loc) • 4.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MethodLength = exports.MethodLengthConf = void 0;
const issue_1 = require("../issue");
const Objects = require("../objects");
const method_length_stats_1 = require("../utils/method_length_stats");
const _irule_1 = require("./_irule");
const _basic_rule_config_1 = require("./_basic_rule_config");
const form_length_stats_1 = require("../utils/form_length_stats");
class MethodLengthConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
/** Maximum method/form length in statements. */
this.statements = 100;
/** Checks for empty methods/forms. */
this.errorWhenEmpty = true;
/** Option to ignore test classes for this check. */
this.ignoreTestClasses = false;
/** Option to check forms. */
this.checkForms = true;
}
}
exports.MethodLengthConf = MethodLengthConf;
var IssueType;
(function (IssueType) {
IssueType[IssueType["EmptyMethod"] = 0] = "EmptyMethod";
IssueType[IssueType["MaxStatements"] = 1] = "MaxStatements";
})(IssueType || (IssueType = {}));
class MethodLength {
constructor() {
this.conf = new MethodLengthConf();
}
getMetadata() {
return {
key: "method_length",
title: "Method/Form Length",
shortDescription: `Checks relating to method/form length.`,
extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#keep-methods-small
Abstract methods without statements are considered okay.`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.SingleFile],
};
}
getDescription(issueType, actual, type) {
switch (issueType) {
case IssueType.EmptyMethod: {
return "Empty " + type;
}
case IssueType.MaxStatements: {
return "Reduce " + type + " length to max " + this.conf.statements + " statements, currently " + actual;
}
default: {
return "";
}
}
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
initialize(_reg) {
return this;
}
run(obj) {
var _a;
if (this.conf.ignoreTestClasses === true
&& obj instanceof Objects.Class
&& ((_a = obj.getClassDefinition()) === null || _a === void 0 ? void 0 : _a.isForTesting) === true) {
return [];
}
const methodStats = method_length_stats_1.MethodLengthStats.run(obj);
const methodIssues = this.check(methodStats, "METHOD");
let formIssues = [];
if (this.conf.checkForms) {
const formStats = form_length_stats_1.FormLengthStats.run(obj);
formIssues = this.check(formStats, "FORM");
}
return methodIssues.concat(formIssues);
}
// ***********************
check(stats, type) {
const issues = [];
for (const s of stats) {
if ((this.conf.ignoreTestClasses === true)
&& s.file.getFilename().includes(".testclasses.")) {
continue;
}
if (s.count === 0 && this.conf.errorWhenEmpty === true) {
if (this.isAbstract(s)) {
continue;
}
const issue = issue_1.Issue.atPosition(s.file, s.pos, this.getDescription(IssueType.EmptyMethod, "0", type), this.getMetadata().key, this.conf.severity);
issues.push(issue);
continue;
}
if (s.count > this.conf.statements) {
const message = this.getDescription(IssueType.MaxStatements, s.count.toString(), type);
const issue = issue_1.Issue.atPosition(s.file, s.pos, message, this.getMetadata().key, this.conf.severity);
issues.push(issue);
}
}
return issues;
}
isAbstract(result) {
const cdef = result.file.getInfo().getClassDefinitionByName(result.className);
return (cdef === null || cdef === void 0 ? void 0 : cdef.isAbstract) === true;
}
}
exports.MethodLength = MethodLength;
//# sourceMappingURL=method_length.js.map