1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 | var __extends = (this && this.__extends) || function (d, b) {
|
7 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 |
|
12 | var ElmType = (function () {
|
13 | function ElmType() {
|
14 | }
|
15 | return ElmType;
|
16 | }());
|
17 | exports.ElmType = ElmType;
|
18 | var ElmTypeName = (function (_super) {
|
19 | __extends(ElmTypeName, _super);
|
20 | function ElmTypeName(name) {
|
21 | _super.call(this);
|
22 | this.name = name;
|
23 | }
|
24 | return ElmTypeName;
|
25 | }(ElmType));
|
26 | exports.ElmTypeName = ElmTypeName;
|
27 | var ElmTypeApp = (function (_super) {
|
28 | __extends(ElmTypeApp, _super);
|
29 | function ElmTypeApp(name, args) {
|
30 | _super.call(this);
|
31 | this.name = name;
|
32 | this.args = args;
|
33 | }
|
34 | return ElmTypeApp;
|
35 | }(ElmType));
|
36 | exports.ElmTypeApp = ElmTypeApp;
|
37 | var ElmTypeRecord = (function (_super) {
|
38 | __extends(ElmTypeRecord, _super);
|
39 | function ElmTypeRecord(fields, typeParam) {
|
40 | _super.call(this);
|
41 | this.fields = fields;
|
42 | this.typeParam = typeParam;
|
43 | }
|
44 | return ElmTypeRecord;
|
45 | }(ElmType));
|
46 | exports.ElmTypeRecord = ElmTypeRecord;
|
47 |
|
48 | var ElmDecl = (function () {
|
49 | function ElmDecl() {
|
50 | }
|
51 | return ElmDecl;
|
52 | }());
|
53 | exports.ElmDecl = ElmDecl;
|
54 | var ElmTypeDecl = (function (_super) {
|
55 | __extends(ElmTypeDecl, _super);
|
56 | function ElmTypeDecl(name, constructors) {
|
57 | _super.call(this);
|
58 | this.name = name;
|
59 | this.constructors = constructors;
|
60 | }
|
61 | return ElmTypeDecl;
|
62 | }(ElmDecl));
|
63 | exports.ElmTypeDecl = ElmTypeDecl;
|
64 | var ElmTypeAliasDecl = (function (_super) {
|
65 | __extends(ElmTypeAliasDecl, _super);
|
66 | function ElmTypeAliasDecl(name, type, typeParams) {
|
67 | if (typeParams === void 0) { typeParams = []; }
|
68 | _super.call(this);
|
69 | this.name = name;
|
70 | this.type = type;
|
71 | this.typeParams = typeParams;
|
72 | }
|
73 | return ElmTypeAliasDecl;
|
74 | }(ElmDecl));
|
75 | exports.ElmTypeAliasDecl = ElmTypeAliasDecl;
|
76 | var ElmFunctionDecl = (function (_super) {
|
77 | __extends(ElmFunctionDecl, _super);
|
78 | function ElmFunctionDecl(name, parameters, returnType, body) {
|
79 | _super.call(this);
|
80 | this.name = name;
|
81 | this.parameters = parameters;
|
82 | this.returnType = returnType;
|
83 | this.body = body;
|
84 | }
|
85 | return ElmFunctionDecl;
|
86 | }(ElmDecl));
|
87 | exports.ElmFunctionDecl = ElmFunctionDecl;
|
88 | var ElmFieldDecl = (function () {
|
89 | function ElmFieldDecl(name, type) {
|
90 | this.name = name;
|
91 | this.type = type;
|
92 | }
|
93 | return ElmFieldDecl;
|
94 | }());
|
95 | exports.ElmFieldDecl = ElmFieldDecl;
|
96 | var ElmParameterDecl = (function () {
|
97 | function ElmParameterDecl(name, type) {
|
98 | this.name = name;
|
99 | this.type = type;
|
100 | }
|
101 | return ElmParameterDecl;
|
102 | }());
|
103 | exports.ElmParameterDecl = ElmParameterDecl;
|
104 | function moduleToString(name, expose, imports, decls) {
|
105 | var warn = '{-\n This file was automatically generated by elm-graphql.\n-}\n';
|
106 | return warn + 'module ' + name + ' exposing (' + expose.join(', ') + ')\n' +
|
107 | imports.map(function (str) { return '\nimport ' + str; }).join('') + '\n\n' +
|
108 | decls.map(declToString).join('\n\n');
|
109 | }
|
110 | exports.moduleToString = moduleToString;
|
111 | function declToString(decl) {
|
112 | if (decl instanceof ElmTypeDecl) {
|
113 | return typeDeclToString(decl);
|
114 | }
|
115 | else if (decl instanceof ElmFunctionDecl) {
|
116 | return funtionToString(decl);
|
117 | }
|
118 | else if (decl instanceof ElmTypeAliasDecl) {
|
119 | return typeAliasDeclToString(decl);
|
120 | }
|
121 | else {
|
122 | throw new Error('unexpected decl: ' + decl.constructor.name + ' ' + JSON.stringify(decl));
|
123 | }
|
124 | }
|
125 | exports.declToString = declToString;
|
126 | function typeDeclToString(type) {
|
127 | return 'type ' + type.name + '\n' +
|
128 | ' = ' + type.constructors.join('\n | ') + '\n';
|
129 | }
|
130 | exports.typeDeclToString = typeDeclToString;
|
131 | function typeAliasDeclToString(type) {
|
132 | return 'type alias ' + type.name + ' ' + type.typeParams.join(' ') + '\n' +
|
133 | ' = ' + typeToString(type.type, 0) + '\n';
|
134 | }
|
135 | exports.typeAliasDeclToString = typeAliasDeclToString;
|
136 | function funtionToString(func) {
|
137 | var paramTypes = func.parameters.map(function (p) { return typeToString(p.type, 1); }).join(' -> ');
|
138 | var paramNames = func.parameters.map(function (p) { return p.name; }).join(' ');
|
139 | var arrow = paramTypes.length > 0 ? ' -> ' : '';
|
140 | var space = paramTypes.length > 0 ? ' ' : '';
|
141 | return func.name + ' : ' + paramTypes + arrow + typeToString(func.returnType, 0) + '\n' +
|
142 | func.name + space + paramNames + ' =\n ' + exprToString(func.body, 0) + '\n';
|
143 | }
|
144 | exports.funtionToString = funtionToString;
|
145 | function fieldToString(field, level) {
|
146 | return field.name + ' : ' + typeToString(field.type, level, true) + '\n';
|
147 | }
|
148 | function typeToString(ty, level, isField) {
|
149 | if (ty instanceof ElmTypeName) {
|
150 | return ty.name;
|
151 | }
|
152 | else if (ty instanceof ElmTypeApp) {
|
153 | var str = ty.name + ' ' + ty.args.map(function (arg) { return typeToString(arg, level); }).join(' ');
|
154 | if (isField) {
|
155 | return str;
|
156 | }
|
157 | else {
|
158 | return '(' + str + ')';
|
159 | }
|
160 | }
|
161 | else if (ty instanceof ElmTypeRecord) {
|
162 | var indent = makeIndent(level);
|
163 | var pipe = ty.typeParam ? ty.typeParam + ' | ' : '';
|
164 | return (indent + "{ ") + pipe +
|
165 | ty.fields.map(function (f) { return fieldToString(f, level + 1); }).join(indent + " , ") +
|
166 | (indent + "}");
|
167 | }
|
168 | else {
|
169 | throw new Error('unexpected type: ' + ty.constructor.name + ' ' + JSON.stringify(ty));
|
170 | }
|
171 | }
|
172 | exports.typeToString = typeToString;
|
173 | function makeIndent(level) {
|
174 | var str = '';
|
175 | for (var i = 0; i < level; i++) {
|
176 | str += ' ';
|
177 | }
|
178 | return str;
|
179 | }
|
180 | function exprToString(expr, level) {
|
181 | return expr.expr;
|
182 | }
|
183 | exports.exprToString = exprToString;
|
184 |
|
\ | No newline at end of file |