UNPKG

@abaplint/core

Version:
73 lines 2.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShortCase = exports.ShortCaseConf = void 0; const issue_1 = require("../issue"); const _abap_rule_1 = require("./_abap_rule"); const Statements = require("../abap/2_statements/statements"); const Expressions = require("../abap/2_statements/expressions"); const Structures = require("../abap/3_structures/structures"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _irule_1 = require("./_irule"); class ShortCaseConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** The smallest number of WHEN branches which will trigger a violation. * Example: if length = 1, at least 2 branches are required */ this.length = 1; /** List of inputs for CASE which are allowed * @uniqueItems true */ this.allow = []; } } exports.ShortCaseConf = ShortCaseConf; class ShortCase extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new ShortCaseConf(); } getMetadata() { return { key: "short_case", title: "Short CASE", shortDescription: `Checks for CASE statements which have fewer than the specified number of branches`, extendedInformation: `Short CASE constructs can be changed to IF`, tags: [_irule_1.RuleTag.SingleFile], badExample: "CASE moo.\nWHEN 'X'.\nENDCASE.", goodExample: "IF moo = 'X'.\nENDIF.", }; } getMessage() { return "CASE construct too short, it must have a minimum of " + (this.conf.length + 1) + " WHEN branches"; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { const issues = []; const struc = file.getStructure(); if (struc === undefined) { return []; } for (const c of struc.findAllStructures(Structures.Case)) { const clist = c.findDirectStatements(Statements.Case); if (clist.length > 0 && this.conf.allow && this.conf.allow.find((e) => { return e === clist[0].getTokens()[1].getStr(); })) { continue; } if (c.findDirectStructures(Structures.When).length <= this.conf.length) { if (c.findAllExpressions(Expressions.Or).length > 0) { continue; } const issue = issue_1.Issue.atToken(file, c.getFirstToken(), this.getMessage(), this.getMetadata().key, this.conf.severity); issues.push(issue); } } return issues; } } exports.ShortCase = ShortCase; //# sourceMappingURL=short_case.js.map