All files / src/rules align_parameters.ts

82.93% Statements 350/422
80.95% Branches 68/84
100% Functions 11/11
82.93% Lines 350/422

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 410 411 412 413 414 415 416 417 418 419 420 421 422 4231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10932x 10932x 10932x 10932x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 32509x 10932x 10932x 10363x 10363x 10932x 10932x 258x 258x 10932x 10932x 309x 309x 309x 309x 12x 12x 297x 297x 297x 297x 297x 297x 297x 297x 309x 36x 36x 16x 16x 36x 297x 297x 297x 10932x 10932x 36x     36x 36x 36x 36x 75x 75x 2x 2x 73x 75x 54x 54x 75x 34x 36x 60x 16x 16x 13x 16x 3x 3x 16x 16x 16x 60x 18x 18x 18x 10932x 10932x 297x 297x 297x 4x 4x 4x 4x                                 4x 4x 4x 2x 4x 4x     4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 297x 297x 297x 10932x 10932x 297x 297x 297x 6x 6x 6x 1x 1x 6x 10x 10x     10x 10x 10x 10x 10x 10x 5x 5x 5x 6x 297x 297x 4x 4x 4x 1x 1x 4x 6x 6x     6x 6x 6x 6x 6x 6x 3x 3x 3x 4x 297x 297x 297x 10932x 10932x 297x 297x 297x 297x 297x 297x 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 297x 297x 297x 10932x 10932x 297x 297x 297x 19x 19x 19x 7x 7x     7x 7x 7x 7x 7x 7x 19x 19x 19x 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 19x 19x 10x 10x 19x 297x 297x 297x 10932x 10932x 297x 297x 8x 8x 8x 16x 16x     16x 16x 16x 16x 16x 16x 8x 8x                         8x 8x 8x                         8x 8x 8x 8x 8x 297x 297x 10932x 10932x  
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 "..";
import {EditHelper, IEdit} from "../edit_helper";
 
 
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/
 
If parameters are on the same row, no issues are reported, see
https://rules.abaplint.org/max_one_method_parameter_per_line/ for splitting parameters to lines`,
      tags: [RuleTag.SingleFile, RuleTag.Whitespace, RuleTag.Styleguide, RuleTag.Quickfix],
      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;
    let row = 0;
    for (const p of candidate.parameters) {
      const currentCol = p.left.getLastToken().getCol() + p.left.getLastToken().getStr().length + 1;
      if (p.eq.getRow() === row) {
        return undefined;
      }
      row = p.eq.getRow();
      if (currentCol > expectedEqualsColumn) {
        expectedEqualsColumn = currentCol;
      }
    }
 
    for (const p of candidate.parameters) {
      if (p.eq.getCol() !== expectedEqualsColumn) {
        let fix: IEdit | undefined;
        if (p.eq.getCol() < expectedEqualsColumn) {
          fix = EditHelper.insertAt(file, p.eq, " ".repeat(expectedEqualsColumn - p.eq.getCol()));
        } else {
          fix = EditHelper.deleteRange(file, new Position(p.eq.getRow(), expectedEqualsColumn), p.eq);
        }
        const message = "Align parameters to column " + expectedEqualsColumn;
        return Issue.atPosition(file, p.eq, message, this.getMetadata().key, this.getConfig().severity, fix);
      }
    }
 
    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;
  }
 
}