autouml
Version:
Autogenerate UML diagrams using d2
164 lines (163 loc) • 5.79 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.d2Codegen = void 0;
var visitor_1 = require("../visitor");
var arrows_1 = require("./arrows");
function accessToPrefix(access) {
var tor = "";
if (access.has(1 /* autouml.mapping.AccessModifier.PRIVATE */)) {
tor = "-";
}
else if (access.has(2 /* autouml.mapping.AccessModifier.PROTECTED */)) {
tor = "\\#";
}
else {
tor = "+";
}
return tor;
}
var TAB_SPACE = 4;
var TAB = new Array(TAB_SPACE)
.fill(0)
.map(function (_) { return " "; })
.join("");
function indentLines(lines) {
for (var i = 0; i < lines.length; i++) {
lines[i] = TAB + lines[i];
}
return lines;
}
var d2Codegen = /** @class */ (function (_super) {
__extends(d2Codegen, _super);
function d2Codegen(map) {
var _this = _super.call(this, map) || this;
_this.target = 0 /* autouml.codegen.Target.d2 */;
return _this;
}
d2Codegen.prototype.visitProgram = function (scope, childData) {
var lines = childData.flat();
return lines;
// return [`Program: {`, ...indentLines(lines), `}`];
};
d2Codegen.prototype.visitFile = function (scope, childData) {
var lines = childData.flat();
return __spreadArray(__spreadArray([
"".concat(scope.name
.replace(/\./g, "\\.")
.replace(/\\(?!\.)/g, ".")
.replace(/\/(?!\.)/g, ".")
.split(/(?<!\\)\./g)
.map(function (x) { return "\"".concat(x, "\""); })
.join("."), " {")
], indentLines(lines), true), [
"}",
], false);
};
d2Codegen.prototype.visitNamespace = function (scope, childData) {
var lines = childData.flat();
return __spreadArray(__spreadArray([
"".concat(scope.name, " {")
], indentLines(__spreadArray(["shape: package"], lines, true)), true), [
"}",
], false);
};
d2Codegen.prototype.visitClass = function (scope, childData, fieldData, methodData) {
var fLines = fieldData.flat();
var mLines = methodData.flat();
return __spreadArray(__spreadArray([
"".concat(scope.name, " {")
], indentLines(__spreadArray(__spreadArray(__spreadArray([
"shape: class",
"# Field Data"
], fLines, true), [
"# Method Data"
], false), mLines, true)), true), [
"}",
], false);
};
d2Codegen.prototype.visitInterface = function (scope, childData, fieldData) {
var fLines = fieldData.flat();
return __spreadArray(__spreadArray([
"".concat(scope.name, " {")
], indentLines(__spreadArray([
"shape: class",
"# Field Data"
], fLines, true)), true), [
"}",
], false);
};
d2Codegen.prototype.visitEnum = function (scope, childData, enumData) {
// TODO: this is not the right d2 shape
return __spreadArray(__spreadArray([
"".concat(scope.name, " {")
], indentLines([
"shape: class",
"# Field Data",
"".concat(enumData.flat().join(", ")),
]), true), [
"}",
], false);
};
d2Codegen.prototype.visitEnumField = function (f) {
return [f];
};
d2Codegen.prototype.visitInterfaceField = function (f) {
return [
"+".concat(escapeName(f.name), " : ").concat(typeToString(f.type)),
];
};
d2Codegen.prototype.visitClassField = function (f) {
return [
"".concat(accessToPrefix(f.access)).concat(f.name, " : ").concat(typeToString(f.type)),
];
};
d2Codegen.prototype.visitClassMethod = function (m) {
var params = m.parameters.map(function (x) {
return "".concat(x.name, "\\: ").concat(typeToString(x.type, false));
});
return [
"".concat(accessToPrefix(m.access)).concat(m.name, "(").concat(params, ") : ").concat(typeToString(m.type)),
];
};
d2Codegen.prototype.compileRelations = function () {
return (0, arrows_1.compileRelations)(this.relations);
};
return d2Codegen;
}(visitor_1.Visitor));
exports.d2Codegen = d2Codegen;
function escapeName(s) {
return s.replace(/[\[\]:]/g, "\\$&");
}
function typeToString(t, atEnd) {
if (atEnd === void 0) { atEnd = true; }
if (atEnd) {
return "|||ts ".concat(t.name, "|||");
}
else {
return t.name.replace(/[\[\]\.><]/g, "\\$&");
}
}