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 | 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 1x 1x 1x 21101x 21101x 21101x 21101x 21101x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 204505x 21101x 21101x 20205x 20205x 21101x 21101x 374x 374x 21101x 21101x 420x 420x 420x 21101x 21101x 630x 208x 208x 422x 422x 630x 20x 20x 402x 402x 630x 412x 2660x 2660x 2553x 2553x 2660x 2660x 2660x 2660x 87x 87x 20x 20x 20x 2660x 20x 20x 2660x 4x 4x 16x 16x 16x 16x 16x 16x 16x 412x 402x 402x 402x 21101x 21101x 21101x 107x 14x 14x 93x 107x 70x 70x 23x 23x 107x 23x 23x 21101x 21101x 21101x 21101x 20x 9x 20x 5x 11x 2x 2x 4x 4x 21101x 21101x | import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ExpressionNode} from "../abap/nodes";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {CLikeType, CSequenceType, XSequenceType} from "../abap/types/basic";
import {EditHelper} from "../edit_helper";
import {Issue} from "../issue";
import {ABAPObject} from "../objects/_abap_object";
import {IObject} from "../objects/_iobject";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {IRegistry} from "../_iregistry";
import {typedIdentifierAt} from "./_type_lookup";
export class InlineGenericSourceConf extends BasicRuleConfig {
}
interface IFixedType {
/** name of the generic type, as the syntax check names it */
generic: string;
/** the type the inline declaration gets, as the syntax check names it */
fixed: string;
/** the same type, for CONV */
conv: string;
}
export class InlineGenericSource implements IRule {
private reg: IRegistry;
private conf = new InlineGenericSourceConf();
public getMetadata(): IRuleMetadata {
return {
key: "inline_generic_source",
title: "Inline declaration from a generically typed source",
shortDescription: `Finds inline declarations whose value is a generically typed parameter or field symbol`,
extendedInformation: `DATA( ) or FINAL( ) takes the type of the source. When the source is a parameter or
field symbol with a generic type, the type is fixed when the program is compiled: string for clike and
csequence, xstring for xsequence. The syntax check warns "The fixed type STRING is used for the generic
type CLIKE".
Reported for DATA(var) = source and FINAL(var) = source when the source is just the parameter or field
symbol. Most other generic types cannot be made concrete, that is a syntax error which check_syntax reports.
Declare the variable with a type, or name the type with CONV.
The pragma ##TYPE, which hides the syntax check warning, also hides this issue.
prefer_inline does not suggest inline declarations for generic sources, this rule reports the ones
that exist.
https://help.sap.com/doc/abapdocu_758_index_htm/7.58/en-US/abapmove.htm`,
tags: [RuleTag.Quickfix],
pragma: "##TYPE",
badExample: `METHOD m. " IMPORTING iv TYPE clike
DATA(lv) = iv.
ENDMETHOD.`,
goodExample: `METHOD m. " IMPORTING iv TYPE clike
DATA(lv) = CONV string( iv ).
ENDMETHOD.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: InlineGenericSourceConf) {
this.conf = conf;
}
public initialize(reg: IRegistry) {
this.reg = reg;
return this;
}
public run(obj: IObject): readonly Issue[] {
if (!(obj instanceof ABAPObject)) {
return [];
}
const syntax = new SyntaxLogic(this.reg, obj).run();
if (syntax.issues.length > 0) {
return [];
}
const issues: Issue[] = [];
for (const file of obj.getABAPFiles()) {
for (const statement of file.getStatements()) {
if (!(statement.get() instanceof Statements.Move)
|| statement.getPragmas().some(p => p.getStr().toUpperCase() === this.getMetadata().pragma)) {
continue;
}
const inline = statement.findDirectExpression(Expressions.Target)?.findDirectExpression(Expressions.InlineData);
const name = inline?.findDirectExpression(Expressions.TargetField)?.getFirstToken();
const operand = this.singleOperand(statement.findDirectExpression(Expressions.Source));
if (name === undefined || operand === undefined) {
continue;
}
const scope = syntax.spaghetti.lookupPosition(name.getStart(), file.getFilename());
const resolved = typedIdentifierAt(scope, operand.getFirstToken().getStart(), [ReferenceType.DataReadReference]);
if (resolved === undefined) {
continue;
}
const fixed = this.fixedType(resolved.getType());
if (fixed === undefined) {
continue;
}
const message = `The fixed type ${fixed.fixed} is used for the generic type ${fixed.generic} of "${operand.concatTokens()}", ` +
`declare "${name.getStr()}" with a type or use CONV`;
const replacement = `CONV ${fixed.conv}( ${operand.concatTokens()} )`;
const fix = EditHelper.replaceRange(file, operand.getFirstToken().getStart(), operand.getLastToken().getEnd(), replacement);
issues.push(Issue.atToken(file, name, message, this.getMetadata().key, this.conf.severity, fix));
}
}
return issues;
}
/** The source when it is a single field or field symbol, without components, offset/length or dereference */
private singleOperand(source: ExpressionNode | undefined): ExpressionNode | undefined {
if (source === undefined || source.getChildren().length !== 1) {
return undefined;
}
const chain = source.findDirectExpression(Expressions.FieldChain);
if (chain === undefined || chain.getChildren().length !== 1) {
return undefined;
}
const first = chain.getFirstChild();
if (first instanceof ExpressionNode
&& (first.get() instanceof Expressions.SourceField || first.get() instanceof Expressions.SourceFieldSymbol)) {
return chain;
}
return undefined;
}
/** The type the system uses for a generic type in an inline declaration, undefined if the type is not generic,
* or is a generic type that check_syntax does not make concrete */
private fixedType(type: AbstractType): IFixedType | undefined {
if (type instanceof CLikeType) {
return {generic: "CLIKE", fixed: "STRING", conv: "string"};
} else if (type instanceof CSequenceType) {
return {generic: "CSEQUENCE", fixed: "STRING", conv: "string"};
} else if (type instanceof XSequenceType) {
return {generic: "XSEQUENCE", fixed: "XSTRING", conv: "xstring"};
}
return undefined;
}
}
|