@abaplint/core 2.120.108 → 2.120.110

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.
@@ -50,6 +50,7 @@ __exportStar(require("./cds_parser_error"), exports);
50
50
  __exportStar(require("./chain_mainly_declarations"), exports);
51
51
  __exportStar(require("./change_if_to_case"), exports);
52
52
  __exportStar(require("./changing_sorted_line"), exports);
53
+ __exportStar(require("./character_field_literal_blanks"), exports);
53
54
  __exportStar(require("./check_abstract"), exports);
54
55
  __exportStar(require("./check_comments"), exports);
55
56
  __exportStar(require("./check_ddic"), exports);
@@ -91,6 +92,7 @@ __exportStar(require("./else_after_all_returns"), exports);
91
92
  __exportStar(require("./exit_or_check"), exports);
92
93
  __exportStar(require("./expand_macros"), exports);
93
94
  __exportStar(require("./exporting"), exports);
95
+ __exportStar(require("./field_symbol_never_assigned"), exports);
94
96
  __exportStar(require("./final_newline"), exports);
95
97
  __exportStar(require("./fm_global_parameters_obsolete"), exports);
96
98
  __exportStar(require("./forbidden_identifier"), exports);
@@ -130,6 +132,7 @@ __exportStar(require("./line_only_punc"), exports);
130
132
  __exportStar(require("./local_class_naming"), exports);
131
133
  __exportStar(require("./local_testclass_consistency"), exports);
132
134
  __exportStar(require("./local_variable_names"), exports);
135
+ __exportStar(require("./loop_target_misuse"), exports);
133
136
  __exportStar(require("./macro_naming"), exports);
134
137
  __exportStar(require("./main_file_contents"), exports);
135
138
  __exportStar(require("./many_parentheses"), exports);
@@ -205,16 +208,19 @@ __exportStar(require("./single_use_expression_temp"), exports);
205
208
  __exportStar(require("./slow_parameter_passing"), exports);
206
209
  __exportStar(require("./smim_consistency"), exports);
207
210
  __exportStar(require("./sort_empty_key"), exports);
211
+ __exportStar(require("./sort_required"), exports);
208
212
  __exportStar(require("./space_before_colon"), exports);
209
213
  __exportStar(require("./space_before_dot"), exports);
210
214
  __exportStar(require("./sql_escape_host_variables"), exports);
211
215
  __exportStar(require("./sql_value_conversion"), exports);
216
+ __exportStar(require("./stale_subrc"), exports);
212
217
  __exportStar(require("./start_at_tab"), exports);
213
218
  __exportStar(require("./static_call_via_instance"), exports);
214
219
  __exportStar(require("./strict_sql"), exports);
215
220
  __exportStar(require("./subrc_after_assign"), exports);
216
221
  __exportStar(require("./superclass_final"), exports);
217
222
  __exportStar(require("./superfluous_value"), exports);
223
+ __exportStar(require("./suspicious_loop"), exports);
218
224
  __exportStar(require("./sy_modification"), exports);
219
225
  __exportStar(require("./sy_read_restriction"), exports);
220
226
  __exportStar(require("./tabl_enhancement_category"), exports);
@@ -224,6 +230,7 @@ __exportStar(require("./try_without_catch"), exports);
224
230
  __exportStar(require("./type_form_parameters"), exports);
225
231
  __exportStar(require("./types_naming"), exports);
226
232
  __exportStar(require("./uncaught_exception"), exports);
233
+ __exportStar(require("./unit_test_assertions"), exports);
227
234
  __exportStar(require("./unknown_types"), exports);
228
235
  __exportStar(require("./unnecessary_chaining"), exports);
229
236
  __exportStar(require("./unnecessary_copy"), exports);
@@ -0,0 +1,301 @@
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.LoopTargetMisuse = exports.LoopTargetMisuseConf = void 0;
37
+ const Statements = __importStar(require("../abap/2_statements/statements"));
38
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
39
+ const issue_1 = require("../issue");
40
+ const _basic_rule_config_1 = require("./_basic_rule_config");
41
+ const _abap_rule_1 = require("./_abap_rule");
42
+ const _irule_1 = require("./_irule");
43
+ class LoopTargetMisuseConf extends _basic_rule_config_1.BasicRuleConfig {
44
+ constructor() {
45
+ super(...arguments);
46
+ /** Report a LOOP inside a LOOP with the same target, when the outer loop body uses the target after the inner ENDLOOP */
47
+ this.nestedLoop = true;
48
+ /** Report LOOP AT ... INTO <fs> and READ TABLE ... INTO <fs> when the local field symbol is only bound by ASSIGNING,
49
+ * so the statement overwrites a table row */
50
+ this.intoFieldSymbol = true;
51
+ }
52
+ }
53
+ exports.LoopTargetMisuseConf = LoopTargetMisuseConf;
54
+ class LoopTargetMisuse extends _abap_rule_1.ABAPRule {
55
+ constructor() {
56
+ super(...arguments);
57
+ this.conf = new LoopTargetMisuseConf();
58
+ }
59
+ getMetadata() {
60
+ return {
61
+ key: "loop_target_misuse",
62
+ title: "LOOP and READ TABLE targets used wrongly",
63
+ shortDescription: `Reports a nested LOOP that reuses the target of the enclosing LOOP, and INTO <fs> where ASSIGNING was meant`,
64
+ extendedInformation: `Option nestedLoop: a LOOP inside a LOOP with the same INTO, REFERENCE INTO or ASSIGNING target.
65
+ After the inner ENDLOOP, the target holds the last row of the inner table, not the row of the
66
+ outer loop. Reported when the outer loop body uses the target after the inner ENDLOOP, before
67
+ it is assigned again.
68
+ Not reported: a target of a loop that has already ended, a target that is not a plain variable
69
+ or field symbol, an enclosing LOOP with GROUP BY, and a target the outer loop body does not use
70
+ after the inner loop.
71
+
72
+ Option intoFieldSymbol: LOOP AT ... INTO <fs> or READ TABLE ... INTO <fs>. INTO copies the row
73
+ into the data object the field symbol points to. When <fs> was bound by ASSIGNING, that data
74
+ object is a row of a table, and the statement overwrites it. ASSIGNING was meant.
75
+ 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.`,
77
+ tags: [_irule_1.RuleTag.SingleFile],
78
+ badExample: `LOOP AT lt_header INTO ls_row.
79
+ LOOP AT lt_items INTO ls_row WHERE parent = ls_row-id.
80
+ lv_sum = lv_sum + ls_row-amount.
81
+ ENDLOOP.
82
+ ls_row-amount = lv_sum.
83
+ APPEND ls_row TO lt_result.
84
+ ENDLOOP.
85
+
86
+ LOOP AT lt_a ASSIGNING <ls_a>.
87
+ ENDLOOP.
88
+ READ TABLE lt_a INTO <ls_a> INDEX 1.`,
89
+ goodExample: `LOOP AT lt_header INTO ls_row.
90
+ LOOP AT lt_items INTO ls_item WHERE parent = ls_row-id.
91
+ lv_sum = lv_sum + ls_item-amount.
92
+ ENDLOOP.
93
+ ls_row-amount = lv_sum.
94
+ APPEND ls_row TO lt_result.
95
+ ENDLOOP.
96
+
97
+ LOOP AT lt_a ASSIGNING <ls_a>.
98
+ ENDLOOP.
99
+ READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
100
+ };
101
+ }
102
+ getConfig() {
103
+ return this.conf;
104
+ }
105
+ setConfig(conf) {
106
+ this.conf = conf;
107
+ }
108
+ runParsed(file) {
109
+ const issues = [];
110
+ const statements = file.getStatements();
111
+ if (this.conf.nestedLoop !== false) {
112
+ issues.push(...this.checkNestedLoop(file, statements));
113
+ }
114
+ if (this.conf.intoFieldSymbol !== false) {
115
+ issues.push(...this.checkIntoFieldSymbol(file, statements));
116
+ }
117
+ return issues;
118
+ }
119
+ ////////////////
120
+ checkNestedLoop(file, statements) {
121
+ const issues = [];
122
+ // one entry per open LOOP, key undefined when the LOOP has no target to compare
123
+ let open = [];
124
+ for (let i = 0; i < statements.length; i++) {
125
+ const statement = statements[i];
126
+ const s = statement.get();
127
+ if (this.isBlockBoundary(statement)) {
128
+ open = [];
129
+ }
130
+ else if (s instanceof Statements.EndLoop) {
131
+ open.pop();
132
+ }
133
+ else if (s instanceof Statements.Loop) {
134
+ const node = this.loopTarget(statement);
135
+ const target = node ? this.targetName(node) : undefined;
136
+ // INTO <fs> inside LOOP ... ASSIGNING <fs> is left to the option intoFieldSymbol
137
+ const key = target === undefined ? undefined : ((node === null || node === void 0 ? void 0 : node.get()) instanceof Expressions.Target ? "INTO " : "ASSIGNING ") + target;
138
+ const outer = key === undefined ? undefined : [...open].reverse().find(o => o.key === key);
139
+ if (node !== undefined && target !== undefined && outer !== undefined) {
140
+ const innerEnd = this.findEndLoop(statements, i);
141
+ const outerEnd = this.findEndLoop(statements, outer.index);
142
+ if (innerEnd !== undefined && outerEnd !== undefined && this.usedBetween(statements, innerEnd + 1, outerEnd, target)) {
143
+ const name = node.concatTokens();
144
+ const message = `Inner LOOP reuses ${name}, the target of the enclosing LOOP, `
145
+ + `which is used after the inner ENDLOOP and then holds a row of the inner table`;
146
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
147
+ }
148
+ }
149
+ const grouped = statement.findDirectExpression(Expressions.LoopGroupBy) !== undefined;
150
+ open.push({ index: i, key: grouped ? undefined : key });
151
+ }
152
+ }
153
+ return issues;
154
+ }
155
+ /** the Target or FSTarget after INTO, REFERENCE INTO or ASSIGNING of a LOOP */
156
+ loopTarget(statement) {
157
+ const target = statement.findDirectExpression(Expressions.LoopTarget);
158
+ return (target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.Target)) || (target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.FSTarget));
159
+ }
160
+ /** upper case name of a Target or FSTarget that is a plain variable or field symbol, also declared inline */
161
+ targetName(node) {
162
+ var _a, _b;
163
+ const inline = ((_a = node.findDirectExpression(Expressions.InlineData)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.TargetField))
164
+ || ((_b = node.findDirectExpression(Expressions.InlineFS)) === null || _b === void 0 ? void 0 : _b.findDirectExpression(Expressions.TargetFieldSymbol));
165
+ const tokens = (inline || node).getAllTokens();
166
+ return tokens.length === 1 ? tokens[0].getStr().toUpperCase() : undefined;
167
+ }
168
+ /** index of the ENDLOOP that closes the LOOP at the index */
169
+ findEndLoop(statements, index) {
170
+ let depth = 0;
171
+ for (let i = index + 1; i < statements.length && this.isBlockBoundary(statements[i]) === false; i++) {
172
+ const s = statements[i].get();
173
+ if (s instanceof Statements.Loop) {
174
+ depth++;
175
+ }
176
+ else if (s instanceof Statements.EndLoop) {
177
+ if (depth === 0) {
178
+ return i;
179
+ }
180
+ depth--;
181
+ }
182
+ }
183
+ return undefined;
184
+ }
185
+ /** true when a statement in [from, to) uses the variable before a statement assigns all of it */
186
+ usedBetween(statements, from, to, name) {
187
+ for (let i = from; i < to; i++) {
188
+ const statement = statements[i];
189
+ const s = statement.get();
190
+ if (s instanceof Statements.Data || s instanceof Statements.FieldSymbol) {
191
+ continue;
192
+ }
193
+ const whole = new Set();
194
+ for (const t of [...statement.findAllExpressions(Expressions.Target), ...statement.findAllExpressions(Expressions.FSTarget)]) {
195
+ if (this.targetName(t) === name) {
196
+ t.getAllTokens().forEach(token => whole.add(token));
197
+ }
198
+ }
199
+ const tokens = statement.getTokens();
200
+ for (let j = 0; j < tokens.length; j++) {
201
+ const previous = j > 0 ? tokens[j - 1].getStr() : "";
202
+ if (tokens[j].getStr().toUpperCase() === name
203
+ && whole.has(tokens[j]) === false
204
+ && ["-", "->", "=>", "~"].includes(previous) === false) {
205
+ return s instanceof Statements.Unassign ? false : true;
206
+ }
207
+ }
208
+ if (whole.size > 0) {
209
+ return false;
210
+ }
211
+ }
212
+ return false;
213
+ }
214
+ checkIntoFieldSymbol(file, statements) {
215
+ const issues = [];
216
+ for (let i = 0; i < statements.length; i++) {
217
+ const statement = statements[i];
218
+ const s = statement.get();
219
+ if (!(s instanceof Statements.Loop) && !(s instanceof Statements.ReadTable)) {
220
+ continue;
221
+ }
222
+ const target = statement.findDirectExpression(Expressions.LoopTarget)
223
+ || statement.findDirectExpression(Expressions.ReadTableTarget);
224
+ const into = target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.Target);
225
+ const fs = into === null || into === void 0 ? void 0 : into.findDirectExpression(Expressions.TargetFieldSymbol);
226
+ if (into === undefined || fs === undefined || fs.concatTokens() !== into.concatTokens()) {
227
+ continue;
228
+ }
229
+ const name = fs.concatTokens().toUpperCase();
230
+ if (this.onlyBoundToRows(statements, i, name) === false) {
231
+ continue;
232
+ }
233
+ const what = s instanceof Statements.Loop ? "LOOP AT" : "READ TABLE";
234
+ const message = `${what} ... INTO ${fs.concatTokens()} overwrites the row the field symbol points to, use ASSIGNING`;
235
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
236
+ }
237
+ return issues;
238
+ }
239
+ /** true when the field symbol is declared in the block around the statement and every binding of it
240
+ * in that block is an ASSIGNING, so it can only point to a table row */
241
+ onlyBoundToRows(statements, index, name) {
242
+ var _a;
243
+ let start = index;
244
+ while (start > 0 && this.isBlockBoundary(statements[start]) === false) {
245
+ start--;
246
+ }
247
+ if (this.startsBlock(statements[start]) === false) {
248
+ return false;
249
+ }
250
+ let declared = false;
251
+ let assigning = false;
252
+ for (let i = start + 1; i < statements.length && this.endsBlock(statements[i]) === false; i++) {
253
+ const statement = statements[i];
254
+ if (statement.get() instanceof Statements.FieldSymbol) {
255
+ if (((_a = statement.findDirectExpression(Expressions.FieldSymbol)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase()) === name) {
256
+ if (statement.findDirectTokenByText("DEFAULT") !== undefined) {
257
+ return false;
258
+ }
259
+ declared = true;
260
+ }
261
+ continue;
262
+ }
263
+ for (const fsTarget of statement.findAllExpressions(Expressions.FSTarget)) {
264
+ if (this.fsName(fsTarget) !== name) {
265
+ continue;
266
+ }
267
+ if (statement.get() instanceof Statements.Assign) {
268
+ return false;
269
+ }
270
+ assigning = true;
271
+ if (fsTarget.findDirectExpression(Expressions.InlineFS) !== undefined) {
272
+ declared = true;
273
+ }
274
+ }
275
+ }
276
+ return declared && assigning;
277
+ }
278
+ fsName(fsTarget) {
279
+ var _a;
280
+ return (_a = fsTarget.findFirstExpression(Expressions.FieldSymbol)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase();
281
+ }
282
+ isBlockBoundary(statement) {
283
+ return this.startsBlock(statement) || this.endsBlock(statement);
284
+ }
285
+ startsBlock(statement) {
286
+ const s = statement.get();
287
+ return s instanceof Statements.MethodImplementation
288
+ || s instanceof Statements.Form
289
+ || s instanceof Statements.FunctionModule
290
+ || s instanceof Statements.Module;
291
+ }
292
+ endsBlock(statement) {
293
+ const s = statement.get();
294
+ return s instanceof Statements.EndMethod
295
+ || s instanceof Statements.EndForm
296
+ || s instanceof Statements.EndFunction
297
+ || s instanceof Statements.EndModule;
298
+ }
299
+ }
300
+ exports.LoopTargetMisuse = LoopTargetMisuse;
301
+ //# sourceMappingURL=loop_target_misuse.js.map