@abaplint/transpiler
Version:
200 lines • 8.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TranspileTypes = void 0;
const abaplint = require("@abaplint/core");
const traversal_1 = require("./traversal");
const featureHexUInt8 = false;
// todo: change this class to static for performance?
class TranspileTypes {
declare(t) {
const type = t.getType();
return "let " + traversal_1.Traversal.prefixVariable(t.getName().toLowerCase()) + " = " + this.toType(type) + ";";
}
declareStaticSkipVoid(pre, t) {
const type = t.getType();
const code = this.toType(type);
// todo, this should look at the configuration, for runtime vs compile time errors
if (code.includes("Void type") || code.includes("abap.types.typeTodo")) {
return "";
}
return pre + t.getName().toLowerCase() + " = " + code + ";\n";
}
toType(type) {
let resolved = "";
let extra = "";
if (type instanceof abaplint.BasicTypes.ObjectReferenceType
|| type instanceof abaplint.BasicTypes.GenericObjectReferenceType) {
resolved = "ABAPObject";
extra = "{qualifiedName: " + JSON.stringify(type.getQualifiedName()?.toUpperCase()) +
", RTTIName: " + JSON.stringify(type.getRTTIName()?.toUpperCase()) + "}";
}
else if (type instanceof abaplint.BasicTypes.TableType) {
resolved = "Table";
extra = this.toType(type.getRowType());
extra += ", " + JSON.stringify(type.getOptions());
if (type.getQualifiedName() !== undefined) {
extra += ", \"" + type.getQualifiedName() + "\"";
}
return "abap.types.TableFactory.construct(" + extra + ")";
}
else if (type instanceof abaplint.BasicTypes.IntegerType) {
resolved = "Integer";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.Integer8Type) {
resolved = "Integer8";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.StringType) {
resolved = "String";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.UTCLongType) {
resolved = "UTCLong";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.DateType) {
resolved = "Date";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.TimeType) {
resolved = "Time";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.DataReference) {
resolved = "DataReference";
extra = this.toType(type.getType());
}
else if (type instanceof abaplint.BasicTypes.StructureType) {
resolved = "Structure";
const list = [];
const suffix = {};
const asInclude = {};
for (const c of type.getComponents()) {
list.push(`"` + c.name.toLowerCase() + `": ` + this.toType(c.type));
if (c.suffix) {
suffix[c.name.toLowerCase()] = c.suffix;
}
if (c.asInclude) {
asInclude[c.name.toLowerCase()] = true;
}
}
extra = "{\n" + list.join(",\n") + "}";
if (type.getQualifiedName() !== undefined) {
extra += ", \"" + type.getQualifiedName() + "\"";
}
else {
extra += ", undefined";
}
if (type.getDDICName() !== undefined) {
extra += ", \"" + type.getQualifiedName() + "\"";
}
else {
extra += ", undefined";
}
extra += ", " + JSON.stringify(suffix);
extra += ", " + JSON.stringify(asInclude);
}
else if (type instanceof abaplint.BasicTypes.CLikeType
|| type instanceof abaplint.BasicTypes.CGenericType
|| type instanceof abaplint.BasicTypes.CSequenceType) {
// if not supplied its a Character(1)
resolved = "Character";
}
else if (type instanceof abaplint.BasicTypes.AnyType
|| type instanceof abaplint.BasicTypes.DataType) {
// if not supplied its a Character(4)
resolved = "Character";
extra = "4";
}
else if (type instanceof abaplint.BasicTypes.SimpleType) {
// if not supplied its a Character(1)
resolved = "Character";
}
else if (type instanceof abaplint.BasicTypes.CharacterType) {
resolved = "Character";
extra = type.getLength() + ", " + JSON.stringify(type.getAbstractTypeData());
}
else if (type instanceof abaplint.BasicTypes.NumericType) {
resolved = "Numc";
if (type.getQualifiedName() && type.getLength() !== 1) {
extra = "{length: " + type.getLength() + ", qualifiedName: \"" + type.getQualifiedName() + "\"}";
}
else if (type.getLength() !== 1) {
extra = "{length: " + type.getLength() + "}";
}
else if (type.getQualifiedName()) {
extra = "{qualifiedName: \"" + type.getQualifiedName() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.PackedType) {
resolved = "Packed";
if (type.getQualifiedName()) {
extra = "{length: " + type.getLength() + ", decimals: " + type.getDecimals() + ", qualifiedName: \"" + type.getQualifiedName() + "\"}";
}
else {
extra = "{length: " + type.getLength() + ", decimals: " + type.getDecimals() + "}";
}
}
else if (type instanceof abaplint.BasicTypes.NumericGenericType) {
resolved = "Packed";
extra = "{length: 8, decimals: 2}";
}
else if (type instanceof abaplint.BasicTypes.XStringType) {
resolved = "XString";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.XSequenceType
|| type instanceof abaplint.BasicTypes.XGenericType) {
// if not supplied itsa a Hex(1)
resolved = "Hex";
}
else if (type instanceof abaplint.BasicTypes.HexType) {
resolved = featureHexUInt8 ? "HexUInt8" : "Hex";
if (type.getLength() !== 1) {
extra = "{length: " + type.getLength() + "}";
}
}
else if (type instanceof abaplint.BasicTypes.FloatType) {
resolved = "Float";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.FloatingPointType) {
resolved = "Float";
if (type.getQualifiedName() !== undefined) {
extra = "{qualifiedName: \"" + type.getQualifiedName()?.toUpperCase() + "\"}";
}
}
else if (type instanceof abaplint.BasicTypes.DecFloat34Type) {
resolved = "DecFloat34";
}
else if (type instanceof abaplint.BasicTypes.UnknownType) {
return `(() => { throw new Error("Unknown type: ${type.getError()}") })()`;
}
else if (type instanceof abaplint.BasicTypes.VoidType) {
return `(() => { throw new Error("Void type: ${type.getVoided()}") })()`;
}
else {
resolved = "typeTodo" + type.constructor.name;
}
return "new abap.types." + resolved + "(" + extra + ")";
}
}
exports.TranspileTypes = TranspileTypes;
//# sourceMappingURL=transpile_types.js.map