@abaplint/core
Version:
abaplint - Core API
103 lines (102 loc) • 4.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreferIsNot = exports.PreferIsNotConf = void 0;
const issue_1 = require("../issue");
const _abap_rule_1 = require("./_abap_rule");
const Expressions = require("../abap/2_statements/expressions");
const _basic_rule_config_1 = require("./_basic_rule_config");
const edit_helper_1 = require("../edit_helper");
const _irule_1 = require("./_irule");
const position_1 = require("../position");
class PreferIsNotConf extends _basic_rule_config_1.BasicRuleConfig {
}
exports.PreferIsNotConf = PreferIsNotConf;
class PreferIsNot extends _abap_rule_1.ABAPRule {
constructor() {
super(...arguments);
this.conf = new PreferIsNotConf();
}
getMetadata() {
return {
key: "prefer_is_not",
title: "Prefer IS NOT to NOT IS",
shortDescription: `Prefer IS NOT to NOT IS`,
extendedInformation: `
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-is-not-to-not-is
"if not is_valid( )." examples are skipped`,
tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix, _irule_1.RuleTag.SingleFile],
goodExample: `IF variable IS NOT INITIAL.
IF variable NP 'TODO*'.
IF variable <> 42.
IF variable CO 'hello'.`,
badExample: `IF NOT variable IS INITIAL.
IF NOT variable CP 'TODO*'.
IF NOT variable = 42.
IF NOT variable CA 'hello'.`,
};
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
runParsed(file) {
const issues = [];
for (const s of file.getStatements()) {
for (const c of s.findAllExpressions(Expressions.Compare)) {
if (c.concatTokens().toUpperCase().startsWith("NOT ") === false) {
continue;
}
else if (c.getChildren().length === 2 && c.getChildren()[1].get() instanceof Expressions.MethodCallChain) {
continue;
}
const message = "Prefer IS NOT to NOT IS";
const fix = this.getFix(file, c);
issues.push(issue_1.Issue.atToken(file, c.getFirstToken(), message, this.getMetadata().key, this.conf.severity, fix));
}
}
return issues;
}
getFix(file, c) {
let insertFix;
if (c.getChildren()[2].getFirstToken().getStr().toUpperCase() === "IS") {
const tokenPositionBeforeDelete = c.getChildren()[2].getLastToken().getEnd();
const tokenPosition = new position_1.Position(tokenPositionBeforeDelete.getRow(), tokenPositionBeforeDelete.getCol() + 1);
insertFix = edit_helper_1.EditHelper.insertAt(file, tokenPosition, "NOT ");
}
else if (c.getChildren()[2].getFirstToken().getStr().toUpperCase() === "IN" || c.getChildren()[2].getFirstToken().getStr().toUpperCase() === "BETWEEN") {
const tokenPositionBeforeDelete = c.getChildren()[1].getLastToken().getEnd();
const tokenPosition = new position_1.Position(tokenPositionBeforeDelete.getRow(), tokenPositionBeforeDelete.getCol() + 1);
insertFix = edit_helper_1.EditHelper.insertAt(file, tokenPosition, "NOT ");
}
else if (c.getChildren()[2].getFirstToken().getStr() === "=") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), "<>");
}
else if (c.getChildren()[2].getFirstToken().getStr() === "<>") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), "=");
}
else if (c.getChildren()[2].getFirstToken().getStr() === "<") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), ">");
}
else if (c.getChildren()[2].getFirstToken().getStr() === ">") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), "<");
}
else if (c.getChildren()[2].getFirstToken().getStr() === "<=") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), ">=");
}
else if (c.getChildren()[2].getFirstToken().getStr() === ">=") {
insertFix = edit_helper_1.EditHelper.replaceToken(file, c.getChildren()[2].getLastToken(), "<=");
}
else {
return;
}
const endCol = c.getChildren()[0].getFirstToken().getEnd().getCol() + 1;
const endPosition = new position_1.Position(c.getChildren()[0].getFirstToken().getEnd().getRow(), endCol);
const deleteFix = edit_helper_1.EditHelper.deleteRange(file, c.getChildren()[0].getFirstToken().getStart(), endPosition);
const finalFix = edit_helper_1.EditHelper.merge(insertFix, deleteFix);
return finalFix;
}
}
exports.PreferIsNot = PreferIsNot;
//# sourceMappingURL=prefer_is_not.js.map