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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 29269x 1x 1x 1x 14642x 14642x 14642x 14642x 14642x 14642x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 71966x 14642x 14642x 14416x 14416x 14642x 14642x 264x 264x 264x 14642x 14642x 14906x 14906x 14906x 14906x 14906x 14906x 14906x 14906x 14906x 14906x 14642x 14642x 306x 306x 306x 306x 306x 306x 12x 12x 12x 294x 294x 306x 294x 294x 294x 294x 306x 294x 294x 294x 294x 306x 294x 294x 294x 294x 306x 294x 294x 294x 294x 306x 294x 294x 294x 294x 306x 294x 294x 294x 294x 294x 294x 294x 14642x 14642x 294x 294x 294x 294x 294x 294x 160x 160x 160x 160x 160x 160x 25x 25x 25x 160x 294x 294x 8x 8x 8x 4x 4x 4x 8x 294x 294x 294x 14642x 14642x 294x 294x 294x 294x 294x 294x 294x 14642x 14642x 294x 294x 294x 4x 4x 4x 1x 1x 1x 4x 294x 294x 3x 3x 3x 2x 2x 2x 3x 294x 294x 294x 14642x 14642x 294x 294x 294x 294x 25x 25x 25x 25x 4x 4x 4x 25x 294x 294x 294x 14642x 14642x 294x 294x 294x 294x 294x 294x 294x 294x 140x 140x 140x 140x 44x 44x 44x 140x 294x 294x 294x 14642x 14642x 294x 294x 294x 74x 44x 44x 44x 44x 44x 3x 3x 41x 44x 44x 9x 9x 9x 44x 74x 294x 294x 294x 14642x 14642x | import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {StructureNode} from "../abap/nodes";
import {IFile} from "../files/_ifile";
export class NoPrefixesConf extends BasicRuleConfig {
/** DATA, CLASS-DATA, DATA BEGIN OF, CLASS-DATA BEGIN OF, FINAL(), DATA(), case insensitive regex */
public data: string = "^[LGM].?_";
/** STATICS, STATICS BEGIN OF, case insensitive regex */
public statics: string = "^S.?_";
/** FIELD-SYMBOLS and inline FIELD-SYMBOLS(), case insensitive regex */
public fieldSymbols: string = "^<[LGM].?_";
/** CONSTANTS, CONSTANTS BEGIN OF, case insensitive regex */
public constants: string = "^[LGM]?C.?_";
/** TYPES, ENUM, MESH, case insensitive regex */
public types: string = "^TY_";
/** importing, exporting, returning and changing parameters, case insensitive regex */
public methodParameters: string = "^[ICER].?_";
public allowIsPrefixBoolean: boolean = true;
public maxIssuesPerFile: number | undefined = 10;
// todo, public localClass: string = "";
// todo, public localInterface: string = "";
// todo, public functionModuleParameters: string = "";
// todo, public parameters: string = "";
// todo, public selectOptions: string = "";
// todo, public formParameters: string = "";
}
const MESSAGE = "Avoid hungarian notation";
export class NoPrefixes extends ABAPRule {
private conf = new NoPrefixesConf();
private regex = this.compile();
public getMetadata(): IRuleMetadata {
return {
key: "no_prefixes",
title: "No Prefixes",
shortDescription: `Dont use hungarian notation`,
extendedInformation: `
Note: not prefixing TYPES will require changing the errorNamespace in the abaplint configuration,
allowing all types to become voided, abaplint will then provide less precise syntax errors.
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#avoid-encodings-esp-hungarian-notation-and-prefixes
https://github.com/SAP/styleguides/blob/main/clean-abap/sub-sections/AvoidEncodings.md`,
tags: [RuleTag.SingleFile, RuleTag.Styleguide],
badExample: `DATA lv_foo TYPE i.`,
goodExample: `DATA foo TYPE i.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: NoPrefixesConf) {
this.conf = conf;
this.regex = this.compile();
}
private compile() {
const compile = (pattern: string | undefined) => pattern !== undefined && pattern !== "" ? new RegExp(pattern, "i") : undefined;
return {
data: compile(this.conf.data),
statics: compile(this.conf.statics),
fieldSymbols: compile(this.conf.fieldSymbols),
constants: compile(this.conf.constants),
types: compile(this.conf.types),
methodParameters: compile(this.conf.methodParameters),
};
}
public runParsed(file: ABAPFile) {
const ret: Issue[] = [];
const config = this.getConfig();
const structure = file.getStructure();
if (structure === undefined) {
// syntax error, skip
return [];
}
let max = config.maxIssuesPerFile;
if (max === undefined || max < 1) {
max = 10;
}
if (this.regex.data !== undefined) {
ret.push(...this.checkData(structure, this.regex.data, file));
}
if (ret.length >= max) {
return ret;
}
if (this.regex.statics !== undefined) {
ret.push(...this.checkStatics(structure, this.regex.statics, file));
}
if (ret.length >= max) {
return ret;
}
if (this.regex.fieldSymbols !== undefined) {
ret.push(...this.checkFieldSymbols(structure, this.regex.fieldSymbols, file));
}
if (ret.length >= max) {
return ret;
}
if (this.regex.constants !== undefined) {
ret.push(...this.checkConstants(structure, this.regex.constants, file));
}
if (ret.length >= max) {
return ret;
}
if (this.regex.types !== undefined) {
ret.push(...this.checkTypes(structure, this.regex.types, file));
}
if (ret.length >= max) {
return ret;
}
if (this.regex.methodParameters !== undefined) {
ret.push(...this.checkMethodParameters(structure, this.regex.methodParameters, file));
}
return ret;
}
private checkData(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const data of topNode.findAllStatements(Statements.Data).concat(
topNode.findAllStatements(Statements.DataBegin)).concat(
topNode.findAllStatements(Statements.ClassDataBegin)).concat(
topNode.findAllStatements(Statements.ClassData))) {
const nameExpression = data.findFirstExpression(Expressions.DefinitionName)
|| data.findFirstExpression(Expressions.NamespaceSimpleName);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
for (const data of topNode.findAllExpressions(Expressions.InlineData)) {
const nameExpression = data.findFirstExpression(Expressions.TargetField);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
return ret;
}
private checkStatics(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const data of topNode.findAllStatements(Statements.Static).concat(
topNode.findAllStatements(Statements.StaticBegin))) {
const nameExpression = data.findFirstExpression(Expressions.DefinitionName);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
return ret;
}
private checkFieldSymbols(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const data of topNode.findAllStatements(Statements.FieldSymbol)) {
const nameExpression = data.findFirstExpression(Expressions.FieldSymbol);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
for (const data of topNode.findAllExpressions(Expressions.InlineFS)) {
const nameExpression = data.findFirstExpression(Expressions.FieldSymbol);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
return ret;
}
private checkConstants(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const data of topNode.findAllStatements(Statements.Constant).concat(
topNode.findAllStatements(Statements.ConstantBegin))) {
const nameExpression = data.findFirstExpression(Expressions.DefinitionName);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
return ret;
}
private checkTypes(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const data of topNode.findAllStatements(Statements.Type).concat(
topNode.findAllStatements(Statements.TypeEnum)).concat(
topNode.findAllStatements(Statements.TypeEnumBegin)).concat(
topNode.findAllStatements(Statements.TypeMesh)).concat(
topNode.findAllStatements(Statements.TypeMeshBegin)).concat(
topNode.findAllStatements(Statements.TypeBegin))) {
const nameExpression = data.findFirstExpression(Expressions.NamespaceSimpleName);
const name = nameExpression?.concatTokens() || "";
if (name !== "" && nameExpression && name.match(regex)) {
const issue = Issue.atToken(file, nameExpression.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
return ret;
}
private checkMethodParameters(topNode: StructureNode, regex: RegExp, file: IFile): Issue[] {
const ret: Issue[] = [];
for (const method of topNode.findAllStatements(Statements.MethodDef)) {
for (const param of method.findAllExpressionsMulti([Expressions.MethodDefReturning, Expressions.MethodParam])) {
const nameToken = param?.findFirstExpression(Expressions.MethodParamName);
const typeName = param?.findFirstExpression(Expressions.TypeParam)
?.findFirstExpression(Expressions.TypeNameOrInfer)?.concatTokens()?.toUpperCase();
if (this.getConfig().allowIsPrefixBoolean === true && typeName === "ABAP_BOOL") {
continue;
}
const name = nameToken?.concatTokens();
if (nameToken && name && name !== "" && name.match(regex)) {
const issue = Issue.atToken(file, nameToken.getFirstToken(), MESSAGE, this.getMetadata().key, this.conf.severity);
ret.push(issue);
}
}
}
return ret;
}
}
|