@abaplint/core
Version:
abaplint - Core API
118 lines • 4.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MethodParameterNames = exports.MethodParameterNamesConf = void 0;
const issue_1 = require("../issue");
const _irule_1 = require("./_irule");
const _abap_object_1 = require("../objects/_abap_object");
const _naming_rule_config_1 = require("./_naming_rule_config");
const name_validator_1 = require("../utils/name_validator");
const _abap_file_information_1 = require("../abap/4_file_information/_abap_file_information");
const ddic_1 = require("../ddic");
class MethodParameterNamesConf extends _naming_rule_config_1.NamingRuleConfig {
constructor() {
super(...arguments);
/** Ignore parameters in methods of exception classes */
this.ignoreExceptions = true;
/** The pattern for importing parameters */
this.importing = "^I._.+$";
/** The pattern for returning parameters */
this.returning = "^R._.+$";
/** The pattern for changing parameters */
this.changing = "^C._.+$";
/** The pattern for exporting parameters */
this.exporting = "^E._.+$";
}
}
exports.MethodParameterNamesConf = MethodParameterNamesConf;
class MethodParameterNames {
constructor() {
this.conf = new MethodParameterNamesConf();
}
getMetadata() {
return {
key: "method_parameter_names",
title: "Method parameter naming conventions",
shortDescription: `Allows you to enforce a pattern, such as a prefix, for method parameter names`,
tags: [_irule_1.RuleTag.Naming, _irule_1.RuleTag.SingleFile],
};
}
getDescription(expected, actual) {
return this.conf.patternKind === "required" ?
"Method parameter name does not match pattern " + expected + ": " + actual :
"Method parameter name must not match pattern " + expected + ": " + actual;
}
getConfig() {
return this.conf;
}
setConfig(conf) {
this.conf = conf;
}
initialize(reg) {
this.reg = reg;
return this;
}
run(obj) {
let ret = [];
if (this.conf.patternKind === undefined) {
this.conf.patternKind = "required";
}
if (!(obj instanceof _abap_object_1.ABAPObject)) {
return [];
}
const ddic = new ddic_1.DDIC(this.reg);
for (const file of obj.getABAPFiles()) {
for (const def of file.getInfo().listInterfaceDefinitions()) {
for (const method of def.methods) {
ret = ret.concat(this.checkMethod(method));
}
}
for (const def of file.getInfo().listClassDefinitions()) {
if (this.conf.ignoreExceptions && ddic.isException(def, obj)) {
continue;
}
for (const method of def.methods) {
if (method.isEventHandler) {
continue;
}
ret = ret.concat(this.checkMethod(method));
}
}
}
return ret;
}
checkMethod(method) {
let ret = [];
for (const p of method.parameters) {
switch (p.direction) {
case _abap_file_information_1.MethodParameterDirection.Importing:
ret = ret.concat(this.checkParameter(p, this.conf.importing));
break;
case _abap_file_information_1.MethodParameterDirection.Exporting:
ret = ret.concat(this.checkParameter(p, this.conf.exporting));
break;
case _abap_file_information_1.MethodParameterDirection.Changing:
ret = ret.concat(this.checkParameter(p, this.conf.changing));
break;
case _abap_file_information_1.MethodParameterDirection.Returning:
ret = ret.concat(this.checkParameter(p, this.conf.returning));
break;
default:
break;
}
}
return ret;
}
checkParameter(param, expected) {
const ret = [];
const regex = new RegExp(expected, "i");
const name = param.name;
if (name_validator_1.NameValidator.violatesRule(name, regex, this.conf)) {
const message = this.getDescription(expected, name);
const issue = issue_1.Issue.atIdentifier(param.identifier, message, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
return ret;
}
}
exports.MethodParameterNames = MethodParameterNames;
//# sourceMappingURL=method_parameter_names.js.map