@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.
- package/build/abaplint.d.ts +1 -0
- package/build/src/abap/5_syntax/_key_compare.js +66 -0
- package/build/src/abap/5_syntax/statements/insert_internal.js +8 -0
- package/build/src/abap/5_syntax/statements/loop.js +2 -27
- package/build/src/abap/types/class_definition.js +52 -1
- package/build/src/registry.js +1 -1
- package/build/src/rules/_procedure_analysis.js +1 -0
- package/build/src/rules/character_field_literal_blanks.js +264 -0
- package/build/src/rules/constant_condition.js +201 -3
- package/build/src/rules/field_symbol_never_assigned.js +194 -0
- package/build/src/rules/identical_conditions.js +76 -2
- package/build/src/rules/index.js +7 -0
- package/build/src/rules/inline_generic_source.js +170 -0
- package/build/src/rules/loop_target_misuse.js +301 -0
- package/build/src/rules/redundant_conversion.js +99 -2
- package/build/src/rules/sequential_read_sorted.js +318 -0
- package/build/src/rules/stale_subrc.js +176 -0
- package/build/src/rules/unit_test_assertions.js +263 -0
- package/build/src/rules/xml_consistency.js +24 -0
- package/package.json +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
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.InlineGenericSource = exports.InlineGenericSourceConf = void 0;
|
|
37
|
+
const Expressions = __importStar(require("../abap/2_statements/expressions"));
|
|
38
|
+
const Statements = __importStar(require("../abap/2_statements/statements"));
|
|
39
|
+
const _reference_1 = require("../abap/5_syntax/_reference");
|
|
40
|
+
const syntax_1 = require("../abap/5_syntax/syntax");
|
|
41
|
+
const nodes_1 = require("../abap/nodes");
|
|
42
|
+
const _typed_identifier_1 = require("../abap/types/_typed_identifier");
|
|
43
|
+
const basic_1 = require("../abap/types/basic");
|
|
44
|
+
const edit_helper_1 = require("../edit_helper");
|
|
45
|
+
const issue_1 = require("../issue");
|
|
46
|
+
const _abap_object_1 = require("../objects/_abap_object");
|
|
47
|
+
const _basic_rule_config_1 = require("./_basic_rule_config");
|
|
48
|
+
const _irule_1 = require("./_irule");
|
|
49
|
+
class InlineGenericSourceConf extends _basic_rule_config_1.BasicRuleConfig {
|
|
50
|
+
}
|
|
51
|
+
exports.InlineGenericSourceConf = InlineGenericSourceConf;
|
|
52
|
+
class InlineGenericSource {
|
|
53
|
+
constructor() {
|
|
54
|
+
this.conf = new InlineGenericSourceConf();
|
|
55
|
+
}
|
|
56
|
+
getMetadata() {
|
|
57
|
+
return {
|
|
58
|
+
key: "inline_generic_source",
|
|
59
|
+
title: "Inline declaration from a generically typed source",
|
|
60
|
+
shortDescription: `Finds inline declarations whose value is a generically typed parameter or field symbol`,
|
|
61
|
+
extendedInformation: `DATA( ) or FINAL( ) takes the type of the source. When the source is a parameter or
|
|
62
|
+
field symbol with a generic type, the type is fixed when the program is compiled: string for clike and
|
|
63
|
+
csequence, xstring for xsequence. The syntax check warns "The fixed type STRING is used for the generic
|
|
64
|
+
type CLIKE".
|
|
65
|
+
|
|
66
|
+
Reported for DATA(var) = source and FINAL(var) = source when the source is just the parameter or field
|
|
67
|
+
symbol. Most other generic types cannot be made concrete, that is a syntax error which check_syntax reports.
|
|
68
|
+
|
|
69
|
+
Declare the variable with a type, or name the type with CONV.
|
|
70
|
+
|
|
71
|
+
The pragma ##TYPE, which hides the syntax check warning, also hides this issue.
|
|
72
|
+
|
|
73
|
+
prefer_inline does not suggest inline declarations for generic sources, this rule reports the ones
|
|
74
|
+
that exist.
|
|
75
|
+
|
|
76
|
+
https://help.sap.com/doc/abapdocu_758_index_htm/7.58/en-US/abapmove.htm`,
|
|
77
|
+
tags: [_irule_1.RuleTag.Quickfix],
|
|
78
|
+
pragma: "##TYPE",
|
|
79
|
+
badExample: `METHOD m. " IMPORTING iv TYPE clike
|
|
80
|
+
DATA(lv) = iv.
|
|
81
|
+
ENDMETHOD.`,
|
|
82
|
+
goodExample: `METHOD m. " IMPORTING iv TYPE clike
|
|
83
|
+
DATA(lv) = CONV string( iv ).
|
|
84
|
+
ENDMETHOD.`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
getConfig() {
|
|
88
|
+
return this.conf;
|
|
89
|
+
}
|
|
90
|
+
setConfig(conf) {
|
|
91
|
+
this.conf = conf;
|
|
92
|
+
}
|
|
93
|
+
initialize(reg) {
|
|
94
|
+
this.reg = reg;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
run(obj) {
|
|
98
|
+
var _a, _b, _c;
|
|
99
|
+
if (!(obj instanceof _abap_object_1.ABAPObject)) {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
const syntax = new syntax_1.SyntaxLogic(this.reg, obj).run();
|
|
103
|
+
if (syntax.issues.length > 0) {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
const issues = [];
|
|
107
|
+
for (const file of obj.getABAPFiles()) {
|
|
108
|
+
for (const statement of file.getStatements()) {
|
|
109
|
+
if (!(statement.get() instanceof Statements.Move)
|
|
110
|
+
|| statement.getPragmas().some(p => p.getStr().toUpperCase() === this.getMetadata().pragma)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const inline = (_a = statement.findDirectExpression(Expressions.Target)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.InlineData);
|
|
114
|
+
const name = (_b = inline === null || inline === void 0 ? void 0 : inline.findDirectExpression(Expressions.TargetField)) === null || _b === void 0 ? void 0 : _b.getFirstToken();
|
|
115
|
+
const operand = this.singleOperand(statement.findDirectExpression(Expressions.Source));
|
|
116
|
+
if (name === undefined || operand === undefined) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const scope = syntax.spaghetti.lookupPosition(name.getStart(), file.getFilename());
|
|
120
|
+
const resolved = (_c = scope === null || scope === void 0 ? void 0 : scope.getData().references.find(reference => reference.referenceType === _reference_1.ReferenceType.DataReadReference
|
|
121
|
+
&& reference.position.getStart().equals(operand.getFirstToken().getStart()))) === null || _c === void 0 ? void 0 : _c.resolved;
|
|
122
|
+
if (!(resolved instanceof _typed_identifier_1.TypedIdentifier)) {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const fixed = this.fixedType(resolved.getType());
|
|
126
|
+
if (fixed === undefined) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
const message = `The fixed type ${fixed.fixed} is used for the generic type ${fixed.generic} of "${operand.concatTokens()}", ` +
|
|
130
|
+
`declare "${name.getStr()}" with a type or use CONV`;
|
|
131
|
+
const replacement = `CONV ${fixed.conv}( ${operand.concatTokens()} )`;
|
|
132
|
+
const fix = edit_helper_1.EditHelper.replaceRange(file, operand.getFirstToken().getStart(), operand.getLastToken().getEnd(), replacement);
|
|
133
|
+
issues.push(issue_1.Issue.atToken(file, name, message, this.getMetadata().key, this.conf.severity, fix));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return issues;
|
|
137
|
+
}
|
|
138
|
+
/** The source when it is a single field or field symbol, without components, offset/length or dereference */
|
|
139
|
+
singleOperand(source) {
|
|
140
|
+
if (source === undefined || source.getChildren().length !== 1) {
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
const chain = source.findDirectExpression(Expressions.FieldChain);
|
|
144
|
+
if (chain === undefined || chain.getChildren().length !== 1) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
const first = chain.getFirstChild();
|
|
148
|
+
if (first instanceof nodes_1.ExpressionNode
|
|
149
|
+
&& (first.get() instanceof Expressions.SourceField || first.get() instanceof Expressions.SourceFieldSymbol)) {
|
|
150
|
+
return chain;
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
/** The type the system uses for a generic type in an inline declaration, undefined if the type is not generic,
|
|
155
|
+
* or is a generic type that check_syntax does not make concrete */
|
|
156
|
+
fixedType(type) {
|
|
157
|
+
if (type instanceof basic_1.CLikeType) {
|
|
158
|
+
return { generic: "CLIKE", fixed: "STRING", conv: "string" };
|
|
159
|
+
}
|
|
160
|
+
else if (type instanceof basic_1.CSequenceType) {
|
|
161
|
+
return { generic: "CSEQUENCE", fixed: "STRING", conv: "string" };
|
|
162
|
+
}
|
|
163
|
+
else if (type instanceof basic_1.XSequenceType) {
|
|
164
|
+
return { generic: "XSEQUENCE", fixed: "XSTRING", conv: "xstring" };
|
|
165
|
+
}
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
exports.InlineGenericSource = InlineGenericSource;
|
|
170
|
+
//# sourceMappingURL=inline_generic_source.js.map
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.LoopTargetMisuse = exports.LoopTargetMisuseConf = void 0;
|
|
37
|
+
const Statements = __importStar(require("../abap/2_statements/statements"));
|
|
38
|
+
const Expressions = __importStar(require("../abap/2_statements/expressions"));
|
|
39
|
+
const issue_1 = require("../issue");
|
|
40
|
+
const _basic_rule_config_1 = require("./_basic_rule_config");
|
|
41
|
+
const _abap_rule_1 = require("./_abap_rule");
|
|
42
|
+
const _irule_1 = require("./_irule");
|
|
43
|
+
class LoopTargetMisuseConf extends _basic_rule_config_1.BasicRuleConfig {
|
|
44
|
+
constructor() {
|
|
45
|
+
super(...arguments);
|
|
46
|
+
/** Report a LOOP inside a LOOP with the same target, when the outer loop body uses the target after the inner ENDLOOP */
|
|
47
|
+
this.nestedLoop = true;
|
|
48
|
+
/** Report LOOP AT ... INTO <fs> and READ TABLE ... INTO <fs> when the local field symbol is only bound by ASSIGNING,
|
|
49
|
+
* so the statement overwrites a table row */
|
|
50
|
+
this.intoFieldSymbol = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.LoopTargetMisuseConf = LoopTargetMisuseConf;
|
|
54
|
+
class LoopTargetMisuse extends _abap_rule_1.ABAPRule {
|
|
55
|
+
constructor() {
|
|
56
|
+
super(...arguments);
|
|
57
|
+
this.conf = new LoopTargetMisuseConf();
|
|
58
|
+
}
|
|
59
|
+
getMetadata() {
|
|
60
|
+
return {
|
|
61
|
+
key: "loop_target_misuse",
|
|
62
|
+
title: "LOOP and READ TABLE targets used wrongly",
|
|
63
|
+
shortDescription: `Reports a nested LOOP that reuses the target of the enclosing LOOP, and INTO <fs> where ASSIGNING was meant`,
|
|
64
|
+
extendedInformation: `Option nestedLoop: a LOOP inside a LOOP with the same INTO, REFERENCE INTO or ASSIGNING target.
|
|
65
|
+
After the inner ENDLOOP, the target holds the last row of the inner table, not the row of the
|
|
66
|
+
outer loop. Reported when the outer loop body uses the target after the inner ENDLOOP, before
|
|
67
|
+
it is assigned again.
|
|
68
|
+
Not reported: a target of a loop that has already ended, a target that is not a plain variable
|
|
69
|
+
or field symbol, an enclosing LOOP with GROUP BY, and a target the outer loop body does not use
|
|
70
|
+
after the inner loop.
|
|
71
|
+
|
|
72
|
+
Option intoFieldSymbol: LOOP AT ... INTO <fs> or READ TABLE ... INTO <fs>. INTO copies the row
|
|
73
|
+
into the data object the field symbol points to. When <fs> was bound by ASSIGNING, that data
|
|
74
|
+
object is a row of a table, and the statement overwrites it. ASSIGNING was meant.
|
|
75
|
+
Reported only for a field symbol declared in the method, form or function module, and only when
|
|
76
|
+
no ASSIGN and no STRUCTURE ... DEFAULT binds it there, so it can only point to a table row.`,
|
|
77
|
+
tags: [_irule_1.RuleTag.SingleFile],
|
|
78
|
+
badExample: `LOOP AT lt_header INTO ls_row.
|
|
79
|
+
LOOP AT lt_items INTO ls_row WHERE parent = ls_row-id.
|
|
80
|
+
lv_sum = lv_sum + ls_row-amount.
|
|
81
|
+
ENDLOOP.
|
|
82
|
+
ls_row-amount = lv_sum.
|
|
83
|
+
APPEND ls_row TO lt_result.
|
|
84
|
+
ENDLOOP.
|
|
85
|
+
|
|
86
|
+
LOOP AT lt_a ASSIGNING <ls_a>.
|
|
87
|
+
ENDLOOP.
|
|
88
|
+
READ TABLE lt_a INTO <ls_a> INDEX 1.`,
|
|
89
|
+
goodExample: `LOOP AT lt_header INTO ls_row.
|
|
90
|
+
LOOP AT lt_items INTO ls_item WHERE parent = ls_row-id.
|
|
91
|
+
lv_sum = lv_sum + ls_item-amount.
|
|
92
|
+
ENDLOOP.
|
|
93
|
+
ls_row-amount = lv_sum.
|
|
94
|
+
APPEND ls_row TO lt_result.
|
|
95
|
+
ENDLOOP.
|
|
96
|
+
|
|
97
|
+
LOOP AT lt_a ASSIGNING <ls_a>.
|
|
98
|
+
ENDLOOP.
|
|
99
|
+
READ TABLE lt_a ASSIGNING <ls_a> INDEX 1.`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
getConfig() {
|
|
103
|
+
return this.conf;
|
|
104
|
+
}
|
|
105
|
+
setConfig(conf) {
|
|
106
|
+
this.conf = conf;
|
|
107
|
+
}
|
|
108
|
+
runParsed(file) {
|
|
109
|
+
const issues = [];
|
|
110
|
+
const statements = file.getStatements();
|
|
111
|
+
if (this.conf.nestedLoop !== false) {
|
|
112
|
+
issues.push(...this.checkNestedLoop(file, statements));
|
|
113
|
+
}
|
|
114
|
+
if (this.conf.intoFieldSymbol !== false) {
|
|
115
|
+
issues.push(...this.checkIntoFieldSymbol(file, statements));
|
|
116
|
+
}
|
|
117
|
+
return issues;
|
|
118
|
+
}
|
|
119
|
+
////////////////
|
|
120
|
+
checkNestedLoop(file, statements) {
|
|
121
|
+
const issues = [];
|
|
122
|
+
// one entry per open LOOP, key undefined when the LOOP has no target to compare
|
|
123
|
+
let open = [];
|
|
124
|
+
for (let i = 0; i < statements.length; i++) {
|
|
125
|
+
const statement = statements[i];
|
|
126
|
+
const s = statement.get();
|
|
127
|
+
if (this.isBlockBoundary(statement)) {
|
|
128
|
+
open = [];
|
|
129
|
+
}
|
|
130
|
+
else if (s instanceof Statements.EndLoop) {
|
|
131
|
+
open.pop();
|
|
132
|
+
}
|
|
133
|
+
else if (s instanceof Statements.Loop) {
|
|
134
|
+
const node = this.loopTarget(statement);
|
|
135
|
+
const target = node ? this.targetName(node) : undefined;
|
|
136
|
+
// INTO <fs> inside LOOP ... ASSIGNING <fs> is left to the option intoFieldSymbol
|
|
137
|
+
const key = target === undefined ? undefined : ((node === null || node === void 0 ? void 0 : node.get()) instanceof Expressions.Target ? "INTO " : "ASSIGNING ") + target;
|
|
138
|
+
const outer = key === undefined ? undefined : [...open].reverse().find(o => o.key === key);
|
|
139
|
+
if (node !== undefined && target !== undefined && outer !== undefined) {
|
|
140
|
+
const innerEnd = this.findEndLoop(statements, i);
|
|
141
|
+
const outerEnd = this.findEndLoop(statements, outer.index);
|
|
142
|
+
if (innerEnd !== undefined && outerEnd !== undefined && this.usedBetween(statements, innerEnd + 1, outerEnd, target)) {
|
|
143
|
+
const name = node.concatTokens();
|
|
144
|
+
const message = `Inner LOOP reuses ${name}, the target of the enclosing LOOP, `
|
|
145
|
+
+ `which is used after the inner ENDLOOP and then holds a row of the inner table`;
|
|
146
|
+
issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const grouped = statement.findDirectExpression(Expressions.LoopGroupBy) !== undefined;
|
|
150
|
+
open.push({ index: i, key: grouped ? undefined : key });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return issues;
|
|
154
|
+
}
|
|
155
|
+
/** the Target or FSTarget after INTO, REFERENCE INTO or ASSIGNING of a LOOP */
|
|
156
|
+
loopTarget(statement) {
|
|
157
|
+
const target = statement.findDirectExpression(Expressions.LoopTarget);
|
|
158
|
+
return (target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.Target)) || (target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.FSTarget));
|
|
159
|
+
}
|
|
160
|
+
/** upper case name of a Target or FSTarget that is a plain variable or field symbol, also declared inline */
|
|
161
|
+
targetName(node) {
|
|
162
|
+
var _a, _b;
|
|
163
|
+
const inline = ((_a = node.findDirectExpression(Expressions.InlineData)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.TargetField))
|
|
164
|
+
|| ((_b = node.findDirectExpression(Expressions.InlineFS)) === null || _b === void 0 ? void 0 : _b.findDirectExpression(Expressions.TargetFieldSymbol));
|
|
165
|
+
const tokens = (inline || node).getAllTokens();
|
|
166
|
+
return tokens.length === 1 ? tokens[0].getStr().toUpperCase() : undefined;
|
|
167
|
+
}
|
|
168
|
+
/** index of the ENDLOOP that closes the LOOP at the index */
|
|
169
|
+
findEndLoop(statements, index) {
|
|
170
|
+
let depth = 0;
|
|
171
|
+
for (let i = index + 1; i < statements.length && this.isBlockBoundary(statements[i]) === false; i++) {
|
|
172
|
+
const s = statements[i].get();
|
|
173
|
+
if (s instanceof Statements.Loop) {
|
|
174
|
+
depth++;
|
|
175
|
+
}
|
|
176
|
+
else if (s instanceof Statements.EndLoop) {
|
|
177
|
+
if (depth === 0) {
|
|
178
|
+
return i;
|
|
179
|
+
}
|
|
180
|
+
depth--;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
/** true when a statement in [from, to) uses the variable before a statement assigns all of it */
|
|
186
|
+
usedBetween(statements, from, to, name) {
|
|
187
|
+
for (let i = from; i < to; i++) {
|
|
188
|
+
const statement = statements[i];
|
|
189
|
+
const s = statement.get();
|
|
190
|
+
if (s instanceof Statements.Data || s instanceof Statements.FieldSymbol) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const whole = new Set();
|
|
194
|
+
for (const t of [...statement.findAllExpressions(Expressions.Target), ...statement.findAllExpressions(Expressions.FSTarget)]) {
|
|
195
|
+
if (this.targetName(t) === name) {
|
|
196
|
+
t.getAllTokens().forEach(token => whole.add(token));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const tokens = statement.getTokens();
|
|
200
|
+
for (let j = 0; j < tokens.length; j++) {
|
|
201
|
+
const previous = j > 0 ? tokens[j - 1].getStr() : "";
|
|
202
|
+
if (tokens[j].getStr().toUpperCase() === name
|
|
203
|
+
&& whole.has(tokens[j]) === false
|
|
204
|
+
&& ["-", "->", "=>", "~"].includes(previous) === false) {
|
|
205
|
+
return s instanceof Statements.Unassign ? false : true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (whole.size > 0) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
checkIntoFieldSymbol(file, statements) {
|
|
215
|
+
const issues = [];
|
|
216
|
+
for (let i = 0; i < statements.length; i++) {
|
|
217
|
+
const statement = statements[i];
|
|
218
|
+
const s = statement.get();
|
|
219
|
+
if (!(s instanceof Statements.Loop) && !(s instanceof Statements.ReadTable)) {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const target = statement.findDirectExpression(Expressions.LoopTarget)
|
|
223
|
+
|| statement.findDirectExpression(Expressions.ReadTableTarget);
|
|
224
|
+
const into = target === null || target === void 0 ? void 0 : target.findDirectExpression(Expressions.Target);
|
|
225
|
+
const fs = into === null || into === void 0 ? void 0 : into.findDirectExpression(Expressions.TargetFieldSymbol);
|
|
226
|
+
if (into === undefined || fs === undefined || fs.concatTokens() !== into.concatTokens()) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const name = fs.concatTokens().toUpperCase();
|
|
230
|
+
if (this.onlyBoundToRows(statements, i, name) === false) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const what = s instanceof Statements.Loop ? "LOOP AT" : "READ TABLE";
|
|
234
|
+
const message = `${what} ... INTO ${fs.concatTokens()} overwrites the row the field symbol points to, use ASSIGNING`;
|
|
235
|
+
issues.push(issue_1.Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
|
|
236
|
+
}
|
|
237
|
+
return issues;
|
|
238
|
+
}
|
|
239
|
+
/** true when the field symbol is declared in the block around the statement and every binding of it
|
|
240
|
+
* in that block is an ASSIGNING, so it can only point to a table row */
|
|
241
|
+
onlyBoundToRows(statements, index, name) {
|
|
242
|
+
var _a;
|
|
243
|
+
let start = index;
|
|
244
|
+
while (start > 0 && this.isBlockBoundary(statements[start]) === false) {
|
|
245
|
+
start--;
|
|
246
|
+
}
|
|
247
|
+
if (this.startsBlock(statements[start]) === false) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
let declared = false;
|
|
251
|
+
let assigning = false;
|
|
252
|
+
for (let i = start + 1; i < statements.length && this.endsBlock(statements[i]) === false; i++) {
|
|
253
|
+
const statement = statements[i];
|
|
254
|
+
if (statement.get() instanceof Statements.FieldSymbol) {
|
|
255
|
+
if (((_a = statement.findDirectExpression(Expressions.FieldSymbol)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase()) === name) {
|
|
256
|
+
if (statement.findDirectTokenByText("DEFAULT") !== undefined) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
declared = true;
|
|
260
|
+
}
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
for (const fsTarget of statement.findAllExpressions(Expressions.FSTarget)) {
|
|
264
|
+
if (this.fsName(fsTarget) !== name) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (statement.get() instanceof Statements.Assign) {
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
assigning = true;
|
|
271
|
+
if (fsTarget.findDirectExpression(Expressions.InlineFS) !== undefined) {
|
|
272
|
+
declared = true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return declared && assigning;
|
|
277
|
+
}
|
|
278
|
+
fsName(fsTarget) {
|
|
279
|
+
var _a;
|
|
280
|
+
return (_a = fsTarget.findFirstExpression(Expressions.FieldSymbol)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase();
|
|
281
|
+
}
|
|
282
|
+
isBlockBoundary(statement) {
|
|
283
|
+
return this.startsBlock(statement) || this.endsBlock(statement);
|
|
284
|
+
}
|
|
285
|
+
startsBlock(statement) {
|
|
286
|
+
const s = statement.get();
|
|
287
|
+
return s instanceof Statements.MethodImplementation
|
|
288
|
+
|| s instanceof Statements.Form
|
|
289
|
+
|| s instanceof Statements.FunctionModule
|
|
290
|
+
|| s instanceof Statements.Module;
|
|
291
|
+
}
|
|
292
|
+
endsBlock(statement) {
|
|
293
|
+
const s = statement.get();
|
|
294
|
+
return s instanceof Statements.EndMethod
|
|
295
|
+
|| s instanceof Statements.EndForm
|
|
296
|
+
|| s instanceof Statements.EndFunction
|
|
297
|
+
|| s instanceof Statements.EndModule;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
exports.LoopTargetMisuse = LoopTargetMisuse;
|
|
301
|
+
//# sourceMappingURL=loop_target_misuse.js.map
|
|
@@ -49,6 +49,12 @@ const _irule_1 = require("./_irule");
|
|
|
49
49
|
const edit_helper_1 = require("../edit_helper");
|
|
50
50
|
const version_1 = require("../version");
|
|
51
51
|
class RedundantConversionConf extends _basic_rule_config_1.BasicRuleConfig {
|
|
52
|
+
constructor() {
|
|
53
|
+
super(...arguments);
|
|
54
|
+
/** Also report a CONV that is the whole right-hand side of an assignment whose target already has the
|
|
55
|
+
* conversion type, e.g. "count = CONV i( text )." with count TYPE i, the assignment converts by itself */
|
|
56
|
+
this.assignmentTarget = true;
|
|
57
|
+
}
|
|
52
58
|
}
|
|
53
59
|
exports.RedundantConversionConf = RedundantConversionConf;
|
|
54
60
|
class RedundantConversion {
|
|
@@ -61,7 +67,12 @@ class RedundantConversion {
|
|
|
61
67
|
title: "Redundant Conversion",
|
|
62
68
|
shortDescription: `Find redundant CONV expressions`,
|
|
63
69
|
extendedInformation: `Reports CONV expressions whose operand already has the conversion's target type,
|
|
64
|
-
including the result of a built-in function with a fixed result type, like to_upper( ) or strlen( )
|
|
70
|
+
including the result of a built-in function with a fixed result type, like to_upper( ) or strlen( ).
|
|
71
|
+
|
|
72
|
+
With option assignmentTarget, also reports a CONV that is the whole right-hand side of an assignment
|
|
73
|
+
whose target already has the conversion's type, "count = CONV i( text )." with count TYPE i, the
|
|
74
|
+
assignment converts by itself. Not reported for inline declarations, enumerated types, LET, and operands
|
|
75
|
+
with a generic type.`,
|
|
65
76
|
tags: [_irule_1.RuleTag.Quickfix],
|
|
66
77
|
badExample: `DATA text TYPE string.
|
|
67
78
|
text = CONV string( text ).`,
|
|
@@ -121,7 +132,11 @@ text = text.`,
|
|
|
121
132
|
}
|
|
122
133
|
const operandHasSameType = operandType !== undefined && this.sameType(target.getType(), operandType);
|
|
123
134
|
const arithmeticHasSameContext = this.arithmeticUsesSameTargetType(source, bodySource, file, scope, target.getType());
|
|
124
|
-
|
|
135
|
+
const assignmentHasSameType = this.conf.assignmentTarget !== false
|
|
136
|
+
&& this.assignmentTargetHasSameType(source, file, scope, target.getType());
|
|
137
|
+
if (operandHasSameType === false
|
|
138
|
+
&& arithmeticHasSameContext === false
|
|
139
|
+
&& assignmentHasSameType === false) {
|
|
125
140
|
continue;
|
|
126
141
|
}
|
|
127
142
|
const closingParen = source.getDirectTokens().find(token => token.getStr() === ")" && token.getStart().isAfter(bodySource.getLastToken().getStart()));
|
|
@@ -156,6 +171,88 @@ text = text.`,
|
|
|
156
171
|
return assignmentTarget instanceof _typed_identifier_1.TypedIdentifier
|
|
157
172
|
&& this.sameType(conversionType, assignmentTarget.getType());
|
|
158
173
|
}
|
|
174
|
+
/** "target = CONV i( text )." where target has the conversion type, the assignment converts the same way */
|
|
175
|
+
assignmentTargetHasSameType(conversion, file, scope, conversionType) {
|
|
176
|
+
var _a;
|
|
177
|
+
// CONV converts the base type to an enumerated type, a plain assignment does not compile
|
|
178
|
+
if (conversionType instanceof basic_1.EnumType) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
const body = conversion.findDirectExpression(Expressions.ConvBody);
|
|
182
|
+
if (body === undefined
|
|
183
|
+
|| body.findDirectExpression(Expressions.Let) !== undefined
|
|
184
|
+
// "CONV type ( body )" without a trailing operator
|
|
185
|
+
|| conversion.getChildren().length !== 5) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
// "target = CONV ...", not "+=", "?=", "a = b = CONV ..." or MOVE
|
|
189
|
+
const statement = file.getStatements().find(candidate => candidate.includesToken(conversion.getFirstToken()));
|
|
190
|
+
const children = (_a = statement === null || statement === void 0 ? void 0 : statement.getChildren()) !== null && _a !== void 0 ? _a : [];
|
|
191
|
+
const target = children[0];
|
|
192
|
+
if (!((statement === null || statement === void 0 ? void 0 : statement.get()) instanceof Statements.Move)
|
|
193
|
+
|| children.length !== 4
|
|
194
|
+
|| !(target instanceof nodes_1.ExpressionNode)
|
|
195
|
+
|| !(target.get() instanceof Expressions.Target)
|
|
196
|
+
|| children[1].concatTokens() !== "="
|
|
197
|
+
|| children[2] !== conversion) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
// a generic operand gets its type from the CONV, also "ref->*" with ref TYPE REF TO data
|
|
201
|
+
const bodyStart = body.getFirstToken().getStart();
|
|
202
|
+
const bodyEnd = body.getLastToken().getEnd();
|
|
203
|
+
const genericOperand = scope.getData().references.some(reference => {
|
|
204
|
+
if (reference.referenceType !== _reference_1.ReferenceType.DataReadReference
|
|
205
|
+
|| reference.position.getFilename() !== file.getFilename()
|
|
206
|
+
|| reference.position.getStart().isBetween(bodyStart, bodyEnd) === false
|
|
207
|
+
|| !(reference.resolved instanceof _typed_identifier_1.TypedIdentifier)) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
const type = reference.resolved.getType();
|
|
211
|
+
return type.isGeneric() || (type instanceof basic_1.DataReference && type.getType().isGeneric());
|
|
212
|
+
});
|
|
213
|
+
if (genericOperand === true) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
const targetType = this.targetType(target, scope);
|
|
217
|
+
return targetType !== undefined && this.sameType(conversionType, targetType);
|
|
218
|
+
}
|
|
219
|
+
/** Type of a variable, field symbol, attribute or structure component, undefined for anything else */
|
|
220
|
+
targetType(target, scope) {
|
|
221
|
+
var _a;
|
|
222
|
+
let type;
|
|
223
|
+
for (const child of target.getChildren()) {
|
|
224
|
+
if (child instanceof nodes_1.TokenNode) {
|
|
225
|
+
if (["-", "->", "=>"].includes(child.getFirstToken().getStr()) === false) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else if (child.get() instanceof Expressions.TargetField
|
|
230
|
+
|| child.get() instanceof Expressions.TargetFieldSymbol
|
|
231
|
+
|| child.get() instanceof Expressions.AttributeName) {
|
|
232
|
+
const resolved = (_a = scope.getData().references.find(reference => (reference.referenceType === _reference_1.ReferenceType.DataWriteReference
|
|
233
|
+
|| reference.referenceType === _reference_1.ReferenceType.DataReadReference)
|
|
234
|
+
&& reference.position.getStart().equals(child.getFirstToken().getStart()))) === null || _a === void 0 ? void 0 : _a.resolved;
|
|
235
|
+
if (!(resolved instanceof _typed_identifier_1.TypedIdentifier)) {
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
type = resolved.getType();
|
|
239
|
+
}
|
|
240
|
+
else if (child.get() instanceof Expressions.ComponentName) {
|
|
241
|
+
if (type instanceof basic_1.TableType) {
|
|
242
|
+
type = type.getRowType();
|
|
243
|
+
}
|
|
244
|
+
if (!(type instanceof basic_1.StructureType)) {
|
|
245
|
+
return undefined;
|
|
246
|
+
}
|
|
247
|
+
type = type.getComponentByName(child.concatTokens());
|
|
248
|
+
}
|
|
249
|
+
else if (!(child.get() instanceof Expressions.ClassName)) {
|
|
250
|
+
// inline declarations, dereferences, table expressions, offsets and lengths
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return type;
|
|
255
|
+
}
|
|
159
256
|
sourceType(source, scope) {
|
|
160
257
|
let context;
|
|
161
258
|
// boolc( <condition> ) is not a MethodCallChain, xsdbool( ) has the same shape but is not reported
|