UNPKG

@abaplint/core

Version:
96 lines 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PreferredCompareOperator = exports.PreferredCompareOperatorConf = void 0; const Expressions = require("../abap/2_statements/expressions"); 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"); class PreferredCompareOperatorConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** Operators which are not allowed */ this.badOperators = ["EQ", "><", "NE", "GE", "GT", "LT", "LE"]; } } exports.PreferredCompareOperatorConf = PreferredCompareOperatorConf; class PreferredCompareOperator extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new PreferredCompareOperatorConf(); this.operatorMapping = new Map(); } getMetadata() { return { key: "preferred_compare_operator", title: "Preferred compare operator", shortDescription: `Configure undesired operator variants`, tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Quickfix], badExample: `IF foo EQ bar. ENDIF.`, goodExample: `IF foo = bar. ENDIF.`, }; } getDescription(operator) { return "Compare operator \"" + operator + "\" not preferred"; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { this.buildMapping(); const issues = []; const struc = file.getStructure(); if (struc === undefined) { return []; } const operators = struc.findAllExpressionsMulti([Expressions.CompareOperator, Expressions.SQLCompareOperator]); for (const op of operators) { const token = op.getLastToken(); // todo, performance, lookup in hash map instead(JS object) if (this.conf.badOperators.indexOf(token.getStr().toUpperCase()) >= 0) { issues.push(this.createIssue(token, file)); } } return issues; } buildMapping() { if (this.operatorMapping.size === 0) { this.operatorMapping.set("EQ", "="); this.operatorMapping.set("><", "<>"); this.operatorMapping.set("NE", "<>"); this.operatorMapping.set("GE", ">="); this.operatorMapping.set("GT", ">"); this.operatorMapping.set("LT", "<"); this.operatorMapping.set("LE", "<="); this.operatorMapping.set("=", "EQ"); this.operatorMapping.set("<>", "NE"); this.operatorMapping.set(">=", "GE"); this.operatorMapping.set(">", "GT"); this.operatorMapping.set("<", "LT"); this.operatorMapping.set("<=", "LE"); } } createIssue(token, file) { var _a; const message = this.getDescription(token.getStr()); const replacementToken = (_a = this.operatorMapping) === null || _a === void 0 ? void 0 : _a.get(token.getStr()); // values in badOperators can be entered by the user and may not necessarily be actual operators if (replacementToken) { const fix = edit_helper_1.EditHelper.replaceRange(file, token.getStart(), token.getEnd(), replacementToken); const issue = issue_1.Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity, fix); return issue; } else { const issue = issue_1.Issue.atToken(file, token, message, this.getMetadata().key, this.conf.severity); return issue; } } } exports.PreferredCompareOperator = PreferredCompareOperator; //# sourceMappingURL=preferred_compare_operator.js.map