@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,318 @@
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.SequentialReadSorted = exports.SequentialReadSortedConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
39
+ const _statement_1 = require("../abap/2_statements/statements/_statement");
40
+ const _basic_rule_config_1 = require("./_basic_rule_config");
41
+ const issue_1 = require("../issue");
42
+ const _irule_1 = require("./_irule");
43
+ const syntax_1 = require("../abap/5_syntax/syntax");
44
+ const _key_compare_1 = require("../abap/5_syntax/_key_compare");
45
+ const _abap_object_1 = require("../objects/_abap_object");
46
+ const basic_1 = require("../abap/types/basic");
47
+ const nodes_1 = require("../abap/nodes");
48
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
49
+ const tokens_1 = require("../abap/1_lexer/tokens");
50
+ const severity_1 = require("../severity");
51
+ class SequentialReadSortedConf extends _basic_rule_config_1.BasicRuleConfig {
52
+ constructor() {
53
+ super(...arguments);
54
+ this.severity = severity_1.Severity.Warning;
55
+ }
56
+ }
57
+ exports.SequentialReadSortedConf = SequentialReadSortedConf;
58
+ class SequentialReadSorted {
59
+ constructor() {
60
+ this.conf = new SequentialReadSortedConf();
61
+ }
62
+ getMetadata() {
63
+ return {
64
+ key: "sequential_read_sorted",
65
+ title: "Sequential read on a sorted table",
66
+ shortDescription: `A WHERE condition or a free key on a sorted table that does not use the primary key`,
67
+ extendedInformation: `A sorted table is read with a binary search only when the condition fixes an initial part of
68
+ its key. When the first key component is not compared with = or EQ (or IS INITIAL) in a part of the condition joined
69
+ by AND, every line is read, like on a standard table. The SAP check "Low Performance Operations on Internal Tables"
70
+ reports this as "Possible sequential read on a sorted table".
71
+
72
+ Checked: LOOP AT ... WHERE, DELETE ... WHERE, MODIFY ... WHERE, READ TABLE ... WITH KEY and table expressions
73
+ itab[ comp = ... ], on a table whose type is statically known as SORTED with explicit key components, using the
74
+ primary key.
75
+
76
+ Not checked: standard, hashed, generic and unknown table types, tables with a default key, a dynamic WHERE or
77
+ dynamic components, WITH TABLE KEY, a named key (USING KEY, KEY ... COMPONENTS) other than primary_key,
78
+ READ TABLE ... BINARY SEARCH and access by index, including LOOP AT and DELETE with FROM or TO.
79
+
80
+ Fix: compare the first key component with =, change the key or the table type, or add a secondary key.
81
+ If the sequential read is intended, add the pseudo comment "#EC CI_SORTSEQ to the statement.`,
82
+ tags: [_irule_1.RuleTag.SingleFile, _irule_1.RuleTag.Performance],
83
+ pseudoComment: "EC CI_SORTSEQ",
84
+ badExample: `DATA lt_nodes TYPE SORTED TABLE OF ty_node WITH UNIQUE KEY path name.
85
+ LOOP AT lt_nodes INTO ls_node WHERE name = lv_name.
86
+ ENDLOOP.
87
+ DELETE lt_nodes WHERE path CP lv_pattern.`,
88
+ goodExample: `DATA lt_nodes TYPE SORTED TABLE OF ty_node WITH UNIQUE KEY path name.
89
+ LOOP AT lt_nodes INTO ls_node WHERE path = lv_path AND name = lv_name.
90
+ ENDLOOP.
91
+ DELETE lt_nodes WHERE path CP lv_pattern. "#EC CI_SORTSEQ`,
92
+ };
93
+ }
94
+ getConfig() {
95
+ return this.conf;
96
+ }
97
+ initialize(reg) {
98
+ this.reg = reg;
99
+ return this;
100
+ }
101
+ setConfig(conf) {
102
+ this.conf = conf;
103
+ }
104
+ run(obj) {
105
+ if (!(obj instanceof _abap_object_1.ABAPObject) || obj.getType() === "INTF") {
106
+ return [];
107
+ }
108
+ const issues = [];
109
+ let spaghetti = undefined;
110
+ for (const file of obj.getABAPFiles()) {
111
+ const statements = file.getStatements();
112
+ const pseudoRows = this.findPseudoCommentRows(statements);
113
+ for (const statement of statements) {
114
+ const get = statement.get();
115
+ if (get instanceof _statement_1.Comment) {
116
+ continue;
117
+ }
118
+ const isWhere = get instanceof Statements.Loop
119
+ || get instanceof Statements.DeleteInternal
120
+ || get instanceof Statements.ModifyInternal;
121
+ const isRead = get instanceof Statements.ReadTable;
122
+ if (isWhere === false
123
+ && isRead === false
124
+ && statement.findFirstExpression(Expressions.TableExpression) === undefined) {
125
+ continue;
126
+ }
127
+ if (this.hasPseudoComment(statement, pseudoRows)) {
128
+ continue;
129
+ }
130
+ if (spaghetti === undefined) {
131
+ spaghetti = new syntax_1.SyntaxLogic(this.reg, obj).run().spaghetti;
132
+ }
133
+ if (isWhere === true) {
134
+ this.checkWhere(statement, spaghetti, file, issues);
135
+ }
136
+ else if (isRead === true) {
137
+ this.checkReadTable(statement, spaghetti, file, issues);
138
+ }
139
+ this.checkTableExpressions(statement, statement, spaghetti, file, issues);
140
+ }
141
+ }
142
+ return issues;
143
+ }
144
+ ////////////////
145
+ findPseudoCommentRows(statements) {
146
+ const rows = new Set();
147
+ for (const s of statements) {
148
+ if (s.get() instanceof _statement_1.Comment && s.concatTokens().toUpperCase().includes(this.getMetadata().pseudoComment + "")) {
149
+ rows.add(s.getStart().getRow());
150
+ }
151
+ }
152
+ return rows;
153
+ }
154
+ /** the pseudo comment is accepted on any row of the statement */
155
+ hasPseudoComment(statement, pseudoRows) {
156
+ for (let row = statement.getStart().getRow(); row <= statement.getEnd().getRow(); row++) {
157
+ if (pseudoRows.has(row)) {
158
+ return true;
159
+ }
160
+ }
161
+ return false;
162
+ }
163
+ /** LOOP AT, DELETE and MODIFY with a static WHERE condition */
164
+ checkWhere(statement, spaghetti, file, issues) {
165
+ var _a;
166
+ const cond = statement.findDirectExpression(Expressions.ComponentCond);
167
+ if (cond === undefined || this.usesPrimaryKey(statement) === false) {
168
+ return;
169
+ }
170
+ let table;
171
+ if (statement.get() instanceof Statements.Loop) {
172
+ table = (_a = statement.findDirectExpression(Expressions.LoopSource)) === null || _a === void 0 ? void 0 : _a.getFirstChild();
173
+ }
174
+ else {
175
+ table = statement.findDirectExpression(Expressions.Target);
176
+ }
177
+ if (table === undefined) {
178
+ return;
179
+ }
180
+ if (!(statement.get() instanceof Statements.ModifyInternal)
181
+ && (statement.findDirectTokenByText("FROM") !== undefined || statement.findDirectTokenByText("TO") !== undefined)) {
182
+ // an index range
183
+ return;
184
+ }
185
+ const first = this.firstKeyComponent(this.findType(table, spaghetti, file));
186
+ if (first !== undefined && (0, _key_compare_1.hasKeyCompare)(cond, first) === false) {
187
+ this.report(statement, table.concatTokens(), first, file, issues);
188
+ }
189
+ }
190
+ checkReadTable(statement, spaghetti, file, issues) {
191
+ var _a;
192
+ if (statement.findTokenSequencePosition("WITH", "KEY") === undefined
193
+ || statement.findDirectTokenByText("COMPONENTS") !== undefined
194
+ || statement.findDirectTokenByText("BINARY") !== undefined) {
195
+ return;
196
+ }
197
+ const compare = statement.findDirectExpression(Expressions.ComponentCompareSimple);
198
+ const table = (_a = statement.findDirectExpression(Expressions.SimpleSource2)) !== null && _a !== void 0 ? _a : statement.findDirectExpression(Expressions.Source);
199
+ if (compare === undefined || table === undefined) {
200
+ return;
201
+ }
202
+ const first = this.firstKeyComponent(this.findType(table, spaghetti, file));
203
+ if (first !== undefined && this.hasFreeKeyComponent(compare, first) === false) {
204
+ this.report(statement, table.concatTokens(), first, file, issues);
205
+ }
206
+ }
207
+ /** table expressions with a free key, itab[ comp = ... ], anywhere in the statement */
208
+ checkTableExpressions(node, statement, spaghetti, file, issues) {
209
+ var _a;
210
+ const children = node.getChildren();
211
+ for (let i = 0; i < children.length; i++) {
212
+ const child = children[i];
213
+ if (child instanceof nodes_1.TokenNode) {
214
+ continue;
215
+ }
216
+ if (child.get() instanceof Expressions.TableExpression
217
+ && child instanceof nodes_1.ExpressionNode
218
+ && (node.get() instanceof Expressions.FieldChain || node.get() instanceof Expressions.Target)
219
+ && ((_a = child.getChildren()[1]) === null || _a === void 0 ? void 0 : _a.get()) instanceof Expressions.ComponentChainSimple) {
220
+ // a free key, not an index and not a named key: KEY name [COMPONENTS]
221
+ const first = this.firstKeyComponent(this.findChainType(children.slice(0, i), spaghetti, file));
222
+ if (first !== undefined && this.hasFreeKeyComponent(child, first) === false) {
223
+ const name = children.slice(0, i).map(c => c.concatTokens()).join("");
224
+ this.report(statement, name, first, file, issues);
225
+ }
226
+ }
227
+ this.checkTableExpressions(child, statement, spaghetti, file, issues);
228
+ }
229
+ }
230
+ report(statement, name, first, file, issues) {
231
+ const message = `Sequential read on sorted table ${name}, the first key component ${first} is not compared with =, ` +
232
+ `add it to the condition or "#EC CI_SORTSEQ`;
233
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
234
+ }
235
+ /** false if USING KEY names a key other than the primary key, or a dynamic one */
236
+ usesPrimaryKey(statement) {
237
+ if (statement.findDirectTokenByText("USING") === undefined) {
238
+ return true;
239
+ }
240
+ const keyName = statement.findExpressionAfterToken("KEY");
241
+ return (keyName === null || keyName === void 0 ? void 0 : keyName.get()) instanceof Expressions.SimpleName && keyName.concatTokens().toUpperCase() === "PRIMARY_KEY";
242
+ }
243
+ /** true if a free key, ComponentCompareSimple or the fields of a TableExpression, specifies the component;
244
+ * also true when a component is dynamic, as it cannot be decided */
245
+ hasFreeKeyComponent(node, component) {
246
+ if (node.findDirectExpression(Expressions.Dynamic) !== undefined) {
247
+ return true;
248
+ }
249
+ return node.findDirectExpressions(Expressions.ComponentChainSimple)
250
+ .some(c => c.concatTokens().toUpperCase().replace(/\s+/g, "") === component);
251
+ }
252
+ /** The first component of the primary key of a sorted table with explicit key components, else undefined */
253
+ firstKeyComponent(type) {
254
+ var _a, _b;
255
+ if (!(type instanceof basic_1.TableType)
256
+ || type.getAccessType() !== basic_1.TableAccessType.sorted
257
+ || type.getOptions().keyType !== basic_1.TableKeyType.user
258
+ || type.isGeneric()) {
259
+ return undefined;
260
+ }
261
+ return (_b = (_a = type.getOptions().primaryKey) === null || _a === void 0 ? void 0 : _a.keyFields[0]) === null || _b === void 0 ? void 0 : _b.toUpperCase();
262
+ }
263
+ findType(node, spaghetti, file) {
264
+ if (!(node.get() instanceof Expressions.Target)) {
265
+ const chain = node.getChildren().length === 1 ? node.findDirectExpression(Expressions.FieldChain) : undefined;
266
+ if (chain === undefined) {
267
+ return undefined;
268
+ }
269
+ node = chain;
270
+ }
271
+ return this.findChainType(node.getChildren(), spaghetti, file);
272
+ }
273
+ /** The type of a chain of names, components, attributes, dereferences and table expressions, else undefined */
274
+ findChainType(children, spaghetti, file) {
275
+ var _a;
276
+ let type = undefined;
277
+ for (const child of children) {
278
+ const get = child.get();
279
+ if (get instanceof Expressions.SourceField
280
+ || get instanceof Expressions.SourceFieldSymbol
281
+ || get instanceof Expressions.TargetField
282
+ || get instanceof Expressions.TargetFieldSymbol
283
+ || get instanceof Expressions.AttributeName) {
284
+ type = (_a = this.findReference(child.getFirstToken().getStart(), spaghetti, file)) === null || _a === void 0 ? void 0 : _a.getType();
285
+ }
286
+ else if (get instanceof Expressions.ComponentName) {
287
+ type = type instanceof basic_1.StructureType ? type.getComponentByName(child.concatTokens()) : undefined;
288
+ }
289
+ else if (get instanceof Expressions.Dereference) {
290
+ type = type instanceof basic_1.DataReference ? type.getType() : undefined;
291
+ }
292
+ else if (get instanceof Expressions.TableExpression) {
293
+ type = type instanceof basic_1.TableType ? type.getRowType() : undefined;
294
+ }
295
+ else if (get instanceof Expressions.ClassName || get instanceof tokens_1.StaticArrow) {
296
+ continue;
297
+ }
298
+ else if (!(get instanceof tokens_1.Dash) && !(get instanceof tokens_1.InstanceArrow)) {
299
+ return undefined;
300
+ }
301
+ if (type === undefined) {
302
+ return undefined;
303
+ }
304
+ }
305
+ return type;
306
+ }
307
+ findReference(start, spaghetti, file) {
308
+ const scope = spaghetti.lookupPosition(start, file.getFilename());
309
+ for (const r of (scope === null || scope === void 0 ? void 0 : scope.getData().references) || []) {
310
+ if (r.position.getStart().equals(start) && r.resolved instanceof _typed_identifier_1.TypedIdentifier) {
311
+ return r.resolved;
312
+ }
313
+ }
314
+ return undefined;
315
+ }
316
+ }
317
+ exports.SequentialReadSorted = SequentialReadSorted;
318
+ //# sourceMappingURL=sequential_read_sorted.js.map
@@ -0,0 +1,176 @@
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.StaleSubrc = exports.StaleSubrcConf = 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
+ const _statement_1 = require("../abap/2_statements/statements/_statement");
44
+ const severity_1 = require("../severity");
45
+ const _builtin_1 = require("../abap/5_syntax/_builtin");
46
+ class StaleSubrcConf extends _basic_rule_config_1.BasicRuleConfig {
47
+ constructor() {
48
+ super(...arguments);
49
+ this.severity = severity_1.Severity.Warning;
50
+ }
51
+ }
52
+ exports.StaleSubrcConf = StaleSubrcConf;
53
+ class StaleSubrc extends _abap_rule_1.ABAPRule {
54
+ constructor() {
55
+ super(...arguments);
56
+ this.conf = new StaleSubrcConf();
57
+ }
58
+ getMetadata() {
59
+ return {
60
+ key: "stale_subrc",
61
+ title: "sy-subrc checked after a method call",
62
+ shortDescription: `Reports IF, CHECK, ASSERT or CASE on sy-subrc directly after a method call without EXCEPTIONS`,
63
+ extendedInformation: `A method call without EXCEPTIONS does not set sy-subrc. After the call, sy-subrc holds whatever
64
+ the last statement inside the method set, so the check depends on the method's implementation
65
+ and breaks silently when the method changes.
66
+
67
+ Check the result of the method instead, eg. a returning parameter or an exception, or move the
68
+ check directly after the statement that sets sy-subrc.
69
+
70
+ Reported: the statement directly before the check is a method call, CALL METHOD, or an
71
+ assignment with a method call or NEW in its source.
72
+
73
+ Not reported: a call with EXCEPTIONS or EXCEPTION-TABLE, an assignment that only calls built-in
74
+ functions like lines( ), and any other statement before the check, eg. CALL FUNCTION or
75
+ CREATE OBJECT, a check that is the first statement of a block, or a check after ENDIF.
76
+ An assignment or CLEAR without a call does not change sy-subrc, so a check after it still
77
+ reads the statement before it, which is not reported either.
78
+ See also check_subrc and subrc_after_assign.`,
79
+ tags: [_irule_1.RuleTag.SingleFile],
80
+ badExample: `lv_parent = read_parent( ).
81
+ IF sy-subrc = 0.
82
+ WRITE lv_parent.
83
+ ENDIF.`,
84
+ goodExample: `lv_parent = read_parent( ).
85
+ IF lv_parent IS NOT INITIAL.
86
+ WRITE lv_parent.
87
+ ENDIF.`,
88
+ };
89
+ }
90
+ getConfig() {
91
+ return this.conf;
92
+ }
93
+ setConfig(conf) {
94
+ this.conf = conf;
95
+ }
96
+ runParsed(file) {
97
+ const issues = [];
98
+ const statements = file.getStatements();
99
+ for (let i = 0; i < statements.length; i++) {
100
+ const statement = statements[i];
101
+ if (this.isSubrcCheck(statement) === false) {
102
+ continue;
103
+ }
104
+ const previous = this.previousStatement(i, statements);
105
+ if (previous === undefined || this.isCallWithoutExceptions(previous) === false) {
106
+ continue;
107
+ }
108
+ const message = "sy-subrc checked after a method call without EXCEPTIONS, the call does not set sy-subrc";
109
+ issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
110
+ }
111
+ return issues;
112
+ }
113
+ ////////////////
114
+ isSubrcCheck(statement) {
115
+ const s = statement.get();
116
+ if (!(s instanceof Statements.If
117
+ || s instanceof Statements.Check
118
+ || s instanceof Statements.Assert
119
+ || s instanceof Statements.Case)) {
120
+ return false;
121
+ }
122
+ return this.readsSubrc(statement);
123
+ }
124
+ readsSubrc(statement) {
125
+ for (const field of statement.findAllExpressions(Expressions.FieldChain)) {
126
+ if (field.concatTokens().toUpperCase() === "SY-SUBRC") {
127
+ return true;
128
+ }
129
+ }
130
+ return false;
131
+ }
132
+ /** the statement directly before, skipping comments */
133
+ previousStatement(index, statements) {
134
+ for (let i = index - 1; i >= 0; i--) {
135
+ if (statements[i].get() instanceof _statement_1.Comment) {
136
+ continue;
137
+ }
138
+ return statements[i];
139
+ }
140
+ return undefined;
141
+ }
142
+ /** a standalone method call, or an assignment with a method call or NEW in its source */
143
+ isCallWithoutExceptions(statement) {
144
+ const s = statement.get();
145
+ if (s instanceof Statements.Call) {
146
+ return this.hasClassicExceptions(statement) === false;
147
+ }
148
+ else if (!(s instanceof Statements.Move) || this.hasClassicExceptions(statement) === true) {
149
+ return false;
150
+ }
151
+ if (statement.findFirstExpression(Expressions.NewObject) !== undefined) {
152
+ return true;
153
+ }
154
+ const tokens = statement.getTokens();
155
+ for (const call of statement.findAllExpressions(Expressions.MethodCall)) {
156
+ const name = call.getFirstToken();
157
+ const index = tokens.indexOf(name);
158
+ const before = index > 0 ? tokens[index - 1].getStr() : "";
159
+ if (before === "->" || before === "=>" || _builtin_1.BuiltIn.methods[name.getStr().toUpperCase()] === undefined) {
160
+ return true;
161
+ }
162
+ }
163
+ return false;
164
+ }
165
+ hasClassicExceptions(statement) {
166
+ for (const token of statement.getTokens()) {
167
+ const str = token.getStr().toUpperCase();
168
+ if (str === "EXCEPTIONS" || str === "EXCEPTION-TABLE") {
169
+ return true;
170
+ }
171
+ }
172
+ return false;
173
+ }
174
+ }
175
+ exports.StaleSubrc = StaleSubrc;
176
+ //# sourceMappingURL=stale_subrc.js.map