Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 42222x 1x 21117x 21117x 21117x 21117x 21117x 21117x 21117x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 63088x 21117x 21117x 38x 38x 38x 38x 21117x 21117x 20205x 20205x 21117x 21117x 389x 389x 389x 21117x 21117x 21506x 21506x 21506x 21506x 21506x 21506x 21506x 21506x 21117x 21117x 393x 393x 393x 21117x 21117x 603x 603x 603x 603x 208x 208x 395x 395x 395x 603x 405x 66x 18x 18x 66x 405x 169x 2x 2x 169x 72x 11x 11x 61x 61x 167x 405x 395x 395x 395x 21117x 21117x 79x 79x 79x 52x 52x 37x 37x 52x 3x 3x 52x 52x 12x 12x 52x 52x 52x 79x 79x 79x 21117x 21117x 52x 52x 52x 38x 38x 38x 38x 52x 52x 52x 21117x 21117x | import {Issue} from "../issue";
import {IRule, RuleTag} from "./_irule";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {NamingRuleConfig} from "./_naming_rule_config";
import {NameValidator} from "../utils/name_validator";
import {InfoMethodDefinition, MethodParameterDirection, InfoMethodParameter} from "../abap/4_file_information/_abap_file_information";
import {DDIC} from "../ddic";
export class MethodParameterNamesConf extends NamingRuleConfig {
/** Ignore parameters in methods of exception classes */
public ignoreExceptions: boolean = true;
/** The pattern for importing parameters */
public importing: string = "^I._.+$";
/** The pattern for returning parameters */
public returning: string = "^R._.+$";
/** The pattern for changing parameters */
public changing: string = "^C._.+$";
/** The pattern for exporting parameters */
public exporting: string = "^E._.+$";
}
export class MethodParameterNames implements IRule {
private conf = new MethodParameterNamesConf();
private regex = this.compile();
private reg: IRegistry;
public 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: [RuleTag.Naming, RuleTag.SingleFile],
badExample: `METHODS get_name
IMPORTING customer TYPE string
RETURNING VALUE(name) TYPE string.`,
goodExample: `METHODS get_name
IMPORTING iv_customer TYPE string
RETURNING VALUE(rv_name) TYPE string.`,
};
}
private getDescription(expected: string, actual: string): string {
return this.conf.patternKind === "required" ?
"Method parameter name does not match pattern " + expected + ": " + actual :
"Method parameter name must not match pattern " + expected + ": " + actual;
}
public getConfig() {
return this.conf;
}
public setConfig(conf: MethodParameterNamesConf) {
this.conf = conf;
this.regex = this.compile();
}
private compile() {
return {
importing: new RegExp(this.conf.importing, "i"),
returning: new RegExp(this.conf.returning, "i"),
changing: new RegExp(this.conf.changing, "i"),
exporting: new RegExp(this.conf.exporting, "i"),
ignorePatterns: NameValidator.compileIgnorePatterns(this.conf),
};
}
public initialize(reg: IRegistry) {
this.reg = reg;
return this;
}
public run(obj: IObject): Issue[] {
let ret: Issue[] = [];
if (this.conf.patternKind === undefined) {
this.conf.patternKind = "required";
}
if (!(obj instanceof ABAPObject)) {
return [];
}
const ddic = new 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;
}
private checkMethod(method: InfoMethodDefinition): Issue[] {
let ret: Issue[] = [];
for (const p of method.parameters) {
switch (p.direction) {
case MethodParameterDirection.Importing:
ret = ret.concat(this.checkParameter(p, this.conf.importing, this.regex.importing));
break;
case MethodParameterDirection.Exporting:
ret = ret.concat(this.checkParameter(p, this.conf.exporting, this.regex.exporting));
break;
case MethodParameterDirection.Changing:
ret = ret.concat(this.checkParameter(p, this.conf.changing, this.regex.changing));
break;
case MethodParameterDirection.Returning:
ret = ret.concat(this.checkParameter(p, this.conf.returning, this.regex.returning));
break;
default:
break;
}
}
return ret;
}
private checkParameter(param: InfoMethodParameter, expected: string, regex: RegExp): Issue[] {
const ret: Issue[] = [];
const name = param.name;
if (NameValidator.violatesRule(name, regex, this.conf, this.regex.ignorePatterns)) {
const message = this.getDescription(expected, name);
const issue = Issue.atIdentifier(param.identifier, message, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
return ret;
}
}
|