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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23132x 23132x 23132x 23132x 1x 11567x 11567x 11567x 11567x 34548x 34548x 34548x 34548x 34548x 34548x 34548x 34548x 34548x 34548x 11567x 11567x 11204x 11204x 11567x 11567x 235x 235x 11567x 11567x 266x 266x 266x 20x 20x 246x 246x 266x 12x 12x 234x 266x 1303x 1303x 1303x 1303x 1303x 1303x 1172x 1172x 1303x 8x 8x 131x 234x 234x 234x 11567x 11567x 11567x 11567x 133x 133x 133x 11567x 11567x 134x 1x 1x 1x 1x 133x 133x 11567x 11567x 135x 12x 12x 1x 1x 12x 134x 134x 11567x 11567x 132x 4x 4x 1x 1x 4x 3x 131x 131x 11567x 11567x 8x 6x 6x 2x 2x 2x 6x 6x 6x 6x 6x 2x 2x 11567x 11567x 11567x 139x 139x 1048x 139x 139x 1048x 1048x 139x 139x 11567x 11567x 1309x 1309x 1309x 1309x 1309x 11567x 11567x 1x 1x 11567x 11567x 10x 5x 3x 2x 2x 3x 3x 3x 3x 5x 5x 10x 2x 2x 3x 3x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 11567x 11567x | import {Issue} from "../issue";
import {BasicRuleConfig} from "./_basic_rule_config";
import {ABAPRule} from "./_abap_rule";
import {ExpressionNode, StatementNode} from "../abap/nodes";
import * as Expressions from "../abap/2_statements/expressions";
import {IRuleMetadata, RuleTag} from "./_irule";
import {ABAPFile} from "../abap/abap_file";
import {ABAPObject} from "../objects/_abap_object";
import {EditHelper} from "../edit_helper";
export class KeepSingleParameterCallsOnOneLineConf extends BasicRuleConfig {
/** Max line length, in characters */
public length: number = 120;
}
export class KeepSingleParameterCallsOnOneLine extends ABAPRule {
private conf = new KeepSingleParameterCallsOnOneLineConf();
public getMetadata(): IRuleMetadata {
return {
key: "keep_single_parameter_on_one_line",
title: "Keep single parameters on one line",
shortDescription: `Keep single parameter calls on one line`,
extendedInformation: `https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#keep-single-parameter-calls-on-one-line`,
tags: [RuleTag.Whitespace, RuleTag.Styleguide, RuleTag.SingleFile, RuleTag.Quickfix],
badExample: `call_method(\n 2 ).`,
goodExample: `call_method( 2 ).`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: KeepSingleParameterCallsOnOneLineConf) {
this.conf = conf;
}
public runParsed(file: ABAPFile, obj: ABAPObject): Issue[] {
let issues: Issue[] = [];
if (obj.getType() === "INTF") {
return [];
}
const stru = file.getStructure();
if (stru === undefined) {
return [];
}
for (const s of file.getStatements()) {
if (this.isMultiLine(s) === false
|| this.calcStatementLength(s) > this.getConfig().length
|| this.containsNewLineValue(s)
|| this.containsNewLineTableExpression(s)
|| this.containsFieldAssigments(s)
|| this.containsNewLineTemplate(s)) {
continue;
}
for (const c of s.findAllExpressions(Expressions.MethodCallParam)) {
issues = issues.concat(this.check(c, file));
}
}
return issues;
}
///////////////////////////////////////
private containsFieldAssigments(s: StatementNode): boolean {
const fs = s.findAllExpressions(Expressions.FieldAssignment);
return fs.length > 1;
}
private containsNewLineTableExpression(s: StatementNode): boolean {
for (const st of s.findAllExpressions(Expressions.TableExpression)) {
if (st.getFirstToken().getRow() !== st.getLastToken().getRow()) {
return true;
}
}
return false;
}
private containsNewLineValue(s: StatementNode): boolean {
for (const st of s.findAllExpressions(Expressions.Source)) {
const first = st.getFirstToken().getStr().toUpperCase();
if (first === "VALUE" && st.getFirstToken().getRow() !== st.getLastToken().getRow()) {
return true;
}
}
return false;
}
private containsNewLineTemplate(s: StatementNode): boolean {
for (const st of s.findAllExpressions(Expressions.StringTemplate)) {
for (const t of st.getAllTokens()) {
if (t.getStr().includes("\\n")) {
return true;
}
}
}
return false;
}
private check(c: ExpressionNode, file: ABAPFile): Issue[] {
if (this.isSingleParameter(c) === true && this.isMultiLine(c) === true) {
for (const sub of c.findAllExpressions(Expressions.MethodCallParam)) {
if (this.isSingleParameter(sub) === false
&& this.isWithoutParameters(sub) === false) {
return [];
}
}
const message = "Keep single parameter on one line";
const fix = EditHelper.replaceRange(file, c.getFirstToken().getStart(), c.getLastToken().getEnd(), c.concatTokens());
return [Issue.atToken(file, c.getFirstToken(), message, this.getMetadata().key, this.conf.severity, fix)];
}
return [];
}
// including first indentation, worst case calculation add space after each token
private calcStatementLength(c: StatementNode): number {
let length = 0;
for (const t of c.getTokens()) {
if (length === 0) {
length = length + t.getStart().getCol();
}
length = length + t.getStr().length + 1;
}
return length;
}
private isMultiLine(c: ExpressionNode | StatementNode): boolean {
const first = c.getFirstToken();
const last = c.getLastToken();
return first.getStart().getRow() < last.getStart().getRow();
}
private isWithoutParameters(c: ExpressionNode): boolean {
return c.getChildren().length === 2;
}
private isSingleParameter(c: ExpressionNode): boolean {
if (c.findDirectExpression(Expressions.Source)) {
for (const params of c.findAllExpressions(Expressions.ParameterListS)) {
if (params.getChildren().length > 1) {
return false;
}
}
return true;
}
const list = c.findDirectExpression(Expressions.ParameterListS);
if (list) {
return list.getChildren().length === 1;
}
const param = c.findDirectExpression(Expressions.MethodParameters);
if (param) {
if (param.getChildren().length > 2) {
return false;
}
const paramsource = param.findDirectExpression(Expressions.ParameterListS);
if (paramsource && paramsource.getChildren().length === 1) {
return true;
}
const paramtarget = param.findDirectExpression(Expressions.ParameterListT);
if (paramtarget && paramtarget.getChildren().length === 1) {
return true;
}
}
return false;
}
} |