@abaplint/transpiler 2.13.75 → 2.13.77

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.
@@ -3,7 +3,7 @@ import { Chunk } from "../chunk";
3
3
  import { Traversal } from "../traversal";
4
4
  import { IExpressionTranspiler } from "./_expression_transpiler";
5
5
  export declare class ComponentChainSimpleTranspiler implements IExpressionTranspiler {
6
- private prefix;
6
+ private readonly prefix;
7
7
  constructor(prefix?: string);
8
8
  transpile(node: Nodes.ExpressionNode, traversal: Traversal): Chunk;
9
9
  }
@@ -14,6 +14,7 @@ class ComponentChainSimpleTranspiler {
14
14
  transpile(node, traversal) {
15
15
  const offset = [];
16
16
  let ret = new chunk_1.Chunk();
17
+ let prefix = this.prefix;
17
18
  for (const c of node.getChildren()) {
18
19
  const type = c.get();
19
20
  if (type instanceof core_1.Expressions.ComponentName) {
@@ -23,11 +24,17 @@ class ComponentChainSimpleTranspiler {
23
24
  field = interfaceName + "$" + field;
24
25
  }
25
26
  field = traversal_1.Traversal.escapeNamespace(field).replace("~", "$");
26
- ret.append(this.prefix + field, c, traversal);
27
- this.prefix = "";
27
+ if (traversal_1.Traversal.isBracketComponent(field)) {
28
+ ret.append(prefix.replace(/\.$/, "") + `["${field}"]`, c, traversal);
29
+ }
30
+ else {
31
+ ret.append(prefix + field, c, traversal);
32
+ }
33
+ prefix = "";
28
34
  }
29
35
  else if (type instanceof core_1.Expressions.ArrowOrDash) {
30
- ret.append(".get().", c, traversal);
36
+ ret.append(".get()", c, traversal);
37
+ prefix = ".";
31
38
  }
32
39
  else if (c instanceof core_1.Nodes.ExpressionNode && c.get() instanceof core_1.Expressions.FieldOffset) {
33
40
  offset.push("offset: " + new field_offset_1.FieldOffsetTranspiler().transpile(c, traversal).getCode());
@@ -73,8 +73,10 @@ class FieldChainTranspiler {
73
73
  else if (c.get() instanceof core_1.Expressions.AttributeName) {
74
74
  const interfaceName = traversal.isInterfaceAttribute(c.getFirstToken());
75
75
  let name = c.getFirstToken().getStr().toLowerCase();
76
- if (context instanceof abaplint.BasicTypes.ObjectReferenceType) {
77
- const cdef = traversal.findClassDefinition(context.getIdentifierName(), scope);
76
+ const attributeContext = context;
77
+ context = traversal.narrowContextAttribute(context, c.getFirstToken().getStr(), scope);
78
+ if (attributeContext instanceof abaplint.BasicTypes.ObjectReferenceType) {
79
+ const cdef = traversal.findClassDefinition(attributeContext.getIdentifierName(), scope);
78
80
  const tokenName = c.getFirstToken().getStr();
79
81
  const attr = cdef?.getAttributes().findByName(tokenName);
80
82
  // Do not mark constants as private JS fields. Constants are exposed on instances via constructor copying.
@@ -106,6 +108,7 @@ class FieldChainTranspiler {
106
108
  }
107
109
  else if (c.get() instanceof core_1.Expressions.ComponentName) {
108
110
  const name = c.getFirstToken().getStr().toLowerCase();
111
+ context = traversal_1.Traversal.narrowContextComponent(context, c.getFirstToken().getStr());
109
112
  if (traversal_1.Traversal.isBracketComponent(name)) {
110
113
  ret.append(`["` + name + `"]`, c, traversal);
111
114
  }
@@ -135,6 +138,9 @@ class FieldChainTranspiler {
135
138
  }
136
139
  else if (c instanceof core_1.Nodes.ExpressionNode
137
140
  && c.get() instanceof core_1.Expressions.TableExpression) {
141
+ if (context instanceof abaplint.BasicTypes.TableType) {
142
+ context = context.getRowType();
143
+ }
138
144
  ret = new _1.TableExpressionTranspiler().transpile(c, traversal, ret);
139
145
  }
140
146
  }
@@ -55,6 +55,9 @@ class TargetTranspiler {
55
55
  context = scope?.findVariable(c.getFirstToken().getStr())?.getType();
56
56
  }
57
57
  else if (c.get() instanceof core_1.Expressions.TableExpression && c instanceof core_1.Nodes.ExpressionNode) {
58
+ if (context instanceof abaplint.BasicTypes.TableType) {
59
+ context = context.getRowType();
60
+ }
58
61
  ret = new _1.TableExpressionTranspiler().transpile(c, traversal, ret);
59
62
  }
60
63
  else if (c.get() instanceof core_1.Expressions.InlineData && c instanceof core_1.Nodes.ExpressionNode) {
@@ -71,6 +74,7 @@ class TargetTranspiler {
71
74
  }
72
75
  else if (c.get() instanceof core_1.Expressions.ComponentName) {
73
76
  const name = c.getFirstToken().getStr().toLowerCase();
77
+ context = traversal_1.Traversal.narrowContextComponent(context, c.getFirstToken().getStr());
74
78
  if (traversal_1.Traversal.isBracketComponent(name)) {
75
79
  ret.append(`["` + name + `"]`, c, traversal);
76
80
  }
@@ -81,8 +85,10 @@ class TargetTranspiler {
81
85
  else if (c.get() instanceof core_1.Expressions.AttributeName) {
82
86
  let prefix = "";
83
87
  let postfix = "";
84
- if (context instanceof abaplint.BasicTypes.ObjectReferenceType) {
85
- const cdef = traversal.findClassDefinition(context.getIdentifierName(), scope);
88
+ const attributeContext = context;
89
+ context = traversal.narrowContextAttribute(context, c.getFirstToken().getStr(), scope);
90
+ if (attributeContext instanceof abaplint.BasicTypes.ObjectReferenceType) {
91
+ const cdef = traversal.findClassDefinition(attributeContext.getIdentifierName(), scope);
86
92
  const attr = cdef?.getAttributes().findByName(c.getFirstToken().getStr());
87
93
  if (attr?.getVisibility() === abaplint.Visibility.Private) {
88
94
  const id = scope?.getParent()?.getParent()?.getIdentifier();
@@ -97,10 +97,11 @@ class ReadTableTranspiler {
97
97
  const source = compare.getChildren()[(i * 3) + 2];
98
98
  const s = traversal.traverse(source).getCode();
99
99
  let field = "";
100
+ let key = left.concatTokens().toLowerCase();
100
101
  if (left.get() instanceof abaplint.Expressions.Dynamic
101
102
  && left instanceof abaplint.Nodes.ExpressionNode) {
102
- const concat = left.concatTokens().toLowerCase();
103
- field = "i." + concat.substring(2, concat.length - 2);
103
+ key = key.substring(2, key.length - 2);
104
+ field = "i." + key;
104
105
  }
105
106
  else if (left.get() instanceof abaplint.Expressions.ComponentChainSimple
106
107
  && left instanceof abaplint.Nodes.ExpressionNode) {
@@ -117,12 +118,12 @@ class ReadTableTranspiler {
117
118
  prefix += "const " + id + " = " + s + ";\n";
118
119
  withKey.push("abap.compare.eq(" + field + ", " + id + ")");
119
120
  withKeyValue.push(`{key: (i) => {return ${field}}, value: ${id}}`);
120
- withKeySimple.push(`"${field.replace("i.", "").replace(/\$/g, "/")}": ${id}`);
121
+ withKeySimple.push(`${JSON.stringify(key)}: ${id}`);
121
122
  }
122
123
  else {
123
124
  withKey.push("abap.compare.eq(" + field + ", " + s + ")");
124
125
  withKeyValue.push(`{key: (i) => {return ${field}}, value: ${s}}`);
125
- withKeySimple.push(`"${field.replace("i.", "").replace(/\$/g, "/")}": ${s}`);
126
+ withKeySimple.push(`${JSON.stringify(key)}: ${s}`);
126
127
  }
127
128
  }
128
129
  extra.push("withKey: (i) => {return " + withKey.join(" && ") + ";}");
@@ -71,6 +71,9 @@ export declare class Traversal {
71
71
  constructorPrototypePrefix(token: abaplint.Token, def: abaplint.Types.MethodDefinition | undefined, enclosingClass?: string): string | undefined;
72
72
  private isBuiltinVariable;
73
73
  isTypePool(token: abaplint.Token): string | undefined;
74
+ static narrowContextComponent(context: abaplint.AbstractType | undefined, name: string): abaplint.AbstractType | undefined;
75
+ narrowContextAttribute(context: abaplint.AbstractType | undefined, name: string, scope: abaplint.ISpaghettiScopeNode | undefined): abaplint.AbstractType | undefined;
76
+ private static dereferenceContext;
74
77
  isInterfaceAttribute(token: abaplint.Token): string | undefined;
75
78
  findReadOrWriteReference(token: abaplint.Token): abaplint.Identifier | undefined;
76
79
  private findInferredTypeReference;
@@ -517,6 +517,45 @@ class Traversal {
517
517
  }
518
518
  return undefined;
519
519
  }
520
+ // narrows the current type context of a chain, ie. "ls_struct-comp" or "lt_tab[ 1 ]-comp"
521
+ static narrowContextComponent(context, name) {
522
+ const structure = Traversal.dereferenceContext(context);
523
+ if (structure instanceof abaplint.BasicTypes.StructureType) {
524
+ return structure.getComponentByName(name);
525
+ }
526
+ return undefined;
527
+ }
528
+ // narrows the current type context of a chain, ie. "lo_ref->attr"
529
+ narrowContextAttribute(context, name, scope) {
530
+ if (context instanceof abaplint.BasicTypes.ObjectReferenceType === false) {
531
+ return undefined;
532
+ }
533
+ let identifierName = context.getIdentifierName();
534
+ const visited = new Set();
535
+ while (identifierName !== undefined && visited.has(identifierName.toUpperCase()) === false) {
536
+ visited.add(identifierName.toUpperCase());
537
+ const def = this.findClassDefinition(identifierName, scope) || this.findInterfaceDefinition(identifierName, scope);
538
+ if (def === undefined) {
539
+ return undefined;
540
+ }
541
+ const type = def.getAttributes().findByName(name)?.getType();
542
+ if (type !== undefined) {
543
+ return type;
544
+ }
545
+ identifierName = def.getSuperClass();
546
+ }
547
+ return undefined;
548
+ }
549
+ static dereferenceContext(context) {
550
+ let ret = context;
551
+ if (ret instanceof abaplint.BasicTypes.TableType) {
552
+ ret = ret.getRowType();
553
+ }
554
+ if (ret instanceof abaplint.BasicTypes.DataReference) {
555
+ ret = ret.getType();
556
+ }
557
+ return ret;
558
+ }
520
559
  // returns the interface name if interfaced
521
560
  isInterfaceAttribute(token) {
522
561
  const ref = this.findReadOrWriteReference(token);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abaplint/transpiler",
3
- "version": "2.13.75",
3
+ "version": "2.13.77",
4
4
  "description": "Transpiler",
5
5
  "main": "build/src/index.js",
6
6
  "typings": "build/src/index.d.ts",