UNPKG

@abaplint/core

Version:
82 lines 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CatchAndRaise = exports.CatchAndRaiseConf = void 0; const issue_1 = require("../issue"); const _abap_rule_1 = require("./_abap_rule"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _irule_1 = require("./_irule"); const Structures = require("../abap/3_structures/structures"); const Statements = require("../abap/2_statements/statements"); const Expressions = require("../abap/2_statements/expressions"); class CatchAndRaiseConf extends _basic_rule_config_1.BasicRuleConfig { } exports.CatchAndRaiseConf = CatchAndRaiseConf; class CatchAndRaise extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new CatchAndRaiseConf(); } getMetadata() { return { key: "catch_and_raise", title: "Catch and re-raise same exception", shortDescription: `Reports CATCH blocks that only re-raise the caught exception without any handling`, badExample: `TRY.\n something( ).\nCATCH zcx_something INTO DATA(lv_exc).\n RAISE EXCEPTION lv_exc.\nENDTRY.`, goodExample: `TRY.\n something( ).\nCATCH zcx_something.\n " handle exception\nENDTRY.`, tags: [_irule_1.RuleTag.SingleFile], }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { const issues = []; const stru = file.getStructure(); if (stru === undefined) { return []; } for (const catchStruct of stru.findAllStructures(Structures.Catch)) { const catchStatement = catchStruct.findDirectStatement(Statements.Catch); if (catchStatement === undefined) { continue; } const target = catchStatement.findFirstExpression(Expressions.Target); if (target === undefined) { continue; } const targetField = target.findFirstExpression(Expressions.TargetField); if (targetField === undefined) { continue; } const caughtVar = targetField.getFirstToken().getStr().toUpperCase(); const allStatements = catchStruct.findAllStatementNodes(); if (allStatements.length !== 2) { continue; } if (!(allStatements[1].get() instanceof Statements.Raise)) { continue; } const raiseStatement = allStatements[1]; const raiseConcat = raiseStatement.concatTokens().toUpperCase().replace(/\.$/, "").trim(); if (!raiseConcat.startsWith("RAISE EXCEPTION ")) { continue; } const parts = raiseConcat.split(/\s+/); if (parts.length !== 3) { continue; } if (parts[2] === caughtVar) { issues.push(issue_1.Issue.atStatement(file, catchStatement, this.getMessage(), this.getMetadata().key, this.conf.severity)); } } return issues; } getMessage() { return "Caught exception is immediately re-raised, CATCH block has no effect"; } } exports.CatchAndRaise = CatchAndRaise; //# sourceMappingURL=catch_and_raise.js.map