UNPKG

@abaplint/core

Version:
150 lines (149 loc) 5.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IdenticalConditions = exports.IdenticalConditionsConf = void 0; const Expressions = require("../abap/2_statements/expressions"); const Statements = require("../abap/2_statements/statements"); const Structures = require("../abap/3_structures/structures"); const issue_1 = require("../issue"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _abap_rule_1 = require("./_abap_rule"); const _irule_1 = require("./_irule"); const nodes_1 = require("../abap/nodes"); class Conditions { constructor() { this.arr = []; this.arr = []; } push(e) { this.arr.push(e.concatTokens()); } findFirstDuplicate() { return this.arr.find(x => this.arr.indexOf(x) !== this.arr.lastIndexOf(x)); } } class IdenticalConditionsConf extends _basic_rule_config_1.BasicRuleConfig { } exports.IdenticalConditionsConf = IdenticalConditionsConf; class IdenticalConditions extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new IdenticalConditionsConf(); } getMetadata() { return { key: "identical_conditions", title: "Identical conditions", shortDescription: `Find identical conditions in IF + CASE + WHILE etc Prerequsites: code is pretty printed with identical cAsE`, tags: [_irule_1.RuleTag.SingleFile], badExample: `IF foo = bar OR 1 = a OR foo = bar. ENDIF. CASE bar. WHEN '1'. WHEN 'A' OR '1'. ENDCASE.`, goodExample: `IF foo = bar OR 1 = a. ENDIF. CASE bar. WHEN '1'. WHEN 'A'. ENDCASE.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { let issues = []; const structure = file.getStructure(); if (structure === undefined) { return []; } for (const i of structure.findAllStructures(Structures.If)) { issues = issues.concat(this.analyzeIf(file, i)); } for (const i of structure.findAllStructures(Structures.Case)) { issues = issues.concat(this.analyzeWhen(file, i)); } for (const i of structure.findAllExpressions(Expressions.Cond)) { issues = issues.concat(this.analyzeCond(file, i)); } return issues; } //////////////// analyzeCond(file, node) { const conditions = new Conditions(); let operator = ""; for (const c of node.getChildren()) { if (c instanceof nodes_1.ExpressionNode) { conditions.push(c); } else if (operator === "") { operator = c.get().getStr().toUpperCase(); } else if (operator !== c.get().getStr().toUpperCase()) { return []; } } const duplicate = conditions.findFirstDuplicate(); if (duplicate) { const message = "Identical conditions: " + duplicate; const issue = issue_1.Issue.atToken(file, node.getFirstToken(), message, this.getMetadata().key, this.conf.severity); return [issue]; } return []; } analyzeIf(file, node) { var _a; const conditions = new Conditions(); const i = node.findDirectStatement(Statements.If); if (i === undefined) { throw new Error("identical_conditions, no IF found"); } const c = i === null || i === void 0 ? void 0 : i.findDirectExpression(Expressions.Cond); if (c) { conditions.push(c); } for (const e of node.findDirectStructures(Structures.ElseIf)) { const c = (_a = e.findDirectStatement(Statements.ElseIf)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.Cond); if (c) { conditions.push(c); } } const duplicate = conditions.findFirstDuplicate(); if (duplicate) { const message = "Identical conditions: " + duplicate; const issue = issue_1.Issue.atStatement(file, i, message, this.getMetadata().key, this.conf.severity); return [issue]; } return []; } analyzeWhen(file, node) { const conditions = new Conditions(); const i = node.findDirectStatement(Statements.Case); if (i === undefined) { throw new Error("identical_conditions, no CASE found"); } for (const e of node.findDirectStructures(Structures.When)) { const w = e.findDirectStatement(Statements.When); if (w === undefined) { continue; } for (const s of w.findAllExpressions(Expressions.Source)) { conditions.push(s); } } const duplicate = conditions.findFirstDuplicate(); if (duplicate) { const message = "Identical conditions: " + duplicate; const issue = issue_1.Issue.atStatement(file, i, message, this.getMetadata().key, this.conf.severity); return [issue]; } return []; } } exports.IdenticalConditions = IdenticalConditions; //# sourceMappingURL=identical_conditions.js.map