All files / src/rules align_parameters.ts

81.41% Statements 333/409
76% Branches 57/75
100% Functions 11/11
81.41% Lines 333/409

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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 4101x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9396x 9396x 9396x 9396x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 27937x 9396x 9396x 8913x 8913x 9396x 9396x 216x 216x 9396x 9396x 267x 267x 267x 267x 12x 12x 255x 255x 255x 255x 255x 255x 255x 255x 267x 31x 31x 15x 15x 31x 255x 255x 255x 9396x 9396x 31x     31x 31x 31x 65x 65x 46x 46x 65x 31x 31x 55x 15x 15x 15x 55x 16x 16x 16x 9396x 9396x 255x 255x 255x 4x 4x 4x 4x                                 4x 4x 4x 2x 4x 4x     4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 255x 255x 255x 9396x 9396x 255x 255x 255x 5x 5x 5x     5x 10x 10x     10x 10x 10x 10x 10x 10x 5x 5x 5x 5x 255x 255x 3x 3x 3x     3x 6x 6x     6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 255x 255x 255x 9396x 9396x 255x 255x 255x 255x 255x 255x 13x 13x 13x 15x 15x     15x 15x 15x 15x 15x 15x 13x 13x 13x 5x 5x     5x 5x 5x 5x 5x 5x 13x 13x 8x 8x 13x 255x 255x 255x 9396x 9396x 255x 255x 255x 18x 18x 18x 5x 5x     5x 5x 5x 5x 5x 5x 18x 18x 18x 6x 4x 4x     4x 4x 4x 4x 4x 4x 6x 6x 2x 4x 4x     4x 4x 4x 4x 4x 4x 2x 6x 6x 6x                     6x 6x 6x 4x 4x     4x 4x 4x 4x 4x 4x 6x 18x 18x 9x 9x 18x 255x 255x 255x 9396x 9396x 255x 255x 4x 4x 4x 8x 8x     8x 8x 8x 8x 8x 8x 4x 4x                         4x 4x 4x                         4x 4x 4x 4x 4x 255x 255x 9396x 9396x  
import {Issue} from "../issue";
import * as Expressions from "../abap/2_statements/expressions";
import {ABAPRule} from "./_abap_rule";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {Position} from "../position";
import {StructureNode} from "../abap/nodes";
import {INode} from "../abap/nodes/_inode";
import {Statements} from "..";
 
 
export class AlignParametersConf extends BasicRuleConfig {
}
 
interface IParameterData {
  left: INode;
  eq: Position;
  right: INode;
}
 
interface ICandidate {
  parameters: IParameterData[];
}
 
export class AlignParameters extends ABAPRule {
  private conf = new AlignParametersConf();
 
  public getMetadata(): IRuleMetadata {
    return {
      key: "align_parameters",
      title: "Align Parameters",
      shortDescription: `Checks for vertially aligned parameters`,
      extendedInformation: `Checks:
* function module calls
* method calls
* VALUE constructors
* NEW constructors
* RAISE EXCEPTION statements
* CREATE OBJECT statements
* RAISE EVENT statements
 
https://github.com/SAP/styleguides/blob/master/clean-abap/CleanABAP.md#align-parameters
 
Does not take effect on non functional method calls, use https://rules.abaplint.org/functional_writing/
 
Also https://rules.abaplint.org/max_one_method_parameter_per_line/ can help aligning parameter syntax`,
      tags: [RuleTag.SingleFile, RuleTag.Whitespace, RuleTag.Styleguide],
      badExample: `CALL FUNCTION 'FOOBAR'
  EXPORTING
    foo = 2
    parameter = 3.
 
foobar( moo = 1
  param = 1 ).
 
foo = VALUE #(
    foo = bar
        moo = 2 ).`,
      goodExample: `CALL FUNCTION 'FOOBAR'
  EXPORTING
    foo       = 2
    parameter = 3.
 
foobar( moo   = 1
        param = 1 ).
 
foo = VALUE #(
    foo = bar
    moo = 2 ).
 
DATA(sdf) = VALUE type(
  common_val     = 2
  another_common = 5
  ( row_value = 4
    value_foo = 5 ) ).`,
    };
  }
 
  public getConfig() {
    return this.conf;
  }
 
  public setConfig(conf: AlignParametersConf) {
    this.conf = conf;
  }
 
  public runParsed(file: ABAPFile) {
    const issues: Issue[] = [];
 
    const stru = file.getStructure();
    if (stru === undefined) {
      return issues; // parser error
    }
 
    const candidates: ICandidate[] = [];
    candidates.push(...this.functionParameterCandidates(stru));
    candidates.push(...this.methodCallParamCandidates(stru));
    candidates.push(...this.valueBodyCandidates(stru));
    candidates.push(...this.raiseAndCreateCandidates(stru));
    candidates.push(...this.newCandidates(stru));
 
    for (const c of candidates) {
      const i = this.checkCandidate(c, file);
      if (i) {
        issues.push(i);
      }
    }
 
    return issues;
  }
 
  private checkCandidate(candidate: ICandidate, file: ABAPFile): Issue | undefined {
    if (candidate.parameters.length === 0) {
      return undefined;
    }
 
    let expectedEqualsColumn = 0;
    for (const p of candidate.parameters) {
      const currentCol = p.left.getLastToken().getCol() + p.left.getLastToken().getStr().length + 1;
      if (currentCol > expectedEqualsColumn) {
        expectedEqualsColumn = currentCol;
      }
    }
 
    for (const p of candidate.parameters) {
      if (p.eq.getCol() !== expectedEqualsColumn) {
        const message = "Align parameters to column " + expectedEqualsColumn;
        return Issue.atPosition(file, p.eq, message, this.getMetadata().key, this.getConfig().severity);
      }
    }
 
    return undefined;
  }
 
  private newCandidates(stru: StructureNode): ICandidate[] {
    const candidates: ICandidate[] = [];
 
    for (const vb of stru.findAllExpressionsRecursive(Expressions.NewObject)) {
      const parameters: IParameterData[] = [];
 
      const fieldAssignments = vb.findDirectExpressions(Expressions.FieldAssignment);
      if (fieldAssignments.length >= 2) {
        for (const fs of fieldAssignments) {
          const children = fs.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
        if (parameters.length > 0) {
          candidates.push({parameters});
          continue;
        }
      }
 
      const list = vb.findDirectExpression(Expressions.ParameterListS);
      if (list) {
        for (const c of list.getChildren()) {
          const children = c.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
        if (parameters.length > 0) {
          candidates.push({parameters});
        }
      }
    }
 
    return candidates;
  }
 
  private valueBodyCandidates(stru: StructureNode): ICandidate[] {
    const candidates: ICandidate[] = [];
 
    for (const vb of stru.findAllExpressionsRecursive(Expressions.ValueBody)) {
      const parameters: IParameterData[] = [];
      const fieldAssignments = vb.findDirectExpressions(Expressions.FieldAssignment);
      if (fieldAssignments.length <= 1) {
        continue;
      }
      for (const fs of fieldAssignments) {
        const children = fs.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
      if (parameters.length > 0) {
        candidates.push({parameters});
      }
    }
 
    for (const vb of stru.findAllExpressionsRecursive(Expressions.ValueBodyLine)) {
      const parameters: IParameterData[] = [];
      const fieldAssignments = vb.findDirectExpressions(Expressions.FieldAssignment);
      if (fieldAssignments.length <= 1) {
        continue;
      }
      for (const fs of fieldAssignments) {
        const children = fs.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
      if (parameters.length > 0) {
        candidates.push({parameters});
      }
    }
 
    return candidates;
  }
 
  private raiseAndCreateCandidates(stru: StructureNode): ICandidate[] {
    const candidates: ICandidate[] = [];
 
    const statements = stru.findAllStatements(Statements.Raise);
    statements.push(...stru.findAllStatements(Statements.CreateObject));
    statements.push(...stru.findAllStatements(Statements.RaiseEvent));
    for (const raise of statements) {
      const parameters: IParameterData[] = [];
      const param = raise.findDirectExpression(Expressions.ParameterListS);
      for (const p of param?.getChildren() || []) {
        const children = p.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
 
      const ex = raise.findDirectExpression(Expressions.ParameterListExceptions);
      for (const e of ex?.getChildren() || []) {
        const children = e.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
 
      if (parameters.length > 0) {
        candidates.push({parameters});
      }
    }
 
    return candidates;
  }
 
  private methodCallParamCandidates(stru: StructureNode): ICandidate[] {
    const candidates: ICandidate[] = [];
 
    for (const mcp of stru.findAllExpressionsRecursive(Expressions.MethodCallParam)) {
      const parameters: IParameterData[] = [];
 
      for (const param of mcp.findDirectExpression(Expressions.ParameterListS)?.getChildren() || []) {
        const children = param.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
 
      const mp = mcp.findDirectExpression(Expressions.MethodParameters);
      if (mp) {
        for (const p of mp.findDirectExpression(Expressions.ParameterListS)?.getChildren() || []) {
          const children = p.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
 
        for (const l of mp.findDirectExpressions(Expressions.ParameterListT)) {
          for (const p of l.findDirectExpressions(Expressions.ParameterT) || []) {
            const children = p.getChildren();
            if (children.length < 3) {
              continue; // unexpected
            }
            parameters.push({
              left: children[0],
              eq: children[1].getFirstToken().getStart(),
              right: children[2],
            });
          }
        }
 
        const rec = mp.findDirectExpression(Expressions.ParameterT);
        if (rec) {
          const children = rec.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
 
 
        for (const ex of mp.findDirectExpression(Expressions.ParameterListExceptions)?.getChildren() || []) {
          const children = ex.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
      }
 
      if (parameters.length > 0) {
        candidates.push({parameters});
      }
    }
 
    return candidates;
  }
 
  private functionParameterCandidates(stru: StructureNode): ICandidate[] {
    const candidates: ICandidate[] = [];
    for (const fp of stru.findAllExpressionsRecursive(Expressions.FunctionParameters)) {
      const parameters: IParameterData[] = [];
 
      for (const p of fp.findAllExpressions(Expressions.FunctionExportingParameter)) {
        const children = p.getChildren();
        if (children.length < 3) {
          continue; // unexpected
        }
        parameters.push({
          left: children[0],
          eq: children[1].getFirstToken().getStart(),
          right: children[2],
        });
      }
 
      for (const list of fp.findDirectExpressions(Expressions.ParameterListT)) {
        for (const pt of list.findDirectExpressions(Expressions.ParameterT)) {
          const children = pt.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
      }
 
      const list = fp.findDirectExpression(Expressions.ParameterListExceptions);
      if (list) {
        for (const pt of list.findDirectExpressions(Expressions.ParameterException)) {
          const children = pt.getChildren();
          if (children.length < 3) {
            continue; // unexpected
          }
          parameters.push({
            left: children[0],
            eq: children[1].getFirstToken().getStart(),
            right: children[2],
          });
        }
      }
 
      if (parameters.length > 0) {
        candidates.push({parameters});
      }
    }
    return candidates;
  }
 
}