@abaplint/core 2.120.111 → 2.120.113

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.
Files changed (46) hide show
  1. package/build/abaplint.d.ts +7 -4
  2. package/build/src/abap/2_statements/expressions/loop_group_by.js +1 -1
  3. package/build/src/abap/2_statements/expressions/method_name.js +1 -1
  4. package/build/src/abap/5_syntax/_builtin_call.js +98 -0
  5. package/build/src/abap/5_syntax/_object_oriented.js +11 -0
  6. package/build/src/abap/5_syntax/_reserved_names.js +12 -0
  7. package/build/src/abap/5_syntax/basic_types.js +109 -5
  8. package/build/src/abap/5_syntax/expressions/_check_header_line.js +59 -0
  9. package/build/src/abap/5_syntax/expressions/_substring.js +5 -4
  10. package/build/src/abap/5_syntax/expressions/compare.js +17 -31
  11. package/build/src/abap/5_syntax/expressions/cond_body.js +7 -0
  12. package/build/src/abap/5_syntax/expressions/inline_data.js +2 -0
  13. package/build/src/abap/5_syntax/expressions/method_call_chain.js +1 -1
  14. package/build/src/abap/5_syntax/expressions/method_call_param.js +4 -0
  15. package/build/src/abap/5_syntax/expressions/method_parameters.js +4 -0
  16. package/build/src/abap/5_syntax/expressions/source.js +46 -0
  17. package/build/src/abap/5_syntax/expressions/switch_body.js +6 -0
  18. package/build/src/abap/5_syntax/expressions/type_table.js +21 -1
  19. package/build/src/abap/5_syntax/statements/class_data.js +2 -0
  20. package/build/src/abap/5_syntax/statements/data.js +2 -0
  21. package/build/src/abap/5_syntax/statements/delete_internal.js +11 -0
  22. package/build/src/abap/5_syntax/statements/insert_database.js +12 -0
  23. package/build/src/abap/5_syntax/statements/insert_internal.js +14 -2
  24. package/build/src/abap/5_syntax/statements/message.js +10 -0
  25. package/build/src/abap/5_syntax/statements/modify_internal.js +7 -0
  26. package/build/src/abap/5_syntax/statements/raise.js +5 -0
  27. package/build/src/abap/5_syntax/statements/write.js +10 -0
  28. package/build/src/abap/5_syntax/syntax.js +9 -0
  29. package/build/src/abap/types/basic/floating_point_type.js +2 -2
  30. package/build/src/abap/types/basic/hex_type.js +2 -2
  31. package/build/src/abap/types/basic/numeric_type.js +2 -2
  32. package/build/src/abap/types/basic/table_type.js +2 -1
  33. package/build/src/abap/types/method_parameters.js +8 -1
  34. package/build/src/ddic.js +9 -9
  35. package/build/src/objects/table_type.js +8 -7
  36. package/build/src/registry.js +1 -1
  37. package/build/src/rules/check_subrc.js +87 -3
  38. package/build/src/rules/commented_code.js +21 -1
  39. package/build/src/rules/forbidden_statement.js +122 -0
  40. package/build/src/rules/index.js +1 -0
  41. package/build/src/rules/loop_target_misuse.js +268 -4
  42. package/build/src/rules/obsolete_statement.js +31 -1
  43. package/build/src/rules/redundant_conversion.js +15 -3
  44. package/build/src/rules/rfc_error_handling.js +63 -13
  45. package/build/src/rules/unhandled_classic_exception.js +31 -4
  46. package/package.json +1 -1
@@ -56,6 +56,10 @@ class CheckSubrcConf extends _basic_rule_config_1.BasicRuleConfig {
56
56
  this.readTable = true;
57
57
  this.assign = true;
58
58
  this.find = true;
59
+ /** CALL FUNCTION with EXCEPTIONS, unless every exception is mapped to 0 */
60
+ this.callFunction = true;
61
+ /** Method call with EXCEPTIONS, unless every exception is mapped to 0 */
62
+ this.callMethod = true;
59
63
  }
60
64
  }
61
65
  exports.CheckSubrcConf = CheckSubrcConf;
@@ -77,7 +81,17 @@ If sy-dbcnt is checked after database statements, it is considered okay.
77
81
 
78
82
  If IS ASSIGNED is checked after assigning, it is considered okay.
79
83
 
80
- FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
84
+ FIND statement with MATCH COUNT is considered okay if subrc is not checked
85
+
86
+ CALL FUNCTION and method calls are checked if they have an EXCEPTIONS addition that maps an exception to a value other than 0,
87
+ including CALL FUNCTION DESTINATION and STARTING NEW TASK. Not checked: calls without EXCEPTIONS, calls where every exception
88
+ is mapped to 0, EXCEPTION-TABLE, CALL FUNCTION IN UPDATE TASK and IN BACKGROUND.
89
+ For CALL FUNCTION the pragma ##FM_SUBRC_OK is also accepted.
90
+
91
+ A call of a method with SUBRC in its name, like check_subrc( ), is considered a check.
92
+
93
+ After ELSE, ELSEIF, WHEN, CATCH and CLEANUP the check continues after the end of the block, as these branches are not run
94
+ after the statement`,
81
95
  tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Quickfix],
82
96
  pseudoComment: "EC CI_SUBRC",
83
97
  pragma: "##SUBRC_OK",
@@ -102,7 +116,6 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
102
116
  const config = this.getConfig();
103
117
  for (let i = 0; i < statements.length; i++) {
104
118
  const statement = statements[i];
105
- // todo: CALL FUNCTION
106
119
  if (statement.getPragmas().some(t => t.getStr() === this.getMetadata().pragma)) {
107
120
  continue;
108
121
  }
@@ -188,6 +201,22 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
188
201
  const fix = this.buildFix(file, statement);
189
202
  issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
190
203
  }
204
+ else if (config.callFunction === true
205
+ && statement.get() instanceof Statements.CallFunction
206
+ && statement.getPragmas().some(t => t.getStr().toUpperCase() === "##FM_SUBRC_OK") === false
207
+ && this.isUpdateOrBackgroundTask(statement) === false
208
+ && this.mapsExceptionToNonZero(statement) === true
209
+ && this.isChecked(i, statements) === false) {
210
+ const fix = this.buildFix(file, statement);
211
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
212
+ }
213
+ else if (config.callMethod === true
214
+ && statement.get() instanceof Statements.Call
215
+ && this.mapsExceptionToNonZero(statement) === true
216
+ && this.isChecked(i, statements) === false) {
217
+ const fix = this.buildFix(file, statement);
218
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity, undefined, [fix]));
219
+ }
191
220
  }
192
221
  return issues;
193
222
  }
@@ -202,6 +231,22 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
202
231
  }
203
232
  return false;
204
233
  }
234
+ /** true if the EXCEPTIONS addition maps an exception to a value other than 0, then the call sets sy-subrc */
235
+ mapsExceptionToNonZero(statement) {
236
+ for (const list of statement.findAllExpressions(Expressions.ParameterListExceptions)) {
237
+ for (const exception of list.findDirectExpressions(Expressions.ParameterException)) {
238
+ const value = exception.findDirectExpression(Expressions.Integer);
239
+ if (value === undefined || parseInt(value.concatTokens().replace(/\s/g, ""), 10) !== 0) {
240
+ return true;
241
+ }
242
+ }
243
+ }
244
+ return false;
245
+ }
246
+ isUpdateOrBackgroundTask(statement) {
247
+ const concat = statement.concatTokens().toUpperCase();
248
+ return concat.includes(" IN UPDATE TASK") || concat.includes(" IN BACKGROUND ");
249
+ }
205
250
  isExemptedFind(s) {
206
251
  // see https://github.com/abaplint/abaplint/issues/2130
207
252
  return s.concatTokens().toUpperCase().includes(" MATCH COUNT ") === true;
@@ -223,6 +268,28 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
223
268
  }
224
269
  return false;
225
270
  }
271
+ /** index of the end statement that closes the block the statement at index is in */
272
+ findEnd(index, statements, open, close) {
273
+ let depth = 0;
274
+ for (let i = index + 1; i < statements.length; i++) {
275
+ const statement = statements[i].get();
276
+ if (open.some(o => statement instanceof o)) {
277
+ depth++;
278
+ }
279
+ else if (statement instanceof close) {
280
+ if (depth === 0) {
281
+ return i;
282
+ }
283
+ depth--;
284
+ }
285
+ }
286
+ return statements.length;
287
+ }
288
+ /** a method call like check_subrc( ), the method reads sy-subrc */
289
+ callsSubrcMethod(statement) {
290
+ return statement.get() instanceof Statements.Call
291
+ && statement.findAllExpressions(Expressions.MethodName).some(m => m.concatTokens().toUpperCase().includes("SUBRC"));
292
+ }
226
293
  isChecked(index, statements) {
227
294
  var _a, _b;
228
295
  let assigned = undefined;
@@ -242,9 +309,25 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
242
309
  }
243
310
  }
244
311
  else if (statement.get() instanceof Statements.EndIf
312
+ || statement.get() instanceof Statements.EndCase
313
+ || statement.get() instanceof Statements.EndTry
245
314
  || statement.get() instanceof Statements.EndTestSeam) {
246
315
  continue;
247
316
  }
317
+ else if (statement.get() instanceof Statements.Else
318
+ || statement.get() instanceof Statements.ElseIf) {
319
+ // the other branches are not run after this one, continue after ENDIF
320
+ i = this.findEnd(i, statements, [Statements.If], Statements.EndIf);
321
+ }
322
+ else if (statement.get() instanceof Statements.When
323
+ || statement.get() instanceof Statements.WhenOthers
324
+ || statement.get() instanceof Statements.WhenType) {
325
+ i = this.findEnd(i, statements, [Statements.Case, Statements.CaseType], Statements.EndCase);
326
+ }
327
+ else if (statement.get() instanceof Statements.Catch
328
+ || statement.get() instanceof Statements.Cleanup) {
329
+ i = this.findEnd(i, statements, [Statements.Try], Statements.EndTry);
330
+ }
248
331
  else {
249
332
  if (assigned && concat.includes(assigned)) {
250
333
  return true;
@@ -253,7 +336,8 @@ FIND statement with MATCH COUNT is considered okay if subrc is not checked`,
253
336
  return true;
254
337
  }
255
338
  return concat.includes(" SY-SUBRC")
256
- || concat.includes("CL_ABAP_UNIT_ASSERT=>ASSERT_SUBRC");
339
+ || concat.includes("CL_ABAP_UNIT_ASSERT=>ASSERT_SUBRC")
340
+ || this.callsSubrcMethod(statement);
257
341
  }
258
342
  }
259
343
  return false;
@@ -81,7 +81,8 @@ https://docs.abapopenchecks.org/checks/14/`,
81
81
  return [];
82
82
  }
83
83
  let containsStatement = false;
84
- for (const statementNode of statementNodes) {
84
+ for (let i = 0; i < statementNodes.length; i++) {
85
+ const statementNode = statementNodes[i];
85
86
  const statement = statementNode.get();
86
87
  if (this.getConfig().allowIncludeInFugr === true
87
88
  && obj instanceof objects_1.FunctionGroup
@@ -91,6 +92,9 @@ https://docs.abapopenchecks.org/checks/14/`,
91
92
  if (!(statement instanceof _statement_1.Unknown
92
93
  || statement instanceof _statement_1.Empty
93
94
  || statement instanceof _statement_1.Comment)) {
95
+ if (this.isSplitFromText(statementNodes, i)) {
96
+ continue;
97
+ }
94
98
  containsStatement = true;
95
99
  break;
96
100
  }
@@ -102,6 +106,22 @@ https://docs.abapopenchecks.org/checks/14/`,
102
106
  const issue = issue_1.Issue.atRange(file, posStart, posEnd, this.getMessage(), this.getMetadata().key, this.conf.severity, fix);
103
107
  return [issue];
104
108
  }
109
+ /**
110
+ * The statement parser splits an Unknown statement at a line break if the rest parses as a statement.
111
+ * In comment text, such a split is code only if no further text follows the statement on its line.
112
+ */
113
+ isSplitFromText(statementNodes, index) {
114
+ const previous = statementNodes[index - 1];
115
+ if (previous === undefined
116
+ || !(previous.get() instanceof _statement_1.Unknown)
117
+ || [".", ","].includes(previous.getLastToken().getStr())) {
118
+ return false;
119
+ }
120
+ const next = statementNodes[index + 1];
121
+ return next !== undefined
122
+ && next.get() instanceof _statement_1.Unknown
123
+ && next.getFirstToken().getRow() === statementNodes[index].getLastToken().getRow();
124
+ }
105
125
  isCommentLine(text) {
106
126
  return (text.substr(0, 1) === "*")
107
127
  || (text.trim().substr(0, 1) === "\"" && text.trim().substr(1, 1) !== "!");
@@ -0,0 +1,122 @@
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.ForbiddenStatement = exports.ForbiddenStatementConf = void 0;
37
+ const Statements = __importStar(require("../abap/2_statements/statements"));
38
+ const _abap_rule_1 = require("./_abap_rule");
39
+ const _basic_rule_config_1 = require("./_basic_rule_config");
40
+ const issue_1 = require("../issue");
41
+ const _irule_1 = require("./_irule");
42
+ class ForbiddenStatementConf extends _basic_rule_config_1.BasicRuleConfig {
43
+ constructor() {
44
+ super(...arguments);
45
+ /** List of forbidden statements, given as abaplint statement names,
46
+ * case insensitive, spaces, underscores and dashes are ignored,
47
+ * so "READ TABLE", "ReadTable" and "read_table" are the same
48
+ * @uniqueItems true
49
+ */
50
+ this.statements = [];
51
+ }
52
+ }
53
+ exports.ForbiddenStatementConf = ForbiddenStatementConf;
54
+ class ForbiddenStatement extends _abap_rule_1.ABAPRule {
55
+ constructor() {
56
+ super(...arguments);
57
+ this.conf = new ForbiddenStatementConf();
58
+ /** statement class -> configured spelling, used in the message */
59
+ this.forbidden = new Map();
60
+ }
61
+ getMetadata() {
62
+ return {
63
+ key: "forbidden_statement",
64
+ title: "Forbidden Statement",
65
+ shortDescription: `Forbid use of specified statements.`,
66
+ extendedInformation: `Nothing is reported unless statements are configured.
67
+
68
+ A configured entry matches the abaplint statement with the same name, compared case insensitive,
69
+ ignoring spaces, underscores and dashes, so "READ TABLE", "ReadTable" and "read_table" all match ReadTable.
70
+
71
+ The names are the class names in https://github.com/abaplint/abaplint/tree/main/packages/core/src/abap/2_statements/statements,
72
+ some keywords are split into several statements, eg. "INSERT" does not match anything,
73
+ use "InsertInternal" or "InsertDatabase", likewise for DELETE, MODIFY and READ. Entries that do not match a statement are ignored.
74
+
75
+ Each part of a chained statement is checked as a separate statement.`,
76
+ tags: [_irule_1.RuleTag.SingleFile],
77
+ badExample: `* with "statements": ["READ TABLE"]
78
+ READ TABLE items INDEX 1 INTO item.`,
79
+ goodExample: `* with "statements": ["READ TABLE"]
80
+ item = items[ 1 ].`,
81
+ };
82
+ }
83
+ getConfig() {
84
+ return this.conf;
85
+ }
86
+ setConfig(conf) {
87
+ this.conf = conf;
88
+ if (this.conf.statements === undefined) {
89
+ this.conf.statements = [];
90
+ }
91
+ const byName = new Map();
92
+ for (const [name, value] of Object.entries(Statements)) {
93
+ byName.set(ForbiddenStatement.normalize(name), value);
94
+ }
95
+ this.forbidden = new Map();
96
+ for (const entry of this.conf.statements) {
97
+ const found = byName.get(ForbiddenStatement.normalize(entry));
98
+ if (found !== undefined && this.forbidden.has(found) === false) {
99
+ this.forbidden.set(found, entry.trim());
100
+ }
101
+ }
102
+ }
103
+ runParsed(file) {
104
+ if (this.forbidden.size === 0) {
105
+ return [];
106
+ }
107
+ const ret = [];
108
+ for (const s of file.getStatements()) {
109
+ const name = this.forbidden.get(s.get().constructor);
110
+ if (name !== undefined) {
111
+ const message = "Statement \"" + name + "\" is forbidden";
112
+ ret.push(issue_1.Issue.atStatement(file, s, message, this.getMetadata().key, this.conf.severity));
113
+ }
114
+ }
115
+ return ret;
116
+ }
117
+ static normalize(name) {
118
+ return name.toLowerCase().replace(/[\s_-]/g, "");
119
+ }
120
+ }
121
+ exports.ForbiddenStatement = ForbiddenStatement;
122
+ //# sourceMappingURL=forbidden_statement.js.map
@@ -98,6 +98,7 @@ __exportStar(require("./fm_global_parameters_obsolete"), exports);
98
98
  __exportStar(require("./forbidden_identifier"), exports);
99
99
  __exportStar(require("./forbidden_kernel_write"), exports);
100
100
  __exportStar(require("./forbidden_pseudo_and_pragma"), exports);
101
+ __exportStar(require("./forbidden_statement"), exports);
101
102
  __exportStar(require("./forbidden_void_type"), exports);
102
103
  __exportStar(require("./form_tables_obsolete"), exports);
103
104
  __exportStar(require("./fully_type_constants"), exports);
@@ -40,6 +40,7 @@ const issue_1 = require("../issue");
40
40
  const _basic_rule_config_1 = require("./_basic_rule_config");
41
41
  const _abap_rule_1 = require("./_abap_rule");
42
42
  const _irule_1 = require("./_irule");
43
+ const _statement_1 = require("../abap/2_statements/statements/_statement");
43
44
  class LoopTargetMisuseConf extends _basic_rule_config_1.BasicRuleConfig {
44
45
  constructor() {
45
46
  super(...arguments);
@@ -48,6 +49,9 @@ class LoopTargetMisuseConf extends _basic_rule_config_1.BasicRuleConfig {
48
49
  /** Report LOOP AT ... INTO <fs> and READ TABLE ... INTO <fs> when the local field symbol is only bound by ASSIGNING,
49
50
  * so the statement overwrites a table row */
50
51
  this.intoFieldSymbol = true;
52
+ /** Report a field symbol of LOOP AT ... ASSIGNING used after ENDLOOP, and a field symbol of READ TABLE ... ASSIGNING
53
+ * used after the check of sy-subrc, outside the branch that found the row */
54
+ this.usedAfterLoop = true;
51
55
  }
52
56
  }
53
57
  exports.LoopTargetMisuseConf = LoopTargetMisuseConf;
@@ -73,7 +77,21 @@ Option intoFieldSymbol: LOOP AT ... INTO <fs> or READ TABLE ... INTO <fs>. INTO
73
77
  into the data object the field symbol points to. When <fs> was bound by ASSIGNING, that data
74
78
  object is a row of a table, and the statement overwrites it. ASSIGNING was meant.
75
79
  Reported only for a field symbol declared in the method, form or function module, and only when
76
- no ASSIGN and no STRUCTURE ... DEFAULT binds it there, so it can only point to a table row.`,
80
+ no ASSIGN and no STRUCTURE ... DEFAULT binds it there, so it can only point to a table row.
81
+
82
+ Option usedAfterLoop: a field symbol of LOOP AT ... ASSIGNING used after ENDLOOP. It points to
83
+ the last row the loop processed. When the loop processed no row, it is not assigned, and the use
84
+ raises GETWA_NOT_ASSIGNED, or it still points to what it was bound to before the loop.
85
+ Also a field symbol of READ TABLE ... ASSIGNING used after sy-subrc was checked, outside the
86
+ branch of that check. When the READ fails, the field symbol keeps its previous binding.
87
+ Reported at the first such use in the method, form or function module.
88
+ Not reported: a use inside IF, ELSEIF, CASE or WHILE whose condition reads sy-subrc, the table of
89
+ the loop or a variable the loop body sets (a flag), a use after such a check that leaves with
90
+ RETURN, EXIT, CONTINUE, CHECK, RAISE or MESSAGE or that does not use the field symbol, a use after
91
+ CHECK or ASSERT of such a condition, and any use after IS ASSIGNED, UNASSIGN or another binding of
92
+ the field symbol, or after a macro call. Work areas of INTO are not checked, they keep a value.
93
+ A READ TABLE whose sy-subrc is not checked at all is left to check_subrc, a LOOP inside a LOOP
94
+ with the same field symbol to the option nestedLoop.`,
77
95
  tags: [_irule_1.RuleTag.SingleFile],
78
96
  badExample: `LOOP AT lt_header INTO ls_row.
79
97
  LOOP AT lt_items INTO ls_row WHERE parent = ls_row-id.
@@ -85,7 +103,12 @@ ENDLOOP.
85
103
 
86
104
  LOOP AT lt_a ASSIGNING <ls_a>.
87
105
  ENDLOOP.
88
- READ TABLE lt_a INTO <ls_a> INDEX 1.`,
106
+ READ TABLE lt_a INTO <ls_a> INDEX 1.
107
+
108
+ LOOP AT lt_a ASSIGNING <ls_a> WHERE id = lv_id.
109
+ EXIT.
110
+ ENDLOOP.
111
+ <ls_a>-flag = abap_true.`,
89
112
  goodExample: `LOOP AT lt_header INTO ls_row.
90
113
  LOOP AT lt_items INTO ls_item WHERE parent = ls_row-id.
91
114
  lv_sum = lv_sum + ls_item-amount.
@@ -96,7 +119,14 @@ ENDLOOP.
96
119
 
97
120
  LOOP AT lt_a ASSIGNING <ls_a>.
98
121
  ENDLOOP.
99
- READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
122
+ READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.
123
+
124
+ LOOP AT lt_a ASSIGNING <ls_a> WHERE id = lv_id.
125
+ EXIT.
126
+ ENDLOOP.
127
+ IF sy-subrc = 0.
128
+ <ls_a>-flag = abap_true.
129
+ ENDIF.`,
100
130
  };
101
131
  }
102
132
  getConfig() {
@@ -114,6 +144,14 @@ READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
114
144
  if (this.conf.intoFieldSymbol !== false) {
115
145
  issues.push(...this.checkIntoFieldSymbol(file, statements));
116
146
  }
147
+ if (this.conf.usedAfterLoop !== false) {
148
+ for (const issue of this.checkUsedAfterLoop(file, statements)) {
149
+ // a READ TABLE ... INTO <fs> after the loop is already reported by the option intoFieldSymbol
150
+ if (issues.some(i => i.getStart().equals(issue.getStart())) === false) {
151
+ issues.push(issue);
152
+ }
153
+ }
154
+ }
117
155
  return issues;
118
156
  }
119
157
  ////////////////
@@ -170,7 +208,7 @@ READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
170
208
  let depth = 0;
171
209
  for (let i = index + 1; i < statements.length && this.isBlockBoundary(statements[i]) === false; i++) {
172
210
  const s = statements[i].get();
173
- if (s instanceof Statements.Loop) {
211
+ if (s instanceof Statements.Loop || s instanceof Statements.LoopAtScreen || s instanceof Statements.LoopExtract) {
174
212
  depth++;
175
213
  }
176
214
  else if (s instanceof Statements.EndLoop) {
@@ -279,6 +317,232 @@ READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
279
317
  var _a;
280
318
  return (_a = fsTarget.findFirstExpression(Expressions.FieldSymbol)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase();
281
319
  }
320
+ checkUsedAfterLoop(file, statements) {
321
+ var _a;
322
+ const issues = [];
323
+ // the field symbols of the open LOOPs, undefined for a LOOP without field symbol
324
+ let open = [];
325
+ for (let i = 0; i < statements.length; i++) {
326
+ const statement = statements[i];
327
+ const s = statement.get();
328
+ if (this.isBlockBoundary(statement)) {
329
+ open = [];
330
+ continue;
331
+ }
332
+ else if (s instanceof Statements.EndLoop) {
333
+ open.pop();
334
+ continue;
335
+ }
336
+ let use = undefined;
337
+ let name = undefined;
338
+ let what = "";
339
+ if (s instanceof Statements.Loop) {
340
+ const node = this.loopTarget(statement);
341
+ name = (node === null || node === void 0 ? void 0 : node.get()) instanceof Expressions.FSTarget ? this.targetName(node) : undefined;
342
+ const end = this.findEndLoop(statements, i);
343
+ // a LOOP inside a LOOP with the same field symbol is left to the option nestedLoop
344
+ if (name !== undefined && end !== undefined && open.includes(name) === false) {
345
+ const guards = this.loopGuards(statements, i, end, name);
346
+ use = this.findUncheckedUse(statements, end + 1, name, guards, false);
347
+ what = `after ENDLOOP, it points to the last row, or is not assigned when the loop processed no row`;
348
+ }
349
+ open.push(name);
350
+ }
351
+ else if (s instanceof Statements.LoopAtScreen || s instanceof Statements.LoopExtract) {
352
+ open.push(undefined);
353
+ }
354
+ else if (s instanceof Statements.ReadTable) {
355
+ const fs = (_a = statement.findDirectExpression(Expressions.ReadTableTarget)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.FSTarget);
356
+ name = fs ? this.targetName(fs) : undefined;
357
+ if (name !== undefined) {
358
+ use = this.findUncheckedUse(statements, i + 1, name, new Set(), true);
359
+ what = `outside the check of sy-subrc, when the READ TABLE fails it is not assigned or points to an earlier row`;
360
+ }
361
+ }
362
+ if (use !== undefined && name !== undefined) {
363
+ const token = statements[use].getTokens().find(t => t.getStr().toUpperCase() === name);
364
+ const message = `${token === null || token === void 0 ? void 0 : token.getStr()} is used ${what}`;
365
+ issues.push(issue_1.Issue.atStatement(file, statements[use], message, this.getMetadata().key, this.conf.severity));
366
+ }
367
+ }
368
+ return issues;
369
+ }
370
+ /** upper case names of the variables whose check counts as a check that the LOOP processed a row:
371
+ * the table and the variables the loop body writes */
372
+ loopGuards(statements, loop, end, name) {
373
+ var _a;
374
+ const guards = new Set();
375
+ const first = (_a = statements[loop].findDirectExpression(Expressions.LoopSource)) === null || _a === void 0 ? void 0 : _a.getFirstToken().getStr().toUpperCase();
376
+ if (first !== undefined) {
377
+ guards.add(first);
378
+ }
379
+ for (let i = loop + 1; i < end; i++) {
380
+ for (const target of statements[i].findAllExpressions(Expressions.Target)) {
381
+ guards.add(this.firstName(target));
382
+ }
383
+ }
384
+ guards.delete(name);
385
+ return guards;
386
+ }
387
+ /** upper case name of the variable a Target writes, also declared inline */
388
+ firstName(target) {
389
+ const inline = target.findFirstExpression(Expressions.TargetField);
390
+ return (inline ? inline.getFirstToken() : target.getFirstToken()).getStr().toUpperCase();
391
+ }
392
+ /** index of the first statement from the index on that uses the field symbol without a check before it,
393
+ * looking until the end of the method, form or function module or until the field symbol is bound again.
394
+ * With checkFirst, only a use after a check is returned, an unchecked READ TABLE is left to check_subrc */
395
+ findUncheckedUse(statements, from, name, guards, checkFirst) {
396
+ const frames = [];
397
+ let checked = false;
398
+ let sawCheck = false;
399
+ for (let i = from; i < statements.length && this.isBlockBoundary(statements[i]) === false; i++) {
400
+ const statement = statements[i];
401
+ const s = statement.get();
402
+ if (s instanceof _statement_1.MacroCall || s instanceof _statement_1.Unknown || this.bindsOrTests(statement, name)) {
403
+ return undefined;
404
+ }
405
+ const isCondition = s instanceof Statements.If || s instanceof Statements.ElseIf || s instanceof Statements.Case
406
+ || s instanceof Statements.When || s instanceof Statements.While
407
+ || s instanceof Statements.Check || s instanceof Statements.Assert;
408
+ const guard = isCondition && this.isGuard(statement, guards);
409
+ sawCheck = sawCheck || guard;
410
+ if ((s instanceof Statements.Check || s instanceof Statements.Assert) && guard) {
411
+ checked = true;
412
+ }
413
+ if (s instanceof Statements.FieldSymbol === false && this.mentions(statement, name)) {
414
+ if (guard || checked || frames.some(f => f.guard)) {
415
+ frames.forEach(f => f.uses = true);
416
+ }
417
+ else if (checkFirst === false || sawCheck === true) {
418
+ return i;
419
+ }
420
+ }
421
+ if (guard || frames.some(f => f.guard)) {
422
+ // a flag set where the check holds, like lv_found = abap_true, checks it as well
423
+ for (const target of statement.findAllExpressions(Expressions.Target)) {
424
+ guards.add(this.firstName(target));
425
+ }
426
+ guards.delete(name);
427
+ }
428
+ if (this.leaves(statement)) {
429
+ frames.forEach(f => f.leaves = true);
430
+ }
431
+ if (this.opensBlock(statement)) {
432
+ frames.push({ guard, uses: false, leaves: false });
433
+ }
434
+ else if (this.isBranch(statement)) {
435
+ if (frames.length === 0) {
436
+ // another branch of a block around the binding, it does not run after it
437
+ return undefined;
438
+ }
439
+ frames[frames.length - 1].guard = frames[frames.length - 1].guard || guard;
440
+ }
441
+ else if (this.closesBlock(statement)) {
442
+ const frame = frames.pop();
443
+ // IF sy-subrc <> 0. RETURN. ENDIF., or a check that sets a flag
444
+ if (frame && frame.guard && (frame.leaves || frame.uses === false)) {
445
+ checked = true;
446
+ }
447
+ }
448
+ }
449
+ return undefined;
450
+ }
451
+ /** true when the statement binds the field symbol again, unassigns it, or checks IS ASSIGNED */
452
+ bindsOrTests(statement, name) {
453
+ if (statement.get() instanceof Statements.Unassign && this.mentions(statement, name)) {
454
+ return true;
455
+ }
456
+ for (const fsTarget of statement.findAllExpressions(Expressions.FSTarget)) {
457
+ if (this.targetName(fsTarget) === name) {
458
+ return true;
459
+ }
460
+ }
461
+ const tokens = statement.getTokens().map(t => t.getStr().toUpperCase());
462
+ for (let i = 0; i < tokens.length - 2; i++) {
463
+ if (tokens[i] === name && tokens[i + 1] === "IS"
464
+ && (tokens[i + 2] === "ASSIGNED" || (tokens[i + 2] === "NOT" && tokens[i + 3] === "ASSIGNED"))) {
465
+ return true;
466
+ }
467
+ }
468
+ return false;
469
+ }
470
+ /** true when the condition reads sy-subrc or one of the variables */
471
+ isGuard(statement, guards) {
472
+ var _a, _b;
473
+ const tokens = statement.getTokens();
474
+ for (let i = 0; i < tokens.length; i++) {
475
+ const str = tokens[i].getStr().toUpperCase();
476
+ const previous = i > 0 ? tokens[i - 1].getStr() : "";
477
+ if (["-", "->", "=>", "~"].includes(previous)) {
478
+ continue;
479
+ }
480
+ if ((str === "SY" && ((_a = tokens[i + 1]) === null || _a === void 0 ? void 0 : _a.getStr()) === "-" && ((_b = tokens[i + 2]) === null || _b === void 0 ? void 0 : _b.getStr().toUpperCase()) === "SUBRC")
481
+ || guards.has(str)) {
482
+ return true;
483
+ }
484
+ }
485
+ return false;
486
+ }
487
+ mentions(statement, name) {
488
+ return statement.getTokens().some(t => t.getStr().toUpperCase() === name);
489
+ }
490
+ /** true for a statement that can leave the current block, a call of a method named like RAISE counts */
491
+ leaves(statement) {
492
+ const s = statement.get();
493
+ return s instanceof Statements.Return
494
+ || s instanceof Statements.Exit
495
+ || s instanceof Statements.Continue
496
+ || s instanceof Statements.Check
497
+ || s instanceof Statements.Raise
498
+ || s instanceof Statements.Leave
499
+ || s instanceof Statements.Message
500
+ || s instanceof Statements.Assert
501
+ || (s instanceof Statements.Call && statement.getTokens().some(t => /raise/i.test(t.getStr())));
502
+ }
503
+ opensBlock(statement) {
504
+ const s = statement.get();
505
+ return s instanceof Statements.If
506
+ || s instanceof Statements.Case
507
+ || s instanceof Statements.CaseType
508
+ || s instanceof Statements.Loop
509
+ || s instanceof Statements.LoopAtScreen
510
+ || s instanceof Statements.LoopExtract
511
+ || s instanceof Statements.Do
512
+ || s instanceof Statements.While
513
+ || s instanceof Statements.Try
514
+ || s instanceof Statements.SelectLoop
515
+ || s instanceof Statements.At
516
+ || s instanceof Statements.AtFirst
517
+ || s instanceof Statements.AtLast
518
+ || s instanceof Statements.OnChange
519
+ || s instanceof Statements.Provide
520
+ || s instanceof Statements.CatchSystemExceptions;
521
+ }
522
+ isBranch(statement) {
523
+ const s = statement.get();
524
+ return s instanceof Statements.ElseIf
525
+ || s instanceof Statements.Else
526
+ || s instanceof Statements.When
527
+ || s instanceof Statements.WhenOthers
528
+ || s instanceof Statements.WhenType
529
+ || s instanceof Statements.Catch
530
+ || s instanceof Statements.Cleanup;
531
+ }
532
+ closesBlock(statement) {
533
+ const s = statement.get();
534
+ return s instanceof Statements.EndIf
535
+ || s instanceof Statements.EndCase
536
+ || s instanceof Statements.EndLoop
537
+ || s instanceof Statements.EndDo
538
+ || s instanceof Statements.EndWhile
539
+ || s instanceof Statements.EndTry
540
+ || s instanceof Statements.EndSelect
541
+ || s instanceof Statements.EndAt
542
+ || s instanceof Statements.EndOn
543
+ || s instanceof Statements.EndProvide
544
+ || s instanceof Statements.EndCatch;
545
+ }
282
546
  isBlockBoundary(statement) {
283
547
  return this.startsBlock(statement) || this.endsBlock(statement);
284
548
  }