338 lines
13 KiB
JavaScript
338 lines
13 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.printType = printType;
|
|
exports.printFunction = printFunction;
|
|
exports.printProperty = printProperty;
|
|
exports.printTypeParameters = printTypeParameters;
|
|
exports.printComments = printComments;
|
|
exports.printModule = printModule;
|
|
exports.printFsFile = printFsFile;
|
|
|
|
var _String = require("./fable-core/String");
|
|
|
|
var _Seq = require("./fable-core/Seq");
|
|
|
|
var _List = require("./fable-core/List");
|
|
|
|
var _syntax = require("./syntax");
|
|
|
|
var _Option = require("./fable-core/Option");
|
|
|
|
var _naming = require("./naming");
|
|
|
|
var _CurriedLambda = require("./fable-core/CurriedLambda");
|
|
|
|
var _CurriedLambda2 = _interopRequireDefault(_CurriedLambda);
|
|
|
|
var _version = require("./version");
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function printType(tp) {
|
|
switch (tp.tag) {
|
|
case 7:
|
|
return tp.data.Name;
|
|
|
|
case 5:
|
|
return "obj";
|
|
|
|
case 4:
|
|
return (0, _String.toText)((0, _String.printf)("ResizeArray<%s>"))(printType(tp.data));
|
|
|
|
case 9:
|
|
if (tp.data.Types.length === 1) {
|
|
return (0, _String.toText)((0, _String.printf)("%s%s"))(printType((0, _Seq.item)(0, tp.data.Types)), tp.data.Option ? " option" : "");
|
|
} else {
|
|
const line = [];
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)("U%d<"))(tp.data.Types.length));
|
|
line.push.bind(line)((0, _String.join)(", ", (0, _List.map)(printType, tp.data.Types)));
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)(">%s"))(tp.data.Option ? " option" : ""));
|
|
return (0, _String.join)("", line);
|
|
}
|
|
|
|
case 11:
|
|
const line_1 = [];
|
|
line_1.push.bind(line_1)((0, _String.toText)((0, _String.printf)("%s"))(printType(tp.data.Type)));
|
|
|
|
if (tp.data.TypeParameters.length > 0) {
|
|
line_1.push.bind(line_1)("<");
|
|
line_1.push.bind(line_1)((0, _String.join)(", ", (0, _List.map)(printType, tp.data.TypeParameters)));
|
|
line_1.push.bind(line_1)(">");
|
|
}
|
|
|
|
return (0, _String.join)("", line_1);
|
|
|
|
case 8:
|
|
const line_2 = [];
|
|
const typs = tp.data.Params.length === 0 ? (0, _List.ofArray)([(0, _syntax.simpleType)("unit"), tp.data.ReturnType]) : (0, _List.append)((0, _List.map)(function (p) {
|
|
return p.Type;
|
|
}, tp.data.Params), (0, _List.ofArray)([tp.data.ReturnType]));
|
|
line_2.push.bind(line_2)("(");
|
|
line_2.push.bind(line_2)((0, _String.join)(" -> ", (0, _List.map)(printType, typs)));
|
|
line_2.push.bind(line_2)(")");
|
|
return (0, _String.join)("", line_2);
|
|
|
|
case 12:
|
|
const line_3 = [];
|
|
line_3.push.bind(line_3)((0, _String.join)(" * ", (0, _List.map)(printType, tp.data.Types)));
|
|
return (0, _String.join)("", line_3);
|
|
|
|
case 16:
|
|
const vtp = printType(tp.data.Type);
|
|
return (0, _String.toText)((0, _String.printf)("abstract %s: %s%s"))(tp.data.Name, vtp, tp.data.IsConst ? "" : " with get, set");
|
|
|
|
case 17:
|
|
return "string";
|
|
|
|
case 21:
|
|
return "obj";
|
|
|
|
default:
|
|
(0, _String.toConsole)((0, _String.printf)("unsupported printType %A"))(tp);
|
|
return "obj";
|
|
}
|
|
}
|
|
|
|
function printFunction(fn) {
|
|
const line = [];
|
|
|
|
if (fn.Kind.tag === 1) {
|
|
line.push.bind(line)("[<Emit \"new $0($1...)\">] ");
|
|
} else if (fn.Kind.tag === 2) {
|
|
line.push.bind(line)("[<Emit \"$0($1...)\">] ");
|
|
} else if (fn.Kind.tag === 3) {
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)("[<Emit \"%s\">] "))(fn.Kind.data));
|
|
}
|
|
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)("abstract %s"))((0, _Option.getValue)(fn.Name)));
|
|
const prms = (0, _List.map)(function (p) {
|
|
return p.ParamArray ? (0, _String.toText)((0, _String.printf)("[<ParamArray>] %s%s: %s"))(p.Optional ? "?" : "", p.Name, p.Type.tag === 4 ? printType(p.Type) : ((0, _String.toConsole)((0, _String.printf)("ParamArray function is not an array type: %s"))((0, _syntax.getName)(new _syntax.FsType(8, fn))), printType(p.Type))) : (0, _String.toText)((0, _String.printf)("%s%s: %s"))(p.Optional ? "?" : "", p.Name, printType(p.Type));
|
|
}, fn.Params);
|
|
|
|
if (prms.length === 0) {
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)(": unit")));
|
|
} else {
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)(": %s"))((0, _String.join)(" * ", prms)));
|
|
}
|
|
|
|
line.push.bind(line)((0, _String.toText)((0, _String.printf)(" -> %s"))(printType(fn.ReturnType)));
|
|
return (0, _String.join)("", line);
|
|
}
|
|
|
|
function printProperty(pr) {
|
|
return (0, _String.toText)((0, _String.printf)("%sabstract %s: %s%s%s%s"))(pr.Kind.tag === 1 ? "[<Emit \"$0[$1]{{=$2}}\">] " : "", pr.Name, pr.Index != null ? (0, _String.toText)((0, _String.printf)("%s: %s -> "))((0, _Option.getValue)(pr.Index).Name, printType((0, _Option.getValue)(pr.Index).Type)) : "", printType(pr.Type), pr.Option ? " option" : "", pr.IsReadonly ? "" : " with get, set");
|
|
}
|
|
|
|
function printTypeParameters(tps) {
|
|
if (tps.length === 0) {
|
|
return "";
|
|
} else {
|
|
const line = [];
|
|
line.push("<");
|
|
line.push.bind(line)((0, _String.join)(", ", (0, _List.map)(printType, tps)));
|
|
line.push(">");
|
|
return (0, _String.join)("", line);
|
|
}
|
|
}
|
|
|
|
function printComments(lines, indent, comments) {
|
|
if (function (list) {
|
|
return (0, _Seq.exists)(_syntax.FsCommentModule.isParam.bind(_syntax.FsCommentModule), list);
|
|
}(comments)) {
|
|
const summaryLines = function (list_1) {
|
|
return (0, _List.choose)(_syntax.FsCommentModule.asSummaryLine.bind(_syntax.FsCommentModule), list_1);
|
|
}(comments);
|
|
|
|
(0, _Seq.iterateIndexed)(function (i, desc) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s/// %s%s%s"))(indent, i === 0 ? "<summary>" : "", desc, i === summaryLines.length - 1 ? "</summary>" : ""));
|
|
}, summaryLines);
|
|
(0, _Seq.iterate)(function (comment) {
|
|
(0, _Seq.iterateIndexed)(function (i_1, desc_1) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s/// %s%s%s"))(indent, i_1 === 0 ? (0, _String.toText)((0, _String.printf)("<param name=\"%s\">"))(comment.Name) : "", desc_1, i_1 === comment.Description.length - 1 ? "</param>" : ""));
|
|
}, comment.Description);
|
|
}, function (list_2) {
|
|
return (0, _List.choose)(_syntax.FsCommentModule.asParam.bind(_syntax.FsCommentModule), list_2);
|
|
}(comments));
|
|
} else {
|
|
(0, _Seq.iterate)(function (comment_1) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s/// %s"))(indent, comment_1));
|
|
}, function (list_3) {
|
|
return (0, _List.choose)(_syntax.FsCommentModule.asSummaryLine.bind(_syntax.FsCommentModule), list_3);
|
|
}(comments));
|
|
}
|
|
}
|
|
|
|
function printModule(lines, indent, md) {
|
|
const lineCountStart = lines.length | 0;
|
|
let indent_1;
|
|
|
|
if (md.Name !== "") {
|
|
lines.push.bind(lines)("");
|
|
|
|
if (md.HasAttributes) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s[<%s)>]"))(indent, (0, _String.join)("; ", md.Attributes)));
|
|
}
|
|
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%smodule %s ="))(indent, md.Name));
|
|
indent_1 = (0, _String.toText)((0, _String.printf)("%s "))(indent);
|
|
} else {
|
|
indent_1 = indent;
|
|
}
|
|
|
|
for (let line of md.HelperLines) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s%s"))(indent_1, line));
|
|
}
|
|
|
|
for (let tp of md.Types) {
|
|
if (tp.tag === 20) {
|
|
if (tp.data.tag === 1) {
|
|
if (tp.data.data.Module !== tp.data.data.SpecifiedModule) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%smodule %s = %s"))(indent_1, tp.data.data.Module, tp.data.data.SpecifiedModule));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (let tp_1 of md.Types) {
|
|
if (tp_1.tag === 20) {
|
|
if (tp_1.data.tag === 0) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype %s = %s.%s"))(indent_1, tp_1.data.data.Type, tp_1.data.data.SpecifiedModule, tp_1.data.data.Type));
|
|
}
|
|
}
|
|
}
|
|
|
|
for (let tp_2 of md.Types) {
|
|
switch (tp_2.tag) {
|
|
case 0:
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("")));
|
|
printComments(lines, indent_1, tp_2.data.Comments);
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype [<AllowNullLiteral>] %s%s ="))(indent_1, tp_2.data.Name, printTypeParameters(tp_2.data.TypeParameters)));
|
|
const nLines = {
|
|
contents: 0
|
|
};
|
|
|
|
for (let ih of tp_2.data.Inherits) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s inherit %s"))(indent_1, printType(ih)));
|
|
void nLines.contents++;
|
|
}
|
|
|
|
for (let mbr of tp_2.data.Members) {
|
|
if (mbr.tag === 8) {
|
|
const indent_2 = (0, _String.toText)((0, _String.printf)("%s "))(indent_1);
|
|
printComments(lines, indent_2, mbr.data.AllComments);
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s%s"))(indent_2, printFunction(mbr.data)));
|
|
void nLines.contents++;
|
|
} else if (mbr.tag === 2) {
|
|
const indent_3 = (0, _String.toText)((0, _String.printf)("%s "))(indent_1);
|
|
printComments(lines, indent_3, mbr.data.Comments);
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s%s"))(indent_3, printProperty(mbr.data)));
|
|
void nLines.contents++;
|
|
} else {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s %s"))(indent_1, printType(mbr)));
|
|
void nLines.contents++;
|
|
}
|
|
}
|
|
|
|
if (nLines.contents === 0) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s interface end"))(indent_1));
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("")));
|
|
const matchValue = tp_2.data.Type;
|
|
|
|
if (matchValue.tag === 1) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype [<StringEnum>] [<RequireQualifiedAccess>] %s ="))(indent_1, tp_2.data.Name));
|
|
|
|
for (let cs of tp_2.data.Cases) {
|
|
const v = function (option) {
|
|
return (0, _Option.defaultArg)(option, cs.Name);
|
|
}(cs.Value);
|
|
|
|
const unm = (0, _naming.createEnumName)(cs.Name);
|
|
const line_1 = [];
|
|
|
|
if ((0, _naming.nameEqualsDefaultFableValue)(unm, v)) {
|
|
line_1.push.bind(line_1)((0, _String.toText)((0, _String.printf)(" | %s"))(unm));
|
|
} else {
|
|
line_1.push.bind(line_1)((0, _String.toText)((0, _String.printf)(" | [<CompiledName \"%s\">] %s"))(v, unm));
|
|
}
|
|
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s%s"))(indent_1, (0, _String.join)("", line_1)));
|
|
}
|
|
} else if (matchValue.tag === 2) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype %s ="))(indent_1, tp_2.data.Name));
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s obj"))(indent_1));
|
|
} else {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype [<RequireQualifiedAccess>] %s ="))(indent_1, tp_2.data.Name));
|
|
|
|
for (let cs_1 of tp_2.data.Cases) {
|
|
const unm_1 = (0, _naming.createEnumName)(cs_1.Name);
|
|
const line_2 = [];
|
|
line_2.push.bind(line_2)((0, _String.toText)((0, _String.printf)(" | %s"))(unm_1));
|
|
|
|
if ((0, _CurriedLambda2.default)(() => cs_1.Value != null)()) {
|
|
line_2.push.bind(line_2)((0, _String.toText)((0, _String.printf)(" = %s"))((0, _Option.getValue)(cs_1.Value)));
|
|
}
|
|
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s%s"))(indent_1, (0, _String.join)("", line_2)));
|
|
}
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("")));
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%stype %s%s ="))(indent_1, tp_2.data.Name, printTypeParameters(tp_2.data.TypeParameters)));
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%s %s"))(indent_1, printType(tp_2.data.Type)));
|
|
break;
|
|
|
|
case 13:
|
|
printModule(lines, indent_1, tp_2.data);
|
|
break;
|
|
|
|
case 16:
|
|
if (tp_2.data.HasDeclare) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("%slet %s%s: %s = jsNative"))(indent_1, tp_2.data.Export != null ? (0, _Option.getValue)(tp_2.data.Export).IsGlobal ? "[<Global>] " : (0, _String.toText)((0, _String.printf)("[<Import(\"%s\",\"%s\")>] "))((0, _Option.getValue)(tp_2.data.Export).Selector, (0, _Option.getValue)(tp_2.data.Export).Path) : "", tp_2.data.Name, printType(tp_2.data.Type)));
|
|
}
|
|
|
|
break;
|
|
|
|
default:}
|
|
}
|
|
|
|
const addedLines = lines.length - lineCountStart | 0;
|
|
|
|
if (addedLines === 2) {
|
|
for (let forLoopVar = 0; forLoopVar <= 1; forLoopVar++) {
|
|
lines.splice(lines.length - 1, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
function printFsFile(file) {
|
|
const lines = [];
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("// ts2fable %s"))(_version.version));
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("module rec %s"))(file.Namespace));
|
|
|
|
for (let opn of file.Opens) {
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("open %s"))(opn));
|
|
}
|
|
|
|
lines.push.bind(lines)((0, _String.toText)((0, _String.printf)("")));
|
|
|
|
for (let f of file.Files) {
|
|
(0, _Seq.iterate)(function (md) {
|
|
printModule(lines, "", md);
|
|
}, (0, _List.filter)(function (md_1) {
|
|
return md_1.Types.length > 0;
|
|
}, f.Modules));
|
|
}
|
|
|
|
return lines;
|
|
} |