@abaplint/core 2.120.109 → 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.
- package/build/abaplint.d.ts +12 -0
- package/build/src/abap/5_syntax/_current_scope.js +49 -0
- package/build/src/abap/5_syntax/expressions/for.js +6 -0
- package/build/src/abap/5_syntax/expressions/inline_field_definition.js +2 -2
- package/build/src/abap/5_syntax/expressions/inline_loop_definition.js +10 -0
- package/build/src/registry.js +1 -1
- package/build/src/rules/index.js +2 -0
- package/build/src/rules/sort_required.js +487 -0
- package/build/src/rules/suspicious_loop.js +333 -0
- package/package.json +2 -2
package/build/abaplint.d.ts
CHANGED
|
@@ -1591,6 +1591,8 @@ export declare class CurrentScope {
|
|
|
1591
1591
|
protected allowHeaderUse: string | undefined;
|
|
1592
1592
|
protected parentObj: IObject;
|
|
1593
1593
|
private readonly localFriends;
|
|
1594
|
+
/** types of the FOR and LET names used in a procedure, key is the scope node of the procedure */
|
|
1595
|
+
private readonly localNameTypes;
|
|
1594
1596
|
static buildDefault(reg: IRegistry, obj: IObject): CurrentScope;
|
|
1595
1597
|
private static addBuiltIn;
|
|
1596
1598
|
private constructor();
|
|
@@ -1651,6 +1653,16 @@ export declare class CurrentScope {
|
|
|
1651
1653
|
isInStaticMethod(): boolean;
|
|
1652
1654
|
addLocalFriend(className: string, friendName: string): void;
|
|
1653
1655
|
isLocalFriend(className: string, friendName: string): boolean;
|
|
1656
|
+
/**
|
|
1657
|
+
* Checks a local name of a constructor expression, FOR, LET and REDUCE INIT, before it is added,
|
|
1658
|
+
* returns the error message, or undefined when the name can be declared.
|
|
1659
|
+
* The name cannot be a data object of the procedure or of an enclosing expression, attributes and
|
|
1660
|
+
* globals seen from a method or a form are only obscured, which ABAP reports as a warning.
|
|
1661
|
+
* Within a procedure, a name can be reused by another expression only with the same type.
|
|
1662
|
+
*/
|
|
1663
|
+
checkLocalName(name: string, type: AbstractType | undefined): string | undefined;
|
|
1664
|
+
/** types are compared by their structure, ABAP compares the declared types, so this finds a subset of the errors */
|
|
1665
|
+
private static isComparable;
|
|
1654
1666
|
getEnclosingMethodName(): string | undefined;
|
|
1655
1667
|
getEnclosingClassName(): string | undefined;
|
|
1656
1668
|
isGlobalOO(): boolean;
|
|
@@ -8,6 +8,9 @@ const spaghetti_scope_1 = require("./spaghetti_scope");
|
|
|
8
8
|
const _identifier_1 = require("../4_file_information/_identifier");
|
|
9
9
|
const _scope_type_1 = require("./_scope_type");
|
|
10
10
|
const syntax_1 = require("./syntax");
|
|
11
|
+
const basic_1 = require("../types/basic");
|
|
12
|
+
/** scopes of constructor expressions, their names are local to the expression */
|
|
13
|
+
const EXPRESSION_SCOPES = new Set([_scope_type_1.ScopeType.For, _scope_type_1.ScopeType.Let, _scope_type_1.ScopeType.OpenSQL]);
|
|
11
14
|
class CurrentScope {
|
|
12
15
|
static buildDefault(reg, obj) {
|
|
13
16
|
const s = new CurrentScope(reg, obj);
|
|
@@ -30,6 +33,8 @@ class CurrentScope {
|
|
|
30
33
|
}
|
|
31
34
|
constructor(reg, obj) {
|
|
32
35
|
this.localFriends = new Map();
|
|
36
|
+
/** types of the FOR and LET names used in a procedure, key is the scope node of the procedure */
|
|
37
|
+
this.localNameTypes = new WeakMap();
|
|
33
38
|
this.current = undefined;
|
|
34
39
|
this.parentObj = obj;
|
|
35
40
|
this.reg = reg;
|
|
@@ -518,6 +523,50 @@ class CurrentScope {
|
|
|
518
523
|
var _a;
|
|
519
524
|
return ((_a = this.localFriends.get(className.toUpperCase())) === null || _a === void 0 ? void 0 : _a.includes(friendName.toUpperCase())) === true;
|
|
520
525
|
}
|
|
526
|
+
/**
|
|
527
|
+
* Checks a local name of a constructor expression, FOR, LET and REDUCE INIT, before it is added,
|
|
528
|
+
* returns the error message, or undefined when the name can be declared.
|
|
529
|
+
* The name cannot be a data object of the procedure or of an enclosing expression, attributes and
|
|
530
|
+
* globals seen from a method or a form are only obscured, which ABAP reports as a warning.
|
|
531
|
+
* Within a procedure, a name can be reused by another expression only with the same type.
|
|
532
|
+
*/
|
|
533
|
+
checkLocalName(name, type) {
|
|
534
|
+
const upper = name.toUpperCase();
|
|
535
|
+
let procedure = this.current;
|
|
536
|
+
while (procedure !== undefined) {
|
|
537
|
+
if (procedure.getData().vars[upper] !== undefined) {
|
|
538
|
+
return `Variable ${name} already defined`;
|
|
539
|
+
}
|
|
540
|
+
if (EXPRESSION_SCOPES.has(procedure.getIdentifier().stype) === false) {
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
procedure = procedure.getParent();
|
|
544
|
+
}
|
|
545
|
+
if (procedure === undefined || type === undefined || CurrentScope.isComparable(type) === false) {
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
let names = this.localNameTypes.get(procedure);
|
|
549
|
+
if (names === undefined) {
|
|
550
|
+
names = new Map();
|
|
551
|
+
this.localNameTypes.set(procedure, names);
|
|
552
|
+
}
|
|
553
|
+
const previous = names.get(upper);
|
|
554
|
+
if (previous === undefined) {
|
|
555
|
+
names.set(upper, type);
|
|
556
|
+
}
|
|
557
|
+
else if (previous.toText(0) !== type.toText(0)) {
|
|
558
|
+
return `Variable ${name} already defined with a different type`;
|
|
559
|
+
}
|
|
560
|
+
return undefined;
|
|
561
|
+
}
|
|
562
|
+
/** types are compared by their structure, ABAP compares the declared types, so this finds a subset of the errors */
|
|
563
|
+
static isComparable(type) {
|
|
564
|
+
return !(type instanceof basic_1.VoidType)
|
|
565
|
+
&& !(type instanceof basic_1.UnknownType)
|
|
566
|
+
&& type.containsVoid() === false
|
|
567
|
+
&& type.isGeneric() === false
|
|
568
|
+
&& type.toText(0).includes("Unknown type") === false;
|
|
569
|
+
}
|
|
521
570
|
getEnclosingMethodName() {
|
|
522
571
|
let curr = this.current;
|
|
523
572
|
while (curr !== undefined) {
|
|
@@ -45,6 +45,7 @@ const basic_1 = require("../../types/basic");
|
|
|
45
45
|
const _typed_identifier_1 = require("../../types/_typed_identifier");
|
|
46
46
|
const _reference_1 = require("../_reference");
|
|
47
47
|
const let_1 = require("./let");
|
|
48
|
+
const _syntax_input_1 = require("../_syntax_input");
|
|
48
49
|
class For {
|
|
49
50
|
static runSyntax(node, input) {
|
|
50
51
|
var _a;
|
|
@@ -69,6 +70,11 @@ class For {
|
|
|
69
70
|
inline_field_definition_1.InlineFieldDefinition.runSyntax(f, input);
|
|
70
71
|
}
|
|
71
72
|
if (groupsToken !== undefined) {
|
|
73
|
+
const message = input.scope.checkLocalName(groupsToken.getStr(), undefined);
|
|
74
|
+
if (message !== undefined) {
|
|
75
|
+
input.issues.push((0, _syntax_input_1.syntaxIssue)(input, groupsToken, message));
|
|
76
|
+
return scoped;
|
|
77
|
+
}
|
|
72
78
|
const type = basic_1.VoidType.get("todoGroupBy");
|
|
73
79
|
const identifier = new _typed_identifier_1.TypedIdentifier(groupsToken, input.filename, type, ["inline" /* IdentifierMeta.InlineDefinition */]);
|
|
74
80
|
input.scope.addIdentifier(identifier);
|
|
@@ -66,8 +66,8 @@ class InlineFieldDefinition {
|
|
|
66
66
|
type = new unknown_type_1.UnknownType("InlineFieldDefinition, fallback");
|
|
67
67
|
}
|
|
68
68
|
const name = field.getStr();
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
const message = input.scope.checkLocalName(name, type);
|
|
70
|
+
if (message !== undefined) {
|
|
71
71
|
input.issues.push((0, _syntax_input_1.syntaxIssue)(input, node.getFirstToken(), message));
|
|
72
72
|
return basic_1.VoidType.get(_syntax_input_1.CheckSyntaxKey);
|
|
73
73
|
}
|
|
@@ -74,6 +74,11 @@ class InlineLoopDefinition {
|
|
|
74
74
|
input.issues.push((0, _syntax_input_1.syntaxIssue)(input, node.getFirstToken(), message));
|
|
75
75
|
return;
|
|
76
76
|
}
|
|
77
|
+
const message = input.scope.checkLocalName(target.getFirstToken().getStr(), rowType);
|
|
78
|
+
if (message !== undefined) {
|
|
79
|
+
input.issues.push((0, _syntax_input_1.syntaxIssue)(input, target.getFirstToken(), message));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
77
82
|
const identifier = new _typed_identifier_1.TypedIdentifier(target.getFirstToken(), input.filename, rowType, ["inline" /* IdentifierMeta.InlineDefinition */]);
|
|
78
83
|
input.scope.addReference(target.getFirstToken(), identifier, _reference_1.ReferenceType.DataWriteReference, input.filename);
|
|
79
84
|
input.scope.addReference(target.getFirstToken(), identifier, _reference_1.ReferenceType.DataReadReference, input.filename);
|
|
@@ -81,6 +86,11 @@ class InlineLoopDefinition {
|
|
|
81
86
|
}
|
|
82
87
|
const index = node.findExpressionAfterToken("INTO");
|
|
83
88
|
if (index && index.get() instanceof Expressions.TargetField) {
|
|
89
|
+
const message = input.scope.checkLocalName(index.getFirstToken().getStr(), basic_1.IntegerType.get());
|
|
90
|
+
if (message !== undefined) {
|
|
91
|
+
input.issues.push((0, _syntax_input_1.syntaxIssue)(input, index.getFirstToken(), message));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
84
94
|
const identifier = new _typed_identifier_1.TypedIdentifier(index.getFirstToken(), input.filename, basic_1.IntegerType.get(), ["inline" /* IdentifierMeta.InlineDefinition */]);
|
|
85
95
|
input.scope.addReference(index.getFirstToken(), identifier, _reference_1.ReferenceType.DataWriteReference, input.filename);
|
|
86
96
|
input.scope.addIdentifier(identifier);
|
package/build/src/registry.js
CHANGED
package/build/src/rules/index.js
CHANGED
|
@@ -208,6 +208,7 @@ __exportStar(require("./single_use_expression_temp"), exports);
|
|
|
208
208
|
__exportStar(require("./slow_parameter_passing"), exports);
|
|
209
209
|
__exportStar(require("./smim_consistency"), exports);
|
|
210
210
|
__exportStar(require("./sort_empty_key"), exports);
|
|
211
|
+
__exportStar(require("./sort_required"), exports);
|
|
211
212
|
__exportStar(require("./space_before_colon"), exports);
|
|
212
213
|
__exportStar(require("./space_before_dot"), exports);
|
|
213
214
|
__exportStar(require("./sql_escape_host_variables"), exports);
|
|
@@ -219,6 +220,7 @@ __exportStar(require("./strict_sql"), exports);
|
|
|
219
220
|
__exportStar(require("./subrc_after_assign"), exports);
|
|
220
221
|
__exportStar(require("./superclass_final"), exports);
|
|
221
222
|
__exportStar(require("./superfluous_value"), exports);
|
|
223
|
+
__exportStar(require("./suspicious_loop"), exports);
|
|
222
224
|
__exportStar(require("./sy_modification"), exports);
|
|
223
225
|
__exportStar(require("./sy_read_restriction"), exports);
|
|
224
226
|
__exportStar(require("./tabl_enhancement_category"), exports);
|
|
@@ -0,0 +1,487 @@
|
|
|
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.SortRequired = exports.SortRequiredConf = void 0;
|
|
37
|
+
const Expressions = __importStar(require("../abap/2_statements/expressions"));
|
|
38
|
+
const Statements = __importStar(require("../abap/2_statements/statements"));
|
|
39
|
+
const _basic_rule_config_1 = require("./_basic_rule_config");
|
|
40
|
+
const issue_1 = require("../issue");
|
|
41
|
+
const _irule_1 = require("./_irule");
|
|
42
|
+
const syntax_1 = require("../abap/5_syntax/syntax");
|
|
43
|
+
const _abap_object_1 = require("../objects/_abap_object");
|
|
44
|
+
const basic_1 = require("../abap/types/basic");
|
|
45
|
+
const nodes_1 = require("../abap/nodes");
|
|
46
|
+
const _typed_identifier_1 = require("../abap/types/_typed_identifier");
|
|
47
|
+
const severity_1 = require("../severity");
|
|
48
|
+
class SortRequiredConf extends _basic_rule_config_1.BasicRuleConfig {
|
|
49
|
+
constructor() {
|
|
50
|
+
super(...arguments);
|
|
51
|
+
this.severity = severity_1.Severity.Warning;
|
|
52
|
+
/** Check READ TABLE ... WITH KEY ... BINARY SEARCH */
|
|
53
|
+
this.binarySearch = true;
|
|
54
|
+
/** Check DELETE ADJACENT DUPLICATES ... COMPARING */
|
|
55
|
+
this.adjacentDuplicates = true;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.SortRequiredConf = SortRequiredConf;
|
|
59
|
+
/** what a statement that writes the table does to its order */
|
|
60
|
+
var Fill;
|
|
61
|
+
(function (Fill) {
|
|
62
|
+
/** SELECT without ORDER BY, or APPEND, INSERT or COLLECT in such a SELECT loop: rows in no defined order */
|
|
63
|
+
Fill[Fill["unordered"] = 0] = "unordered";
|
|
64
|
+
/** CLEAR, DELETE, MODIFY, SORT, and APPEND, INSERT or COLLECT elsewhere: the code can add the rows in order */
|
|
65
|
+
Fill[Fill["none"] = 1] = "none";
|
|
66
|
+
/** anything else: the rows can come sorted from elsewhere */
|
|
67
|
+
Fill[Fill["unknown"] = 2] = "unknown";
|
|
68
|
+
})(Fill || (Fill = {}));
|
|
69
|
+
class SortRequired {
|
|
70
|
+
constructor() {
|
|
71
|
+
this.conf = new SortRequiredConf();
|
|
72
|
+
}
|
|
73
|
+
getMetadata() {
|
|
74
|
+
return {
|
|
75
|
+
key: "sort_required",
|
|
76
|
+
title: "BINARY SEARCH or DELETE ADJACENT DUPLICATES without a matching SORT",
|
|
77
|
+
shortDescription: `BINARY SEARCH or DELETE ADJACENT DUPLICATES ... COMPARING on a local table not sorted by those fields`,
|
|
78
|
+
extendedInformation: `READ TABLE ... WITH KEY ... BINARY SEARCH only finds the row when the table is sorted ascending by
|
|
79
|
+
the key fields, in the order of the key. DELETE ADJACENT DUPLICATES ... COMPARING only removes all duplicates
|
|
80
|
+
when rows with equal values in the compared fields are next to each other. Neither statement checks the order:
|
|
81
|
+
on an unsorted table they give wrong results, without a dump.
|
|
82
|
+
|
|
83
|
+
Checked: a standard table declared with DATA in the method, form or function module, read with WITH KEY
|
|
84
|
+
and BINARY SEARCH, or with DELETE ADJACENT DUPLICATES ... COMPARING. Reported when
|
|
85
|
+
- the last SORT of the table before the statement is not by the key fields: for BINARY SEARCH they must be
|
|
86
|
+
the first sort fields, in the same order and ascending; for COMPARING the first sort fields must be the
|
|
87
|
+
compared fields, in any order. A SORT without BY uses the primary key.
|
|
88
|
+
- there is no SORT of the table before the statement, and it is filled by a SELECT without ORDER BY: the
|
|
89
|
+
database returns the rows in no defined order.
|
|
90
|
+
|
|
91
|
+
Not reported: parameters, attributes, global and static variables, a table that can get its rows sorted
|
|
92
|
+
from elsewhere (an assignment, a method or function call, APPEND or INSERT LINES OF, SELECT ... ORDER BY)
|
|
93
|
+
after the SORT or, without a SORT, anywhere in the procedure, a table without SORT filled only by APPEND,
|
|
94
|
+
INSERT or COLLECT (the code can add the rows in order), sorted and hashed tables, SORT with STABLE or with
|
|
95
|
+
dynamic fields, a SORT later in a loop around the statement, a SORT without BY on a default key of a
|
|
96
|
+
structured row, WITH TABLE KEY, USING KEY, dynamic components and COMPARING ALL FIELDS.
|
|
97
|
+
See also sort_empty_key and sequential_read_sorted.`,
|
|
98
|
+
tags: [_irule_1.RuleTag.SingleFile],
|
|
99
|
+
badExample: `SORT lt_titles BY id.
|
|
100
|
+
DELETE ADJACENT DUPLICATES FROM lt_titles COMPARING spras id.
|
|
101
|
+
SELECT * FROM tadir INTO TABLE lt_tadir WHERE devclass = lv_package.
|
|
102
|
+
READ TABLE lt_tadir WITH KEY object = lv_type obj_name = lv_name BINARY SEARCH TRANSPORTING NO FIELDS.`,
|
|
103
|
+
goodExample: `SORT lt_titles BY spras id.
|
|
104
|
+
DELETE ADJACENT DUPLICATES FROM lt_titles COMPARING spras id.
|
|
105
|
+
SELECT * FROM tadir INTO TABLE lt_tadir WHERE devclass = lv_package.
|
|
106
|
+
SORT lt_tadir BY object obj_name.
|
|
107
|
+
READ TABLE lt_tadir WITH KEY object = lv_type obj_name = lv_name BINARY SEARCH TRANSPORTING NO FIELDS.`,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
getConfig() {
|
|
111
|
+
return this.conf;
|
|
112
|
+
}
|
|
113
|
+
initialize(reg) {
|
|
114
|
+
this.reg = reg;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
setConfig(conf) {
|
|
118
|
+
this.conf = conf;
|
|
119
|
+
}
|
|
120
|
+
run(obj) {
|
|
121
|
+
if (!(obj instanceof _abap_object_1.ABAPObject) || obj.getType() === "INTF") {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
const issues = [];
|
|
125
|
+
let spaghetti = undefined;
|
|
126
|
+
for (const file of obj.getABAPFiles()) {
|
|
127
|
+
const statements = file.getStatements();
|
|
128
|
+
for (let i = 0; i < statements.length; i++) {
|
|
129
|
+
const statement = statements[i];
|
|
130
|
+
let table;
|
|
131
|
+
let fields;
|
|
132
|
+
let binary;
|
|
133
|
+
if (statement.get() instanceof Statements.ReadTable && this.conf.binarySearch !== false) {
|
|
134
|
+
binary = true;
|
|
135
|
+
table = statement.getChildren().find(c => c instanceof nodes_1.ExpressionNode);
|
|
136
|
+
fields = this.readKey(statement);
|
|
137
|
+
}
|
|
138
|
+
else if (statement.get() instanceof Statements.DeleteInternal && this.conf.adjacentDuplicates !== false) {
|
|
139
|
+
binary = false;
|
|
140
|
+
table = statement.findDirectExpression(Expressions.Target);
|
|
141
|
+
fields = this.comparing(statement);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (table === undefined || fields === undefined || /^[\w/]+$/.test(table.concatTokens()) === false) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
const name = table.concatTokens().toUpperCase();
|
|
150
|
+
const start = this.procedureStart(i, statements);
|
|
151
|
+
if (start === undefined) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const end = this.procedureEnd(i, statements);
|
|
155
|
+
if (spaghetti === undefined) {
|
|
156
|
+
spaghetti = new syntax_1.SyntaxLogic(this.reg, obj).run().spaghetti;
|
|
157
|
+
}
|
|
158
|
+
const id = this.findReference(table, spaghetti, file);
|
|
159
|
+
const declaration = id === undefined ? undefined : this.findDeclaration(id, file, statements, start, i);
|
|
160
|
+
const type = id === null || id === void 0 ? void 0 : id.getType();
|
|
161
|
+
if (declaration === undefined || !(type instanceof basic_1.TableType) || this.isStandard(type, declaration) === false) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const verb = binary ? "READ TABLE ... BINARY SEARCH" : "DELETE ADJACENT DUPLICATES ... COMPARING";
|
|
165
|
+
const sortIndex = this.previousSort(name, statements, start, i);
|
|
166
|
+
let message = undefined;
|
|
167
|
+
if (sortIndex !== undefined) {
|
|
168
|
+
const sortFields = this.sortFields(statements[sortIndex], type);
|
|
169
|
+
if (sortFields !== undefined
|
|
170
|
+
&& this.matches(sortFields, fields, binary) === false
|
|
171
|
+
&& this.writtenUnknown(name, statements, sortIndex, i) === false) {
|
|
172
|
+
const by = sortFields.map(f => f.name.toLowerCase() + (f.descending ? " DESCENDING" : "")).join(" ");
|
|
173
|
+
message = `${verb} on ${table.concatTokens()} needs it sorted by ${fields.join(" ").toLowerCase()}, `
|
|
174
|
+
+ `the SORT before is by ${by}`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (this.sortedLaterInLoop(name, statements, start, i) === false
|
|
178
|
+
&& this.writtenUnknown(name, statements, start, end) === false
|
|
179
|
+
&& this.filledUnordered(name, statements, start, end) === true) {
|
|
180
|
+
message = `${verb} on ${table.concatTokens()} needs it sorted by ${fields.join(" ").toLowerCase()}, `
|
|
181
|
+
+ `there is no SORT before`;
|
|
182
|
+
}
|
|
183
|
+
if (message !== undefined) {
|
|
184
|
+
issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return issues;
|
|
189
|
+
}
|
|
190
|
+
////////////////
|
|
191
|
+
/** the components of WITH KEY, for a READ TABLE ... BINARY SEARCH with a free key */
|
|
192
|
+
readKey(statement) {
|
|
193
|
+
if (statement.findDirectTokenByText("BINARY") === undefined
|
|
194
|
+
|| statement.findDirectTokenByText("USING") !== undefined
|
|
195
|
+
|| statement.findDirectTokenByText("COMPONENTS") !== undefined) {
|
|
196
|
+
return undefined;
|
|
197
|
+
}
|
|
198
|
+
const tokens = statement.getTokens().map(t => t.getStr().toUpperCase());
|
|
199
|
+
const withKey = tokens.findIndex((t, index) => t === "WITH" && tokens[index + 1] === "KEY");
|
|
200
|
+
const compare = statement.findDirectExpression(Expressions.ComponentCompareSimple);
|
|
201
|
+
if (withKey < 0 || compare === undefined || compare.findDirectExpression(Expressions.Dynamic) !== undefined) {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
return compare.findDirectExpressions(Expressions.ComponentChainSimple).map(c => c.concatTokens().toUpperCase());
|
|
205
|
+
}
|
|
206
|
+
/** the fields after COMPARING, for a DELETE ADJACENT DUPLICATES with static fields */
|
|
207
|
+
comparing(statement) {
|
|
208
|
+
if (statement.findDirectTokenByText("ADJACENT") === undefined
|
|
209
|
+
|| statement.findDirectTokenByText("USING") !== undefined
|
|
210
|
+
|| statement.findDirectTokenByText("ALL") !== undefined
|
|
211
|
+
|| statement.findDirectTokenByText("COMPARING") === undefined
|
|
212
|
+
|| statement.findDirectExpression(Expressions.Dynamic) !== undefined
|
|
213
|
+
|| statement.findDirectExpression(Expressions.FieldOffset) !== undefined
|
|
214
|
+
|| statement.findDirectExpression(Expressions.FieldLength) !== undefined) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
return statement.findDirectExpressions(Expressions.SimpleFieldChain2).map(c => c.concatTokens().toUpperCase());
|
|
218
|
+
}
|
|
219
|
+
/** the sort fields of a SORT, undefined when not known */
|
|
220
|
+
sortFields(statement, type) {
|
|
221
|
+
var _a;
|
|
222
|
+
if (statement.findDirectTokenByText("STABLE") !== undefined
|
|
223
|
+
|| statement.findDirectExpression(Expressions.Dynamic) !== undefined
|
|
224
|
+
|| statement.findDirectExpression(Expressions.SourceFieldSymbol) !== undefined) {
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
const children = statement.getChildren();
|
|
228
|
+
const by = children.findIndex(c => c.concatTokens().toUpperCase() === "BY");
|
|
229
|
+
const descending = children.slice(0, by < 0 ? children.length : by)
|
|
230
|
+
.some(c => c.concatTokens().toUpperCase() === "DESCENDING");
|
|
231
|
+
if (by < 0) {
|
|
232
|
+
return (_a = this.primaryKey(type)) === null || _a === void 0 ? void 0 : _a.map(name => ({ name, descending }));
|
|
233
|
+
}
|
|
234
|
+
const result = [];
|
|
235
|
+
for (let j = by + 1; j < children.length; j++) {
|
|
236
|
+
const child = children[j];
|
|
237
|
+
const text = child.concatTokens().toUpperCase();
|
|
238
|
+
if (child.get() instanceof Expressions.ComponentChain) {
|
|
239
|
+
result.push({ name: text, descending });
|
|
240
|
+
}
|
|
241
|
+
else if (text === "DESCENDING" && result.length > 0) {
|
|
242
|
+
result[result.length - 1].descending = true;
|
|
243
|
+
}
|
|
244
|
+
else if (text === "ASCENDING" && result.length > 0) {
|
|
245
|
+
result[result.length - 1].descending = false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return result;
|
|
249
|
+
}
|
|
250
|
+
/** the primary key components, undefined for a default key of a structured row or when not known */
|
|
251
|
+
primaryKey(type) {
|
|
252
|
+
var _a;
|
|
253
|
+
const row = type.getRowType();
|
|
254
|
+
if (row instanceof basic_1.VoidType || row instanceof basic_1.UnknownType || row.isGeneric()) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const keyType = type.getOptions().keyType;
|
|
258
|
+
const fields = ((_a = type.getOptions().primaryKey) === null || _a === void 0 ? void 0 : _a.keyFields) || [];
|
|
259
|
+
if (keyType === basic_1.TableKeyType.user && fields.length > 0) {
|
|
260
|
+
return fields;
|
|
261
|
+
}
|
|
262
|
+
else if (keyType === basic_1.TableKeyType.empty) {
|
|
263
|
+
return [];
|
|
264
|
+
}
|
|
265
|
+
return row instanceof basic_1.StructureType || row instanceof basic_1.TableType ? undefined : ["TABLE_LINE"];
|
|
266
|
+
}
|
|
267
|
+
matches(sortFields, fields, binary) {
|
|
268
|
+
if (sortFields.length < fields.length) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
const first = sortFields.slice(0, fields.length);
|
|
272
|
+
if (binary === true) {
|
|
273
|
+
return first.every((f, index) => f.name === fields[index] && f.descending === false);
|
|
274
|
+
}
|
|
275
|
+
const names = first.map(f => f.name);
|
|
276
|
+
return fields.every(f => names.includes(f));
|
|
277
|
+
}
|
|
278
|
+
/** index of the last SORT of the table before the statement, in the same procedure */
|
|
279
|
+
previousSort(name, statements, start, index) {
|
|
280
|
+
for (let i = index - 1; i > start; i--) {
|
|
281
|
+
if (this.isSortOf(statements[i], name)) {
|
|
282
|
+
return i;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
/** a SORT of the table after the statement but inside a loop around it runs before the next pass */
|
|
288
|
+
sortedLaterInLoop(name, statements, start, index) {
|
|
289
|
+
let outer = undefined;
|
|
290
|
+
let depth = 0;
|
|
291
|
+
for (let i = index - 1; i > start; i--) {
|
|
292
|
+
if (this.endsLoop(statements[i])) {
|
|
293
|
+
depth++;
|
|
294
|
+
}
|
|
295
|
+
else if (this.startsLoop(statements[i])) {
|
|
296
|
+
if (depth === 0) {
|
|
297
|
+
outer = i;
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
depth--;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (outer === undefined) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
depth = 0;
|
|
308
|
+
for (let i = outer + 1; i < statements.length; i++) {
|
|
309
|
+
const statement = statements[i];
|
|
310
|
+
if (this.startsLoop(statement)) {
|
|
311
|
+
depth++;
|
|
312
|
+
}
|
|
313
|
+
else if (this.endsLoop(statement)) {
|
|
314
|
+
if (depth === 0) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
depth--;
|
|
318
|
+
}
|
|
319
|
+
else if (i > index && this.isSortOf(statement, name)) {
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
isSortOf(statement, name) {
|
|
326
|
+
var _a;
|
|
327
|
+
return statement.get() instanceof Statements.Sort
|
|
328
|
+
&& ((_a = statement.findDirectExpression(Expressions.Target)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase()) === name;
|
|
329
|
+
}
|
|
330
|
+
/** true when a statement between from and to fills the table from a source that can be sorted */
|
|
331
|
+
writtenUnknown(name, statements, from, to) {
|
|
332
|
+
for (let i = from + 1; i < to; i++) {
|
|
333
|
+
if (this.writes(statements[i], name) && this.fillKind(i, statements, name) === Fill.unknown) {
|
|
334
|
+
return true;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
/** true when a statement in the procedure brings rows in no defined order */
|
|
340
|
+
filledUnordered(name, statements, start, end) {
|
|
341
|
+
for (let i = start + 1; i < end; i++) {
|
|
342
|
+
if (this.writes(statements[i], name) && this.fillKind(i, statements, name) === Fill.unordered) {
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
writes(statement, name) {
|
|
349
|
+
return statement.get() instanceof Statements.Data === false
|
|
350
|
+
&& statement.findAllExpressions(Expressions.Target).some(t => this.targetName(t) === name);
|
|
351
|
+
}
|
|
352
|
+
targetName(target) {
|
|
353
|
+
var _a;
|
|
354
|
+
const inline = target.findDirectExpression(Expressions.InlineData);
|
|
355
|
+
if (inline !== undefined) {
|
|
356
|
+
return ((_a = inline.findDirectExpression(Expressions.TargetField)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase()) || "";
|
|
357
|
+
}
|
|
358
|
+
return target.concatTokens().toUpperCase();
|
|
359
|
+
}
|
|
360
|
+
fillKind(index, statements, name) {
|
|
361
|
+
var _a;
|
|
362
|
+
const statement = statements[index];
|
|
363
|
+
const s = statement.get();
|
|
364
|
+
if (s instanceof Statements.Clear
|
|
365
|
+
|| s instanceof Statements.Refresh
|
|
366
|
+
|| s instanceof Statements.Free
|
|
367
|
+
|| s instanceof Statements.DeleteInternal
|
|
368
|
+
|| s instanceof Statements.ModifyInternal
|
|
369
|
+
|| s instanceof Statements.Sort) {
|
|
370
|
+
return Fill.none;
|
|
371
|
+
}
|
|
372
|
+
else if (s instanceof Statements.Append || s instanceof Statements.InsertInternal || s instanceof Statements.Collect) {
|
|
373
|
+
if (statement.findDirectTokenByText("LINES") !== undefined || statement.findDirectTokenByText("SORTED") !== undefined) {
|
|
374
|
+
return Fill.unknown;
|
|
375
|
+
}
|
|
376
|
+
const loop = this.innermostLoop(index, statements);
|
|
377
|
+
return loop !== undefined && this.selectsUnordered(loop) ? Fill.unordered : Fill.none;
|
|
378
|
+
}
|
|
379
|
+
else if (s instanceof Statements.Select || s instanceof Statements.SelectLoop) {
|
|
380
|
+
const target = (_a = statement.findFirstExpression(Expressions.SQLIntoTable)) === null || _a === void 0 ? void 0 : _a.findFirstExpression(Expressions.Target);
|
|
381
|
+
return target !== undefined && this.targetName(target) === name && this.selectsUnordered(statement)
|
|
382
|
+
? Fill.unordered : Fill.unknown;
|
|
383
|
+
}
|
|
384
|
+
return Fill.unknown;
|
|
385
|
+
}
|
|
386
|
+
selectsUnordered(statement) {
|
|
387
|
+
return (statement.get() instanceof Statements.Select || statement.get() instanceof Statements.SelectLoop)
|
|
388
|
+
&& statement.findFirstExpression(Expressions.SQLOrderBy) === undefined;
|
|
389
|
+
}
|
|
390
|
+
innermostLoop(index, statements) {
|
|
391
|
+
let depth = 0;
|
|
392
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
393
|
+
if (this.endsLoop(statements[i])) {
|
|
394
|
+
depth++;
|
|
395
|
+
}
|
|
396
|
+
else if (this.startsLoop(statements[i])) {
|
|
397
|
+
if (depth === 0) {
|
|
398
|
+
return statements[i];
|
|
399
|
+
}
|
|
400
|
+
depth--;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return undefined;
|
|
404
|
+
}
|
|
405
|
+
/** the DATA statement or inline declaration of the table inside the procedure, before the statement;
|
|
406
|
+
* parameters, attributes, globals and statics are declared elsewhere or with another statement */
|
|
407
|
+
findDeclaration(id, file, statements, start, index) {
|
|
408
|
+
if (id.getFilename() !== file.getFilename()) {
|
|
409
|
+
return undefined;
|
|
410
|
+
}
|
|
411
|
+
const position = id.getStart();
|
|
412
|
+
for (let i = start + 1; i < index; i++) {
|
|
413
|
+
const statement = statements[i];
|
|
414
|
+
if (position.isBetween(statement.getStart(), statement.getEnd()) === false) {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const inline = statement.findAllExpressions(Expressions.InlineData)
|
|
418
|
+
.some(d => position.isBetween(d.getFirstToken().getStart(), d.getLastToken().getEnd()));
|
|
419
|
+
return statement.get() instanceof Statements.Data || inline ? statement : undefined;
|
|
420
|
+
}
|
|
421
|
+
return undefined;
|
|
422
|
+
}
|
|
423
|
+
/** a standard table; the result of an inline SELECT has no access type set, it is a standard table */
|
|
424
|
+
isStandard(type, declaration) {
|
|
425
|
+
const access = type.getAccessType();
|
|
426
|
+
if (access === undefined) {
|
|
427
|
+
return declaration.get() instanceof Statements.Select || declaration.get() instanceof Statements.SelectLoop;
|
|
428
|
+
}
|
|
429
|
+
return access === basic_1.TableAccessType.standard;
|
|
430
|
+
}
|
|
431
|
+
findReference(node, spaghetti, file) {
|
|
432
|
+
const start = node.getFirstToken().getStart();
|
|
433
|
+
const scope = spaghetti.lookupPosition(start, file.getFilename());
|
|
434
|
+
for (const r of (scope === null || scope === void 0 ? void 0 : scope.getData().references) || []) {
|
|
435
|
+
if (r.position.getStart().equals(start) && r.resolved instanceof _typed_identifier_1.TypedIdentifier) {
|
|
436
|
+
return r.resolved;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
procedureStart(index, statements) {
|
|
442
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
443
|
+
const s = statements[i].get();
|
|
444
|
+
if (s instanceof Statements.MethodImplementation
|
|
445
|
+
|| s instanceof Statements.Form
|
|
446
|
+
|| s instanceof Statements.FunctionModule
|
|
447
|
+
|| s instanceof Statements.Module) {
|
|
448
|
+
return i;
|
|
449
|
+
}
|
|
450
|
+
else if (s instanceof Statements.EndMethod
|
|
451
|
+
|| s instanceof Statements.EndForm
|
|
452
|
+
|| s instanceof Statements.EndFunction
|
|
453
|
+
|| s instanceof Statements.EndModule) {
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return undefined;
|
|
458
|
+
}
|
|
459
|
+
procedureEnd(index, statements) {
|
|
460
|
+
for (let i = index + 1; i < statements.length; i++) {
|
|
461
|
+
const s = statements[i].get();
|
|
462
|
+
if (s instanceof Statements.EndMethod
|
|
463
|
+
|| s instanceof Statements.EndForm
|
|
464
|
+
|| s instanceof Statements.EndFunction
|
|
465
|
+
|| s instanceof Statements.EndModule) {
|
|
466
|
+
return i;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return statements.length;
|
|
470
|
+
}
|
|
471
|
+
startsLoop(statement) {
|
|
472
|
+
const s = statement.get();
|
|
473
|
+
return s instanceof Statements.Loop
|
|
474
|
+
|| s instanceof Statements.Do
|
|
475
|
+
|| s instanceof Statements.While
|
|
476
|
+
|| s instanceof Statements.SelectLoop;
|
|
477
|
+
}
|
|
478
|
+
endsLoop(statement) {
|
|
479
|
+
const s = statement.get();
|
|
480
|
+
return s instanceof Statements.EndLoop
|
|
481
|
+
|| s instanceof Statements.EndDo
|
|
482
|
+
|| s instanceof Statements.EndWhile
|
|
483
|
+
|| s instanceof Statements.EndSelect;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
exports.SortRequired = SortRequired;
|
|
487
|
+
//# sourceMappingURL=sort_required.js.map
|
|
@@ -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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abaplint/core",
|
|
3
|
-
"version": "2.120.
|
|
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": "^
|
|
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",
|