All files / src/rules definitions_in_module.ts

100% Statements 62/62
100% Branches 12/12
100% Functions 5/5
100% Lines 62/62

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 631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21103x 21103x 21103x 21103x 21103x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 83258x 21103x 21103x 20208x 20208x 21103x 21103x 374x 374x 21103x 21103x 391x 391x 391x 391x 13x 13x 378x 378x 391x 4x 12x 3x 3x 12x 4x 378x 378x 378x 21103x 21103x  
import {Issue} from "../issue";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
 
export class DefinitionsInModuleConf extends BasicRuleConfig {
}
 
export class DefinitionsInModule extends ABAPRule {
 
  private conf = new DefinitionsInModuleConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "definitions_in_module",
      title: "Definitions inside MODULE",
      shortDescription: `Checks that DATA, TYPES and CONSTANTS are not defined inside MODULE ... ENDMODULE`,
      extendedInformation: `Definitions inside MODULE blocks are part of the global program scope, not local to the module.`,
      tags: [RuleTag.SingleFile],
      badExample: `MODULE init_2000 OUTPUT.
  DATA: lt_field_cat TYPE lvc_t_fcat.
ENDMODULE.`,
      goodExample: `DATA: lt_field_cat TYPE lvc_t_fcat.
 
MODULE init_2000 OUTPUT.
  WRITE 'hello'.
ENDMODULE.`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: DefinitionsInModuleConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile): Issue[] {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return [];
    }
 
    const definitions = [Statements.Data, Statements.Type, Statements.Constant];
    for (const mod of stru.findAllStructures(Structures.Module)) {
      for (const statement of mod.findAllStatementNodes()) {
        if (definitions.some(d => statement.get() instanceof d)) {
          issues.push(Issue.atStatement(file, statement, "Definitions inside MODULE are global, move them outside the module", this.getMetadata().key, this.getConfig().severity));
        }
      }
    }
 
    return issues;
  }
 
}