UNPKG

@abaplint/core

Version:
88 lines (86 loc) 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReduceProceduralCode = exports.ReduceProceduralCodeConf = void 0; const _basic_rule_config_1 = require("./_basic_rule_config"); const _abap_rule_1 = require("./_abap_rule"); const _irule_1 = require("./_irule"); const Statements = require("../abap/2_statements/statements"); const issue_1 = require("../issue"); const _statement_1 = require("../abap/2_statements/statements/_statement"); class ReduceProceduralCodeConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); this.maxStatements = 10; } } exports.ReduceProceduralCodeConf = ReduceProceduralCodeConf; class ReduceProceduralCode extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new ReduceProceduralCodeConf(); } getMetadata() { return { key: "reduce_procedural_code", title: "Reduce procedural code", shortDescription: `Checks FORM and FUNCTION-MODULE have few statements`, extendedInformation: `Delegate logic to a class method instead of using FORM or FUNCTION-MODULE. https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-object-orientation-to-procedural-programming Comments are not counted as statements.`, tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Styleguide], badExample: `FORM foo. DATA lv_bar TYPE i. lv_bar = 2 + 2. IF lv_bar = 4. WRITE 'hello world'. ENDIF. DATA lv_bar TYPE i. lv_bar = 2 + 2. IF lv_bar = 4. WRITE 'hello world'. ENDIF. ENDFORM.`, goodExample: `FORM foo. NEW zcl_global_class( )->run_logic( ). ENDFORM.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { const issues = []; if (file.getStructure() === undefined) { // constains syntax errors, skip this check return issues; } let doCount = undefined; let count = 0; for (const statement of file.getStatements()) { if (statement.get() instanceof Statements.Form || statement.get() instanceof Statements.FunctionModule) { doCount = statement; count = 0; } else if (statement.get() instanceof Statements.EndForm || statement.get() instanceof Statements.EndFunction) { if (count >= this.conf.maxStatements && doCount !== undefined) { const message = "Reduce procedural code, max " + this.conf.maxStatements + " statements"; const issue = issue_1.Issue.atStatement(file, doCount, message, this.getMetadata().key, this.conf.severity); issues.push(issue); } doCount = undefined; } else if (statement.get() instanceof _statement_1.Comment) { continue; } else if (doCount !== undefined) { count = count + 1; } } return issues; } } exports.ReduceProceduralCode = ReduceProceduralCode; //# sourceMappingURL=reduce_procedural_code.js.map