@abaplint/core 2.120.107 → 2.120.109

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,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
@@ -41,6 +41,7 @@ const _abap_object_1 = require("../objects/_abap_object");
41
41
  const _basic_rule_config_1 = require("./_basic_rule_config");
42
42
  const fast_xml_parser_1 = require("fast-xml-parser");
43
43
  const severity_1 = require("../severity");
44
+ const xml_utils_1 = require("../xml_utils");
44
45
  const ABAPGIT_ATTRIBUTES = [
45
46
  ["version", /<abapGit\b[^>]*\bversion="([^"]+)"/],
46
47
  ["serializer_version", /<abapGit\b[^>]*\bserializer_version="([^"]+)"/],
@@ -73,6 +74,7 @@ class XMLConsistency {
73
74
  * QUAN fields in TABL objects have reference table and field values
74
75
  * Transparent TABL objects have delivery class and size category set
75
76
  * TABL fields with a built-in type carry the INTTYPE abapGit serializes for it, for the confirmed pairs
77
+ * Documentation lines(TDLINE) in CLAS and INTF objects are max 72 characters, longer lines are cut by the system
76
78
  * Lock parameter names in ENQU objects are max 16 characters
77
79
  * DEVC objects are serialized as package.devc.xml
78
80
  * Texts and translations do not exceed maximum allowed length.`,
@@ -160,6 +162,27 @@ class XMLConsistency {
160
162
  issues.push(issue_1.Issue.atRow(file, 1, "Class matching XML name not found in ABAP file", this.getMetadata().key, this.conf.severity));
161
163
  }
162
164
  issues.push(...this.checkClearedFields(obj, file, "VSEOCLASS", XMLConsistency.clearedClassFields));
165
+ issues.push(...this.checkDocumentationLines(obj, file));
166
+ return issues;
167
+ }
168
+ // the documentation editor stores lines of max 72 characters, longer TDLINEs are cut when deserialized
169
+ checkDocumentationLines(obj, file) {
170
+ const maxLength = 72;
171
+ const issues = [];
172
+ const xml = obj.getXML();
173
+ if (xml === undefined) {
174
+ return issues;
175
+ }
176
+ const regex = /<TDLINE>([^<]*)<\/TDLINE>/g;
177
+ let match;
178
+ while ((match = regex.exec(xml)) !== null) {
179
+ const text = (0, xml_utils_1.unescape)(match[1]);
180
+ if (text.length > maxLength) {
181
+ const row = xml.substring(0, match.index).split("\n").length;
182
+ const message = `Documentation line exceeds maximum length of ${maxLength} characters (actual: ${text.length}), "${text}"`;
183
+ issues.push(issue_1.Issue.atRow(file, row, message, this.getMetadata().key, this.conf.textAndTranslationLengthSeverity));
184
+ }
185
+ }
163
186
  return issues;
164
187
  }
165
188
  checkClearedFields(obj, file, structure, fields) {
@@ -221,6 +244,7 @@ class XMLConsistency {
221
244
  issues.push(issue_1.Issue.atRow(file, 1, "Interface matching XML name not found in ABAP file", this.getMetadata().key, this.conf.severity));
222
245
  }
223
246
  issues.push(...this.checkClearedFields(obj, file, "VSEOINTERF", XMLConsistency.clearedInterfaceFields));
247
+ issues.push(...this.checkDocumentationLines(obj, file));
224
248
  return issues;
225
249
  }
226
250
  checkTextLength(file, fieldName, text, maxLength, lang) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abaplint/core",
3
- "version": "2.120.107",
3
+ "version": "2.120.109",
4
4
  "description": "abaplint - Core API",
5
5
  "main": "build/src/index.js",
6
6
  "typings": "build/abaplint.d.ts",