UNPKG

@abaplint/core

Version:
64 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NoChainedAssignment = exports.NoChainedAssignmentConf = void 0; const Expressions = require("../abap/2_statements/expressions"); const Statements = require("../abap/2_statements/statements"); 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"); const edit_helper_1 = require("../edit_helper"); class NoChainedAssignmentConf extends _basic_rule_config_1.BasicRuleConfig { } exports.NoChainedAssignmentConf = NoChainedAssignmentConf; class NoChainedAssignment extends _abap_rule_1.ABAPRule { constructor() { super(...arguments); this.conf = new NoChainedAssignmentConf(); } getMetadata() { return { key: "no_chained_assignment", title: "No chained assignment", shortDescription: `Find chained assingments and reports issues`, extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#dont-chain-assignments`, tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix], badExample: `var1 = var2 = var3.`, goodExample: `var2 = var3. var1 = var2.`, }; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } runParsed(file) { const issues = []; for (const s of file.getStatements()) { if (!(s.get() instanceof Statements.Move)) { continue; } if (s.findDirectExpressions(Expressions.Target).length >= 2) { const message = "No chained assignment"; const fix = this.buildFix(file, s); const issue = issue_1.Issue.atStatement(file, s, message, this.getMetadata().key, this.getConfig().severity, fix); issues.push(issue); } } return issues; } buildFix(file, node) { // window of 3 expressions const children = node.getChildren(); let res = ""; for (let i = children.length - 4; i >= 0; i = i - 2) { const concat = children[i].concatTokens() + " " + children[i + 1].concatTokens() + " " + children[i + 2].concatTokens(); res += concat + ".\n"; } return edit_helper_1.EditHelper.replaceRange(file, node.getStart(), node.getEnd(), res.trimEnd()); } } exports.NoChainedAssignment = NoChainedAssignment; //# sourceMappingURL=no_chained_assignment.js.map