@abaplint/core
Version:
abaplint - Core API
112 lines • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnreachableCode = exports.UnreachableCodeConf = void 0;
const issue_1 = require("../issue");
const _abap_rule_1 = require("./_abap_rule");
const _basic_rule_config_1 = require("./_basic_rule_config");
const _statement_1 = require("../abap/2_statements/statements/_statement");
const Statements = require("../abap/2_statements/statements");
const Expressions = require("../abap/2_statements/expressions");
const _irule_1 = require("./_irule");
class UnreachableCodeConf extends _basic_rule_config_1.BasicRuleConfig {
}
exports.UnreachableCodeConf = UnreachableCodeConf;
class UnreachableCode extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new UnreachableCodeConf();
}
getMetadata() {
return {
key: "unreachable_code",
title: "Unreachable code",
shortDescription: `Checks for unreachable code.`,
tags: [_irule_1.RuleTag.SingleFile],
badExample: `RETURN.\nWRITE 'hello'.`,
goodExample: `WRITE 'hello'.\nRETURN.`,
};
}
getMessage() {
return "Unreachable code";
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
const output = [];
let exit = false;
for (const node of file.getStatements()) {
if (node.get() instanceof _statement_1.Comment
|| node.get() instanceof _statement_1.MacroContent
|| node.get() instanceof _statement_1.Empty) {
continue;
}
else if (this.isExit(node)) {
exit = true;
continue;
}
else if (this.isStructure(node.get())) {
exit = false;
continue;
}
if (exit === true) {
const issue = issue_1.Issue.atStatement(file, node, this.getMessage(), this.getMetadata().key, this.conf.severity);
output.push(issue);
exit = false;
}
}
return output;
}
isExit(n) {
const s = n.get();
// todo, RESUMABLE exception
if (s instanceof Statements.Submit && n.findFirstExpression(Expressions.AndReturn) === undefined) {
return true;
}
else if (s instanceof Statements.Leave && n.findFirstExpression(Expressions.AndReturn) === undefined) {
const concat = n.concatTokens();
if (concat.includes(" TO LIST-PROCESSING")) {
return false;
}
return true;
}
else if (s instanceof Statements.Return
|| s instanceof Statements.Continue
|| s instanceof Statements.Exit
|| s instanceof Statements.Raise) {
return true;
}
return false;
}
isStructure(s) {
if (s instanceof Statements.EndIf
|| s instanceof Statements.Else
|| s instanceof Statements.EndLoop
|| s instanceof Statements.EndTry
|| s instanceof Statements.EndMethod
|| s instanceof Statements.EndModule
|| s instanceof Statements.EndForm
|| s instanceof Statements.EndTestSeam
|| s instanceof Statements.EndAt
|| s instanceof Statements.EndSelect
|| s instanceof Statements.AtSelectionScreen
|| s instanceof Statements.EndFunction
|| s instanceof Statements.EndCase
|| s instanceof Statements.EndWhile
|| s instanceof Statements.EndDo
|| s instanceof Statements.Cleanup
|| s instanceof Statements.When
|| s instanceof Statements.WhenOthers
|| s instanceof Statements.WhenType
|| s instanceof Statements.Catch
|| s instanceof Statements.ElseIf) {
return true;
}
return false;
}
}
exports.UnreachableCode = UnreachableCode;
//# sourceMappingURL=unreachable_code.js.map