@abaplint/core 2.120.80 → 2.120.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,132 @@
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.AliasNeverReassigned = exports.AliasNeverReassignedConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const edit_helper_1 = require("../edit_helper");
39
+ const issue_1 = require("../issue");
40
+ const _basic_rule_config_1 = require("./_basic_rule_config");
41
+ const _irule_1 = require("./_irule");
42
+ const _procedure_analysis_1 = require("./_procedure_analysis");
43
+ class AliasNeverReassignedConf extends _basic_rule_config_1.BasicRuleConfig {
44
+ }
45
+ exports.AliasNeverReassignedConf = AliasNeverReassignedConf;
46
+ class AliasNeverReassigned {
47
+ constructor() {
48
+ this.conf = new AliasNeverReassignedConf();
49
+ }
50
+ getMetadata() {
51
+ return {
52
+ key: "alias_never_reassigned",
53
+ title: "Alias never reassigned",
54
+ shortDescription: `Finds DATA(a) = b. where neither a nor b changes afterwards, so b can be read instead of a`,
55
+ extendedInformation: `Reports DATA(a) = b. when all of the following hold:
56
+
57
+ * a is a local variable of a METHOD, FORM or FUNCTION, and has at least two more references, all of them reads,
58
+ which do not take its address, bind a field symbol to it, or pass it to USING, CHANGING or EXPORTING
59
+ * b is a local variable, or a component of one, whose address is never taken and which is not written
60
+ after the declaration of a; or b is a constant or me
61
+ * the declaration is executed before every read on every path: each read is in the block of the declaration
62
+ or a nested block
63
+
64
+ a has the type of b, so reading b instead does not change any conversion.
65
+ A constant b read in IF, ELSEIF, WHILE, CHECK, CASE or WHEN is not reported, see constant_condition.
66
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
67
+ INCLUDE, test seams, enhancements or native SQL.
68
+
69
+ The quick fix deletes the declaration and replaces every read of a with b. No release requirement.
70
+
71
+ See also unnecessary_copy for copies that are read once, also of attributes and parameters.`,
72
+ tags: [_irule_1.RuleTag.Quickfix],
73
+ badExample: `DATA(lo_file) = lo_input.
74
+ lo_file->read( ).
75
+ rv_name = lo_file->get_name( ).`,
76
+ goodExample: `lo_input->read( ).
77
+ rv_name = lo_input->get_name( ).`,
78
+ };
79
+ }
80
+ getConfig() {
81
+ return this.conf;
82
+ }
83
+ setConfig(conf) {
84
+ this.conf = conf;
85
+ }
86
+ initialize(reg) {
87
+ this.reg = reg;
88
+ return this;
89
+ }
90
+ run(obj) {
91
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
92
+ if (analysis === undefined) {
93
+ return [];
94
+ }
95
+ const issues = [];
96
+ for (const procedure of analysis.procedures) {
97
+ if (procedure.supported === false) {
98
+ continue;
99
+ }
100
+ for (const variable of procedure.locals()) {
101
+ const declaration = variable.declaration;
102
+ const move = variable.inline ? (0, _procedure_analysis_1.plainMove)(declaration) : undefined;
103
+ const reads = variable.occurrences;
104
+ if (move === undefined || move.operator !== "="
105
+ || move.target.getChildren().length !== 1
106
+ || move.target.findDirectExpression(Expressions.InlineData) === undefined
107
+ || reads.length < 2 || reads.some(r => r.kind !== _procedure_analysis_1.OccurrenceKind.Read)
108
+ || reads.some(r => !procedure.dominates(declaration, r.statement))) {
109
+ continue;
110
+ }
111
+ const source = (0, _procedure_analysis_1.dataSource)(procedure, move.source);
112
+ if (source === undefined || source.base === variable
113
+ || (source.kind === _procedure_analysis_1.SourceKind.Immutable && reads.some(r => (0, _procedure_analysis_1.isCondition)(r.statement)))
114
+ || (source.kind === _procedure_analysis_1.SourceKind.Local
115
+ && (source.base.escapes() || source.base.writes().some(w => w.index >= procedure.index(declaration))))
116
+ || source.kind === _procedure_analysis_1.SourceKind.Volatile) {
117
+ continue;
118
+ }
119
+ const edits = [(0, _procedure_analysis_1.deleteStatement)(procedure.file, declaration)];
120
+ for (const read of reads) {
121
+ edits.push(edit_helper_1.EditHelper.replaceToken(procedure.file, read.token, source.text));
122
+ }
123
+ const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, edits);
124
+ const message = variable.identifier.getName() + " is a copy of " + source.text + ", and neither changes";
125
+ issues.push(issue_1.Issue.atStatement(procedure.file, declaration, message, this.getMetadata().key, this.conf.severity, fix));
126
+ }
127
+ }
128
+ return issues;
129
+ }
130
+ }
131
+ exports.AliasNeverReassigned = AliasNeverReassigned;
132
+ //# sourceMappingURL=alias_never_reassigned.js.map
@@ -0,0 +1,175 @@
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.BooleanFlagViaIf = exports.BooleanFlagViaIfConf = void 0;
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");
40
+ const edit_helper_1 = require("../edit_helper");
41
+ const issue_1 = require("../issue");
42
+ const version_1 = require("../version");
43
+ const _basic_rule_config_1 = require("./_basic_rule_config");
44
+ const _irule_1 = require("./_irule");
45
+ const _procedure_analysis_1 = require("./_procedure_analysis");
46
+ class BooleanFlagViaIfConf extends _basic_rule_config_1.BasicRuleConfig {
47
+ }
48
+ exports.BooleanFlagViaIfConf = BooleanFlagViaIfConf;
49
+ class BooleanFlagViaIf {
50
+ constructor() {
51
+ this.conf = new BooleanFlagViaIfConf();
52
+ }
53
+ getMetadata() {
54
+ return {
55
+ key: "boolean_flag_via_if",
56
+ title: "Boolean flag via IF",
57
+ shortDescription: `Finds a flag that is initialized to false and set to true in a single IF, use xsdbool`,
58
+ extendedInformation: `Reports DATA(f) = VALUE abap_bool( )., DATA(f) = abap_false. or DATA f TYPE abap_bool.,
59
+ later followed by IF cond. f = abap_true. ENDIF., when all of the following hold:
60
+
61
+ * f is a local variable of a METHOD, FORM or FUNCTION, of type c LENGTH 1, its address is never taken
62
+ * the IF has no ELSEIF and no ELSE, its only statement is f = abap_true. and it contains no comments
63
+ * f has no other write, and is not read before the IF
64
+ * the IF is in the same block and the same loop as the declaration; DATA f TYPE abap_bool. is not
65
+ executed per loop iteration, so neither it nor the IF may be in a loop
66
+
67
+ Requires xsdbool, ie. 7.40 SP08 or above, ABAP Cloud or open-abap.
68
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
69
+ INCLUDE, test seams, enhancements or native SQL.
70
+
71
+ The quick fix deletes an inline declaration and replaces the IF with DATA(f) = xsdbool( cond ).,
72
+ or keeps DATA f TYPE abap_bool. and replaces the IF with f = xsdbool( cond ).
73
+
74
+ use_bool_expression covers IF with an ELSE branch, without looking at the declaration and the other uses.`,
75
+ tags: [_irule_1.RuleTag.Quickfix],
76
+ badExample: `DATA(lv_found) = abap_false.
77
+ IF line_exists( lt_rows[ id = lv_id ] ).
78
+ lv_found = abap_true.
79
+ ENDIF.`,
80
+ goodExample: `DATA(lv_found) = xsdbool( line_exists( lt_rows[ id = lv_id ] ) ).`,
81
+ };
82
+ }
83
+ getConfig() {
84
+ return this.conf;
85
+ }
86
+ setConfig(conf) {
87
+ this.conf = conf;
88
+ }
89
+ initialize(reg) {
90
+ this.reg = reg;
91
+ return this;
92
+ }
93
+ run(obj) {
94
+ var _a, _b;
95
+ const config = this.reg.getConfig();
96
+ if (!(0, version_1.releaseAtLeast)(config.getRelease(), version_1.Release.v740sp08)
97
+ && config.getLanguageVersion() !== version_1.LanguageVersion.Cloud
98
+ && !config.isOpenABAP()) {
99
+ return [];
100
+ }
101
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
102
+ if (analysis === undefined) {
103
+ return [];
104
+ }
105
+ const issues = [];
106
+ for (const procedure of analysis.procedures) {
107
+ if (procedure.supported === false) {
108
+ continue;
109
+ }
110
+ for (const variable of procedure.locals()) {
111
+ const declaration = variable.declaration;
112
+ const type = variable.identifier.getType();
113
+ const writes = variable.writes();
114
+ if (!(type instanceof basic_1.CharacterType) || type.getLength() !== 1 || variable.escapes()
115
+ || writes.length !== 1 || !this.initiallyFalse(procedure, variable.inline, declaration)) {
116
+ continue;
117
+ }
118
+ const write = writes[0];
119
+ const move = (0, _procedure_analysis_1.plainMove)(write.statement);
120
+ const value = move === undefined ? undefined : (0, _procedure_analysis_1.characterConstant)(procedure, move.source);
121
+ const check = procedure.statements[write.index - 1];
122
+ const structure = procedure.ifStructure(check);
123
+ const branches = structure === undefined ? undefined : (0, _procedure_analysis_1.ifBranches)(structure);
124
+ const endIf = structure === null || structure === void 0 ? void 0 : structure.findDirectStatement(Statements.EndIf);
125
+ if (move === undefined || move.operator !== "=" || value === undefined || !(0, _procedure_analysis_1.sameCharacters)(value, "X")
126
+ || ((_a = (0, _procedure_analysis_1.singleTarget)(move.target)) === null || _a === void 0 ? void 0 : _a.getStart().equals(write.token.getStart())) !== true
127
+ || !((check === null || check === void 0 ? void 0 : check.get()) instanceof Statements.If) || check.getColon() !== undefined
128
+ || structure === undefined || endIf === undefined || (branches === null || branches === void 0 ? void 0 : branches.length) !== 1
129
+ || ((_b = branches[0].body) === null || _b === void 0 ? void 0 : _b.findAllStatementNodes().length) !== 1
130
+ || procedure.block(check) !== procedure.block(declaration)
131
+ || !procedure.sameLoop(check, declaration)
132
+ || (!variable.inline && (procedure.loop(check) !== undefined || procedure.loop(declaration) !== undefined))
133
+ || variable.occurrences.some(o => o.kind === _procedure_analysis_1.OccurrenceKind.Read && o.index <= procedure.index(endIf))
134
+ || procedure.hasComment(check.getStart(), endIf.getEnd())) {
135
+ continue;
136
+ }
137
+ const cond = check.findDirectExpression(Expressions.Cond);
138
+ if (cond === undefined) {
139
+ continue;
140
+ }
141
+ const name = write.token.getStr();
142
+ const assignment = (variable.inline ? "DATA(" + name + ")" : name) + " = xsdbool( " + (0, _procedure_analysis_1.nodeText)(procedure.file, cond) + " ).";
143
+ const edits = [edit_helper_1.EditHelper.replaceRange(procedure.file, check.getStart(), endIf.getEnd(), assignment)];
144
+ if (variable.inline) {
145
+ edits.push((0, _procedure_analysis_1.deleteStatement)(procedure.file, declaration));
146
+ }
147
+ const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, edits);
148
+ issues.push(issue_1.Issue.atStatement(procedure.file, check, "Use xsdbool to set " + name, this.getMetadata().key, this.conf.severity, fix));
149
+ }
150
+ }
151
+ return issues;
152
+ }
153
+ /** DATA(f) = VALUE abap_bool( ). DATA(f) = abap_false. or DATA f TYPE abap_bool. */
154
+ initiallyFalse(procedure, inline, declaration) {
155
+ if (inline === false) {
156
+ return (0, _procedure_analysis_1.plainData)(declaration) && declaration.getColon() === undefined;
157
+ }
158
+ const move = (0, _procedure_analysis_1.plainMove)(declaration);
159
+ if (move === undefined || move.operator !== "=" || move.target.getChildren().length !== 1
160
+ || move.target.findDirectExpression(Expressions.InlineData) === undefined) {
161
+ return false;
162
+ }
163
+ const constant = (0, _procedure_analysis_1.characterConstant)(procedure, move.source);
164
+ return (constant !== undefined && (0, _procedure_analysis_1.sameCharacters)(constant, "")) || this.initialValue(move.source);
165
+ }
166
+ /** VALUE type( ) */
167
+ initialValue(source) {
168
+ const tokens = source.getTokens();
169
+ return tokens.length === 4 && tokens[0].getStr().toUpperCase() === "VALUE"
170
+ && source.findDirectExpression(Expressions.TypeNameOrInfer) !== undefined
171
+ && tokens[1].getStr() !== "#" && tokens[2].getStr() === "(" && tokens[3].getStr() === ")";
172
+ }
173
+ }
174
+ exports.BooleanFlagViaIf = BooleanFlagViaIf;
175
+ //# sourceMappingURL=boolean_flag_via_if.js.map
@@ -0,0 +1,262 @@
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.BoundCheckAfterCreate = exports.BoundCheckAfterCreateConf = void 0;
37
+ const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
39
+ const nodes_1 = require("../abap/nodes");
40
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
41
+ const basic_1 = require("../abap/types/basic");
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 BoundCheckAfterCreateConf extends _basic_rule_config_1.BasicRuleConfig {
46
+ }
47
+ exports.BoundCheckAfterCreateConf = BoundCheckAfterCreateConf;
48
+ class BoundCheckAfterCreate {
49
+ constructor() {
50
+ this.conf = new BoundCheckAfterCreateConf();
51
+ }
52
+ getMetadata() {
53
+ return {
54
+ key: "bound_check_after_create",
55
+ title: "Bound check after create",
56
+ shortDescription: `Finds IF r IS BOUND / IS NOT BOUND directly after r was created`,
57
+ extendedInformation: `Reports IF r IS BOUND. and IF r IS NOT BOUND. after CREATE OBJECT r., r = NEW ...( ). or
58
+ DATA(r) = NEW ...( ). in the same block, when all of the following hold:
59
+
60
+ * r is a local variable whose address is never taken, or an attribute: attr, me->attr or local->attr
61
+ * CREATE OBJECT has no EXCEPTIONS addition and no dynamic type, NEW is not followed by a method call
62
+ * every statement in between is a direct statement of the same block, not a nested structure
63
+ * a local r is not written in between
64
+ * for an attribute: no method, function or form call and no constructor in between, the local before ->
65
+ is not written, and no write in between can change the attribute: no write to an attribute with the
66
+ same name, to a field symbol, via ->*, or to a parameter passed by reference
67
+
68
+ The quick fix keeps the IS BOUND branch and removes the rest, or removes the IS NOT BOUND branch;
69
+ nothing is reported if a removed branch declares data that is used outside of it.
70
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
71
+ INCLUDE, test seams, enhancements or native SQL. No release requirement.`,
72
+ tags: [_irule_1.RuleTag.Quickfix],
73
+ badExample: `CREATE OBJECT lo_log.
74
+ IF lo_log IS BOUND.
75
+ lo_log->add( 'started' ).
76
+ ENDIF.`,
77
+ goodExample: `CREATE OBJECT lo_log.
78
+ lo_log->add( 'started' ).`,
79
+ };
80
+ }
81
+ getConfig() {
82
+ return this.conf;
83
+ }
84
+ setConfig(conf) {
85
+ this.conf = conf;
86
+ }
87
+ initialize(reg) {
88
+ this.reg = reg;
89
+ return this;
90
+ }
91
+ run(obj) {
92
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
93
+ if (analysis === undefined) {
94
+ return [];
95
+ }
96
+ const issues = [];
97
+ for (const procedure of analysis.procedures) {
98
+ if (procedure.supported === false) {
99
+ continue;
100
+ }
101
+ const candidates = [];
102
+ for (const statement of procedure.statements) {
103
+ const predicate = (0, _procedure_analysis_1.simplePredicate)(statement);
104
+ if (!(statement.get() instanceof Statements.If) || (predicate === null || predicate === void 0 ? void 0 : predicate.kind) !== _procedure_analysis_1.PredicateKind.Bound) {
105
+ continue;
106
+ }
107
+ const checked = this.path(procedure, predicate.left);
108
+ const create = checked === undefined ? undefined : this.findCreate(procedure, statement, checked);
109
+ const structure = procedure.ifStructure(statement);
110
+ if (create === undefined || structure === undefined) {
111
+ continue;
112
+ }
113
+ const bound = predicate.negated === false;
114
+ const fix = (0, _procedure_analysis_1.foldIf)(procedure, structure, statement, bound);
115
+ if (fix === undefined) {
116
+ continue;
117
+ }
118
+ const message = predicate.left.concatTokens() + " is always bound, it is created in line " + create.getStart().getRow();
119
+ candidates.push({ structure, statement, message, fix });
120
+ }
121
+ issues.push(...(0, _procedure_analysis_1.foldIssues)(procedure.file, candidates, this.getMetadata().key, this.conf.severity));
122
+ }
123
+ return issues;
124
+ }
125
+ /** the CREATE OBJECT or NEW before the IF in the same block, with only straight statements in between
126
+ * that cannot change the reference */
127
+ findCreate(procedure, check, path) {
128
+ const index = procedure.index(check);
129
+ const block = procedure.block(check);
130
+ for (let i = index - 1; i >= 0; i--) {
131
+ const statement = procedure.statements[i];
132
+ if (procedure.block(statement) !== block || !procedure.isSimple(statement)) {
133
+ return undefined;
134
+ }
135
+ const target = this.createdTarget(statement);
136
+ const created = target === undefined ? undefined : this.path(procedure, target);
137
+ if (created !== undefined && this.samePath(created, path)) {
138
+ const between = procedure.straightBetween(statement, check);
139
+ return between !== undefined && between.every(s => this.keeps(procedure, s, path)) ? statement : undefined;
140
+ }
141
+ }
142
+ return undefined;
143
+ }
144
+ /** the target of CREATE OBJECT r. or r = NEW ...( ). */
145
+ createdTarget(statement) {
146
+ if (statement.get() instanceof Statements.CreateObject) {
147
+ if (statement.findDirectTokenByText("EXCEPTIONS") !== undefined
148
+ || statement.findFirstExpression(Expressions.Dynamic) !== undefined
149
+ || statement.getColon() !== undefined) {
150
+ return undefined;
151
+ }
152
+ return statement.findDirectExpression(Expressions.Target);
153
+ }
154
+ const move = (0, _procedure_analysis_1.plainMove)(statement);
155
+ const chain = move === null || move === void 0 ? void 0 : move.source.findDirectExpression(Expressions.MethodCallChain);
156
+ if (move === undefined || move.operator !== "=" || move.source.getChildren().length !== 1
157
+ || (chain === null || chain === void 0 ? void 0 : chain.getChildren().length) !== 1 || chain.findDirectExpression(Expressions.NewObject) === undefined) {
158
+ return undefined;
159
+ }
160
+ return move.target;
161
+ }
162
+ /** r, DATA(r), attr, me->attr or local->attr, as target or source */
163
+ path(procedure, node) {
164
+ var _a;
165
+ let children = node.getChildren();
166
+ if (node.get() instanceof Expressions.Source) {
167
+ const chain = node.findDirectExpression(Expressions.FieldChain);
168
+ if (children.length !== 1 || chain === undefined) {
169
+ return undefined;
170
+ }
171
+ children = chain.getChildren();
172
+ }
173
+ else if (children.length === 1 && node.findDirectExpression(Expressions.InlineData) !== undefined) {
174
+ const name = node.findDirectExpression(Expressions.InlineData).findDirectExpression(Expressions.TargetField);
175
+ const local = name === undefined ? undefined : procedure.local(procedure.identifierAt(name.getFirstToken()));
176
+ return local === undefined || local.escapes() ? undefined : { local, attribute: undefined };
177
+ }
178
+ const first = procedure.identifierAt((_a = children[0]) === null || _a === void 0 ? void 0 : _a.getFirstToken());
179
+ if (!(children[0] instanceof nodes_1.ExpressionNode) || !(first instanceof _typed_identifier_1.TypedIdentifier)) {
180
+ return undefined;
181
+ }
182
+ const local = procedure.local(first);
183
+ if (children.length === 1) {
184
+ if (local !== undefined) {
185
+ return local.escapes() ? undefined : { local, attribute: undefined };
186
+ }
187
+ return this.isAttribute(procedure, first) ? { local: undefined, attribute: first } : undefined;
188
+ }
189
+ const attribute = children[2] instanceof nodes_1.ExpressionNode && children[2].get() instanceof Expressions.AttributeName
190
+ ? procedure.identifierAt(children[2].getFirstToken()) : undefined;
191
+ if (children.length !== 3 || !(children[1] instanceof nodes_1.TokenNode) || children[1].getFirstToken().getStr() !== "->"
192
+ || attribute === undefined || !this.isAttribute(procedure, attribute)) {
193
+ return undefined;
194
+ }
195
+ if ((0, _procedure_analysis_1.isImmutable)(first) && first.getType() instanceof basic_1.ObjectReferenceType) {
196
+ return { local: undefined, attribute };
197
+ }
198
+ else if (local !== undefined && !local.escapes()) {
199
+ return { local, attribute };
200
+ }
201
+ return undefined;
202
+ }
203
+ isAttribute(procedure, identifier) {
204
+ if (!(identifier instanceof _typed_identifier_1.TypedIdentifier) || identifier.getMeta().includes("static" /* IdentifierMeta.Static */)
205
+ || procedure.contains(identifier)) {
206
+ return false;
207
+ }
208
+ const meta = identifier.getMeta();
209
+ return !meta.some(m => m === "importing" /* IdentifierMeta.MethodImporting */ || m === "exporting" /* IdentifierMeta.MethodExporting */
210
+ || m === "changing" /* IdentifierMeta.MethodChanging */ || m === "returning" /* IdentifierMeta.MethodReturning */
211
+ || m === "form_parameter" /* IdentifierMeta.FormParameter */ || m === "event_parameter" /* IdentifierMeta.EventParameter */
212
+ || m === "function_module_importing" /* IdentifierMeta.FunctionModuleImporting */ || m === "function_module_exporting" /* IdentifierMeta.FunctionModuleExporting */
213
+ || m === "function_module_changing" /* IdentifierMeta.FunctionModuleChanging */ || m === "function_module_tables" /* IdentifierMeta.FunctionModuleTables */
214
+ || m === "built-in" /* IdentifierMeta.BuiltIn */ || m === "inline" /* IdentifierMeta.InlineDefinition */);
215
+ }
216
+ samePath(a, b) {
217
+ const key = (i) => i === undefined ? "" : _procedure_analysis_1.ObjectAnalysis.key(i);
218
+ return a.local === b.local && key(a.attribute) === key(b.attribute);
219
+ }
220
+ /** the statement between the create and the check cannot change the reference */
221
+ keeps(procedure, statement, path) {
222
+ const index = procedure.index(statement);
223
+ if (path.local !== undefined && path.local.writes().some(w => w.index === index)) {
224
+ return false;
225
+ }
226
+ if (path.attribute === undefined) {
227
+ return true;
228
+ }
229
+ if (!procedure.isCallFree(statement)) {
230
+ return false;
231
+ }
232
+ const attribute = _procedure_analysis_1.ObjectAnalysis.key(path.attribute);
233
+ for (const target of statement.findAllExpressionsRecursive(Expressions.Target)) {
234
+ if (target.findFirstExpression(Expressions.TargetFieldSymbol) !== undefined
235
+ || target.findFirstExpression(Expressions.Dereference) !== undefined) {
236
+ return false;
237
+ }
238
+ if (target.findDirectExpression(Expressions.InlineData) !== undefined) {
239
+ continue;
240
+ }
241
+ // the storage changed is inside the last attribute of the chain, or inside the first field
242
+ const attributes = target.findDirectExpressions(Expressions.AttributeName);
243
+ const field = attributes.length > 0 ? attributes[attributes.length - 1] : target.findDirectExpression(Expressions.TargetField);
244
+ const changed = field === undefined ? undefined : procedure.identifierAt(field.getFirstToken());
245
+ if (!(changed instanceof _typed_identifier_1.TypedIdentifier) || _procedure_analysis_1.ObjectAnalysis.key(changed) === attribute) {
246
+ return false;
247
+ }
248
+ const meta = changed.getMeta();
249
+ const variable = procedure.variable(changed);
250
+ const parameter = !procedure.contains(changed) && !this.isAttribute(procedure, changed);
251
+ if (parameter && !meta.includes("pass_by_value" /* IdentifierMeta.PassByValue */) && !meta.includes("returning" /* IdentifierMeta.MethodReturning */)) {
252
+ return false;
253
+ }
254
+ else if ((variable === null || variable === void 0 ? void 0 : variable.isLocal()) === true && variable.escapes()) {
255
+ return false;
256
+ }
257
+ }
258
+ return true;
259
+ }
260
+ }
261
+ exports.BoundCheckAfterCreate = BoundCheckAfterCreate;
262
+ //# sourceMappingURL=bound_check_after_create.js.map
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClearAfterDeclaration = exports.ClearAfterDeclarationConf = void 0;
4
+ const issue_1 = require("../issue");
5
+ const _basic_rule_config_1 = require("./_basic_rule_config");
6
+ const _irule_1 = require("./_irule");
7
+ const _procedure_analysis_1 = require("./_procedure_analysis");
8
+ class ClearAfterDeclarationConf extends _basic_rule_config_1.BasicRuleConfig {
9
+ }
10
+ exports.ClearAfterDeclarationConf = ClearAfterDeclarationConf;
11
+ class ClearAfterDeclaration {
12
+ constructor() {
13
+ this.conf = new ClearAfterDeclarationConf();
14
+ }
15
+ getMetadata() {
16
+ return {
17
+ key: "clear_after_declaration",
18
+ title: "CLEAR after declaration",
19
+ shortDescription: `Finds CLEAR of a local variable that is still initial since its declaration`,
20
+ extendedInformation: `Reports CLEAR x. when all of the following hold:
21
+
22
+ * x is a local variable of a METHOD, FORM or FUNCTION, declared with DATA x TYPE/LIKE ... without VALUE
23
+ * 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
26
+
27
+ Any later use of x does not matter, the CLEAR can only be reached with x still initial.
28
+ Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
29
+ INCLUDE, test seams, enhancements or native SQL.
30
+
31
+ The quick fix deletes the CLEAR. No release requirement.`,
32
+ tags: [_irule_1.RuleTag.Quickfix],
33
+ badExample: `DATA lv_count TYPE i.
34
+ CLEAR lv_count.`,
35
+ goodExample: `DATA lv_count TYPE i.`,
36
+ };
37
+ }
38
+ getConfig() {
39
+ return this.conf;
40
+ }
41
+ setConfig(conf) {
42
+ this.conf = conf;
43
+ }
44
+ initialize(reg) {
45
+ this.reg = reg;
46
+ return this;
47
+ }
48
+ run(obj) {
49
+ var _a;
50
+ const analysis = _procedure_analysis_1.ObjectAnalysis.run(this.reg, obj);
51
+ if (analysis === undefined) {
52
+ return [];
53
+ }
54
+ const issues = [];
55
+ for (const procedure of analysis.procedures) {
56
+ if (procedure.supported === false) {
57
+ continue;
58
+ }
59
+ for (const variable of procedure.locals()) {
60
+ const declaration = variable.declaration;
61
+ const first = variable.occurrences[0];
62
+ if (variable.inline || !(0, _procedure_analysis_1.plainData)(declaration) || first === undefined
63
+ || first.kind !== _procedure_analysis_1.OccurrenceKind.Write
64
+ || ((_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
+ continue;
68
+ }
69
+ const fix = (0, _procedure_analysis_1.deleteStatement)(procedure.file, first.statement);
70
+ issues.push(issue_1.Issue.atStatement(procedure.file, first.statement, "CLEAR of " + variable.identifier.getName() + ", which is still initial", this.getMetadata().key, this.conf.severity, fix));
71
+ }
72
+ }
73
+ return issues;
74
+ }
75
+ }
76
+ exports.ClearAfterDeclaration = ClearAfterDeclaration;
77
+ //# sourceMappingURL=clear_after_declaration.js.map