@abaplint/core 2.120.106 → 2.120.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -55,6 +55,11 @@ class Conditions {
55
55
  }
56
56
  }
57
57
  class IdenticalConditionsConf extends _basic_rule_config_1.BasicRuleConfig {
58
+ constructor() {
59
+ super(...arguments);
60
+ /** Report a comparison of an operand with itself, like a = a or a <> a */
61
+ this.selfComparison = true;
62
+ }
58
63
  }
59
64
  exports.IdenticalConditionsConf = IdenticalConditionsConf;
60
65
  class IdenticalConditions extends _abap_rule_1.ABAPRule {
@@ -69,19 +74,30 @@ class IdenticalConditions extends _abap_rule_1.ABAPRule {
69
74
  shortDescription: `Find identical conditions in IF + CASE + WHILE etc
70
75
 
71
76
  Prerequsites: code is pretty printed with identical cAsE`,
77
+ extendedInformation: `Option selfComparison: reports a comparison with =, <>, <, >, <= or >= (and EQ, NE, LT, GT, LE, GE)
78
+ whose two operands are the same data object, like a = a or ls_new-id <> ls_new-id. The comparison is always true or
79
+ always false, usually one side should be another field.
80
+
81
+ Only operands that are data objects are compared: fields, components, attributes, field symbols, dereferences,
82
+ offset/length and table expressions without calls. Not reported: method calls and built-in functions, which can
83
+ return different values, literals like 1 = 1, ASSERT, and SQL conditions.`,
72
84
  tags: [_irule_1.RuleTag.SingleFile],
73
85
  badExample: `IF foo = bar OR 1 = a OR foo = bar.
74
86
  ENDIF.
75
87
  CASE bar.
76
88
  WHEN '1'.
77
89
  WHEN 'A' OR '1'.
78
- ENDCASE.`,
90
+ ENDCASE.
91
+ IF ls_new-id = ls_new-id.
92
+ ENDIF.`,
79
93
  goodExample: `IF foo = bar OR 1 = a.
80
94
  ENDIF.
81
95
  CASE bar.
82
96
  WHEN '1'.
83
97
  WHEN 'A'.
84
- ENDCASE.`,
98
+ ENDCASE.
99
+ IF ls_new-id = ls_old-id.
100
+ ENDIF.`,
85
101
  };
86
102
  }
87
103
  getConfig() {
@@ -105,9 +121,67 @@ ENDCASE.`,
105
121
  for (const i of structure.findAllExpressions(Expressions.Cond)) {
106
122
  issues = issues.concat(this.analyzeCond(file, i));
107
123
  }
124
+ if (this.conf.selfComparison) {
125
+ issues = issues.concat(this.analyzeSelfComparison(file));
126
+ }
108
127
  return issues;
109
128
  }
110
129
  ////////////////
130
+ analyzeSelfComparison(file) {
131
+ const issues = [];
132
+ for (const statement of file.getStatements()) {
133
+ if (statement.get() instanceof Statements.Assert) {
134
+ continue;
135
+ }
136
+ for (const compare of statement.findAllExpressionsRecursive(Expressions.Compare)) {
137
+ const children = compare.getChildren();
138
+ const negated = children[0] instanceof nodes_1.TokenNode && children[0].getFirstToken().getStr().toUpperCase() === "NOT";
139
+ const rest = children.slice(negated ? 1 : 0);
140
+ if (rest.length !== 3
141
+ || !(rest[0] instanceof nodes_1.ExpressionNode) || !(rest[0].get() instanceof Expressions.Source)
142
+ || !(rest[1] instanceof nodes_1.ExpressionNode) || !(rest[1].get() instanceof Expressions.CompareOperator)
143
+ || !(rest[2] instanceof nodes_1.ExpressionNode) || !(rest[2].get() instanceof Expressions.Source)) {
144
+ continue;
145
+ }
146
+ const operator = rest[1].concatTokens().toUpperCase().replace(/ /g, "");
147
+ const value = this.selfValue(operator);
148
+ const left = this.dataObject(rest[0]);
149
+ if (value === undefined || left === undefined || left !== this.dataObject(rest[2])) {
150
+ continue;
151
+ }
152
+ const result = value !== negated ? "true" : "false";
153
+ const message = "Comparison is always " + result + ", " + rest[0].concatTokens() + " is compared with itself";
154
+ issues.push(issue_1.Issue.atRange(file, compare.getFirstToken().getStart(), compare.getLastToken().getEnd(), message, this.getMetadata().key, this.conf.severity));
155
+ }
156
+ }
157
+ return issues;
158
+ }
159
+ /** result of comparing a value with itself, undefined for operators like CO or CP */
160
+ selfValue(operator) {
161
+ if (["=", "EQ", "<=", "=<", "LE", ">=", "=>", "GE"].includes(operator)) {
162
+ return true;
163
+ }
164
+ else if (["<>", "><", "NE", "<", "LT", ">", "GT"].includes(operator)) {
165
+ return false;
166
+ }
167
+ return undefined;
168
+ }
169
+ /** the text of an operand that is a data object without calls, names in upper case, literals as they are */
170
+ dataObject(source) {
171
+ const children = source.getChildren();
172
+ if (!(children[0] instanceof nodes_1.ExpressionNode) || !(children[0].get() instanceof Expressions.FieldChain)
173
+ || (children.length === 2 && !(children[1].get() instanceof Expressions.Dereference))
174
+ || children.length > 2
175
+ || source.findFirstExpression(Expressions.MethodCall) !== undefined
176
+ || source.findFirstExpression(Expressions.MethodCallChain) !== undefined
177
+ || source.findFirstExpression(Expressions.NewObject) !== undefined) {
178
+ return undefined;
179
+ }
180
+ return source.getTokens().map(t => {
181
+ const str = t.getStr();
182
+ return str.startsWith("'") || str.startsWith("`") || str.startsWith("|") || str.startsWith("}") ? str : str.toUpperCase();
183
+ }).join(" ");
184
+ }
111
185
  analyzeCond(file, node) {
112
186
  const conditions = new Conditions();
113
187
  let operator = "";
@@ -116,6 +116,7 @@ __exportStar(require("./in_statement_indentation"), exports);
116
116
  __exportStar(require("./indentation"), exports);
117
117
  __exportStar(require("./index_completely_contained"), exports);
118
118
  __exportStar(require("./inline_data_old_versions"), exports);
119
+ __exportStar(require("./inline_generic_source"), exports);
119
120
  __exportStar(require("./inline_packed_computation"), exports);
120
121
  __exportStar(require("./intf_referencing_clas"), exports);
121
122
  __exportStar(require("./invalid_table_index"), exports);
@@ -197,6 +198,7 @@ __exportStar(require("./select_single_full_key"), exports);
197
198
  __exportStar(require("./selection_screen_naming"), exports);
198
199
  __exportStar(require("./selection_screen_texts_missing"), exports);
199
200
  __exportStar(require("./sequential_blank"), exports);
201
+ __exportStar(require("./sequential_read_sorted"), exports);
200
202
  __exportStar(require("./short_case"), exports);
201
203
  __exportStar(require("./sicf_consistency"), exports);
202
204
  __exportStar(require("./single_use_expression_temp"), exports);
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.InlineGenericSource = exports.InlineGenericSourceConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
39
+ const _reference_1 = require("../abap/5_syntax/_reference");
40
+ const syntax_1 = require("../abap/5_syntax/syntax");
41
+ const nodes_1 = require("../abap/nodes");
42
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
43
+ const basic_1 = require("../abap/types/basic");
44
+ const edit_helper_1 = require("../edit_helper");
45
+ const issue_1 = require("../issue");
46
+ const _abap_object_1 = require("../objects/_abap_object");
47
+ const _basic_rule_config_1 = require("./_basic_rule_config");
48
+ const _irule_1 = require("./_irule");
49
+ class InlineGenericSourceConf extends _basic_rule_config_1.BasicRuleConfig {
50
+ }
51
+ exports.InlineGenericSourceConf = InlineGenericSourceConf;
52
+ class InlineGenericSource {
53
+ constructor() {
54
+ this.conf = new InlineGenericSourceConf();
55
+ }
56
+ getMetadata() {
57
+ return {
58
+ key: "inline_generic_source",
59
+ title: "Inline declaration from a generically typed source",
60
+ shortDescription: `Finds inline declarations whose value is a generically typed parameter or field symbol`,
61
+ extendedInformation: `DATA( ) or FINAL( ) takes the type of the source. When the source is a parameter or
62
+ field symbol with a generic type, the type is fixed when the program is compiled: string for clike and
63
+ csequence, xstring for xsequence. The syntax check warns "The fixed type STRING is used for the generic
64
+ type CLIKE".
65
+
66
+ Reported for DATA(var) = source and FINAL(var) = source when the source is just the parameter or field
67
+ symbol. Most other generic types cannot be made concrete, that is a syntax error which check_syntax reports.
68
+
69
+ Declare the variable with a type, or name the type with CONV.
70
+
71
+ The pragma ##TYPE, which hides the syntax check warning, also hides this issue.
72
+
73
+ prefer_inline does not suggest inline declarations for generic sources, this rule reports the ones
74
+ that exist.
75
+
76
+ https://help.sap.com/doc/abapdocu_758_index_htm/7.58/en-US/abapmove.htm`,
77
+ tags: [_irule_1.RuleTag.Quickfix],
78
+ pragma: "##TYPE",
79
+ badExample: `METHOD m. " IMPORTING iv TYPE clike
80
+ DATA(lv) = iv.
81
+ ENDMETHOD.`,
82
+ goodExample: `METHOD m. " IMPORTING iv TYPE clike
83
+ DATA(lv) = CONV string( iv ).
84
+ ENDMETHOD.`,
85
+ };
86
+ }
87
+ getConfig() {
88
+ return this.conf;
89
+ }
90
+ setConfig(conf) {
91
+ this.conf = conf;
92
+ }
93
+ initialize(reg) {
94
+ this.reg = reg;
95
+ return this;
96
+ }
97
+ run(obj) {
98
+ var _a, _b, _c;
99
+ if (!(obj instanceof _abap_object_1.ABAPObject)) {
100
+ return [];
101
+ }
102
+ const syntax = new syntax_1.SyntaxLogic(this.reg, obj).run();
103
+ if (syntax.issues.length > 0) {
104
+ return [];
105
+ }
106
+ const issues = [];
107
+ for (const file of obj.getABAPFiles()) {
108
+ for (const statement of file.getStatements()) {
109
+ if (!(statement.get() instanceof Statements.Move)
110
+ || statement.getPragmas().some(p => p.getStr().toUpperCase() === this.getMetadata().pragma)) {
111
+ continue;
112
+ }
113
+ const inline = (_a = statement.findDirectExpression(Expressions.Target)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.InlineData);
114
+ const name = (_b = inline === null || inline === void 0 ? void 0 : inline.findDirectExpression(Expressions.TargetField)) === null || _b === void 0 ? void 0 : _b.getFirstToken();
115
+ const operand = this.singleOperand(statement.findDirectExpression(Expressions.Source));
116
+ if (name === undefined || operand === undefined) {
117
+ continue;
118
+ }
119
+ const scope = syntax.spaghetti.lookupPosition(name.getStart(), file.getFilename());
120
+ const resolved = (_c = scope === null || scope === void 0 ? void 0 : scope.getData().references.find(reference => reference.referenceType === _reference_1.ReferenceType.DataReadReference
121
+ && reference.position.getStart().equals(operand.getFirstToken().getStart()))) === null || _c === void 0 ? void 0 : _c.resolved;
122
+ if (!(resolved instanceof _typed_identifier_1.TypedIdentifier)) {
123
+ continue;
124
+ }
125
+ const fixed = this.fixedType(resolved.getType());
126
+ if (fixed === undefined) {
127
+ continue;
128
+ }
129
+ const message = `The fixed type ${fixed.fixed} is used for the generic type ${fixed.generic} of "${operand.concatTokens()}", ` +
130
+ `declare "${name.getStr()}" with a type or use CONV`;
131
+ const replacement = `CONV ${fixed.conv}( ${operand.concatTokens()} )`;
132
+ const fix = edit_helper_1.EditHelper.replaceRange(file, operand.getFirstToken().getStart(), operand.getLastToken().getEnd(), replacement);
133
+ issues.push(issue_1.Issue.atToken(file, name, message, this.getMetadata().key, this.conf.severity, fix));
134
+ }
135
+ }
136
+ return issues;
137
+ }
138
+ /** The source when it is a single field or field symbol, without components, offset/length or dereference */
139
+ singleOperand(source) {
140
+ if (source === undefined || source.getChildren().length !== 1) {
141
+ return undefined;
142
+ }
143
+ const chain = source.findDirectExpression(Expressions.FieldChain);
144
+ if (chain === undefined || chain.getChildren().length !== 1) {
145
+ return undefined;
146
+ }
147
+ const first = chain.getFirstChild();
148
+ if (first instanceof nodes_1.ExpressionNode
149
+ && (first.get() instanceof Expressions.SourceField || first.get() instanceof Expressions.SourceFieldSymbol)) {
150
+ return chain;
151
+ }
152
+ return undefined;
153
+ }
154
+ /** The type the system uses for a generic type in an inline declaration, undefined if the type is not generic,
155
+ * or is a generic type that check_syntax does not make concrete */
156
+ fixedType(type) {
157
+ if (type instanceof basic_1.CLikeType) {
158
+ return { generic: "CLIKE", fixed: "STRING", conv: "string" };
159
+ }
160
+ else if (type instanceof basic_1.CSequenceType) {
161
+ return { generic: "CSEQUENCE", fixed: "STRING", conv: "string" };
162
+ }
163
+ else if (type instanceof basic_1.XSequenceType) {
164
+ return { generic: "XSEQUENCE", fixed: "XSTRING", conv: "xstring" };
165
+ }
166
+ return undefined;
167
+ }
168
+ }
169
+ exports.InlineGenericSource = InlineGenericSource;
170
+ //# sourceMappingURL=inline_generic_source.js.map
@@ -49,6 +49,12 @@ const _irule_1 = require("./_irule");
49
49
  const edit_helper_1 = require("../edit_helper");
50
50
  const version_1 = require("../version");
51
51
  class RedundantConversionConf extends _basic_rule_config_1.BasicRuleConfig {
52
+ constructor() {
53
+ super(...arguments);
54
+ /** Also report a CONV that is the whole right-hand side of an assignment whose target already has the
55
+ * conversion type, e.g. "count = CONV i( text )." with count TYPE i, the assignment converts by itself */
56
+ this.assignmentTarget = true;
57
+ }
52
58
  }
53
59
  exports.RedundantConversionConf = RedundantConversionConf;
54
60
  class RedundantConversion {
@@ -61,7 +67,12 @@ class RedundantConversion {
61
67
  title: "Redundant Conversion",
62
68
  shortDescription: `Find redundant CONV expressions`,
63
69
  extendedInformation: `Reports CONV expressions whose operand already has the conversion's target type,
64
- including the result of a built-in function with a fixed result type, like to_upper( ) or strlen( ).`,
70
+ including the result of a built-in function with a fixed result type, like to_upper( ) or strlen( ).
71
+
72
+ With option assignmentTarget, also reports a CONV that is the whole right-hand side of an assignment
73
+ whose target already has the conversion's type, "count = CONV i( text )." with count TYPE i, the
74
+ assignment converts by itself. Not reported for inline declarations, enumerated types, LET, and operands
75
+ with a generic type.`,
65
76
  tags: [_irule_1.RuleTag.Quickfix],
66
77
  badExample: `DATA text TYPE string.
67
78
  text = CONV string( text ).`,
@@ -121,7 +132,11 @@ text = text.`,
121
132
  }
122
133
  const operandHasSameType = operandType !== undefined && this.sameType(target.getType(), operandType);
123
134
  const arithmeticHasSameContext = this.arithmeticUsesSameTargetType(source, bodySource, file, scope, target.getType());
124
- if (operandHasSameType === false && arithmeticHasSameContext === false) {
135
+ const assignmentHasSameType = this.conf.assignmentTarget !== false
136
+ && this.assignmentTargetHasSameType(source, file, scope, target.getType());
137
+ if (operandHasSameType === false
138
+ && arithmeticHasSameContext === false
139
+ && assignmentHasSameType === false) {
125
140
  continue;
126
141
  }
127
142
  const closingParen = source.getDirectTokens().find(token => token.getStr() === ")" && token.getStart().isAfter(bodySource.getLastToken().getStart()));
@@ -156,6 +171,88 @@ text = text.`,
156
171
  return assignmentTarget instanceof _typed_identifier_1.TypedIdentifier
157
172
  && this.sameType(conversionType, assignmentTarget.getType());
158
173
  }
174
+ /** "target = CONV i( text )." where target has the conversion type, the assignment converts the same way */
175
+ assignmentTargetHasSameType(conversion, file, scope, conversionType) {
176
+ var _a;
177
+ // CONV converts the base type to an enumerated type, a plain assignment does not compile
178
+ if (conversionType instanceof basic_1.EnumType) {
179
+ return false;
180
+ }
181
+ const body = conversion.findDirectExpression(Expressions.ConvBody);
182
+ if (body === undefined
183
+ || body.findDirectExpression(Expressions.Let) !== undefined
184
+ // "CONV type ( body )" without a trailing operator
185
+ || conversion.getChildren().length !== 5) {
186
+ return false;
187
+ }
188
+ // "target = CONV ...", not "+=", "?=", "a = b = CONV ..." or MOVE
189
+ const statement = file.getStatements().find(candidate => candidate.includesToken(conversion.getFirstToken()));
190
+ const children = (_a = statement === null || statement === void 0 ? void 0 : statement.getChildren()) !== null && _a !== void 0 ? _a : [];
191
+ const target = children[0];
192
+ if (!((statement === null || statement === void 0 ? void 0 : statement.get()) instanceof Statements.Move)
193
+ || children.length !== 4
194
+ || !(target instanceof nodes_1.ExpressionNode)
195
+ || !(target.get() instanceof Expressions.Target)
196
+ || children[1].concatTokens() !== "="
197
+ || children[2] !== conversion) {
198
+ return false;
199
+ }
200
+ // a generic operand gets its type from the CONV, also "ref->*" with ref TYPE REF TO data
201
+ const bodyStart = body.getFirstToken().getStart();
202
+ const bodyEnd = body.getLastToken().getEnd();
203
+ const genericOperand = scope.getData().references.some(reference => {
204
+ if (reference.referenceType !== _reference_1.ReferenceType.DataReadReference
205
+ || reference.position.getFilename() !== file.getFilename()
206
+ || reference.position.getStart().isBetween(bodyStart, bodyEnd) === false
207
+ || !(reference.resolved instanceof _typed_identifier_1.TypedIdentifier)) {
208
+ return false;
209
+ }
210
+ const type = reference.resolved.getType();
211
+ return type.isGeneric() || (type instanceof basic_1.DataReference && type.getType().isGeneric());
212
+ });
213
+ if (genericOperand === true) {
214
+ return false;
215
+ }
216
+ const targetType = this.targetType(target, scope);
217
+ return targetType !== undefined && this.sameType(conversionType, targetType);
218
+ }
219
+ /** Type of a variable, field symbol, attribute or structure component, undefined for anything else */
220
+ targetType(target, scope) {
221
+ var _a;
222
+ let type;
223
+ for (const child of target.getChildren()) {
224
+ if (child instanceof nodes_1.TokenNode) {
225
+ if (["-", "->", "=>"].includes(child.getFirstToken().getStr()) === false) {
226
+ return undefined;
227
+ }
228
+ }
229
+ else if (child.get() instanceof Expressions.TargetField
230
+ || child.get() instanceof Expressions.TargetFieldSymbol
231
+ || child.get() instanceof Expressions.AttributeName) {
232
+ const resolved = (_a = scope.getData().references.find(reference => (reference.referenceType === _reference_1.ReferenceType.DataWriteReference
233
+ || reference.referenceType === _reference_1.ReferenceType.DataReadReference)
234
+ && reference.position.getStart().equals(child.getFirstToken().getStart()))) === null || _a === void 0 ? void 0 : _a.resolved;
235
+ if (!(resolved instanceof _typed_identifier_1.TypedIdentifier)) {
236
+ return undefined;
237
+ }
238
+ type = resolved.getType();
239
+ }
240
+ else if (child.get() instanceof Expressions.ComponentName) {
241
+ if (type instanceof basic_1.TableType) {
242
+ type = type.getRowType();
243
+ }
244
+ if (!(type instanceof basic_1.StructureType)) {
245
+ return undefined;
246
+ }
247
+ type = type.getComponentByName(child.concatTokens());
248
+ }
249
+ else if (!(child.get() instanceof Expressions.ClassName)) {
250
+ // inline declarations, dereferences, table expressions, offsets and lengths
251
+ return undefined;
252
+ }
253
+ }
254
+ return type;
255
+ }
159
256
  sourceType(source, scope) {
160
257
  let context;
161
258
  // boolc( <condition> ) is not a MethodCallChain, xsdbool( ) has the same shape but is not reported