All files / src/rules no_prefixes.ts

96.61% Statements 228/236
67.21% Branches 41/61
100% Functions 12/12
96.61% Lines 228/236

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 2371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 20698x 1x 1x 1x 10356x 10356x 10356x 10356x 10356x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 30927x 10356x 10356x 10131x 10131x 10356x 10356x 241x 241x 10356x 10356x 266x 266x 266x 266x 266x 266x 12x 12x 12x 254x 266x 254x 254x 254x 266x 254x 254x 254x 266x 254x 254x 254x 266x 254x 254x 254x 266x 254x 254x 254x 266x 254x 254x 254x 254x 254x 10356x 10356x 254x 254x 254x 254x 254x 254x 125x 125x 125x 125x 125x 125x 19x 19x 19x 125x 254x 254x 7x 7x 7x 3x 3x 3x 7x 254x 254x 254x 10356x 10356x 254x 254x 254x 254x                 254x 254x 254x 10356x 10356x 254x 254x 254x 4x 4x 4x 1x 1x 1x 4x 254x 254x 3x 3x 3x 2x 2x 2x 3x 254x 254x 254x 10356x 10356x 254x 254x 254x 254x 25x 25x 25x 25x 4x 4x 4x 25x 254x 254x 254x 10356x 10356x 254x 254x 254x 254x 254x 254x 254x 254x 116x 116x 116x 116x 35x 35x 35x 116x 254x 254x 254x 10356x 10356x 254x 254x 254x 57x 35x 35x 35x 35x 2x 2x 33x 35x 35x 6x 6x 6x 35x 57x 254x 254x 254x 10356x 10356x  
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;
 
  // 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();
 
  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;
  }
 
  public runParsed(file: ABAPFile) {
    const ret: Issue[] = [];
 
    const config = this.getConfig();
 
    const structure = file.getStructure();
    if (structure === undefined) {
      // syntax error, skip
      return [];
    }
 
    if (config.data !== undefined && config.data !== "") {
      ret.push(...this.checkData(structure, new RegExp(config.data, "i"), file));
    }
 
    if (config.statics !== undefined && config.statics !== "") {
      ret.push(...this.checkStatics(structure, new RegExp(config.statics, "i"), file));
    }
 
    if (config.fieldSymbols !== undefined && config.fieldSymbols !== "") {
      ret.push(...this.checkFieldSymbols(structure, new RegExp(config.fieldSymbols, "i"), file));
    }
 
    if (config.constants !== undefined && config.constants !== "") {
      ret.push(...this.checkConstants(structure, new RegExp(config.constants, "i"), file));
    }
 
    if (config.types !== undefined && config.types !== "") {
      ret.push(...this.checkTypes(structure, new RegExp(config.types, "i"), file));
    }
 
    if (config.methodParameters !== undefined && config.methodParameters !== "") {
      ret.push(...this.checkMethodParameters(structure, new RegExp(config.methodParameters, "i"), 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 type = param?.findFirstExpression(Expressions.TypeParam)?.concatTokens()?.toUpperCase();
 
        if (this.getConfig().allowIsPrefixBoolean === true && type?.endsWith("TYPE 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;
  }
 
}