UNPKG

@abaplint/core

Version:
104 lines (103 loc) 4.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NoYodaConditions = exports.NoYodaConditionsConf = 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 _irule_1 = require("./_irule"); class NoYodaConditionsConf extends _basic_rule_config_1.BasicRuleConfig { constructor() { super(...arguments); /** Only report issues where the left side is a constant */ this.onlyConstants = false; } } exports.NoYodaConditionsConf = NoYodaConditionsConf; class NoYodaConditions extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new NoYodaConditionsConf(); } getMetadata() { return { key: "no_yoda_conditions", title: "No Yoda conditions", shortDescription: `Finds Yoda conditions and reports issues`, extendedInformation: `https://en.wikipedia.org/wiki/Yoda_conditions Conditions with operators CP, NP, CS, NS, CA, NA, CO, CN are ignored`, tags: [_irule_1.RuleTag.SingleFile], badExample: `IF 0 <> sy-subrc. ENDIF.`, goodExample: `IF sy-subrc <> 0. ENDIF.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { var _a, _b; const issues = []; for (const c of ((_a = file.getStructure()) === null || _a === void 0 ? void 0 : _a.findAllExpressions(Expressions.Compare)) || []) { const operator = (_b = c.findDirectExpression(Expressions.CompareOperator)) === null || _b === void 0 ? void 0 : _b.concatTokens().toUpperCase(); if (operator === undefined || operator === "CP" || operator === "NP" || operator === "CS" || operator === "NS" || operator === "CA" || operator === "NA" || operator === "CO" || operator === "CN") { continue; } const sources = c.findDirectExpressions(Expressions.Source); if (sources.length !== 2) { continue; } if (this.conf.onlyConstants === true) { if (this.isConstant(sources[0]) === true && this.isConstant(sources[1]) === false) { const start = sources[0].getFirstToken().getStart(); const end = sources[1].getLastToken().getEnd(); const issue = issue_1.Issue.atRange(file, start, end, "No Yoda conditions", this.getMetadata().key, this.conf.severity); issues.push(issue); } continue; } // Scenarios: // constant COMPARE chain // constant COMPARE multiple tokens with spaces // fieldChain COMPARE multiple tokens with spaces if ((this.withoutSpaces(sources[0]) === false && this.withoutSpaces(sources[1]) === true) || ((this.isConstant(sources[0]) === true && this.isFieldChain(sources[1]) === true))) { const start = sources[0].getFirstToken().getStart(); const end = sources[1].getLastToken().getEnd(); const issue = issue_1.Issue.atRange(file, start, end, "No Yoda conditions", this.getMetadata().key, this.conf.severity); issues.push(issue); } } return issues; } isConstant(node) { var _a; if (node.getChildren().length > 1) { return false; } return ((_a = node.getFirstChild()) === null || _a === void 0 ? void 0 : _a.get()) instanceof Expressions.Constant; } isFieldChain(node) { var _a; if (node.getChildren().length > 1) { return false; } return ((_a = node.getFirstChild()) === null || _a === void 0 ? void 0 : _a.get()) instanceof Expressions.FieldChain; } withoutSpaces(node) { return node.concatTokensWithoutStringsAndComments().includes(" "); } } exports.NoYodaConditions = NoYodaConditions; //# sourceMappingURL=no_yoda_conditions.js.map