@abaplint/core
Version:
abaplint - Core API
76 lines (75 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreferRaiseExceptionNew = exports.PreferRaiseExceptionNewConf = void 0;
const issue_1 = require("../issue");
const _abap_rule_1 = require("./_abap_rule");
const _basic_rule_config_1 = require("./_basic_rule_config");
const edit_helper_1 = require("../edit_helper");
const _irule_1 = require("./_irule");
const version_1 = require("../version");
const Statements = require("../abap/2_statements/statements");
class PreferRaiseExceptionNewConf extends _basic_rule_config_1.BasicRuleConfig {
}
exports.PreferRaiseExceptionNewConf = PreferRaiseExceptionNewConf;
class PreferRaiseExceptionNew extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new PreferRaiseExceptionNewConf();
}
getMetadata() {
return {
key: "prefer_raise_exception_new",
title: "Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE",
shortDescription: `Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE`,
extendedInformation: `
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-raise-exception-new-to-raise-exception-type
From 752 and up`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.Upport],
goodExample: `RAISE EXCEPTION NEW cx_generation_error( previous = exception ).`,
badExample: `RAISE EXCEPTION TYPE cx_generation_error
EXPORTING
previous = exception.`,
};
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
if (this.reg.getConfig().getVersion() < version_1.Version.v752) {
return [];
}
const issues = [];
for (const statement of file.getStatements()) {
if (statement.get() instanceof Statements.Raise) {
const concat = statement.concatTokens().toUpperCase();
if (concat.includes(" MESSAGE")) {
continue;
}
if (concat.startsWith("RAISE EXCEPTION TYPE ")) {
const message = "Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE";
const fix = this.getFix(file, statement, concat.includes(" EXPORTING") ? true : false);
issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, fix));
}
}
}
return issues;
}
getFix(file, statement, withExporting) {
const children = statement.getChildren();
let contentFix = undefined;
if (withExporting) {
const fixText = "( " + children[5].concatTokens() + " ).";
contentFix = edit_helper_1.EditHelper.replaceRange(file, children[3].getLastToken().getEnd(), statement.getEnd(), fixText);
}
else {
contentFix = edit_helper_1.EditHelper.replaceRange(file, children[3].getLastToken().getEnd(), statement.getEnd(), "( ).");
}
const replaceType = edit_helper_1.EditHelper.replaceToken(file, children[2].getFirstToken(), "NEW");
return edit_helper_1.EditHelper.merge(contentFix, replaceType);
}
}
exports.PreferRaiseExceptionNew = PreferRaiseExceptionNew;
//# sourceMappingURL=prefer_raise_exception_new.js.map