@abaplint/core 2.120.82 → 2.120.84

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.
Files changed (60) hide show
  1. package/build/abaplint.d.ts +19 -5
  2. package/build/src/abap/1_lexer/tokens/abstract_token.js +36 -12
  3. package/build/src/abap/5_syntax/_current_scope.js +14 -11
  4. package/build/src/abap/5_syntax/_eml.js +68 -0
  5. package/build/src/abap/5_syntax/_object_oriented.js +10 -0
  6. package/build/src/abap/5_syntax/spaghetti_scope.js +67 -9
  7. package/build/src/abap/5_syntax/statements/message.js +8 -0
  8. package/build/src/abap/5_syntax/statements/modify_entities.js +2 -0
  9. package/build/src/abap/5_syntax/statements/raise.js +6 -0
  10. package/build/src/abap/5_syntax/statements/read_entities.js +2 -0
  11. package/build/src/abap/5_syntax/syntax.js +2 -2
  12. package/build/src/bdef/bdef_parser.js +75 -0
  13. package/build/src/bdef/expressions/bdef_action.js +15 -0
  14. package/build/src/bdef/expressions/bdef_alias.js +12 -0
  15. package/build/src/bdef/expressions/bdef_association.js +15 -0
  16. package/build/src/bdef/expressions/bdef_association_body.js +18 -0
  17. package/build/src/bdef/expressions/bdef_authorization_context.js +16 -0
  18. package/build/src/bdef/expressions/bdef_cardinality.js +13 -0
  19. package/build/src/bdef/expressions/bdef_default_function.js +14 -0
  20. package/build/src/bdef/expressions/bdef_define_behavior.js +21 -0
  21. package/build/src/bdef/expressions/bdef_definition.js +12 -0
  22. package/build/src/bdef/expressions/bdef_determination.js +15 -0
  23. package/build/src/bdef/expressions/bdef_determine_action.js +15 -0
  24. package/build/src/bdef/expressions/bdef_event.js +14 -0
  25. package/build/src/bdef/expressions/bdef_field.js +13 -0
  26. package/build/src/bdef/expressions/bdef_field_path.js +13 -0
  27. package/build/src/bdef/expressions/bdef_header.js +15 -0
  28. package/build/src/bdef/expressions/bdef_hierarchy.js +16 -0
  29. package/build/src/bdef/expressions/bdef_implementation_in.js +12 -0
  30. package/build/src/bdef/expressions/bdef_implementation_type.js +19 -0
  31. package/build/src/bdef/expressions/bdef_key.js +14 -0
  32. package/build/src/bdef/expressions/bdef_mapping.js +19 -0
  33. package/build/src/bdef/expressions/bdef_name.js +13 -0
  34. package/build/src/bdef/expressions/bdef_operation.js +13 -0
  35. package/build/src/bdef/expressions/bdef_options.js +15 -0
  36. package/build/src/bdef/expressions/bdef_parameter.js +13 -0
  37. package/build/src/bdef/expressions/bdef_reference.js +15 -0
  38. package/build/src/bdef/expressions/bdef_result.js +13 -0
  39. package/build/src/bdef/expressions/bdef_side_effects.js +25 -0
  40. package/build/src/bdef/expressions/bdef_string.js +11 -0
  41. package/build/src/bdef/expressions/bdef_use.js +18 -0
  42. package/build/src/bdef/expressions/index.js +46 -0
  43. package/build/src/lsp/dump_scope.js +3 -2
  44. package/build/src/objects/behavior_definition.js +46 -8
  45. package/build/src/position.js +4 -1
  46. package/build/src/registry.js +1 -1
  47. package/build/src/rules/_procedure_analysis.js +366 -4
  48. package/build/src/rules/bdef_check_syntax.js +88 -0
  49. package/build/src/rules/bdef_parser_error.js +47 -0
  50. package/build/src/rules/boolean_flag_via_if.js +1 -8
  51. package/build/src/rules/cast_via_generic_reference.js +189 -0
  52. package/build/src/rules/clear_after_declaration.js +8 -7
  53. package/build/src/rules/clear_before_overwrite.js +179 -0
  54. package/build/src/rules/constant_condition.js +9 -8
  55. package/build/src/rules/copy_chain.js +80 -10
  56. package/build/src/rules/copy_of_initial_variable.js +4 -2
  57. package/build/src/rules/index.js +4 -0
  58. package/build/src/rules/single_use_expression_temp.js +544 -40
  59. package/build/src/rules/unnecessary_copy.js +7 -4
  60. package/package.json +1 -1
@@ -0,0 +1,189 @@
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.CastViaGenericReference = exports.CastViaGenericReferenceConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const basic_1 = require("../abap/types/basic");
39
+ const interface_definition_1 = require("../abap/types/interface_definition");
40
+ const edit_helper_1 = require("../edit_helper");
41
+ const issue_1 = require("../issue");
42
+ const _basic_rule_config_1 = require("./_basic_rule_config");
43
+ const _irule_1 = require("./_irule");
44
+ const _procedure_analysis_1 = require("./_procedure_analysis");
45
+ class CastViaGenericReferenceConf extends _basic_rule_config_1.BasicRuleConfig {
46
+ }
47
+ exports.CastViaGenericReferenceConf = CastViaGenericReferenceConf;
48
+ class CastViaGenericReference {
49
+ constructor() {
50
+ this.conf = new CastViaGenericReferenceConf();
51
+ }
52
+ getMetadata() {
53
+ return {
54
+ key: "cast_via_generic_reference",
55
+ title: "Cast via generic reference",
56
+ shortDescription: `Finds a down cast from a local reference variable that only holds a copy of another reference`,
57
+ extendedInformation: `Reports DATA o TYPE REF TO object. o = r. ... x ?= o. when all of the following hold:
58
+
59
+ * o is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE and not chained,
60
+ typed REF TO object, or REF TO a class or interface that is the static type of r or a supertype of it
61
+ * o has exactly one write, o = r., exactly one read, the complete source of x ?= o., and its address is never
62
+ taken. x is not an inline declaration
63
+ * r is a plain object reference: a variable, or a chain of attributes and components without calls,
64
+ offsets, table expressions, field symbols or ->*
65
+ * r keeps its value from o = r. until the read, as in unnecessary_copy: for a local r, no write to it and its
66
+ address is never taken; for other r, in addition no method, function or form call and no write except to
67
+ local variables, by value parameters and RETURNING in between
68
+ * o = r. is executed before x ?= o. on every path: the read is in the same block or a nested block, and both
69
+ are in the same loop
70
+ * x ?= r. is valid: x or r is typed REF TO object or an interface, or the class of x and the class of r
71
+ are in the same inheritance line
72
+
73
+ The object is the same, so the cast succeeds or raises cx_sy_move_cast_error the same way.
74
+ try_cast_empty_catch can then replace a TRY around the cast.
75
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
76
+ INCLUDE, test seams, enhancements or native SQL.
77
+
78
+ The quick fix deletes the declaration and o = r., and casts r directly. No release requirement.`,
79
+ tags: [_irule_1.RuleTag.Quickfix],
80
+ badExample: `DATA lo_object TYPE REF TO object.
81
+ lo_object = io_node.
82
+ TRY.
83
+ lo_leaf ?= lo_object.
84
+ CATCH cx_sy_move_cast_error.
85
+ ENDTRY.`,
86
+ goodExample: `TRY.
87
+ lo_leaf ?= io_node.
88
+ CATCH cx_sy_move_cast_error.
89
+ ENDTRY.`,
90
+ };
91
+ }
92
+ getConfig() {
93
+ return this.conf;
94
+ }
95
+ setConfig(conf) {
96
+ this.conf = conf;
97
+ }
98
+ initialize(reg) {
99
+ this.reg = reg;
100
+ return this;
101
+ }
102
+ run(obj) {
103
+ var _a, _b;
104
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
105
+ if (analysis === undefined) {
106
+ return [];
107
+ }
108
+ const issues = [];
109
+ for (const procedure of analysis.procedures) {
110
+ if (procedure.supported === false) {
111
+ continue;
112
+ }
113
+ const hierarchy = new _procedure_analysis_1.Hierarchy(this.reg, procedure);
114
+ for (const variable of procedure.locals()) {
115
+ const declaration = variable.declaration;
116
+ const type = variable.identifier.getType();
117
+ const writes = variable.writes();
118
+ const reads = variable.reads();
119
+ if (variable.inline || !(0, _procedure_analysis_1.plainData)(declaration) || declaration.getColon() !== undefined || variable.escapes()
120
+ || !this.isObjectReference(type) || writes.length !== 1 || reads.length !== 1) {
121
+ continue;
122
+ }
123
+ const write = writes[0];
124
+ const read = reads[0];
125
+ const assign = (0, _procedure_analysis_1.plainMove)(write.statement);
126
+ const cast = (0, _procedure_analysis_1.plainMove)(read.statement);
127
+ if (assign === undefined || cast === undefined || assign.operator !== "=" || cast.operator !== "?="
128
+ || ((_a = (0, _procedure_analysis_1.singleTarget)(assign.target)) === null || _a === void 0 ? void 0 : _a.getStart().equals(write.token.getStart())) !== true
129
+ || ((_b = (0, _procedure_analysis_1.singleField)(cast.source)) === null || _b === void 0 ? void 0 : _b.getStart().equals(read.token.getStart())) !== true
130
+ || cast.target.findFirstExpression(Expressions.InlineData) !== undefined
131
+ || !procedure.dominates(write.statement, read.statement)
132
+ || !procedure.sameLoop(write.statement, read.statement)) {
133
+ continue;
134
+ }
135
+ const source = (0, _procedure_analysis_1.dataSource)(procedure, assign.source);
136
+ const target = (0, _procedure_analysis_1.targetType)(procedure, cast.target);
137
+ if (source === undefined || source.base === variable || target === undefined
138
+ || !this.isObjectReference(source.type)
139
+ || !this.isSupertype(hierarchy, type, source.type)
140
+ || !this.castable(hierarchy, source.type, target)
141
+ || !(0, _procedure_analysis_1.unchangedBetween)(procedure, source, write.statement, read.statement, read.token)) {
142
+ continue;
143
+ }
144
+ const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, [
145
+ (0, _procedure_analysis_1.deleteStatement)(procedure.file, declaration),
146
+ (0, _procedure_analysis_1.deleteStatement)(procedure.file, write.statement),
147
+ edit_helper_1.EditHelper.replaceToken(procedure.file, read.token, source.text)
148
+ ]);
149
+ const message = "Cast " + source.text + " directly instead of via " + variable.identifier.getName();
150
+ issues.push(issue_1.Issue.atStatement(procedure.file, read.statement, message, this.getMetadata().key, this.conf.severity, fix));
151
+ }
152
+ }
153
+ return issues;
154
+ }
155
+ isObjectReference(type) {
156
+ return type instanceof basic_1.GenericObjectReferenceType || type instanceof basic_1.ObjectReferenceType;
157
+ }
158
+ /** every reference of type sub can be assigned to type sup without a cast */
159
+ isSupertype(hierarchy, sup, sub) {
160
+ if (sup instanceof basic_1.GenericObjectReferenceType) {
161
+ return true;
162
+ }
163
+ else if (!(sup instanceof basic_1.ObjectReferenceType) || !(sub instanceof basic_1.ObjectReferenceType)) {
164
+ return false;
165
+ }
166
+ return hierarchy.isSubtype(sub.getIdentifierName(), sup.getIdentifierName());
167
+ }
168
+ /** target ?= source. is statically valid */
169
+ castable(hierarchy, source, target) {
170
+ if (source instanceof basic_1.GenericObjectReferenceType || target instanceof basic_1.GenericObjectReferenceType) {
171
+ return true;
172
+ }
173
+ else if (!(source instanceof basic_1.ObjectReferenceType) || !(target instanceof basic_1.ObjectReferenceType)) {
174
+ return false;
175
+ }
176
+ const sourceDefinition = hierarchy.definition(source);
177
+ const targetDefinition = hierarchy.definition(target);
178
+ if (sourceDefinition === undefined || targetDefinition === undefined) {
179
+ return false;
180
+ }
181
+ else if (sourceDefinition instanceof interface_definition_1.InterfaceDefinition || targetDefinition instanceof interface_definition_1.InterfaceDefinition) {
182
+ return true;
183
+ }
184
+ return hierarchy.isSubtype(source.getIdentifierName(), target.getIdentifierName())
185
+ || hierarchy.isSubtype(target.getIdentifierName(), source.getIdentifierName());
186
+ }
187
+ }
188
+ exports.CastViaGenericReference = CastViaGenericReference;
189
+ //# sourceMappingURL=cast_via_generic_reference.js.map
@@ -19,10 +19,13 @@ class ClearAfterDeclaration {
19
19
  shortDescription: `Finds CLEAR of a local variable that is still initial since its declaration`,
20
20
  extendedInformation: `Reports CLEAR x. when all of the following hold:
21
21
 
22
- * x is a local variable of a METHOD, FORM or FUNCTION, declared with DATA x TYPE/LIKE ... without VALUE
22
+ * x is a local variable of a METHOD, FORM or FUNCTION, declared with DATA x TYPE/LIKE ... without VALUE,
23
+ or with DATA(x) = VALUE t( ). with an explicit type and nothing in the parentheses
23
24
  * the CLEAR is the first reference to x after the declaration, it has no WITH addition
24
- * neither the DATA nor the CLEAR is inside a LOOP, DO, WHILE, SELECT loop, PROVIDE, or a TRY block with
25
- RETRY, RESUME or CATCH BEFORE UNWIND. DATA is not executed per iteration, so a CLEAR in a loop can be needed
25
+ * neither the declaration nor the CLEAR is inside a LOOP, DO, WHILE, SELECT loop, PROVIDE, or a TRY block with
26
+ RETRY, RESUME or CATCH BEFORE UNWIND. DATA is not executed per iteration, so a CLEAR in a loop can be needed.
27
+ DATA(x) = VALUE t( ). is executed per iteration, it may also be in the same loop as the CLEAR, if the CLEAR is
28
+ in the block of the declaration or a nested block
26
29
 
27
30
  Any later use of x does not matter, the CLEAR can only be reached with x still initial.
28
31
  Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
@@ -57,13 +60,11 @@ CLEAR lv_count.`,
57
60
  continue;
58
61
  }
59
62
  for (const variable of procedure.locals()) {
60
- const declaration = variable.declaration;
61
63
  const first = variable.occurrences[0];
62
- if (variable.inline || !(0, _procedure_analysis_1.plainData)(declaration) || first === undefined
64
+ if (first === undefined
63
65
  || first.kind !== _procedure_analysis_1.OccurrenceKind.Write
64
66
  || ((_a = (0, _procedure_analysis_1.plainClear)(first.statement)) === null || _a === void 0 ? void 0 : _a.getStart().equals(first.token.getStart())) !== true
65
- || procedure.loop(declaration) !== undefined
66
- || procedure.loop(first.statement) !== undefined) {
67
+ || !(0, _procedure_analysis_1.initialAtFirstReference)(procedure, variable, first.statement)) {
67
68
  continue;
68
69
  }
69
70
  const fix = (0, _procedure_analysis_1.deleteStatement)(procedure.file, first.statement);
@@ -0,0 +1,179 @@
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.ClearBeforeOverwrite = exports.ClearBeforeOverwriteConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Structures = __importStar(require("../abap/3_structures/structures"));
39
+ const basic_1 = require("../abap/types/basic");
40
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
41
+ const issue_1 = require("../issue");
42
+ const _basic_rule_config_1 = require("./_basic_rule_config");
43
+ const _irule_1 = require("./_irule");
44
+ const _procedure_analysis_1 = require("./_procedure_analysis");
45
+ class ClearBeforeOverwriteConf extends _basic_rule_config_1.BasicRuleConfig {
46
+ }
47
+ exports.ClearBeforeOverwriteConf = ClearBeforeOverwriteConf;
48
+ class ClearBeforeOverwrite {
49
+ constructor() {
50
+ this.conf = new ClearBeforeOverwriteConf();
51
+ }
52
+ getMetadata() {
53
+ return {
54
+ key: "clear_before_overwrite",
55
+ title: "CLEAR before overwrite",
56
+ shortDescription: `Finds CLEAR of a local variable whose next use overwrites it completely`,
57
+ extendedInformation: `Reports CLEAR v. when all of the following hold:
58
+
59
+ * v is a local variable of a METHOD, FORM or FUNCTION, its address is never taken: no field symbol,
60
+ REF #( ), GET REFERENCE, and it is not passed to a parameter by reference
61
+ * the CLEAR is not chained and has no WITH addition
62
+ * the next reference to v is a full overwrite in the same block: v = <expression>., including v = NEW ...,
63
+ or CREATE OBJECT v. with optional TYPE and EXPORTING, where nothing else in the statement references v.
64
+ Not ?=, READ TABLE INTO, offset/length or component writes, CREATE OBJECT with EXCEPTIONS, or other
65
+ statements that can leave v unchanged
66
+ * every statement between the CLEAR and the overwrite is a direct statement of the block, not a nested structure,
67
+ and does not reference v
68
+ * v is not a table with header line
69
+ * if a TRY or CATCH SYSTEM-EXCEPTIONS encloses the CLEAR, nothing from the CLEAR to the overwrite can raise
70
+ an exception, a CATCH could see the old value: the overwrite directly follows the CLEAR and assigns a variable
71
+ or a literal of identical type, or a reference variable
72
+
73
+ Unlike clear_after_declaration, the CLEAR can be in a loop.
74
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
75
+ INCLUDE, test seams, enhancements or native SQL.
76
+
77
+ The quick fix deletes the CLEAR. No release requirement.`,
78
+ tags: [_irule_1.RuleTag.Quickfix],
79
+ badExample: `CLEAR lv_name.
80
+ lv_name = iv_name.`,
81
+ goodExample: `lv_name = iv_name.`,
82
+ };
83
+ }
84
+ getConfig() {
85
+ return this.conf;
86
+ }
87
+ setConfig(conf) {
88
+ this.conf = conf;
89
+ }
90
+ initialize(reg) {
91
+ this.reg = reg;
92
+ return this;
93
+ }
94
+ run(obj) {
95
+ var _a;
96
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
97
+ if (analysis === undefined) {
98
+ return [];
99
+ }
100
+ const issues = [];
101
+ for (const procedure of analysis.procedures) {
102
+ if (procedure.supported === false) {
103
+ continue;
104
+ }
105
+ for (const variable of procedure.locals()) {
106
+ const type = variable.identifier.getType();
107
+ if (variable.escapes() || (type instanceof basic_1.TableType && type.isWithHeader())
108
+ || this.passedByReference(procedure, variable)) {
109
+ continue;
110
+ }
111
+ const occurrences = variable.occurrences;
112
+ for (let i = 0; i < occurrences.length - 1; i++) {
113
+ const clear = occurrences[i];
114
+ const next = occurrences[i + 1];
115
+ if (((_a = (0, _procedure_analysis_1.plainClear)(clear.statement)) === null || _a === void 0 ? void 0 : _a.getStart().equals(clear.token.getStart())) !== true
116
+ || clear.statement.getColon() !== undefined
117
+ || occurrences.some(o => o !== next && o.statement === next.statement)
118
+ || !this.overwrites(next)) {
119
+ continue;
120
+ }
121
+ const between = procedure.straightBetween(clear.statement, next.statement);
122
+ if (between === undefined
123
+ || (procedure.enclosedBy(clear.statement, [Structures.Try, Structures.CatchSystemExceptions])
124
+ && (between.length > 0 || !this.cannotRaise(procedure, variable, next.statement)))) {
125
+ continue;
126
+ }
127
+ const message = "CLEAR of " + variable.identifier.getName() + ", which is overwritten before it is read";
128
+ issues.push(issue_1.Issue.atStatement(procedure.file, clear.statement, message, this.getMetadata().key, this.conf.severity, (0, _procedure_analysis_1.deleteStatement)(procedure.file, clear.statement)));
129
+ }
130
+ }
131
+ }
132
+ return issues;
133
+ }
134
+ /** passed to a method, NEW, CREATE OBJECT or RAISE EXCEPTION, other than to a pass-by-value parameter;
135
+ * the called code could keep a reference to the parameter */
136
+ passedByReference(procedure, variable) {
137
+ var _a;
138
+ for (const read of variable.reads()) {
139
+ const path = (0, _procedure_analysis_1.expressionPath)(read.statement, read.token);
140
+ if (!path.some(e => e.get() instanceof Expressions.MethodCallParam || e.get() instanceof Expressions.ParameterListS
141
+ || e.get() instanceof Expressions.MethodParameters)) {
142
+ continue;
143
+ }
144
+ const formal = (0, _procedure_analysis_1.formalParameter)(procedure, read.statement, read.token);
145
+ if (((_a = formal === null || formal === void 0 ? void 0 : formal.parameter) === null || _a === void 0 ? void 0 : _a.getMeta().includes("pass_by_value" /* IdentifierMeta.PassByValue */)) !== true) {
146
+ return true;
147
+ }
148
+ }
149
+ return false;
150
+ }
151
+ /** v = <expression>. or CREATE OBJECT v [TYPE c] [EXPORTING ...]. */
152
+ overwrites(occurrence) {
153
+ var _a, _b;
154
+ const statement = occurrence.statement;
155
+ const move = (0, _procedure_analysis_1.plainMove)(statement);
156
+ if (move !== undefined) {
157
+ return move.operator === "=" && ((_a = (0, _procedure_analysis_1.singleTarget)(move.target)) === null || _a === void 0 ? void 0 : _a.getStart().equals(occurrence.token.getStart())) === true;
158
+ }
159
+ const create = (0, _procedure_analysis_1.plainCreateObject)(statement);
160
+ return create !== undefined && ((_b = (0, _procedure_analysis_1.singleTarget)(create.target)) === null || _b === void 0 ? void 0 : _b.getStart().equals(occurrence.token.getStart())) === true;
161
+ }
162
+ /** v = w. or v = literal. without conversion */
163
+ cannotRaise(procedure, variable, statement) {
164
+ const move = (0, _procedure_analysis_1.plainMove)(statement);
165
+ if (move === undefined) {
166
+ return false;
167
+ }
168
+ const type = variable.identifier.getType();
169
+ const field = (0, _procedure_analysis_1.singleField)(move.source);
170
+ const constant = move.source.getChildren().length === 1 ? move.source.findDirectExpression(Expressions.Constant) : undefined;
171
+ const identifier = field === undefined ? undefined : procedure.identifierAt(field);
172
+ const source = identifier instanceof _typed_identifier_1.TypedIdentifier ? identifier.getType()
173
+ : constant === undefined ? undefined : (0, _procedure_analysis_1.literalType)(constant);
174
+ return source !== undefined
175
+ && ((0, _procedure_analysis_1.identicalTypes)(source, type) || ((0, _procedure_analysis_1.isReferenceType)(source) && (0, _procedure_analysis_1.isReferenceType)(type) && field !== undefined));
176
+ }
177
+ }
178
+ exports.ClearBeforeOverwrite = ClearBeforeOverwrite;
179
+ //# sourceMappingURL=clear_before_overwrite.js.map
@@ -59,8 +59,9 @@ c = <constant>, c <> <constant>, EQ, NE, c IS INITIAL, c IS NOT INITIAL, optiona
59
59
  The declaration must be executed before the condition on every path: the IF is in the block of the
60
60
  declaration or a nested block. The quick fix deletes the declaration.
61
61
 
62
- 2. DATA v TYPE ... without VALUE, where v is never changed, except by CLEAR v., and its address is never taken,
63
- in a condition v IS [NOT] INITIAL or v IS [NOT] BOUND. The declaration is kept.
62
+ 2. DATA v TYPE ... without VALUE, or DATA(v) = VALUE t( ). with an explicit type and nothing in the parentheses,
63
+ where v is never changed, except by CLEAR v., and its address is never taken, in a condition v IS [NOT] INITIAL
64
+ or v IS [NOT] BOUND. The declaration is kept.
64
65
 
65
66
  The quick fix removes the IF branch that is never taken, or keeps the branch that is always taken and
66
67
  removes the rest, also in ELSEIF chains: a first IF branch that is never taken turns the next ELSEIF into IF,
@@ -101,12 +102,12 @@ ENDIF.`,
101
102
  }
102
103
  const candidates = [];
103
104
  for (const variable of procedure.locals()) {
104
- if (variable.inline) {
105
- this.constantVariable(procedure, variable, candidates);
106
- }
107
- else {
105
+ if ((0, _procedure_analysis_1.initialDeclaration)(variable) !== undefined) {
108
106
  this.initialVariable(procedure, variable, candidates);
109
107
  }
108
+ else if (variable.inline) {
109
+ this.constantVariable(procedure, variable, candidates);
110
+ }
110
111
  }
111
112
  issues.push(...(0, _procedure_analysis_1.foldIssues)(procedure.file, candidates, this.getMetadata().key, this.conf.severity));
112
113
  }
@@ -142,9 +143,9 @@ ENDIF.`,
142
143
  fix: (0, _procedure_analysis_1.mergeEdits)(procedure.file, [fold, (0, _procedure_analysis_1.deleteStatement)(procedure.file, declaration)]),
143
144
  });
144
145
  }
145
- /** DATA v TYPE ... never changed: IF v IS INITIAL / IS BOUND */
146
+ /** DATA v TYPE ... or DATA(v) = VALUE t( ). never changed: IF v IS INITIAL / IS BOUND */
146
147
  initialVariable(procedure, variable, candidates) {
147
- if (!(0, _procedure_analysis_1.plainData)(variable.declaration) || variable.escapes()
148
+ if (variable.escapes()
148
149
  || variable.writes().some(w => { var _a; return ((_a = (0, _procedure_analysis_1.plainClear)(w.statement)) === null || _a === void 0 ? void 0 : _a.getStart().equals(w.token.getStart())) !== true; })) {
149
150
  return;
150
151
  }
@@ -35,6 +35,8 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.CopyChain = exports.CopyChainConf = void 0;
37
37
  const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
39
+ const basic_1 = require("../abap/types/basic");
38
40
  const edit_helper_1 = require("../edit_helper");
39
41
  const issue_1 = require("../issue");
40
42
  const _basic_rule_config_1 = require("./_basic_rule_config");
@@ -52,28 +54,49 @@ class CopyChain {
52
54
  key: "copy_chain",
53
55
  title: "Copy chain",
54
56
  shortDescription: `Finds a local variable that only passes a value on from one variable to another`,
55
- extendedInformation: `Reports DATA v TYPE ... [CLEAR v.] v = src. ... y = v. when all of the following hold:
57
+ extendedInformation: `Reports a local variable that only passes a value on, in two cases.
56
58
 
57
- * v is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE
59
+ 1. DATA v TYPE ... [CLEAR v.] v = src. ... y = v. when all of the following hold:
60
+
61
+ * v is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE, or with
62
+ DATA(v) = VALUE t( ). with an explicit type and nothing in the parentheses
58
63
  * v has exactly one write besides CLEAR: v = src., and all CLEARs are before it
59
64
  * v has exactly one read, the complete source of y = v.
60
- * src is a local variable, its address is never taken, and it is not written from v = src. until y = v.
65
+ * src is a local variable, or a pass-by-value or RETURNING parameter of a method or form, its address is never
66
+ taken, and it is not written from v = src. until y = v.
61
67
  * v = src. is executed before y = v. on every path: y = v. is in the same block or a nested block,
62
68
  and both are in the same loop
63
69
  * v and src are both reference types, or have identical types; for other types the copy into v can convert,
64
70
  e.g. truncate a string into a c field. For DATA(y) = v. the types must be identical, else y changes its type
65
71
  * the declaration, the CLEARs and v = src. are not chained
66
72
 
67
- Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
68
- INCLUDE, test seams, enhancements or native SQL.
69
-
70
73
  The quick fix deletes the declaration, the CLEARs and v = src., and changes the read to y = src.
71
- No release requirement.`,
74
+
75
+ 2. DATA a TYPE REF TO c. CREATE OBJECT a. b = a. when all of the following hold:
76
+
77
+ * a is a local variable, declared with DATA without VALUE and not chained, its address is never taken
78
+ * the only other references to a are CREATE OBJECT a. and the complete source of b = a., which is the next
79
+ statement in the same block, apart from declarations like DATA in between
80
+ * CREATE OBJECT has at most TYPE and EXPORTING, it is not chained. With EXCEPTIONS, a classic exception in the
81
+ constructor would leave b unchanged instead of initial
82
+ * b is a local variable, or a pass-by-value or RETURNING parameter, whose address is never taken, it is
83
+ declared before the CREATE OBJECT and not used in its parameters, so the constructor cannot see b
84
+ * b = a. passes the syntax check, so the static type of b accepts the class of a
85
+
86
+ The quick fix deletes the declaration of a and b = a., and creates the object in b, keeping TYPE and EXPORTING.
87
+ If the static types of a and b differ, it adds TYPE with the class of a.
88
+
89
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
90
+ INCLUDE, test seams, enhancements or native SQL. No release requirement.`,
72
91
  tags: [_irule_1.RuleTag.Quickfix],
73
92
  badExample: `DATA lo_copy TYPE REF TO zcl_node.
74
93
  lo_copy = lo_node.
75
- ro_result = lo_copy.`,
76
- goodExample: `ro_result = lo_node.`,
94
+ ro_result = lo_copy.
95
+ DATA lo_new TYPE REF TO zcl_node.
96
+ CREATE OBJECT lo_new.
97
+ ro_other = lo_new.`,
98
+ goodExample: `ro_result = lo_node.
99
+ CREATE OBJECT ro_other.`,
77
100
  };
78
101
  }
79
102
  getConfig() {
@@ -99,7 +122,7 @@ ro_result = lo_copy.`,
99
122
  }
100
123
  for (const variable of procedure.locals()) {
101
124
  const declaration = variable.declaration;
102
- if (variable.inline || !(0, _procedure_analysis_1.plainData)(declaration) || declaration.getColon() !== undefined
125
+ if ((0, _procedure_analysis_1.initialDeclaration)(variable) === undefined || declaration.getColon() !== undefined
103
126
  || variable.escapes()) {
104
127
  continue;
105
128
  }
@@ -120,6 +143,13 @@ ro_result = lo_copy.`,
120
143
  }
121
144
  const write = writes[0];
122
145
  const read = reads[0];
146
+ if (write.statement.get() instanceof Statements.CreateObject) {
147
+ const issue = clears.length === 0 ? this.createObject(procedure, variable, write, read) : undefined;
148
+ if (issue !== undefined) {
149
+ issues.push(issue);
150
+ }
151
+ continue;
152
+ }
123
153
  const assign = (0, _procedure_analysis_1.plainMove)(write.statement);
124
154
  const use = (0, _procedure_analysis_1.plainMove)(read.statement);
125
155
  if (assign === undefined || use === undefined || assign.operator !== "=" || use.operator !== "="
@@ -157,6 +187,46 @@ ro_result = lo_copy.`,
157
187
  }
158
188
  return issues;
159
189
  }
190
+ /** DATA a TYPE REF TO c. CREATE OBJECT a. b = a. */
191
+ createObject(procedure, variable, write, read) {
192
+ var _a, _b, _c;
193
+ const create = (0, _procedure_analysis_1.plainCreateObject)(write.statement);
194
+ const use = (0, _procedure_analysis_1.plainMove)(read.statement);
195
+ const token = use === undefined ? undefined : (0, _procedure_analysis_1.singleTarget)(use.target);
196
+ const target = token === undefined ? undefined : procedure.private(procedure.identifierAt(token));
197
+ if (create === undefined || use === undefined || use.operator !== "=" || target === undefined
198
+ || ((_a = (0, _procedure_analysis_1.singleTarget)(create.target)) === null || _a === void 0 ? void 0 : _a.getStart().equals(write.token.getStart())) !== true
199
+ || ((_b = (0, _procedure_analysis_1.singleField)(use.source)) === null || _b === void 0 ? void 0 : _b.getStart().equals(read.token.getStart())) !== true
200
+ || read.statement !== procedure.nextExecuted(write.statement) || !procedure.isSimple(read.statement)
201
+ || target.occurrences.some(o => o.statement === write.statement)
202
+ || (target.declaration !== undefined && procedure.index(target.declaration) > write.index)) {
203
+ return undefined;
204
+ }
205
+ const type = variable.identifier.getType();
206
+ const targetType = target.identifier.getType();
207
+ let text = (0, _procedure_analysis_1.nodeText)(procedure.file, use.target);
208
+ if (!(type instanceof basic_1.ObjectReferenceType)
209
+ || !(targetType instanceof basic_1.ObjectReferenceType || targetType instanceof basic_1.GenericObjectReferenceType)) {
210
+ return undefined;
211
+ }
212
+ else if (create.type === undefined && !(0, _procedure_analysis_1.identicalTypes)(type, targetType)) {
213
+ // the class of a, as written in its declaration
214
+ const declared = (_c = variable.declaration.findDirectExpression(Expressions.DataDefinition)) === null || _c === void 0 ? void 0 : _c.findDirectExpression(Expressions.Type);
215
+ const name = declared === null || declared === void 0 ? void 0 : declared.findDirectExpression(Expressions.TypeName);
216
+ if (name === undefined || (declared === null || declared === void 0 ? void 0 : declared.getTokens().length) !== 4) {
217
+ return undefined;
218
+ }
219
+ text = text + " TYPE " + (0, _procedure_analysis_1.nodeText)(procedure.file, name);
220
+ }
221
+ const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, [
222
+ (0, _procedure_analysis_1.deleteStatement)(procedure.file, variable.declaration),
223
+ edit_helper_1.EditHelper.replaceToken(procedure.file, write.token, text),
224
+ (0, _procedure_analysis_1.deleteStatement)(procedure.file, read.statement)
225
+ ]);
226
+ const message = "Copy chain via " + variable.identifier.getName() + ", create the object in "
227
+ + (0, _procedure_analysis_1.nodeText)(procedure.file, use.target) + " directly";
228
+ return issue_1.Issue.atStatement(procedure.file, read.statement, message, this.getMetadata().key, this.conf.severity, fix);
229
+ }
160
230
  }
161
231
  exports.CopyChain = CopyChain;
162
232
  //# sourceMappingURL=copy_chain.js.map
@@ -54,7 +54,9 @@ class CopyOfInitialVariable {
54
54
  shortDescription: `Finds assignments from a local variable that is always initial`,
55
55
  extendedInformation: `Reports DATA v TYPE ... [CLEAR v.] x = v. when all of the following hold:
56
56
 
57
- * v is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE
57
+ * v is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE, or with
58
+ DATA(v) = VALUE t( ). with an explicit type and nothing in the parentheses. Both make v initial, the latter
59
+ every time it is executed, so v is initial wherever it is read, also in loops
58
60
  * v is never changed, except by CLEAR v., and its address is never taken
59
61
  * v has exactly one read, the complete source of x = v., where x is not an inline declaration
60
62
  * x and v are both reference types, or have identical types. Initial values convert differently between
@@ -97,7 +99,7 @@ ro_result = lo_empty.`,
97
99
  const declaration = variable.declaration;
98
100
  const reads = variable.reads();
99
101
  const writes = variable.writes();
100
- if (variable.inline || !(0, _procedure_analysis_1.plainData)(declaration) || declaration.getColon() !== undefined
102
+ if ((0, _procedure_analysis_1.initialDeclaration)(variable) === undefined || declaration.getColon() !== undefined
101
103
  || variable.escapes() || reads.length !== 1
102
104
  || writes.some(w => { var _a; return ((_a = (0, _procedure_analysis_1.plainClear)(w.statement)) === null || _a === void 0 ? void 0 : _a.getStart().equals(w.token.getStart())) !== true
103
105
  || w.statement.getColon() !== undefined; })) {
@@ -29,11 +29,14 @@ __exportStar(require("./allowed_object_naming"), exports);
29
29
  __exportStar(require("./allowed_object_types"), exports);
30
30
  __exportStar(require("./ambiguous_statement"), exports);
31
31
  __exportStar(require("./avoid_use"), exports);
32
+ __exportStar(require("./bdef_check_syntax"), exports);
33
+ __exportStar(require("./bdef_parser_error"), exports);
32
34
  __exportStar(require("./begin_end_names"), exports);
33
35
  __exportStar(require("./begin_single_include"), exports);
34
36
  __exportStar(require("./boolean_flag_via_if"), exports);
35
37
  __exportStar(require("./bound_check_after_create"), exports);
36
38
  __exportStar(require("./call_transaction_authority_check"), exports);
39
+ __exportStar(require("./cast_via_generic_reference"), exports);
37
40
  __exportStar(require("./catch_and_raise"), exports);
38
41
  __exportStar(require("./cds_association_name"), exports);
39
42
  __exportStar(require("./cds_check_syntax"), exports);
@@ -54,6 +57,7 @@ __exportStar(require("./check_syntax"), exports);
54
57
  __exportStar(require("./check_text_elements"), exports);
55
58
  __exportStar(require("./check_transformation_exists"), exports);
56
59
  __exportStar(require("./clear_after_declaration"), exports);
60
+ __exportStar(require("./clear_before_overwrite"), exports);
57
61
  __exportStar(require("./clear_exporting_parameters"), exports);
58
62
  __exportStar(require("./class_attribute_names"), exports);
59
63
  __exportStar(require("./classic_exceptions_overlap"), exports);