@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.
@@ -0,0 +1,333 @@
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.SuspiciousLoop = exports.SuspiciousLoopConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
39
+ const Structures = __importStar(require("../abap/3_structures/structures"));
40
+ const nodes_1 = require("../abap/nodes");
41
+ const _reference_1 = require("../abap/5_syntax/_reference");
42
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
43
+ const issue_1 = require("../issue");
44
+ const _basic_rule_config_1 = require("./_basic_rule_config");
45
+ const severity_1 = require("../severity");
46
+ const _procedure_analysis_1 = require("./_procedure_analysis");
47
+ class SuspiciousLoopConf extends _basic_rule_config_1.BasicRuleConfig {
48
+ constructor() {
49
+ super(...arguments);
50
+ this.severity = severity_1.Severity.Warning;
51
+ /** Report WHILE loops whose condition the loop body never changes, or sets to the same value in every iteration.
52
+ * The loop runs zero times, once or forever */
53
+ this.unchangedCondition = true;
54
+ }
55
+ }
56
+ exports.SuspiciousLoopConf = SuspiciousLoopConf;
57
+ const NESTED_LOOPS = [Structures.Loop, Structures.Do, Structures.While, Structures.Select,
58
+ Structures.Provide, Structures.LoopAtScreen, Structures.LoopExtract];
59
+ /** statements that leave the loop, or may leave it, wherever they are in the loop body */
60
+ const LEAVING = [Statements.Return, Statements.Raise, Statements.Message, Statements.Leave,
61
+ Statements.Submit, Statements.Stop, Statements.Reject];
62
+ /** statements which run no other code than the method calls they contain */
63
+ const CALLING = [Statements.Call, Statements.Move, Statements.If, Statements.ElseIf,
64
+ Statements.Case, Statements.When, Statements.Append, Statements.InsertInternal, Statements.Concatenate];
65
+ class SuspiciousLoop {
66
+ constructor() {
67
+ this.conf = new SuspiciousLoopConf();
68
+ }
69
+ getMetadata() {
70
+ return {
71
+ key: "suspicious_loop",
72
+ title: "Suspicious loop",
73
+ shortDescription: `Finds WHILE loops whose condition the loop body never changes`,
74
+ extendedInformation: `Option unchangedCondition: reports a WHILE loop when the loop body cannot change its condition
75
+ after the first iteration. Then the loop runs zero times, once or forever.
76
+
77
+ The condition may read local variables of the METHOD, FORM or FUNCTION, constants, builtin functions
78
+ like lines( ), and attributes and parameters without ->. Each local variable in it is either
79
+ * not written in the loop body, or
80
+ * only written by var = source. directly in the loop body, not in a nested block, where the source is
81
+ the same in every iteration: no method calls, field symbols or ->*, and it only reads constants,
82
+ local variables that the loop body does not write, and other data that the loop body does not write.
83
+ Attributes and parameters in the condition must not be written in the loop body either.
84
+
85
+ When the condition or a source reads data that is not local, the loop body must not call methods,
86
+ functions or forms, except instance methods via local reference variables, and must not write via
87
+ field symbols or ->*. A source that reads local->attribute also needs that the loop body calls no
88
+ method via that local.
89
+
90
+ Not reported: conditions with system fields like sy-subrc, field symbols, -> or method calls, local
91
+ variables whose address is taken, loop bodies with EXIT, RETURN, RAISE, MESSAGE, LEAVE, SUBMIT, STOP or
92
+ REJECT, and, when the condition variables are written, loop bodies with CONTINUE or CHECK.
93
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
94
+ INCLUDE, test seams, enhancements or native SQL.`,
95
+ tags: [],
96
+ badExample: `DATA(lo_x) = previous.
97
+ WHILE lo_x IS BOUND.
98
+ result = result && lo_x->get_text( ).
99
+ lo_x = previous->previous.
100
+ ENDWHILE.`,
101
+ goodExample: `DATA(lo_x) = previous.
102
+ WHILE lo_x IS BOUND.
103
+ result = result && lo_x->get_text( ).
104
+ lo_x = lo_x->previous.
105
+ ENDWHILE.`,
106
+ };
107
+ }
108
+ getConfig() {
109
+ return this.conf;
110
+ }
111
+ setConfig(conf) {
112
+ this.conf = conf;
113
+ }
114
+ initialize(reg) {
115
+ this.reg = reg;
116
+ return this;
117
+ }
118
+ run(obj) {
119
+ if (this.conf.unchangedCondition === false) {
120
+ return [];
121
+ }
122
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
123
+ if (analysis === undefined) {
124
+ return [];
125
+ }
126
+ const issues = [];
127
+ for (const procedure of analysis.procedures) {
128
+ if (procedure.supported === false) {
129
+ continue;
130
+ }
131
+ for (const loop of procedure.structure.findAllStructuresRecursive(Structures.While)) {
132
+ const issue = this.unchangedCondition(procedure, loop);
133
+ if (issue !== undefined) {
134
+ issues.push(issue);
135
+ }
136
+ }
137
+ }
138
+ return issues;
139
+ }
140
+ ////////////////
141
+ unchangedCondition(procedure, loop) {
142
+ const statement = loop.findDirectStatement(Statements.While);
143
+ const end = loop.findDirectStatement(Statements.EndWhile);
144
+ const cond = statement === null || statement === void 0 ? void 0 : statement.findDirectExpression(Expressions.Cond);
145
+ if (statement === undefined || end === undefined || cond === undefined
146
+ || statement.findDirectTokenByText("VARY") !== undefined
147
+ || cond.getAllTokens().some(t => t.getStr() === "->")) {
148
+ return undefined;
149
+ }
150
+ const reads = this.reads(procedure, cond);
151
+ if (reads === undefined || reads.locals.length + reads.others.length === 0) {
152
+ return undefined;
153
+ }
154
+ const from = procedure.index(statement);
155
+ const to = procedure.index(end);
156
+ const body = loop.findDirectStructure(Structures.Body);
157
+ let continues = false;
158
+ for (const found of body === undefined ? [] : this.loopControl(body, false)) {
159
+ const type = found.get();
160
+ if (type instanceof Statements.Continue || type instanceof Statements.Check) {
161
+ continues = true;
162
+ }
163
+ else {
164
+ return undefined;
165
+ }
166
+ }
167
+ if (reads.others.some(o => this.writtenIn(procedure, o, from, to))
168
+ || (reads.others.length > 0 && this.mayChangeOtherData(procedure, from, to, []))) {
169
+ return undefined;
170
+ }
171
+ let written = undefined;
172
+ for (const variable of reads.locals) {
173
+ for (const write of variable.writes()) {
174
+ if (write.index <= from || write.index >= to) {
175
+ continue;
176
+ }
177
+ else if (continues || body === undefined
178
+ || this.sameValueWrite(procedure, write.statement, write.token, body, from, to) === false) {
179
+ return undefined;
180
+ }
181
+ written = variable.identifier.getName();
182
+ }
183
+ }
184
+ const names = [...reads.locals.map(v => v.identifier.getName()), ...reads.others.map(o => o.getName())].join(", ");
185
+ const message = written === undefined
186
+ ? `WHILE condition never changes, the loop body does not write ${names}, the loop runs zero times or forever`
187
+ : `WHILE condition does not change after the first iteration, the loop body sets ${written} `
188
+ + `to the same value in every iteration, the loop runs once or forever`;
189
+ return issue_1.Issue.atStatement(procedure.file, statement, message, this.getMetadata().key, this.conf.severity);
190
+ }
191
+ /** data read by the condition or source, undefined if it reads anything that can change unseen */
192
+ reads(procedure, node) {
193
+ var _a;
194
+ if (node.findFirstExpression(Expressions.SourceFieldSymbol) !== undefined
195
+ || node.findFirstExpression(Expressions.Dereference) !== undefined
196
+ || node.findFirstExpression(Expressions.NewObject) !== undefined) {
197
+ return undefined;
198
+ }
199
+ for (const call of node.findAllExpressionsRecursive(Expressions.MethodCall)) {
200
+ if (this.isBuiltinCall(procedure, call) === false) {
201
+ return undefined;
202
+ }
203
+ }
204
+ const ret = { locals: [], others: [], receivers: [] };
205
+ const tokens = node.getAllTokens();
206
+ for (const field of [...node.findAllExpressionsRecursive(Expressions.SourceField),
207
+ ...node.findAllExpressionsRecursive(Expressions.AttributeName)]) {
208
+ const token = field.getFirstToken();
209
+ const identifier = procedure.identifierAt(token);
210
+ if ((0, _procedure_analysis_1.isImmutable)(identifier)) {
211
+ continue;
212
+ }
213
+ const local = procedure.local(identifier);
214
+ if (local !== undefined) {
215
+ if (local.escapes()) {
216
+ return undefined;
217
+ }
218
+ if (ret.locals.includes(local) === false) {
219
+ ret.locals.push(local);
220
+ }
221
+ const index = tokens.findIndex(t => t.getStart().equals(token.getStart()));
222
+ if (((_a = tokens[index + 1]) === null || _a === void 0 ? void 0 : _a.getStr()) === "->") {
223
+ ret.receivers.push(local);
224
+ }
225
+ }
226
+ else if (!(identifier instanceof _typed_identifier_1.TypedIdentifier)
227
+ || identifier.getMeta().includes("built-in" /* IdentifierMeta.BuiltIn */)) {
228
+ // unknown, or system fields like sy-subrc, which change without a write reference
229
+ return undefined;
230
+ }
231
+ else if (ret.others.includes(identifier) === false) {
232
+ ret.others.push(identifier);
233
+ }
234
+ }
235
+ return ret;
236
+ }
237
+ isBuiltinCall(procedure, call) {
238
+ var _a;
239
+ const name = (_a = call.findDirectExpression(Expressions.MethodName)) === null || _a === void 0 ? void 0 : _a.getFirstToken();
240
+ if (name === undefined) {
241
+ return false;
242
+ }
243
+ const references = procedure.analysis.referencesAt(procedure.file.getFilename(), name);
244
+ return references.length > 0 && references.every(r => r.referenceType === _reference_1.ReferenceType.BuiltinMethodReference);
245
+ }
246
+ /** statements in the loop body that leave the loop or go to its next iteration */
247
+ loopControl(node, nested) {
248
+ const ret = [];
249
+ for (const child of node.getChildren()) {
250
+ if (child instanceof nodes_1.StructureNode) {
251
+ ret.push(...this.loopControl(child, nested || NESTED_LOOPS.some(l => child.get() instanceof l)));
252
+ continue;
253
+ }
254
+ const type = child.get();
255
+ if (LEAVING.some(l => type instanceof l)
256
+ || (nested === false && (type instanceof Statements.Exit
257
+ || type instanceof Statements.Continue || type instanceof Statements.Check))) {
258
+ ret.push(child);
259
+ }
260
+ }
261
+ return ret;
262
+ }
263
+ /** var = source. directly in the loop body, the source has the same value in every iteration */
264
+ sameValueWrite(procedure, statement, token, body, from, to) {
265
+ const move = (0, _procedure_analysis_1.plainMove)(statement);
266
+ if (move === undefined || move.operator !== "=" || (0, _procedure_analysis_1.singleTarget)(move.target) !== token
267
+ || procedure.block(statement) !== body || procedure.isSimple(statement) === false) {
268
+ return false;
269
+ }
270
+ const reads = this.reads(procedure, move.source);
271
+ if (reads === undefined
272
+ || reads.locals.some(l => l.writes().some(w => w.index > from && w.index < to))
273
+ || reads.others.some(o => this.writtenIn(procedure, o, from, to))) {
274
+ return false;
275
+ }
276
+ return reads.others.length === 0 || this.mayChangeOtherData(procedure, from, to, reads.receivers) === false;
277
+ }
278
+ writtenIn(procedure, identifier, from, to) {
279
+ for (const reference of procedure.analysis.referencesTo(identifier)) {
280
+ if (reference.referenceType !== _reference_1.ReferenceType.DataWriteReference || !procedure.contains(reference.position)) {
281
+ continue;
282
+ }
283
+ const statement = procedure.statementOf(reference.position.getToken());
284
+ const index = statement === undefined ? -1 : procedure.index(statement);
285
+ if (index > from && index < to) {
286
+ return true;
287
+ }
288
+ }
289
+ return false;
290
+ }
291
+ /** the loop body calls methods, functions or forms, or writes via field symbols or ->*, which can change
292
+ * attributes and other data that is not local; instance methods via local reference variables are accepted,
293
+ * unless the variable is one of the receivers */
294
+ mayChangeOtherData(procedure, from, to, receivers) {
295
+ for (let index = from + 1; index < to; index++) {
296
+ const statement = procedure.statements[index];
297
+ if (statement.findFirstExpression(Expressions.TargetFieldSymbol) !== undefined
298
+ || statement.findFirstExpression(Expressions.Dereference) !== undefined) {
299
+ return true;
300
+ }
301
+ else if (procedure.isCallFree(statement)) {
302
+ continue;
303
+ }
304
+ const type = statement.get();
305
+ if (!CALLING.some(c => type instanceof c) || statement.findFirstExpression(Expressions.NewObject) !== undefined) {
306
+ return true;
307
+ }
308
+ const tokens = procedure.tokensOf(statement);
309
+ const methodSource = statement.findDirectExpression(Expressions.MethodSource);
310
+ if (methodSource !== undefined && this.localReceiver(procedure, tokens, methodSource.getLastToken(), receivers) === false) {
311
+ return true;
312
+ }
313
+ for (const call of statement.findAllExpressionsRecursive(Expressions.MethodCall)) {
314
+ if (this.isBuiltinCall(procedure, call) === false
315
+ && this.localReceiver(procedure, tokens, call.getFirstToken(), receivers) === false) {
316
+ return true;
317
+ }
318
+ }
319
+ }
320
+ return false;
321
+ }
322
+ /** the method name token is preceded by local->, a local reference variable that is not one of the receivers */
323
+ localReceiver(procedure, tokens, name, receivers) {
324
+ const index = tokens.findIndex(t => t.getStart().equals(name.getStart()));
325
+ if (index < 2 || tokens[index - 1].getStr() !== "->") {
326
+ return false;
327
+ }
328
+ const local = procedure.local(procedure.identifierAt(tokens[index - 2]));
329
+ return local !== undefined && receivers.includes(local) === false;
330
+ }
331
+ }
332
+ exports.SuspiciousLoop = SuspiciousLoop;
333
+ //# sourceMappingURL=suspicious_loop.js.map
@@ -0,0 +1,263 @@
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.UnitTestAssertions = exports.UnitTestAssertionsConf = void 0;
37
+ const Statements = __importStar(require("../abap/2_statements/statements"));
38
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
39
+ const Structures = __importStar(require("../abap/3_structures/structures"));
40
+ const issue_1 = require("../issue");
41
+ const _basic_rule_config_1 = require("./_basic_rule_config");
42
+ const _abap_rule_1 = require("./_abap_rule");
43
+ const Tokens = __importStar(require("../abap/1_lexer/tokens"));
44
+ const severity_1 = require("../severity");
45
+ class UnitTestAssertionsConf extends _basic_rule_config_1.BasicRuleConfig {
46
+ constructor() {
47
+ super(...arguments);
48
+ this.severity = severity_1.Severity.Warning;
49
+ /** Report assertions that cannot fail: assert_equals or assert_equals_float with the same operand for act and exp,
50
+ * assert_true with abap_true or 'X', assert_false with abap_false, space or ' ' */
51
+ this.cannotFail = true;
52
+ /** Report the ASSERT statement in FOR TESTING classes */
53
+ this.assertStatement = true;
54
+ }
55
+ }
56
+ exports.UnitTestAssertionsConf = UnitTestAssertionsConf;
57
+ const ASSERT_CLASSES = ["CL_ABAP_UNIT_ASSERT", "CL_AUNIT_ASSERT"];
58
+ class UnitTestAssertions extends _abap_rule_1.ABAPRule {
59
+ constructor() {
60
+ super(...arguments);
61
+ this.conf = new UnitTestAssertionsConf();
62
+ }
63
+ getMetadata() {
64
+ return {
65
+ key: "unit_test_assertions",
66
+ title: "Unit test assertions",
67
+ shortDescription: `Reports unit test assertions that cannot fail, and the ASSERT statement in FOR TESTING classes`,
68
+ extendedInformation: `Only code in FOR TESTING classes is checked. Each check has its own option.
69
+
70
+ cannotFail: an assertion of cl_abap_unit_assert or cl_aunit_assert that passes whatever the code under test does.
71
+ assert_equals and assert_equals_float with the same operand for act and exp, assert_true with abap_true or 'X',
72
+ assert_false with abap_false, space or ' '. Not reported: operands that contain a method call or NEW, as two calls
73
+ can return different values; two different literals or constants; assertions that always fail.
74
+
75
+ assertStatement: the ASSERT statement. A failing ASSERT ends in the runtime error ASSERTION_FAILED, so the test
76
+ run shows a short dump instead of a failed test with act, exp and a message. Use cl_abap_unit_assert instead.
77
+ Not reported: ASSERT in classes that are not FOR TESTING.`,
78
+ tags: [],
79
+ badExample: `CLASS ltcl_test DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
80
+ PRIVATE SECTION.
81
+ DATA mt_rows TYPE string_table.
82
+ METHODS test FOR TESTING.
83
+ ENDCLASS.
84
+
85
+ CLASS ltcl_test IMPLEMENTATION.
86
+ METHOD test.
87
+ DATA(lv_count) = lines( mt_rows ).
88
+ cl_abap_unit_assert=>assert_equals( act = lv_count exp = lv_count ).
89
+ cl_abap_unit_assert=>assert_true( abap_true ).
90
+ ASSERT lv_count = 2.
91
+ ENDMETHOD.
92
+ ENDCLASS.`,
93
+ goodExample: `CLASS ltcl_test DEFINITION FINAL FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
94
+ PRIVATE SECTION.
95
+ DATA mt_rows TYPE string_table.
96
+ METHODS test FOR TESTING.
97
+ ENDCLASS.
98
+
99
+ CLASS ltcl_test IMPLEMENTATION.
100
+ METHOD test.
101
+ DATA(lv_count) = lines( mt_rows ).
102
+ cl_abap_unit_assert=>assert_equals( act = lv_count exp = 2 ).
103
+ cl_abap_unit_assert=>assert_true( xsdbool( lv_count > 0 ) ).
104
+ ENDMETHOD.
105
+ ENDCLASS.`,
106
+ };
107
+ }
108
+ getConfig() {
109
+ return this.conf;
110
+ }
111
+ setConfig(conf) {
112
+ this.conf = conf;
113
+ }
114
+ runParsed(file, obj) {
115
+ var _a, _b;
116
+ const issues = [];
117
+ const structure = file.getStructure();
118
+ if (structure === undefined) {
119
+ return [];
120
+ }
121
+ const testClasses = this.testClassNames(obj);
122
+ if (testClasses.size === 0) {
123
+ return [];
124
+ }
125
+ for (const implementation of structure.findAllStructures(Structures.ClassImplementation)) {
126
+ const name = (_b = (_a = implementation.findFirstStatement(Statements.ClassImplementation)) === null || _a === void 0 ? void 0 : _a.findFirstExpression(Expressions.ClassName)) === null || _b === void 0 ? void 0 : _b.concatTokens().toUpperCase();
127
+ if (name === undefined || testClasses.has(name) === false) {
128
+ continue;
129
+ }
130
+ for (const statement of implementation.findAllStatementNodes()) {
131
+ if (this.conf.assertStatement === true && statement.get() instanceof Statements.Assert) {
132
+ const message = "Use cl_abap_unit_assert instead of ASSERT in a test class";
133
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
134
+ }
135
+ else if (this.conf.cannotFail === true && statement.get() instanceof Statements.Call) {
136
+ const message = this.cannotFail(statement);
137
+ if (message !== undefined) {
138
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
139
+ }
140
+ }
141
+ }
142
+ }
143
+ return issues;
144
+ }
145
+ /** names of the FOR TESTING classes defined in any file of the object, upper case */
146
+ testClassNames(obj) {
147
+ const ret = new Set();
148
+ for (const f of obj.getABAPFiles()) {
149
+ for (const def of f.getInfo().listClassDefinitions()) {
150
+ if (def.isForTesting === true) {
151
+ ret.add(def.name.toUpperCase());
152
+ }
153
+ }
154
+ }
155
+ return ret;
156
+ }
157
+ /** returns the message if the call is an assertion that cannot fail */
158
+ cannotFail(statement) {
159
+ const call = this.assertCall(statement);
160
+ if (call === undefined) {
161
+ return undefined;
162
+ }
163
+ if (call.method === "ASSERT_EQUALS" || call.method === "ASSERT_EQUALS_FLOAT") {
164
+ const act = call.parameters.get("ACT");
165
+ const exp = call.parameters.get("EXP");
166
+ if (act === undefined || exp === undefined || this.hasCall(act) || this.hasCall(exp)) {
167
+ return undefined;
168
+ }
169
+ if (this.normalized(act) === this.normalized(exp)) {
170
+ return "Assertion cannot fail, act and exp are the same";
171
+ }
172
+ }
173
+ else if (call.method === "ASSERT_TRUE" || call.method === "ASSERT_FALSE") {
174
+ const act = call.parameters.get("ACT");
175
+ if (act === undefined) {
176
+ return undefined;
177
+ }
178
+ const value = this.normalized(act);
179
+ if (call.method === "ASSERT_TRUE" && (value === "ABAP_TRUE" || value === "'X'")) {
180
+ return "Assertion cannot fail, act is abap_true";
181
+ }
182
+ else if (call.method === "ASSERT_FALSE"
183
+ && (value === "ABAP_FALSE" || value === "SPACE" || value === "''" || value === "' '")) {
184
+ return "Assertion cannot fail, act is abap_false";
185
+ }
186
+ }
187
+ return undefined;
188
+ }
189
+ /** class method call of cl_abap_unit_assert or cl_aunit_assert: method name and the sources by parameter name, upper case */
190
+ assertCall(statement) {
191
+ var _a, _b;
192
+ let className;
193
+ let methodName;
194
+ let parameters;
195
+ const chain = statement.findDirectExpression(Expressions.MethodCallChain);
196
+ const source = statement.findDirectExpression(Expressions.MethodSource);
197
+ if (chain !== undefined) {
198
+ const children = chain.getChildren();
199
+ const methodCall = chain.findDirectExpression(Expressions.MethodCall);
200
+ if (children.length !== 3 || methodCall === undefined) {
201
+ return undefined;
202
+ }
203
+ className = chain.findDirectExpression(Expressions.ClassName);
204
+ methodName = methodCall.findDirectExpression(Expressions.MethodName);
205
+ parameters = methodCall.findDirectExpression(Expressions.MethodCallParam);
206
+ }
207
+ else if (source !== undefined) {
208
+ const children = source.getChildren();
209
+ if (children.length !== 3) {
210
+ return undefined;
211
+ }
212
+ className = source.findDirectExpression(Expressions.ClassName);
213
+ methodName = source.findDirectExpression(Expressions.AttributeName);
214
+ parameters = statement.findDirectExpression(Expressions.MethodCallBody);
215
+ }
216
+ if (className === undefined
217
+ || methodName === undefined
218
+ || parameters === undefined
219
+ || ASSERT_CLASSES.includes(className.concatTokens().toUpperCase()) === false) {
220
+ return undefined;
221
+ }
222
+ // CALL METHOD x( ... ) and the functional call have a MethodCallParam, CALL METHOD x EXPORTING ... has not
223
+ const param = parameters.get() instanceof Expressions.MethodCallParam
224
+ ? parameters : parameters.findDirectExpression(Expressions.MethodCallParam);
225
+ const list = (param === null || param === void 0 ? void 0 : param.findDirectExpression(Expressions.ParameterListS))
226
+ || ((_a = (param || parameters).findDirectExpression(Expressions.MethodParameters)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.ParameterListS));
227
+ const map = new Map();
228
+ const single = param === null || param === void 0 ? void 0 : param.findDirectExpression(Expressions.Source);
229
+ if (single !== undefined) {
230
+ // only the preferred parameter is given, for the assertions checked here it is ACT
231
+ map.set("ACT", single);
232
+ }
233
+ for (const parameter of (list === null || list === void 0 ? void 0 : list.findDirectExpressions(Expressions.ParameterS)) || []) {
234
+ const name = (_b = parameter.findDirectExpression(Expressions.ParameterName)) === null || _b === void 0 ? void 0 : _b.concatTokens().toUpperCase();
235
+ const value = parameter.findDirectExpression(Expressions.Source);
236
+ if (name !== undefined && value !== undefined) {
237
+ map.set(name, value);
238
+ }
239
+ }
240
+ return { method: methodName.concatTokens().toUpperCase(), parameters: map };
241
+ }
242
+ /** two evaluations of the source can give different values */
243
+ hasCall(source) {
244
+ return source.findFirstExpression(Expressions.MethodCallChain) !== undefined
245
+ || source.findFirstExpression(Expressions.MethodCall) !== undefined
246
+ || source.findFirstExpression(Expressions.NewObject) !== undefined;
247
+ }
248
+ /** the tokens of the source, upper case outside of literals */
249
+ normalized(source) {
250
+ return source.getAllTokens().map(token => {
251
+ if (token instanceof Tokens.StringToken
252
+ || token instanceof Tokens.StringTemplate
253
+ || token instanceof Tokens.StringTemplateBegin
254
+ || token instanceof Tokens.StringTemplateMiddle
255
+ || token instanceof Tokens.StringTemplateEnd) {
256
+ return token.getStr();
257
+ }
258
+ return token.getStr().toUpperCase();
259
+ }).join(" ");
260
+ }
261
+ }
262
+ exports.UnitTestAssertions = UnitTestAssertions;
263
+ //# sourceMappingURL=unit_test_assertions.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abaplint/core",
3
- "version": "2.120.108",
3
+ "version": "2.120.110",
4
4
  "description": "abaplint - Core API",
5
5
  "main": "build/src/index.js",
6
6
  "typings": "build/abaplint.d.ts",
@@ -58,7 +58,7 @@
58
58
  "@types/node": "^24.13.3",
59
59
  "c8": "^12.0.0",
60
60
  "chai": "^4.5.0",
61
- "eslint": "^9.39.5",
61
+ "eslint": "^10.15.0",
62
62
  "mocha": "^12.0.3",
63
63
  "rimraf": "^6.1.4",
64
64
  "source-map-support": "^0.5.21",