UNPKG

@abaplint/core

Version:
122 lines (121 loc) 5.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OmitParameterName = exports.OmitParameterNameConf = void 0; const issue_1 = require("../issue"); const _basic_rule_config_1 = require("./_basic_rule_config"); const _irule_1 = require("./_irule"); const Expressions = require("../abap/2_statements/expressions"); const _abap_object_1 = require("../objects/_abap_object"); const syntax_1 = require("../abap/5_syntax/syntax"); const _reference_1 = require("../abap/5_syntax/_reference"); const method_definition_1 = require("../abap/types/method_definition"); const edit_helper_1 = require("../edit_helper"); const _builtin_1 = require("../abap/5_syntax/_builtin"); class OmitParameterNameConf extends _basic_rule_config_1.BasicRuleConfig { } exports.OmitParameterNameConf = OmitParameterNameConf; class OmitParameterName { constructor() { this.conf = new OmitParameterNameConf(); } getMetadata() { return { key: "omit_parameter_name", title: "Omit parameter name", shortDescription: `Omit the parameter name in single parameter calls`, extendedInformation: ` https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#omit-the-parameter-name-in-single-parameter-calls EXPORTING must already be omitted for this rule to take effect, https://rules.abaplint.org/exporting/`, tags: [_irule_1.RuleTag.Styleguide, _irule_1.RuleTag.Quickfix], badExample: `method( param = 2 ).`, goodExample: `method( 2 ).`, }; } initialize(reg) { this.reg = reg; return this; } getConfig() { return this.conf; } setConfig(conf) { this.conf = conf; } run(obj) { var _a, _b; const issues = []; if (!(obj instanceof _abap_object_1.ABAPObject) || obj.getType() === "INTF") { return []; } const spaghetti = new syntax_1.SyntaxLogic(this.reg, obj).run().spaghetti; for (const file of obj.getABAPFiles()) { const stru = file.getStructure(); if (stru === undefined) { continue; } for (const c of stru.findAllExpressions(Expressions.MethodCall)) { if (c.findFirstExpression(Expressions.MethodParameters)) { continue; } // hmm, this will break for nested method calls? const parameters = c.findAllExpressions(Expressions.ParameterS); if (parameters.length > 1 || parameters.length === 0) { continue; } const name = c.findDirectExpression(Expressions.MethodName); if (name === undefined) { continue; } const param = c.findDirectExpression(Expressions.MethodCallParam); if (param === undefined) { continue; } const ref = this.findMethodReference(name.getFirstToken(), spaghetti, file.getFilename()); if (ref === undefined) { continue; } const i = ref.getDefaultImporting(); if (i === undefined) { continue; } const p = (_a = parameters[0].findDirectExpression(Expressions.ParameterName)) === null || _a === void 0 ? void 0 : _a.getFirstToken(); if ((p === null || p === void 0 ? void 0 : p.getStr().toUpperCase()) === i.toUpperCase()) { const message = "Omit default parameter name \"" + i + "\""; const end = (_b = parameters[0].findDirectExpression(Expressions.Source)) === null || _b === void 0 ? void 0 : _b.getFirstToken().getStart(); if (end) { const fix = edit_helper_1.EditHelper.deleteRange(file, p.getStart(), end); issues.push(issue_1.Issue.atRange(file, p.getStart(), end, message, this.getMetadata().key, this.getConfig().severity, fix)); } else { issues.push(issue_1.Issue.atToken(file, name.getFirstToken(), message, this.getMetadata().key, this.getConfig().severity)); } } } } return issues; } /////////////////// findMethodReference(token, spaghetti, filename) { const scope = spaghetti.lookupPosition(token.getStart(), filename); if (scope === undefined) { return undefined; } for (const r of scope.getData().references) { if (r.referenceType !== _reference_1.ReferenceType.MethodReference && r.referenceType !== _reference_1.ReferenceType.BuiltinMethodReference) { continue; } else if (r.position.getStart().equals(token.getStart())) { if (r.resolved instanceof _builtin_1.BuiltInMethod) { return r.resolved; } else if (r.resolved instanceof method_definition_1.MethodDefinition) { return r.resolved.getParameters(); } } } return undefined; } } exports.OmitParameterName = OmitParameterName; //# sourceMappingURL=omit_parameter_name.js.map