@abaplint/core
Version:
abaplint - Core API
80 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExitOrCheck = exports.ExitOrCheckConf = void 0;
const issue_1 = require("../issue");
const Statements = require("../abap/2_statements/statements");
const _abap_rule_1 = require("./_abap_rule");
const _basic_rule_config_1 = require("./_basic_rule_config");
const _irule_1 = require("./_irule");
const edit_helper_1 = require("../edit_helper");
class ExitOrCheckConf extends _basic_rule_config_1.BasicRuleConfig {
constructor() {
super(...arguments);
this.allowExit = false;
this.allowCheck = false;
}
}
exports.ExitOrCheckConf = ExitOrCheckConf;
class ExitOrCheck extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new ExitOrCheckConf();
}
getMetadata() {
return {
key: "exit_or_check",
title: "Find EXIT or CHECK outside loops",
shortDescription: `Detects usages of EXIT or CHECK statements outside of loops.
Use RETURN to leave procesing blocks instead.`,
extendedInformation: `https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abenleave_processing_blocks.htm
https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abapcheck_processing_blocks.htm
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#check-vs-return`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Quickfix],
};
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
const issues = [];
const stack = [];
for (const statement of file.getStatements()) {
const get = statement.get();
if (get instanceof Statements.Loop
|| get instanceof Statements.While
|| get instanceof Statements.LoopAtScreen
|| get instanceof Statements.SelectLoop
|| get instanceof Statements.Do) {
stack.push(statement);
}
else if (get instanceof Statements.EndLoop
|| get instanceof Statements.EndWhile
|| get instanceof Statements.EndSelect
|| get instanceof Statements.EndDo) {
stack.pop();
}
else if (this.conf.allowCheck === false && get instanceof Statements.Check && stack.length === 0) {
const message = "CHECK is not allowed outside of loops";
let tokensString = statement.concatTokens();
tokensString = tokensString.replace(/^check /i, "CHECK ");
tokensString = tokensString.split("CHECK")[1].trim();
const replacement = "IF NOT " + tokensString + "\n RETURN.\nENDIF.";
const fix = edit_helper_1.EditHelper.replaceRange(file, statement.getFirstToken().getStart(), statement.getLastToken().getEnd(), replacement);
const issue = issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, fix);
issues.push(issue);
}
else if (this.conf.allowExit === false && get instanceof Statements.Exit && stack.length === 0) {
const message = "EXIT is not allowed outside of loops";
const fix = edit_helper_1.EditHelper.replaceToken(file, statement.getFirstToken(), "RETURN");
const issue = issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, fix);
issues.push(issue);
}
}
return issues;
}
}
exports.ExitOrCheck = ExitOrCheck;
//# sourceMappingURL=exit_or_check.js.map