@abaplint/core 2.120.106 → 2.120.108

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.
@@ -1257,6 +1257,7 @@ declare class ClassDefinition_3 extends Identifier implements IClassDefinition {
1257
1257
  isSharedMemory(): boolean;
1258
1258
  getCreateVisibility(): Visibility;
1259
1259
  private checkClassNameLength;
1260
+ private checkLocalTypesInPublicSections;
1260
1261
  private findSuper;
1261
1262
  private checkMethodNameLength;
1262
1263
  private checkClassConstructor;
@@ -0,0 +1,66 @@
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.hasKeyCompare = hasKeyCompare;
37
+ const Expressions = __importStar(require("../2_statements/expressions"));
38
+ const nodes_1 = require("../nodes");
39
+ /** True if a conjunct of the WHERE condition reachable through AND only, ie. not under OR or NOT,
40
+ * compares the key component with = or IS INITIAL. Without such a comparison of its first component,
41
+ * a sorted key cannot be used to optimize the WHERE condition */
42
+ function hasKeyCompare(cond, component) {
43
+ if (cond.findDirectTokenByText("OR") !== undefined) {
44
+ return false;
45
+ }
46
+ for (const child of cond.getChildren()) {
47
+ if (child.get() instanceof Expressions.ComponentCompare && child instanceof nodes_1.ExpressionNode) {
48
+ const concat = child.concatTokens().toUpperCase().replace(/\s+/g, " ");
49
+ if (concat.startsWith(component + " = ")
50
+ || concat.startsWith(component + " EQ ")
51
+ || concat === component + " IS INITIAL") {
52
+ return true;
53
+ }
54
+ }
55
+ else if (child.get() instanceof Expressions.ComponentCondSub
56
+ && child instanceof nodes_1.ExpressionNode
57
+ && child.findDirectTokenByText("NOT") === undefined) {
58
+ const sub = child.findDirectExpression(Expressions.ComponentCond);
59
+ if (sub !== undefined && hasKeyCompare(sub, component)) {
60
+ return true;
61
+ }
62
+ }
63
+ }
64
+ return false;
65
+ }
66
+ //# sourceMappingURL=_key_compare.js.map
@@ -127,8 +127,8 @@ class ValueBody {
127
127
  }
128
128
  for (const s of foo.findDirectExpressions(Expressions.Source)) {
129
129
  const sourceType = source_1.Source.runSyntax(s, input, rowType);
130
- if (rowType instanceof basic_1.StringType && sourceType instanceof basic_1.CharacterType) {
131
- const message = "VALUE, source type CharacterType not compatible with StringType";
130
+ if ((rowType instanceof basic_1.StringType || rowType instanceof basic_1.XStringType) && sourceType instanceof basic_1.CharacterType) {
131
+ const message = "VALUE, source type CharacterType not compatible with " + rowType.constructor.name;
132
132
  input.issues.push((0, _syntax_input_1.syntaxIssue)(input, s.getFirstToken(), message));
133
133
  }
134
134
  else if (this.insertsUsingTableKey(targetType)) {
@@ -48,6 +48,11 @@ class Append {
48
48
  let targetType = undefined;
49
49
  const target = node.findDirectExpression(Expressions.Target);
50
50
  if (target) {
51
+ if (target.findDirectExpression(Expressions.TableExpression) !== undefined) {
52
+ const message = "APPEND, table expression not allowed in target";
53
+ input.issues.push((0, _syntax_input_1.syntaxIssue)(input, target.getFirstToken(), message));
54
+ return;
55
+ }
51
56
  targetType = target_1.Target.runSyntax(target, input);
52
57
  }
53
58
  // a hashed table has no index, APPEND adds at the end
@@ -45,6 +45,7 @@ const _type_utils_1 = require("../_type_utils");
45
45
  const _syntax_input_1 = require("../_syntax_input");
46
46
  class InsertInternal {
47
47
  runSyntax(node, input) {
48
+ var _a;
48
49
  let targetType;
49
50
  const t = node.findDirectExpression(Expressions.Target);
50
51
  if (t) {
@@ -107,6 +108,13 @@ class InsertInternal {
107
108
  // yea, well, INSERT doesnt convert the values automatically, like everything else?
108
109
  error = true;
109
110
  }
111
+ else if (sourceType instanceof basic_1.CharacterType
112
+ && targetType instanceof basic_1.CharacterType
113
+ && ((_a = sourceType.getAbstractTypeData()) === null || _a === void 0 ? void 0 : _a.derivedFromConstant) === true
114
+ && sourceType.getLength() !== targetType.getLength()) {
115
+ // character literal of different length than the row type
116
+ error = true;
117
+ }
110
118
  if (error === true) {
111
119
  const message = "Types not compatible";
112
120
  input.issues.push((0, _syntax_input_1.syntaxIssue)(input, node.getFirstToken(), message));
@@ -35,7 +35,6 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Loop = void 0;
37
37
  const Expressions = __importStar(require("../../2_statements/expressions"));
38
- const nodes_1 = require("../../nodes");
39
38
  const basic_1 = require("../../types/basic");
40
39
  const target_1 = require("../expressions/target");
41
40
  const source_1 = require("../expressions/source");
@@ -47,6 +46,7 @@ const dynamic_1 = require("../expressions/dynamic");
47
46
  const loop_group_by_1 = require("../expressions/loop_group_by");
48
47
  const _syntax_input_1 = require("../_syntax_input");
49
48
  const version_1 = require("../../../version");
49
+ const _key_compare_1 = require("../_key_compare");
50
50
  class Loop {
51
51
  runSyntax(node, input) {
52
52
  var _a, _b;
@@ -151,7 +151,7 @@ class Loop {
151
151
  // the first component of a sorted secondary key must be compared with = or IS INITIAL
152
152
  // in a part of the condition joined by AND, otherwise the WHERE cannot be optimized
153
153
  const first = key.keyFields[0].toUpperCase();
154
- if (this.hasKeyCompare(cond, first) === false) {
154
+ if ((0, _key_compare_1.hasKeyCompare)(cond, first) === false) {
155
155
  const message = "Loop, WHERE cannot be optimized for secondary key " + name +
156
156
  ", key component " + first + " needs a comparison with = or IS INITIAL joined by AND";
157
157
  input.issues.push((0, _syntax_input_1.syntaxIssue)(input, node.getFirstToken(), message));
@@ -198,31 +198,6 @@ class Loop {
198
198
  loop_group_by_1.LoopGroupBy.runSyntax(group, input);
199
199
  }
200
200
  }
201
- // true if a conjunct reachable through AND only, ie. not under OR or NOT, compares the component with = or IS INITIAL
202
- hasKeyCompare(cond, component) {
203
- if (cond.findDirectTokenByText("OR") !== undefined) {
204
- return false;
205
- }
206
- for (const child of cond.getChildren()) {
207
- if (child.get() instanceof Expressions.ComponentCompare && child instanceof nodes_1.ExpressionNode) {
208
- const concat = child.concatTokens().toUpperCase().replace(/\s+/g, " ");
209
- if (concat.startsWith(component + " = ")
210
- || concat.startsWith(component + " EQ ")
211
- || concat === component + " IS INITIAL") {
212
- return true;
213
- }
214
- }
215
- else if (child.get() instanceof Expressions.ComponentCondSub
216
- && child instanceof nodes_1.ExpressionNode
217
- && child.findDirectTokenByText("NOT") === undefined) {
218
- const sub = child.findDirectExpression(Expressions.ComponentCond);
219
- if (sub !== undefined && this.hasKeyCompare(sub, component)) {
220
- return true;
221
- }
222
- }
223
- }
224
- return false;
225
- }
226
201
  }
227
202
  exports.Loop = Loop;
228
203
  //# sourceMappingURL=loop.js.map
@@ -48,7 +48,7 @@ const _raise_message_1 = require("../_raise_message");
48
48
  class Raise {
49
49
  runSyntax(node, input) {
50
50
  // todo
51
- var _a, _b, _c, _d, _e, _f;
51
+ var _a, _b, _c, _d, _e, _f, _g;
52
52
  const helper = new _object_oriented_1.ObjectOriented(input.scope);
53
53
  let method;
54
54
  const exceptionName = node.findDirectExpression(Expressions.ExceptionName);
@@ -64,7 +64,12 @@ class Raise {
64
64
  if (found === null || found === void 0 ? void 0 : found.id) {
65
65
  input.scope.addReference(classTok, found.id, _reference_1.ReferenceType.ObjectOrientedReference, input.filename);
66
66
  const def = input.scope.findObjectDefinition(className);
67
- method = (_b = helper.searchMethodName(def, "CONSTRUCTOR")) === null || _b === void 0 ? void 0 : _b.method;
67
+ if (((_b = input.scope.findClassDefinition(className)) === null || _b === void 0 ? void 0 : _b.isAbstract()) === true) {
68
+ const message = className + " is abstract, cannot be instantiated";
69
+ input.issues.push((0, _syntax_input_1.syntaxIssue)(input, classTok, message));
70
+ return;
71
+ }
72
+ method = (_c = helper.searchMethodName(def, "CONSTRUCTOR")) === null || _c === void 0 ? void 0 : _c.method;
68
73
  }
69
74
  else if (input.scope.getDDIC().inErrorNamespace(className) === false) {
70
75
  const extra = { ooName: className, ooType: "Void" };
@@ -88,7 +93,7 @@ class Raise {
88
93
  }
89
94
  else if (type instanceof basic_1.ObjectReferenceType) {
90
95
  const def = input.scope.findObjectDefinition(type.getIdentifierName());
91
- method = (_c = helper.searchMethodName(def, "CONSTRUCTOR")) === null || _c === void 0 ? void 0 : _c.method;
96
+ method = (_d = helper.searchMethodName(def, "CONSTRUCTOR")) === null || _d === void 0 ? void 0 : _d.method;
92
97
  }
93
98
  else if (type !== undefined) {
94
99
  const message = "RAISE EXCEPTION, must be object reference, got " + type.constructor.name;
@@ -117,10 +122,10 @@ class Raise {
117
122
  for (const s of node.findDirectExpressions(Expressions.MessageSource)) {
118
123
  message_source_1.MessageSource.runSyntax(s, input);
119
124
  }
120
- const id = (_d = node.findExpressionAfterToken("ID")) === null || _d === void 0 ? void 0 : _d.concatTokens();
121
- let number = (_e = node.findDirectExpression(Expressions.MessageNumber)) === null || _e === void 0 ? void 0 : _e.concatTokens();
125
+ const id = (_e = node.findExpressionAfterToken("ID")) === null || _e === void 0 ? void 0 : _e.concatTokens();
126
+ let number = (_f = node.findDirectExpression(Expressions.MessageNumber)) === null || _f === void 0 ? void 0 : _f.concatTokens();
122
127
  if (number === undefined) {
123
- const num = (_f = node.findExpressionAfterToken("NUMBER")) === null || _f === void 0 ? void 0 : _f.concatTokens();
128
+ const num = (_g = node.findExpressionAfterToken("NUMBER")) === null || _g === void 0 ? void 0 : _g.concatTokens();
124
129
  if (num === null || num === void 0 ? void 0 : num.startsWith("'")) {
125
130
  number = num.substring(1, num.length - 1).toUpperCase();
126
131
  }
@@ -80,7 +80,7 @@ class ClassDefinition extends _identifier_1.Identifier {
80
80
  this.events.push(new event_definition_1.EventDefinition(e, visibility_1.Visibility.Public, input)); // todo, all these are not Public
81
81
  }
82
82
  this.methodDefs = new method_definitions_1.MethodDefinitions(node, input);
83
- input.scope.pop(node.getLastToken().getEnd());
83
+ const scope = input.scope.pop(node.getLastToken().getEnd());
84
84
  const concat = def.concatTokens().toUpperCase();
85
85
  this.testing = concat.includes(" FOR TESTING");
86
86
  this.sharedMemory = concat.includes(" SHARED MEMORY ENABLED");
@@ -102,6 +102,7 @@ class ClassDefinition extends _identifier_1.Identifier {
102
102
  this.checkClassNameLength(input);
103
103
  this.checkMethodNameLength(input);
104
104
  this.checkClassConstructor(input);
105
+ this.checkLocalTypesInPublicSections(input, node, scope.getTop());
105
106
  }
106
107
  getFriends() {
107
108
  return this.friends;
@@ -156,6 +157,56 @@ class ClassDefinition extends _identifier_1.Identifier {
156
157
  input.issues.push((0, _syntax_input_1.syntaxIssue)(input, this.getToken(), message));
157
158
  }
158
159
  }
160
+ // the local types, classes and interfaces of a class pool can only be used in the PRIVATE SECTION of the global class
161
+ checkLocalTypesInPublicSections(input, node, scope) {
162
+ if (this.globalValue === false || input.filename.toLowerCase().endsWith(".clas.abap") === false) {
163
+ return;
164
+ }
165
+ const prefix = input.filename.toLowerCase().replace(/abap$/, "");
166
+ const isLocal = (filename) => {
167
+ const lower = filename.toLowerCase();
168
+ return lower.startsWith(prefix)
169
+ && (lower.endsWith(".clas.locals_def.abap") || lower.endsWith(".clas.locals_imp.abap"));
170
+ };
171
+ const references = [];
172
+ const collect = (n) => {
173
+ for (const r of n.getData().references) {
174
+ if ((r.referenceType === _reference_1.ReferenceType.TypeReference || r.referenceType === _reference_1.ReferenceType.ObjectOrientedReference)
175
+ && r.position.getFilename() === input.filename
176
+ && r.position.getName().includes("~") === false // lif~meth of an ALIASES, the INTERFACES statement is reported
177
+ && r.resolved !== undefined
178
+ && isLocal(r.resolved.getFilename())) {
179
+ references.push(r);
180
+ }
181
+ }
182
+ n.getChildren().forEach(collect);
183
+ };
184
+ collect(scope);
185
+ if (references.length === 0) {
186
+ return;
187
+ }
188
+ const sections = [
189
+ { section: node.findDirectStructure(Structures.PublicSection), name: "PUBLIC" },
190
+ ];
191
+ // a FINAL class has no subclasses, so the PROTECTED SECTION is effectively private
192
+ if (this.finalValue === false) {
193
+ sections.push({ section: node.findDirectStructure(Structures.ProtectedSection), name: "PROTECTED" });
194
+ }
195
+ for (const { section, name } of sections) {
196
+ for (const statement of (section === null || section === void 0 ? void 0 : section.findAllStatementNodes()) || []) {
197
+ // compare with the tokens, the statements of a chain share the first tokens
198
+ const tokens = statement.getTokens();
199
+ const found = references
200
+ .filter(r => tokens.some(t => t.getStart().equals(r.position.getStart())))
201
+ .sort((a, b) => a.position.getStart().isBefore(b.position.getStart()) ? -1 : 1)[0];
202
+ if (found !== undefined) {
203
+ const message = "\"" + found.position.getName().toUpperCase() +
204
+ "\" is local to the class pool and cannot be used in the " + name + " SECTION of the global class";
205
+ input.issues.push((0, _syntax_input_1.syntaxIssue)(input, found.position.getToken(), message));
206
+ }
207
+ }
208
+ }
209
+ }
159
210
  findSuper(def, input) {
160
211
  var _a;
161
212
  const token = (_a = def === null || def === void 0 ? void 0 : def.findDirectExpression(expressions_1.SuperClassName)) === null || _a === void 0 ? void 0 : _a.getFirstToken();
@@ -78,7 +78,7 @@ class Registry {
78
78
  }
79
79
  static abaplintVersion() {
80
80
  // magic, see build script "version.js"
81
- return "2.120.106";
81
+ return "2.120.108";
82
82
  }
83
83
  getDDICReferences() {
84
84
  return this.ddicReferences;
@@ -50,6 +50,7 @@ exports.isReferenceType = isReferenceType;
50
50
  exports.identicalTypes = identicalTypes;
51
51
  exports.simplePredicate = simplePredicate;
52
52
  exports.characterConstant = characterConstant;
53
+ exports.textLiteral = textLiteral;
53
54
  exports.sameCharacters = sameCharacters;
54
55
  exports.isCondition = isCondition;
55
56
  exports.dataSource = dataSource;
@@ -35,11 +35,20 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.ConstantCondition = exports.ConstantConditionConf = void 0;
37
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");
38
40
  const basic_1 = require("../abap/types/basic");
41
+ const _typed_identifier_1 = require("../abap/types/_typed_identifier");
42
+ const issue_1 = require("../issue");
39
43
  const _basic_rule_config_1 = require("./_basic_rule_config");
40
44
  const _irule_1 = require("./_irule");
41
45
  const _procedure_analysis_1 = require("./_procedure_analysis");
42
46
  class ConstantConditionConf extends _basic_rule_config_1.BasicRuleConfig {
47
+ constructor() {
48
+ super(...arguments);
49
+ /** Report x <> a OR x <> b, always true, and x = a AND x = b, always false, for different character values a and b */
50
+ this.exclusiveValues = true;
51
+ }
43
52
  }
44
53
  exports.ConstantConditionConf = ConstantConditionConf;
45
54
  class ConstantCondition {
@@ -50,7 +59,8 @@ class ConstantCondition {
50
59
  return {
51
60
  key: "constant_condition",
52
61
  title: "Constant condition",
53
- shortDescription: `Finds IF and ELSEIF conditions on local variables whose value is known`,
62
+ shortDescription: `Finds IF and ELSEIF conditions on local variables whose value is known, and conditions that compare
63
+ a field with two different values and are always true or always false`,
54
64
  extendedInformation: `Reports two cases, for local variables of a METHOD, FORM or FUNCTION.
55
65
 
56
66
  1. DATA(c) = <constant>. where the constant is a character literal like 'X', or a character constant like
@@ -69,15 +79,31 @@ an ELSEIF branch that is always taken turns into ELSE. Nothing is reported if a
69
79
  data that is used outside of it.
70
80
 
71
81
  Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
72
- INCLUDE, test seams, enhancements or native SQL. No release requirement.`,
82
+ INCLUDE, test seams, enhancements or native SQL. No release requirement.
83
+
84
+ Option exclusiveValues: reports x <> a OR x <> b, which is always true, and x = a AND x = b, which is always
85
+ false, in any condition of a METHOD, FORM or FUNCTION, also with dynamic code. Also with NE and EQ, with NOT,
86
+ with the operands swapped, and with more conditions joined by the same operator. x is a data object of type c,
87
+ generic c or string, without calls, offsets or table expressions. a and b are character literals or character
88
+ constants, also components of constant structures of a class or interface, with different values. Usually one
89
+ of the operators or one of the values is wrong, there is no quick fix.
90
+ Not reported: conditions that mix AND and OR or contain a call, values that are not known, like constants of
91
+ classes outside the registry, other types of x, which can convert different values to the same one, ASSERT,
92
+ and SQL conditions, where a NULL value makes both comparisons unknown.`,
73
93
  tags: [_irule_1.RuleTag.Quickfix],
74
94
  badExample: `DATA(lv_active) = abap_true.
75
95
  IF lv_active = abap_true.
76
96
  run( ).
77
97
  ELSE.
78
98
  skip( ).
99
+ ENDIF.
100
+ IF lv_kind <> 'N' OR lv_kind <> 'P'.
101
+ skip( ).
102
+ ENDIF.`,
103
+ goodExample: `run( ).
104
+ IF lv_kind <> 'N' AND lv_kind <> 'P'.
105
+ skip( ).
79
106
  ENDIF.`,
80
- goodExample: `run( ).`,
81
107
  };
82
108
  }
83
109
  getConfig() {
@@ -97,6 +123,9 @@ ENDIF.`,
97
123
  }
98
124
  const issues = [];
99
125
  for (const procedure of analysis.procedures) {
126
+ if (this.conf.exclusiveValues) {
127
+ issues.push(...this.exclusiveValues(procedure));
128
+ }
100
129
  if (procedure.supported === false) {
101
130
  continue;
102
131
  }
@@ -198,6 +227,175 @@ ENDIF.`,
198
227
  return source.countTokens() === 1 && source.findDirectExpression(Expressions.FieldChain) !== undefined
199
228
  && source.getFirstToken().getStart().equals(read.token.getStart());
200
229
  }
230
+ /** x <> a OR x <> b, x = a AND x = b */
231
+ exclusiveValues(procedure) {
232
+ const issues = [];
233
+ for (const statement of procedure.statements) {
234
+ if (statement.get() instanceof Statements.Assert) {
235
+ continue;
236
+ }
237
+ for (const cond of statement.findAllExpressionsRecursive(Expressions.Cond)) {
238
+ const operators = cond.getChildren().filter(c => c instanceof nodes_1.TokenNode).map(c => c.getFirstToken().getStr().toUpperCase());
239
+ const operator = operators[0];
240
+ if ((operator !== "OR" && operator !== "AND") || operators.some(o => o !== operator) || this.hasCall(cond)) {
241
+ continue;
242
+ }
243
+ const seen = [];
244
+ for (const child of cond.getChildren()) {
245
+ const comparison = child instanceof nodes_1.ExpressionNode && child.get() instanceof Expressions.Compare
246
+ ? this.valueComparison(procedure, child) : undefined;
247
+ if (comparison === undefined || comparison.equal !== (operator === "AND")) {
248
+ continue;
249
+ }
250
+ const other = seen.find(s => s.subject === comparison.subject && !(0, _procedure_analysis_1.sameCharacters)(s.value, comparison.value));
251
+ if (other !== undefined) {
252
+ const message = "Condition is always " + (operator === "OR" ? "true" : "false") + ", "
253
+ + this.subjectText(comparison) + " cannot be both "
254
+ + other.constant.concatTokens() + " and " + comparison.constant.concatTokens();
255
+ issues.push(issue_1.Issue.atRange(procedure.file, other.compare.getFirstToken().getStart(), comparison.compare.getLastToken().getEnd(), message, this.getMetadata().key, this.conf.severity));
256
+ break;
257
+ }
258
+ seen.push(comparison);
259
+ }
260
+ }
261
+ }
262
+ return issues;
263
+ }
264
+ hasCall(node) {
265
+ return node.findFirstExpression(Expressions.MethodCall) !== undefined
266
+ || node.findFirstExpression(Expressions.MethodCallChain) !== undefined
267
+ || node.findFirstExpression(Expressions.NewObject) !== undefined;
268
+ }
269
+ /** x = a, x <> a, a = x, NOT x = a etc, x character-like and a a known character value */
270
+ valueComparison(procedure, compare) {
271
+ const children = compare.getChildren();
272
+ const negated = children[0] instanceof nodes_1.TokenNode && children[0].getFirstToken().getStr().toUpperCase() === "NOT";
273
+ const rest = children.slice(negated ? 1 : 0);
274
+ if (rest.length !== 3 || !(rest[0] instanceof nodes_1.ExpressionNode) || !(rest[2] instanceof nodes_1.ExpressionNode)
275
+ || !(rest[1].get() instanceof Expressions.CompareOperator)) {
276
+ return undefined;
277
+ }
278
+ const operator = rest[1].concatTokens().toUpperCase().replace(/ /g, "");
279
+ let equal;
280
+ if (operator === "=" || operator === "EQ") {
281
+ equal = true;
282
+ }
283
+ else if (operator === "<>" || operator === "><" || operator === "NE") {
284
+ equal = false;
285
+ }
286
+ else {
287
+ return undefined;
288
+ }
289
+ for (const [subject, constant] of [[rest[0], rest[2]], [rest[2], rest[0]]]) {
290
+ const value = this.constantValue(procedure, constant);
291
+ const type = value === undefined ? undefined : this.subjectType(procedure, subject);
292
+ if (value !== undefined
293
+ && (type instanceof basic_1.CharacterType || type instanceof basic_1.CGenericType || type instanceof basic_1.StringType)) {
294
+ const text = subject.getTokens().map(t => t.getStr().toUpperCase()).join(" ");
295
+ return { subject: text, value, equal: equal !== negated, compare, constant };
296
+ }
297
+ }
298
+ return undefined;
299
+ }
300
+ subjectText(comparison) {
301
+ var _a;
302
+ var _b;
303
+ return (_b = (_a = comparison.compare.getChildren().find(c => c instanceof nodes_1.ExpressionNode && c !== comparison.constant
304
+ && c.get() instanceof Expressions.Source)) === null || _a === void 0 ? void 0 : _a.concatTokens()) !== null && _b !== void 0 ? _b : comparison.subject;
305
+ }
306
+ /** type of a field, field symbol, attribute or component chain, without calls, offsets or table expressions */
307
+ subjectType(procedure, source) {
308
+ var _a;
309
+ var _b;
310
+ const chain = source.getChildren().length === 1 ? source.findDirectExpression(Expressions.FieldChain) : undefined;
311
+ const children = (_b = chain === null || chain === void 0 ? void 0 : chain.getChildren()) !== null && _b !== void 0 ? _b : [];
312
+ let identifier;
313
+ let next;
314
+ if (children[0] instanceof nodes_1.ExpressionNode
315
+ && (children[0].get() instanceof Expressions.SourceField || children[0].get() instanceof Expressions.SourceFieldSymbol)) {
316
+ identifier = procedure.identifierAt(children[0].getFirstToken());
317
+ next = 1;
318
+ }
319
+ else if (children[0] instanceof nodes_1.ExpressionNode && children[0].get() instanceof Expressions.ClassName
320
+ && ((_a = children[1]) === null || _a === void 0 ? void 0 : _a.getFirstToken().getStr()) === "=>" && children[2] instanceof nodes_1.ExpressionNode) {
321
+ identifier = procedure.identifierAt(children[2].getFirstToken());
322
+ next = 3;
323
+ }
324
+ else {
325
+ return undefined;
326
+ }
327
+ let type = identifier instanceof _typed_identifier_1.TypedIdentifier ? identifier.getType() : undefined;
328
+ for (let i = next; i < children.length; i += 2) {
329
+ const separator = children[i];
330
+ const name = children[i + 1];
331
+ if (!(separator instanceof nodes_1.TokenNode) || !(name instanceof nodes_1.ExpressionNode) || type === undefined) {
332
+ return undefined;
333
+ }
334
+ else if (separator.getFirstToken().getStr() === "->" && name.get() instanceof Expressions.AttributeName) {
335
+ const attribute = procedure.identifierAt(name.getFirstToken());
336
+ type = attribute instanceof _typed_identifier_1.TypedIdentifier ? attribute.getType() : undefined;
337
+ }
338
+ else if (separator.getFirstToken().getStr() === "-" && name.get() instanceof Expressions.ComponentName
339
+ && type instanceof basic_1.StructureType) {
340
+ type = type.getComponentByName(name.getFirstToken().getStr());
341
+ }
342
+ else {
343
+ return undefined;
344
+ }
345
+ }
346
+ return type;
347
+ }
348
+ /** value of a character literal, a character constant or a character component of a constant structure */
349
+ constantValue(procedure, source) {
350
+ var _a;
351
+ var _b;
352
+ if (source.findDirectExpression(Expressions.Constant) !== undefined) {
353
+ return (0, _procedure_analysis_1.characterConstant)(procedure, source);
354
+ }
355
+ const chain = source.getChildren().length === 1 ? source.findDirectExpression(Expressions.FieldChain) : undefined;
356
+ const children = (_b = chain === null || chain === void 0 ? void 0 : chain.getChildren()) !== null && _b !== void 0 ? _b : [];
357
+ let identifier;
358
+ let next;
359
+ if (children[0] instanceof nodes_1.ExpressionNode && children[0].get() instanceof Expressions.SourceField) {
360
+ identifier = procedure.identifierAt(children[0].getFirstToken());
361
+ next = 1;
362
+ }
363
+ else if (children[0] instanceof nodes_1.ExpressionNode && children[0].get() instanceof Expressions.ClassName
364
+ && ((_a = children[1]) === null || _a === void 0 ? void 0 : _a.getFirstToken().getStr()) === "=>" && children[2] instanceof nodes_1.ExpressionNode
365
+ && children[2].get() instanceof Expressions.AttributeName) {
366
+ identifier = procedure.identifierAt(children[2].getFirstToken());
367
+ next = 3;
368
+ }
369
+ else {
370
+ return undefined;
371
+ }
372
+ if (!(identifier instanceof _typed_identifier_1.TypedIdentifier) || !(0, _procedure_analysis_1.isImmutable)(identifier)) {
373
+ return undefined;
374
+ }
375
+ let type = identifier.getType();
376
+ let value = identifier.getValue();
377
+ if (children.length === next + 2) {
378
+ const name = children[next + 1];
379
+ if (children[next].getFirstToken().getStr() !== "-" || !(name instanceof nodes_1.ExpressionNode)
380
+ || !(name.get() instanceof Expressions.ComponentName) || !(type instanceof basic_1.StructureType)
381
+ || typeof value !== "object") {
382
+ return undefined;
383
+ }
384
+ const component = name.getFirstToken().getStr().toLowerCase();
385
+ type = type.getComponentByName(component);
386
+ const key = Object.keys(value).find(k => k.toLowerCase() === component);
387
+ value = key === undefined ? undefined : value[key];
388
+ }
389
+ else if (children.length !== next) {
390
+ return undefined;
391
+ }
392
+ const text = typeof value === "string" ? (0, _procedure_analysis_1.textLiteral)(value) : undefined;
393
+ if (!(type instanceof basic_1.CharacterType) || text === undefined) {
394
+ return undefined;
395
+ }
396
+ // the value is cut to the length of the constant
397
+ return text.substring(0, type.getLength());
398
+ }
201
399
  }
202
400
  exports.ConstantCondition = ConstantCondition;
203
401
  //# sourceMappingURL=constant_condition.js.map