@quenk/wml 2.11.4 → 2.13.0
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.
- package/lib/cli.js +24 -29
- package/lib/cli.js.map +1 -1
- package/lib/cli.ts +20 -13
- package/lib/compile/codegen.d.ts +1 -13
- package/lib/compile/codegen.js +550 -656
- package/lib/compile/codegen.js.map +1 -1
- package/lib/compile/codegen.ts +43 -44
- package/lib/compile/index.js +6 -11
- package/lib/compile/index.js.map +1 -1
- package/lib/compile/transform.js +14 -23
- package/lib/compile/transform.js.map +1 -1
- package/lib/dom.js +118 -167
- package/lib/dom.js.map +1 -1
- package/lib/index.js +7 -10
- package/lib/index.js.map +1 -1
- package/lib/main.js +31 -56
- package/lib/main.js.map +1 -1
- package/lib/parse/ast.d.ts +8 -12
- package/lib/parse/ast.js +167 -227
- package/lib/parse/ast.js.map +1 -1
- package/lib/parse/ast.ts +4 -14
- package/lib/parse/generated.js +2335 -2288
- package/lib/parse/index.js +7 -10
- package/lib/parse/index.js.map +1 -1
- package/lib/parse/test.js +285 -55
- package/lib/parse/test.js.map +1 -1
- package/lib/parse/test.ts +23 -2
- package/lib/parse/wml.y +58 -59
- package/lib/tsconfig.json +1 -1
- package/package.json +1 -2
package/lib/compile/codegen.js
CHANGED
|
@@ -2,52 +2,43 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Typescript code generator.
|
|
4
4
|
*/
|
|
5
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
6
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
7
|
-
if (ar || !(i in from)) {
|
|
8
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
9
|
-
ar[i] = from[i];
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
13
|
-
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
16
|
-
exports.unqualifiedIdentifier2TS = exports.qualifiedIdentifier2TS = exports.identifier2TS = exports.qualifiedConstructor2TS = exports.unqualifiedConstructor2TS = exports.constructor2TS = exports.identifierOrConstructor2TS = exports.contextVariable2TS = exports.contextProperty2TS = exports.key2TS = exports.property2TS = exports.list2TS = exports.record2TS = exports.number2TS = exports.string2TS = exports.boolean2TS = exports.literal2TS = exports.functionExpression2TS = exports.memberExpression2TS = exports.args2TS = exports.typeArgs2TS = exports.callExpression2TS = exports.constructExpression2TS = exports.funApplication2TS = exports.viewConstruction2TS = exports.
|
|
6
|
+
exports.forFromStatement2TS = exports.forOfStatement2TS = exports.forInStatement2TS = exports.ifStatement2TS = exports.interpolation2TS = exports.attrs2String = exports.attributeName2TS = exports.attributeValue2TS = exports.attribute2TS = exports.node2TS = exports.widget2TS = exports.tag2TS = exports.child2TS = exports.children2TS = exports.untypedParameter2TS = exports.typedParameter2TS = exports.parameter2TS = exports.parameters2TS = exports.typeMap2TS = exports.expandTypeMap = exports.typeMapFromRecordType = exports.typeMapFromMemberDecs = exports.memberDeclarations2TS = exports.recordType2Ts = exports.tupleType2TS = exports.listType2TS = exports.functionType2TS = exports.constructorType2TS = exports.type2TS = exports.typeParameter2TS = exports.typeParameters2TS = exports.viewStatement2TS = exports.funStatement2TS = exports.letStatement2TS = exports.contextStatement2TS = exports.aliasStatement2TS = exports.export2TS = exports.exports2TS = exports.compositeMember2TS = exports.aliasedMember2TS = exports.aggregateMember2TS = exports.importMember2TS = exports.importStatement2TS = exports.importStatements2TS = exports.CodeGenerator = exports.THIS = exports.DOCUMENT = exports.WML = exports.VIEW = exports.CONTEXT = void 0;
|
|
7
|
+
exports.unqualifiedIdentifier2TS = exports.qualifiedIdentifier2TS = exports.identifier2TS = exports.qualifiedConstructor2TS = exports.unqualifiedConstructor2TS = exports.constructor2TS = exports.identifierOrConstructor2TS = exports.contextVariable2TS = exports.contextProperty2TS = exports.key2TS = exports.property2TS = exports.list2TS = exports.record2TS = exports.number2TS = exports.string2TS = exports.boolean2TS = exports.literal2TS = exports.functionExpression2TS = exports.memberExpression2TS = exports.args2TS = exports.typeArgs2TS = exports.callExpression2TS = exports.constructExpression2TS = exports.funApplication2TS = exports.viewConstruction2TS = exports.unaryExpression2TS = exports.binaryExpression2TS = exports.ifThenExpression2TS = exports.expression2TS = exports.characters2TS = void 0;
|
|
17
8
|
/** imports */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
const ast = require("../parse/ast");
|
|
10
|
+
const path_1 = require("@quenk/noni/lib/data/record/path");
|
|
11
|
+
const record_1 = require("@quenk/noni/lib/data/record");
|
|
12
|
+
const array_1 = require("@quenk/noni/lib/data/array");
|
|
13
|
+
const transform_1 = require("./transform");
|
|
23
14
|
exports.CONTEXT = '__context';
|
|
24
15
|
exports.VIEW = '__view';
|
|
25
16
|
exports.WML = '__wml';
|
|
26
17
|
exports.DOCUMENT = '__document';
|
|
27
18
|
exports.THIS = '__this';
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
19
|
+
const MAYBE = '__Maybe';
|
|
20
|
+
const FROM_NULLABLE = '__fromNullable';
|
|
21
|
+
const FROM_ARRAY = '__fromArray';
|
|
22
|
+
const NODE_PARAMS = `tag:string, attrs:${exports.WML}.Attrs, ` +
|
|
23
|
+
`children: ${exports.WML}.Content[]`;
|
|
24
|
+
const WIDGET_PARAMS = `w: ${exports.WML}.Widget, attrs:${exports.WML}.Attrs`;
|
|
25
|
+
const REGISTER_VIEW_PARAMS = `v:${exports.WML}.View`;
|
|
26
|
+
const REGISTER_PARAMS = `e:${exports.WML}.WMLElement, ` +
|
|
27
|
+
`attrs:${exports.WML}.Attributes<any>`;
|
|
28
|
+
const THROW_CHILD_ERR = ' throw new TypeError(`Can not adopt ' +
|
|
38
29
|
'child ${c} of type \${typeof c}`);';
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
30
|
+
const THROW_INVALIDATE_ERR = ` throw new Error('invalidate(): cannot ` +
|
|
31
|
+
`invalidate this view, it has no parent node!');`;
|
|
32
|
+
const IGNORE_UNUSED = '//@ts-ignore:6192';
|
|
33
|
+
const RECORD = '__Record<A>';
|
|
34
|
+
const IF = '__if';
|
|
35
|
+
const IFARG = `__IfArg`;
|
|
36
|
+
const FOR_OF = '__forOf';
|
|
37
|
+
const FOR_IN = '__forIn';
|
|
38
|
+
const FOR_ALT_TYPE = '__ForAlt';
|
|
39
|
+
const FOR_IN_BODY = '__ForInBody<A>';
|
|
40
|
+
const FOR_OF_BODY = '__ForOfBody<A>';
|
|
41
|
+
const prims = [
|
|
51
42
|
'String',
|
|
52
43
|
'Boolean',
|
|
53
44
|
'Number',
|
|
@@ -58,8 +49,8 @@ var prims = [
|
|
|
58
49
|
'Never',
|
|
59
50
|
'Any'
|
|
60
51
|
];
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
const casters = ['String', 'Boolean', 'Number', 'Object'];
|
|
53
|
+
const operators = {
|
|
63
54
|
'==': '===',
|
|
64
55
|
'!=': '!=='
|
|
65
56
|
};
|
|
@@ -70,125 +61,120 @@ var operators = {
|
|
|
70
61
|
* Given a parsed tree (starting at ast.Module), its generate method will
|
|
71
62
|
* provide a typescript module.
|
|
72
63
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
64
|
+
class CodeGenerator {
|
|
65
|
+
constructor(options) {
|
|
75
66
|
this.options = options;
|
|
76
67
|
}
|
|
77
68
|
/**
|
|
78
69
|
* create a new CodeGenerator instance.
|
|
79
70
|
*/
|
|
80
|
-
|
|
71
|
+
static create(opts) {
|
|
81
72
|
return new CodeGenerator(opts);
|
|
82
|
-
}
|
|
73
|
+
}
|
|
83
74
|
/**
|
|
84
75
|
* generate a Typescript module from an WML AST.
|
|
85
76
|
*/
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
generate(tree) {
|
|
78
|
+
let newTree = (0, transform_1.transformTree)(tree);
|
|
88
79
|
return [
|
|
89
|
-
|
|
90
|
-
|
|
80
|
+
`import * as ${exports.WML} from '${this.options.module}';`,
|
|
81
|
+
`import * as ${exports.DOCUMENT} from '${this.options.dom}';`,
|
|
91
82
|
imports(this),
|
|
92
83
|
(0, exports.importStatements2TS)(this, newTree.imports),
|
|
93
84
|
eol(this),
|
|
94
85
|
typeDefinitions(this),
|
|
95
86
|
eol(this),
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
87
|
+
`// @ts-ignore 6192`,
|
|
88
|
+
`const text = ${exports.DOCUMENT}.text;`,
|
|
89
|
+
`// @ts-ignore 6192`,
|
|
90
|
+
`const unsafe = ${exports.DOCUMENT}.unsafe`,
|
|
91
|
+
`// @ts-ignore 6192`,
|
|
92
|
+
`const isSet = (value:any) => value != null`,
|
|
102
93
|
(0, exports.exports2TS)(this, newTree.exports)
|
|
103
94
|
].join(eol(this));
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
}());
|
|
95
|
+
}
|
|
96
|
+
}
|
|
107
97
|
exports.CodeGenerator = CodeGenerator;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
].join(eol(ctx));
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
].join(eol(ctx));
|
|
98
|
+
const eol = (ctx) => `${ctx.options.EOL}`;
|
|
99
|
+
const imports = (ctx) => [
|
|
100
|
+
`//@ts-ignore: 6192`,
|
|
101
|
+
`import {`,
|
|
102
|
+
`Maybe as ${MAYBE},`,
|
|
103
|
+
`fromNullable as ${FROM_NULLABLE},`,
|
|
104
|
+
`fromArray as ${FROM_ARRAY}`,
|
|
105
|
+
`}`,
|
|
106
|
+
`from '@quenk/noni/lib/data/maybe';`
|
|
107
|
+
].join(eol(ctx));
|
|
108
|
+
const typeDefinitions = (ctx) => [
|
|
109
|
+
`${IGNORE_UNUSED}`,
|
|
110
|
+
`type ${IFARG} = ()=>${exports.WML}.Content[]`,
|
|
111
|
+
``,
|
|
112
|
+
`${IGNORE_UNUSED}`,
|
|
113
|
+
`type ${FOR_ALT_TYPE} = ()=> ${exports.WML}.Content[]`,
|
|
114
|
+
``,
|
|
115
|
+
`${IGNORE_UNUSED}`,
|
|
116
|
+
`type ${FOR_IN_BODY} =(val:A, idx:number, all:A[])=>` +
|
|
117
|
+
`${exports.WML}.Content[]`,
|
|
118
|
+
``,
|
|
119
|
+
`${IGNORE_UNUSED}`,
|
|
120
|
+
`type ${FOR_OF_BODY} = (val:A, key:string, all:object) =>` +
|
|
121
|
+
`${exports.WML}.Content[]`,
|
|
122
|
+
``,
|
|
123
|
+
`${IGNORE_UNUSED}`,
|
|
124
|
+
`interface ${RECORD} {`,
|
|
125
|
+
``,
|
|
126
|
+
` [key:string]: A`,
|
|
127
|
+
``,
|
|
128
|
+
`}`,
|
|
129
|
+
``,
|
|
130
|
+
`${IGNORE_UNUSED}`,
|
|
131
|
+
`const ${IF} = (__expr:boolean, __conseq:${IFARG},__alt?:${IFARG}) ` +
|
|
132
|
+
`: Content[]=>`,
|
|
133
|
+
`(__expr) ? __conseq() : __alt ? __alt() : [];`,
|
|
134
|
+
``,
|
|
135
|
+
`${IGNORE_UNUSED}`,
|
|
136
|
+
`const ${FOR_IN} = <A>(list:A[], f:${FOR_IN_BODY}, alt:` +
|
|
137
|
+
`${FOR_ALT_TYPE}) : ${exports.WML}.Content[] => {`,
|
|
138
|
+
``,
|
|
139
|
+
` let ret:${exports.WML}.Content[] = [];`,
|
|
140
|
+
``,
|
|
141
|
+
` for(let i=0; i<list.length; i++)`,
|
|
142
|
+
` ret = ret.concat(f(list[i], i, list));`,
|
|
143
|
+
``,
|
|
144
|
+
` return ret.length === 0 ? alt() : ret;`,
|
|
145
|
+
``,
|
|
146
|
+
`}`,
|
|
147
|
+
`${IGNORE_UNUSED}`,
|
|
148
|
+
`const ${FOR_OF} = <A>(o:${RECORD}, f:${FOR_OF_BODY},` +
|
|
149
|
+
`alt:${FOR_ALT_TYPE}) : ${exports.WML}.Content[] => {`,
|
|
150
|
+
``,
|
|
151
|
+
` let ret:${exports.WML}.Content[] = [];`,
|
|
152
|
+
``,
|
|
153
|
+
` for(let key in o)`,
|
|
154
|
+
` if(o.hasOwnProperty(key)) `,
|
|
155
|
+
` ret = ret.concat(f((o)[key], key, o));`,
|
|
156
|
+
``,
|
|
157
|
+
` return ret.length === 0 ? alt(): ret;`,
|
|
158
|
+
``,
|
|
159
|
+
`}`
|
|
160
|
+
].join(eol(ctx));
|
|
171
161
|
/**
|
|
172
162
|
* importStatements2TS converts a list of ImportStatements into typescript.
|
|
173
163
|
*/
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
.join(";".concat(eol(ctx)));
|
|
179
|
-
};
|
|
164
|
+
const importStatements2TS = (ctx, list) => list
|
|
165
|
+
.map(exports.importStatement2TS)
|
|
166
|
+
.filter((stmt, idx, list) => list.indexOf(stmt) == idx)
|
|
167
|
+
.join(`;${eol(ctx)}`);
|
|
180
168
|
exports.importStatements2TS = importStatements2TS;
|
|
181
169
|
/**
|
|
182
170
|
* importStatement2TS
|
|
183
171
|
*/
|
|
184
|
-
|
|
185
|
-
return "import ".concat((0, exports.importMember2TS)(n.member), " from '").concat(n.module.value.trim(), "'; ");
|
|
186
|
-
};
|
|
172
|
+
const importStatement2TS = (n) => `import ${(0, exports.importMember2TS)(n.member)} from '${n.module.value.trim()}'; `;
|
|
187
173
|
exports.importStatement2TS = importStatement2TS;
|
|
188
174
|
/**
|
|
189
175
|
* importMember2TS
|
|
190
176
|
*/
|
|
191
|
-
|
|
177
|
+
const importMember2TS = (n) => {
|
|
192
178
|
if (n instanceof ast.AggregateMember)
|
|
193
179
|
return (0, exports.aggregateMember2TS)(n);
|
|
194
180
|
else if (n instanceof ast.AliasedMember)
|
|
@@ -202,38 +188,30 @@ exports.importMember2TS = importMember2TS;
|
|
|
202
188
|
/**
|
|
203
189
|
* aggregateMember2TS
|
|
204
190
|
*/
|
|
205
|
-
|
|
206
|
-
return "* as ".concat((0, exports.identifierOrConstructor2TS)(n.id), " ");
|
|
207
|
-
};
|
|
191
|
+
const aggregateMember2TS = (n) => `* as ${(0, exports.identifierOrConstructor2TS)(n.id)} `;
|
|
208
192
|
exports.aggregateMember2TS = aggregateMember2TS;
|
|
209
193
|
/**
|
|
210
194
|
* aliasedMember2TS
|
|
211
195
|
*/
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
"as ".concat((0, exports.identifierOrConstructor2TS)(n.alias), " ");
|
|
215
|
-
};
|
|
196
|
+
const aliasedMember2TS = (n) => `${(0, exports.identifierOrConstructor2TS)(n.member)} ` +
|
|
197
|
+
`as ${(0, exports.identifierOrConstructor2TS)(n.alias)} `;
|
|
216
198
|
exports.aliasedMember2TS = aliasedMember2TS;
|
|
217
199
|
/**
|
|
218
200
|
* compositeMember2TS
|
|
219
201
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
(0, exports.identifierOrConstructor2TS)(m); }).join(',')) + '}';
|
|
224
|
-
};
|
|
202
|
+
const compositeMember2TS = (n) => '{' + (n.members.map(m => (m instanceof ast.AliasedMember) ?
|
|
203
|
+
(0, exports.aliasedMember2TS)(m) :
|
|
204
|
+
(0, exports.identifierOrConstructor2TS)(m)).join(',')) + '}';
|
|
225
205
|
exports.compositeMember2TS = compositeMember2TS;
|
|
226
206
|
/**
|
|
227
207
|
* exports2TS converts a list of exports to typescript.
|
|
228
208
|
*/
|
|
229
|
-
|
|
230
|
-
return list.map(function (e) { return (0, exports.export2TS)(ctx, e); }).join(';' + eol(ctx));
|
|
231
|
-
};
|
|
209
|
+
const exports2TS = (ctx, list) => list.map(e => (0, exports.export2TS)(ctx, e)).join(';' + eol(ctx));
|
|
232
210
|
exports.exports2TS = exports2TS;
|
|
233
211
|
/**
|
|
234
212
|
* export2TS
|
|
235
213
|
*/
|
|
236
|
-
|
|
214
|
+
const export2TS = (ctx, n) => {
|
|
237
215
|
if (n instanceof ast.AliasStatement)
|
|
238
216
|
return (0, exports.aliasStatement2TS)(n);
|
|
239
217
|
else if (n instanceof ast.ContextStatement)
|
|
@@ -253,27 +231,25 @@ exports.export2TS = export2TS;
|
|
|
253
231
|
/**
|
|
254
232
|
* aliasStatement2TS
|
|
255
233
|
*/
|
|
256
|
-
|
|
257
|
-
|
|
234
|
+
const aliasStatement2TS = (n) => {
|
|
235
|
+
let typeArgs = (n.typeParameters.length > 0) ?
|
|
258
236
|
(0, exports.typeParameters2TS)(n.typeParameters) : '';
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
return
|
|
237
|
+
let preamble = `export type ${n.id.value}${typeArgs}`;
|
|
238
|
+
let members = n.members.map(m => (0, exports.type2TS)(m)).join('|');
|
|
239
|
+
return `${preamble} = ${members};`;
|
|
262
240
|
};
|
|
263
241
|
exports.aliasStatement2TS = aliasStatement2TS;
|
|
264
242
|
/**
|
|
265
243
|
* contextStatement2TS
|
|
266
244
|
*/
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
245
|
+
const contextStatement2TS = (n) => {
|
|
246
|
+
let preamble = `export interface ${n.id.value}`;
|
|
247
|
+
let typeArgs = (n.typeParameters.length > 0) ?
|
|
270
248
|
(0, exports.typeParameters2TS)(n.typeParameters) : '';
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}), parents = _a[0], members = _a[1];
|
|
274
|
-
var parentList = parents
|
|
249
|
+
let [parents, members] = (0, array_1.partition)(n.members, member => member instanceof ast.ConstructorType);
|
|
250
|
+
let parentList = parents
|
|
275
251
|
.map(exports.constructorType2TS).join(',');
|
|
276
|
-
parentList = (parentList !== '') ?
|
|
252
|
+
parentList = (parentList !== '') ? ` extends ${parentList}` : '';
|
|
277
253
|
return [
|
|
278
254
|
preamble,
|
|
279
255
|
typeArgs,
|
|
@@ -287,16 +263,14 @@ exports.contextStatement2TS = contextStatement2TS;
|
|
|
287
263
|
/**
|
|
288
264
|
* letStatement2TS
|
|
289
265
|
*/
|
|
290
|
-
|
|
291
|
-
return _setStatement2TS(ctx, n, 'export const');
|
|
292
|
-
};
|
|
266
|
+
const letStatement2TS = (ctx, n) => _setStatement2TS(ctx, n, 'export const');
|
|
293
267
|
exports.letStatement2TS = letStatement2TS;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
preamble =
|
|
298
|
-
|
|
299
|
-
return
|
|
268
|
+
const _setStatement2TS = (ctx, n, preamble) => {
|
|
269
|
+
let id = (0, exports.identifier2TS)(n.id);
|
|
270
|
+
let cons = (0, exports.constructorType2TS)(n.cons);
|
|
271
|
+
preamble = `${preamble} ${id}:${cons}`;
|
|
272
|
+
let value = (0, exports.expression2TS)(ctx, n.expression);
|
|
273
|
+
return `${preamble} = ${value}`;
|
|
300
274
|
};
|
|
301
275
|
/**
|
|
302
276
|
* funStatement2TS generates Typescript output for fun statements.
|
|
@@ -304,20 +278,20 @@ var _setStatement2TS = function (ctx, n, preamble) {
|
|
|
304
278
|
* This is a curried function that first accepts zero or more arguments then
|
|
305
279
|
* a single Registry, finally the content.
|
|
306
280
|
*/
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
281
|
+
const funStatement2TS = (ctx, n) => {
|
|
282
|
+
let id = (0, exports.unqualifiedIdentifier2TS)(n.id);
|
|
283
|
+
let typeParams = (0, exports.typeParameters2TS)(n.typeParameters);
|
|
284
|
+
let params = (0, exports.parameters2TS)(n.parameters);
|
|
285
|
+
let factory = `(${exports.THIS}:${exports.WML}.Registry) : ${exports.WML}.Content[] =>`;
|
|
286
|
+
let body = (0, exports.children2TS)(ctx, n.body);
|
|
313
287
|
return [
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
288
|
+
`export const ${id} = `,
|
|
289
|
+
``,
|
|
290
|
+
`${typeParams}(${params})=>${factory} {`,
|
|
291
|
+
``,
|
|
292
|
+
` return ${body};`,
|
|
293
|
+
``,
|
|
294
|
+
`};`
|
|
321
295
|
].join(eol(ctx));
|
|
322
296
|
};
|
|
323
297
|
exports.funStatement2TS = funStatement2TS;
|
|
@@ -326,199 +300,197 @@ exports.funStatement2TS = funStatement2TS;
|
|
|
326
300
|
*
|
|
327
301
|
* This is a class with template and various useful helpers.
|
|
328
302
|
*/
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
var typeParams = (0, exports.typeParameters2TS)(n.typeParameters);
|
|
335
|
-
var context = (0, exports.type2TS)((n.context instanceof ast.ContextFromStatement) ?
|
|
303
|
+
const viewStatement2TS = (ctx, n) => {
|
|
304
|
+
let instances = n.directives.map(i => _setStatement2TS(ctx, i, 'let')).join(`;${ctx.options.EOL}`);
|
|
305
|
+
let id = n.id ? (0, exports.constructor2TS)(n.id) : 'Main';
|
|
306
|
+
let typeParams = (0, exports.typeParameters2TS)(n.typeParameters);
|
|
307
|
+
let context = (0, exports.type2TS)((n.context instanceof ast.ContextFromStatement) ?
|
|
336
308
|
n.context.cons : n.context);
|
|
337
|
-
|
|
309
|
+
let template = (0, exports.tag2TS)(ctx, n.root);
|
|
338
310
|
return [
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
311
|
+
`export class ${id} ${typeParams} implements ${exports.WML}.View {`,
|
|
312
|
+
``,
|
|
313
|
+
` constructor(${exports.CONTEXT}: ${context}) {`,
|
|
314
|
+
``,
|
|
315
|
+
` this.template = (${exports.THIS}:${exports.WML}.Registry) => {`,
|
|
316
|
+
``,
|
|
317
|
+
` ${instances}`,
|
|
318
|
+
``,
|
|
319
|
+
` return ${template};`,
|
|
320
|
+
``,
|
|
321
|
+
` }`,
|
|
322
|
+
``,
|
|
323
|
+
` }`,
|
|
324
|
+
``,
|
|
325
|
+
` ids: { [key: string]: ${exports.WML}.WMLElement } = {};`,
|
|
326
|
+
``,
|
|
327
|
+
` groups: { [key: string]: ${exports.WML}.WMLElement[] } = {};`,
|
|
328
|
+
``,
|
|
329
|
+
` views: ${exports.WML}.View[] = [];`,
|
|
330
|
+
``,
|
|
331
|
+
` widgets: ${exports.WML}.Widget[] = [];`,
|
|
332
|
+
``,
|
|
333
|
+
` tree: Node = <Node>${exports.DOCUMENT}.createElement('div');`,
|
|
334
|
+
``,
|
|
335
|
+
` template: ${exports.WML}.Template;`,
|
|
336
|
+
``,
|
|
337
|
+
` registerView(${REGISTER_VIEW_PARAMS}) : ${exports.WML}.View {`,
|
|
338
|
+
``,
|
|
339
|
+
` this.views.push(v);`,
|
|
340
|
+
``,
|
|
341
|
+
` return v;`,
|
|
342
|
+
``,
|
|
343
|
+
`}`,
|
|
344
|
+
` register(${REGISTER_PARAMS}) : ${exports.WML}.WMLElement {`,
|
|
345
|
+
``,
|
|
346
|
+
` let attrsMap = (<${exports.WML}.Attrs><any>attrs)`,
|
|
347
|
+
``,
|
|
348
|
+
` if(attrsMap.wml) {`,
|
|
349
|
+
``,
|
|
350
|
+
` let {id, group} = attrsMap.wml;`,
|
|
351
|
+
``,
|
|
352
|
+
` if(id != null) {`,
|
|
353
|
+
``,
|
|
354
|
+
` if (this.ids.hasOwnProperty(id))`,
|
|
355
|
+
` throw new Error(\`Duplicate id '\${id}' detected!\`);`,
|
|
356
|
+
``,
|
|
357
|
+
` this.ids[id] = e;`,
|
|
358
|
+
``,
|
|
359
|
+
` }`,
|
|
360
|
+
``,
|
|
361
|
+
` if(group != null) {`,
|
|
362
|
+
``,
|
|
363
|
+
` this.groups[group] = this.groups[group] || [];`,
|
|
364
|
+
` this.groups[group].push(e);`,
|
|
365
|
+
``,
|
|
366
|
+
` }`,
|
|
367
|
+
``,
|
|
368
|
+
` }`,
|
|
369
|
+
` return e;`,
|
|
370
|
+
`}`,
|
|
371
|
+
``,
|
|
372
|
+
` node(${NODE_PARAMS}): ${exports.WML}.Content {`,
|
|
373
|
+
``,
|
|
374
|
+
` let e = ${exports.DOCUMENT}.createElement(tag);`,
|
|
375
|
+
``,
|
|
376
|
+
` Object.keys(attrs).forEach(key => {`,
|
|
377
|
+
``,
|
|
378
|
+
` let value = (<any>attrs)[key];`,
|
|
379
|
+
``,
|
|
380
|
+
` if (typeof value === 'function') {`,
|
|
381
|
+
``,
|
|
382
|
+
` (<any>e)[key] = value;`,
|
|
383
|
+
``,
|
|
384
|
+
` } else if (typeof value === 'string') {`,
|
|
385
|
+
``,
|
|
386
|
+
` //prevent setting things like disabled=''`,
|
|
387
|
+
` if (value !== '')`,
|
|
388
|
+
` e.setAttribute(key, value);`,
|
|
389
|
+
``,
|
|
390
|
+
` } else if (typeof value === 'boolean') {`,
|
|
391
|
+
``,
|
|
392
|
+
` e.setAttribute(key, '');`,
|
|
393
|
+
``,
|
|
394
|
+
` } else if(!${exports.DOCUMENT}.isBrowser && `,
|
|
395
|
+
` value instanceof ${exports.DOCUMENT}.WMLDOMText) {`,
|
|
396
|
+
``,
|
|
397
|
+
` e.setAttribute(key, <any>value);`,
|
|
398
|
+
``,
|
|
399
|
+
` }`,
|
|
400
|
+
``,
|
|
401
|
+
` });`,
|
|
402
|
+
``,
|
|
403
|
+
` children.forEach(c => {`,
|
|
404
|
+
``,
|
|
405
|
+
` switch (typeof c) {`,
|
|
406
|
+
``,
|
|
407
|
+
` case 'string':`,
|
|
408
|
+
` case 'number':`,
|
|
409
|
+
` case 'boolean':`,
|
|
410
|
+
` let tn = ${exports.DOCUMENT}.createTextNode(''+c);`,
|
|
411
|
+
` e.appendChild(<Node>tn)`,
|
|
412
|
+
` case 'object':`,
|
|
413
|
+
` e.appendChild(<Node>c);`,
|
|
414
|
+
` break;`,
|
|
415
|
+
` default:`,
|
|
416
|
+
` ${THROW_CHILD_ERR}`,
|
|
417
|
+
``,
|
|
418
|
+
` }})`,
|
|
419
|
+
``,
|
|
420
|
+
` this.register(e, attrs);`,
|
|
421
|
+
``,
|
|
422
|
+
` return e;`,
|
|
423
|
+
``,
|
|
424
|
+
` }`,
|
|
425
|
+
``,
|
|
426
|
+
``,
|
|
427
|
+
` widget(${WIDGET_PARAMS}) : ${exports.WML}.Content {`,
|
|
428
|
+
``,
|
|
429
|
+
` this.register(w, attrs);`,
|
|
430
|
+
``,
|
|
431
|
+
` this.widgets.push(w);`,
|
|
432
|
+
``,
|
|
433
|
+
` return w.render();`,
|
|
434
|
+
``,
|
|
435
|
+
` }`,
|
|
436
|
+
``,
|
|
437
|
+
` findById<E extends ${exports.WML}.WMLElement>(id: string): ${MAYBE}<E> {`,
|
|
438
|
+
``,
|
|
439
|
+
` let mW:${MAYBE}<E> = ${FROM_NULLABLE}<E>(<E>this.ids[id])`,
|
|
440
|
+
``,
|
|
441
|
+
` return this.views.reduce((p,c)=>`,
|
|
442
|
+
` p.isJust() ? p : c.findById(id), mW);`,
|
|
443
|
+
``,
|
|
444
|
+
` }`,
|
|
445
|
+
``,
|
|
446
|
+
` findByGroup<E extends ${exports.WML}.WMLElement>(name: string): ` +
|
|
447
|
+
`${MAYBE}<E[]> {`,
|
|
448
|
+
``,
|
|
449
|
+
` let mGroup:${MAYBE}<E[]> =`,
|
|
450
|
+
` ${FROM_ARRAY}(this.groups.hasOwnProperty(name) ?`,
|
|
451
|
+
` <any>this.groups[name] : `,
|
|
452
|
+
` []);`,
|
|
453
|
+
``,
|
|
454
|
+
` return this.views.reduce((p,c) =>`,
|
|
455
|
+
` p.isJust() ? p : c.findByGroup(name), mGroup);`,
|
|
456
|
+
``,
|
|
457
|
+
` }`,
|
|
458
|
+
``,
|
|
459
|
+
` invalidate() : void {`,
|
|
460
|
+
``,
|
|
461
|
+
` let {tree} = this;`,
|
|
462
|
+
` let parent = <Node>tree.parentNode;`,
|
|
463
|
+
``,
|
|
464
|
+
` if (tree == null)`,
|
|
465
|
+
` return console.warn('invalidate(): '+` +
|
|
466
|
+
` 'Missing DOM tree!');`,
|
|
467
|
+
``,
|
|
468
|
+
` if (tree.parentNode == null)`,
|
|
469
|
+
` ${THROW_INVALIDATE_ERR}`,
|
|
470
|
+
``,
|
|
471
|
+
` parent.replaceChild(<Node>this.render(), tree) `,
|
|
472
|
+
``,
|
|
473
|
+
` }`,
|
|
474
|
+
``,
|
|
475
|
+
` render(): ${exports.WML}.Content {`,
|
|
476
|
+
``,
|
|
477
|
+
` this.ids = {};`,
|
|
478
|
+
` this.widgets.forEach(w => w.removed());`,
|
|
479
|
+
` this.widgets = [];`,
|
|
480
|
+
` this.views = [];`,
|
|
481
|
+
` this.tree = <Node>this.template(this);`,
|
|
482
|
+
``,
|
|
483
|
+
` this.ids['root'] = (this.ids['root']) ?`,
|
|
484
|
+
` this.ids['root'] : `,
|
|
485
|
+
` this.tree;`,
|
|
486
|
+
``,
|
|
487
|
+
` this.widgets.forEach(w => w.rendered());`,
|
|
488
|
+
``,
|
|
489
|
+
` return this.tree;`,
|
|
490
|
+
``,
|
|
491
|
+
` }`,
|
|
492
|
+
``,
|
|
493
|
+
`}`
|
|
522
494
|
].join(eol(ctx));
|
|
523
495
|
};
|
|
524
496
|
exports.viewStatement2TS = viewStatement2TS;
|
|
@@ -526,22 +498,18 @@ exports.viewStatement2TS = viewStatement2TS;
|
|
|
526
498
|
* typeParameters2TS converts a list of typeParameters2TS into the a list of
|
|
527
499
|
* typescript typeParameters2TS.
|
|
528
500
|
*/
|
|
529
|
-
|
|
530
|
-
return (ns.length === 0) ? '' : "<".concat(ns.map(exports.typeParameter2TS).join(','), "> ");
|
|
531
|
-
};
|
|
501
|
+
const typeParameters2TS = (ns) => (ns.length === 0) ? '' : `<${ns.map(exports.typeParameter2TS).join(',')}> `;
|
|
532
502
|
exports.typeParameters2TS = typeParameters2TS;
|
|
533
503
|
/**
|
|
534
504
|
* typeParameter2TS converts a type parameter into a typescript type parameter.
|
|
535
505
|
*/
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
"".concat(n.constraint ? 'extends ' + (0, exports.type2TS)(n.constraint) : '', " ");
|
|
539
|
-
};
|
|
506
|
+
const typeParameter2TS = (n) => `${toPrim((0, exports.identifierOrConstructor2TS)(n.id))} ` +
|
|
507
|
+
`${n.constraint ? 'extends ' + (0, exports.type2TS)(n.constraint) : ''} `;
|
|
540
508
|
exports.typeParameter2TS = typeParameter2TS;
|
|
541
509
|
/**
|
|
542
510
|
* type2TS
|
|
543
511
|
*/
|
|
544
|
-
|
|
512
|
+
const type2TS = (n) => {
|
|
545
513
|
if (n instanceof ast.ConstructorType)
|
|
546
514
|
return (0, exports.constructorType2TS)(n);
|
|
547
515
|
else if (n instanceof ast.RecordType)
|
|
@@ -566,8 +534,8 @@ exports.type2TS = type2TS;
|
|
|
566
534
|
*
|
|
567
535
|
* If the node is generic, the type parameters will be generated as well.
|
|
568
536
|
*/
|
|
569
|
-
|
|
570
|
-
|
|
537
|
+
const constructorType2TS = (n) => {
|
|
538
|
+
let id = toPrim((0, exports.identifierOrConstructor2TS)(n.id));
|
|
571
539
|
return (n.typeParameters.length > 0) ?
|
|
572
540
|
id + (0, exports.typeParameters2TS)(n.typeParameters) : id;
|
|
573
541
|
};
|
|
@@ -575,33 +543,27 @@ exports.constructorType2TS = constructorType2TS;
|
|
|
575
543
|
/**
|
|
576
544
|
* functionType2TS
|
|
577
545
|
*/
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
return
|
|
546
|
+
const functionType2TS = (n) => {
|
|
547
|
+
let params = n.parameters.map((t, k) => `$${k}:${(0, exports.type2TS)(t)}`).join(',');
|
|
548
|
+
let ret = (0, exports.type2TS)(n.returnType);
|
|
549
|
+
return `(${params}) => ${ret}`;
|
|
582
550
|
};
|
|
583
551
|
exports.functionType2TS = functionType2TS;
|
|
584
552
|
/**
|
|
585
553
|
* listType2TS
|
|
586
554
|
*/
|
|
587
|
-
|
|
588
|
-
return "(".concat((0, exports.type2TS)(n.elementType), ")[]");
|
|
589
|
-
};
|
|
555
|
+
const listType2TS = (n) => `(${(0, exports.type2TS)(n.elementType)})[]`;
|
|
590
556
|
exports.listType2TS = listType2TS;
|
|
591
557
|
/**
|
|
592
558
|
* tupleType2TS
|
|
593
559
|
*/
|
|
594
|
-
|
|
595
|
-
return "[".concat(n.members.map(exports.type2TS).join(','), "]");
|
|
596
|
-
};
|
|
560
|
+
const tupleType2TS = (n) => `[${n.members.map(exports.type2TS).join(',')}]`;
|
|
597
561
|
exports.tupleType2TS = tupleType2TS;
|
|
598
562
|
/**
|
|
599
563
|
* recordType2TS converts the RecordType node to the body of a TypeScript
|
|
600
564
|
* record interface.
|
|
601
565
|
*/
|
|
602
|
-
|
|
603
|
-
return '{' + (0, exports.memberDeclarations2TS)(n.members) + '}';
|
|
604
|
-
};
|
|
566
|
+
const recordType2Ts = (n) => '{' + (0, exports.memberDeclarations2TS)(n.members) + '}';
|
|
605
567
|
exports.recordType2Ts = recordType2Ts;
|
|
606
568
|
/**
|
|
607
569
|
* memberDeclarations2TS converts a list of MemberDeclarations to TypeScript.
|
|
@@ -609,29 +571,25 @@ exports.recordType2Ts = recordType2Ts;
|
|
|
609
571
|
* The paths of the MemberDeclarations are expanded so that paths
|
|
610
572
|
* using the "<path1>.<path2>.<path3>" syntax occur as nested records.
|
|
611
573
|
*/
|
|
612
|
-
|
|
613
|
-
return (0, exports.typeMap2TS)((0, exports.expandTypeMap)((0, exports.typeMapFromMemberDecs)(n)));
|
|
614
|
-
};
|
|
574
|
+
const memberDeclarations2TS = (n) => (0, exports.typeMap2TS)((0, exports.expandTypeMap)((0, exports.typeMapFromMemberDecs)(n)));
|
|
615
575
|
exports.memberDeclarations2TS = memberDeclarations2TS;
|
|
616
576
|
/**
|
|
617
577
|
* typeMapFromMemberDecs creates a TypeMap from a list of memberDeclarations.
|
|
618
578
|
*
|
|
619
579
|
* This works recursively and any RecordTypes encountered will be flattened.
|
|
620
580
|
*/
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
}, {});
|
|
634
|
-
};
|
|
581
|
+
const typeMapFromMemberDecs = (list) => list.reduce((p, m) => {
|
|
582
|
+
let paths = m.path.map(p => p.value);
|
|
583
|
+
if (m.kind instanceof ast.RecordType) {
|
|
584
|
+
return (0, exports.typeMapFromRecordType)(m.kind, p, paths);
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
let path = paths2String(paths);
|
|
588
|
+
path = m.optional ? `${path}?` : path;
|
|
589
|
+
p[path] = m.kind;
|
|
590
|
+
return p;
|
|
591
|
+
}
|
|
592
|
+
}, {});
|
|
635
593
|
exports.typeMapFromMemberDecs = typeMapFromMemberDecs;
|
|
636
594
|
/**
|
|
637
595
|
* typeMapFromRecordType produces a map of node.Type instances from
|
|
@@ -639,59 +597,47 @@ exports.typeMapFromMemberDecs = typeMapFromMemberDecs;
|
|
|
639
597
|
*
|
|
640
598
|
* Any encountered RecordTypes will be flattened.
|
|
641
599
|
*/
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
}, init);
|
|
653
|
-
};
|
|
600
|
+
const typeMapFromRecordType = (n, init, prefix) => n.members.reduce((p, m) => {
|
|
601
|
+
let path = [...prefix, ...(m.path.map(p => p.value))];
|
|
602
|
+
if (m.kind instanceof ast.RecordType) {
|
|
603
|
+
return (0, exports.typeMapFromRecordType)(m.kind, init, path);
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
p[paths2String(path)] = m.kind;
|
|
607
|
+
return p;
|
|
608
|
+
}
|
|
609
|
+
}, init);
|
|
654
610
|
exports.typeMapFromRecordType = typeMapFromRecordType;
|
|
655
|
-
|
|
611
|
+
const paths2String = (paths) => paths.join('.');
|
|
656
612
|
/**
|
|
657
613
|
* expandTypeMap to an ExpandedTypeMap.
|
|
658
614
|
*/
|
|
659
|
-
|
|
660
|
-
return (0, record_1.reduce)(m, {}, function (p, c, k) {
|
|
661
|
-
return (0, path_1.set)(k, (0, exports.type2TS)(c), p);
|
|
662
|
-
});
|
|
663
|
-
};
|
|
615
|
+
const expandTypeMap = (m) => (0, record_1.reduce)(m, {}, (p, c, k) => (0, path_1.set)(k, (0, exports.type2TS)(c), p));
|
|
664
616
|
exports.expandTypeMap = expandTypeMap;
|
|
665
617
|
/**
|
|
666
618
|
* typeMap2TS converts a map of type values to TypeScript.
|
|
667
619
|
*/
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
}).join(',\n');
|
|
678
|
-
};
|
|
620
|
+
const typeMap2TS = (m) => (0, record_1.mapTo)(m, (t, k) => {
|
|
621
|
+
if ((0, record_1.isRecord)(t)) {
|
|
622
|
+
let key = isOptional(t) ? `${k}?` : `${k}`;
|
|
623
|
+
return `${key}: {${(0, exports.typeMap2TS)(t)}}`;
|
|
624
|
+
}
|
|
625
|
+
else {
|
|
626
|
+
return `${k} : ${t}`;
|
|
627
|
+
}
|
|
628
|
+
}).join(',\n');
|
|
679
629
|
exports.typeMap2TS = typeMap2TS;
|
|
680
|
-
|
|
681
|
-
return (0, record_1.reduce)(m, false, function (p, _, k) { return p ? p : k.indexOf('?') > -1; });
|
|
682
|
-
};
|
|
630
|
+
const isOptional = (m) => (0, record_1.reduce)(m, false, (p, _, k) => p ? p : k.indexOf('?') > -1);
|
|
683
631
|
/**
|
|
684
632
|
* parameters2TS converts a list Parameter nodes into an parameter list
|
|
685
633
|
* (without parens).
|
|
686
634
|
*/
|
|
687
|
-
|
|
688
|
-
return list.map(function (p) { return (0, exports.parameter2TS)(p); }).join(',');
|
|
689
|
-
};
|
|
635
|
+
const parameters2TS = (list) => list.map(p => (0, exports.parameter2TS)(p)).join(',');
|
|
690
636
|
exports.parameters2TS = parameters2TS;
|
|
691
637
|
/**
|
|
692
638
|
* parameter2TS
|
|
693
639
|
*/
|
|
694
|
-
|
|
640
|
+
const parameter2TS = (n) => {
|
|
695
641
|
if (n instanceof ast.TypedParameter)
|
|
696
642
|
return (0, exports.typedParameter2TS)(n);
|
|
697
643
|
else if (n instanceof ast.UntypedParameter)
|
|
@@ -703,28 +649,24 @@ exports.parameter2TS = parameter2TS;
|
|
|
703
649
|
/**
|
|
704
650
|
* typedParameter2TS
|
|
705
651
|
*/
|
|
706
|
-
|
|
707
|
-
return "".concat((0, exports.identifier2TS)(n.id), ": ").concat((0, exports.type2TS)(n.hint), " ");
|
|
708
|
-
};
|
|
652
|
+
const typedParameter2TS = (n) => `${(0, exports.identifier2TS)(n.id)}: ${(0, exports.type2TS)(n.hint)} `;
|
|
709
653
|
exports.typedParameter2TS = typedParameter2TS;
|
|
710
654
|
/**
|
|
711
655
|
* untypedParameter2TS
|
|
712
656
|
*/
|
|
713
|
-
|
|
714
|
-
return "".concat((0, exports.identifier2TS)(n.id), " ");
|
|
715
|
-
};
|
|
657
|
+
const untypedParameter2TS = (n) => `${(0, exports.identifier2TS)(n.id)} `;
|
|
716
658
|
exports.untypedParameter2TS = untypedParameter2TS;
|
|
717
659
|
/**
|
|
718
660
|
* children2TS
|
|
719
661
|
*/
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
662
|
+
const children2TS = (ctx, list) => `[${ctx.options.EOL}
|
|
663
|
+
${list.map(l => (0, exports.child2TS)(ctx, l)).join(',' + ctx.options.EOL)}
|
|
664
|
+
]`;
|
|
723
665
|
exports.children2TS = children2TS;
|
|
724
666
|
/**
|
|
725
667
|
* child2TS converts children to typescript.
|
|
726
668
|
*/
|
|
727
|
-
|
|
669
|
+
const child2TS = (ctx, n) => {
|
|
728
670
|
if ((n instanceof ast.Node) || (n instanceof ast.Widget))
|
|
729
671
|
return (0, exports.tag2TS)(ctx, n);
|
|
730
672
|
else if (n instanceof ast.Interpolation)
|
|
@@ -756,20 +698,20 @@ exports.child2TS = child2TS;
|
|
|
756
698
|
/**
|
|
757
699
|
* tag2TS converts a tag to typescript.
|
|
758
700
|
*/
|
|
759
|
-
|
|
760
|
-
(0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n); };
|
|
701
|
+
const tag2TS = (ctx, n) => (n instanceof ast.Widget) ? (0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
761
702
|
exports.tag2TS = tag2TS;
|
|
762
703
|
/**
|
|
763
704
|
* widget2TS converts a Widget node into its typescript representation.
|
|
764
705
|
*
|
|
765
706
|
* This is simply a call to the View's widget method.
|
|
766
707
|
*/
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
708
|
+
const widget2TS = (ctx, n) => {
|
|
709
|
+
let name = (0, exports.constructor2TS)(n.open);
|
|
710
|
+
let typeParams = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
711
|
+
let attrs = (0, exports.attrs2String)(ctx, n.attributes);
|
|
712
|
+
let childs = (0, exports.children2TS)(ctx, n.children);
|
|
713
|
+
return `${exports.THIS}.widget(new ${name}${typeParams}(${attrs}, ${childs}),` +
|
|
714
|
+
`<${exports.WML}.Attrs>${attrs})`;
|
|
773
715
|
};
|
|
774
716
|
exports.widget2TS = widget2TS;
|
|
775
717
|
/**
|
|
@@ -777,169 +719,146 @@ exports.widget2TS = widget2TS;
|
|
|
777
719
|
*
|
|
778
720
|
* This is simply a call to the View's node method.
|
|
779
721
|
*/
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
return
|
|
722
|
+
const node2TS = (ctx, n) => {
|
|
723
|
+
let name = (0, exports.identifier2TS)(n.open);
|
|
724
|
+
let attrs = (0, exports.attrs2String)(ctx, n.attributes);
|
|
725
|
+
let childs = (0, exports.children2TS)(ctx, n.children);
|
|
726
|
+
return `${exports.THIS}.node('${name}', <${exports.WML}.Attrs>${attrs}, ${childs})`;
|
|
785
727
|
};
|
|
786
728
|
exports.node2TS = node2TS;
|
|
787
729
|
/**
|
|
788
730
|
* attribute2Value
|
|
789
731
|
*/
|
|
790
|
-
|
|
791
|
-
return "".concat((0, exports.attributeName2TS)(ctx, n), " : ").concat((0, exports.attributeValue2TS)(ctx, n), " ");
|
|
792
|
-
};
|
|
732
|
+
const attribute2TS = (ctx, n) => `${(0, exports.attributeName2TS)(ctx, n)} : ${(0, exports.attributeValue2TS)(ctx, n)} `;
|
|
793
733
|
exports.attribute2TS = attribute2TS;
|
|
794
734
|
/**
|
|
795
735
|
* attributeValue2TS
|
|
796
736
|
*/
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
(0, exports.literal2TS)(ctx, n.value);
|
|
801
|
-
};
|
|
737
|
+
const attributeValue2TS = (ctx, n) => (n.value instanceof ast.Interpolation) ?
|
|
738
|
+
(0, exports.interpolation2TS)(ctx, n.value) :
|
|
739
|
+
(0, exports.literal2TS)(ctx, n.value);
|
|
802
740
|
exports.attributeValue2TS = attributeValue2TS;
|
|
803
741
|
/**
|
|
804
742
|
* attributeName2TS
|
|
805
743
|
*/
|
|
806
|
-
|
|
807
|
-
return "'".concat((0, exports.unqualifiedIdentifier2TS)(n.name), "'");
|
|
808
|
-
};
|
|
744
|
+
const attributeName2TS = (_, n) => `'${(0, exports.unqualifiedIdentifier2TS)(n.name)}'`;
|
|
809
745
|
exports.attributeName2TS = attributeName2TS;
|
|
810
746
|
/**
|
|
811
747
|
* attrs2String
|
|
812
748
|
*/
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
749
|
+
const attrs2String = (ctx, attrs) => {
|
|
750
|
+
// Check for the special wml:attrs attribute.
|
|
751
|
+
let mallAttrs = (0, array_1.find)(attrs, attr => ((attr.namespace.value === 'wml') &&
|
|
752
|
+
(attr.name.value === 'attrs')));
|
|
753
|
+
if (mallAttrs.isJust())
|
|
754
|
+
return (0, exports.attributeValue2TS)(ctx, mallAttrs.get());
|
|
755
|
+
let [nns, ns] = (0, array_1.partition)(attrs, a => (a.namespace.value === ''));
|
|
756
|
+
let nso = ns.reduce((p, n) => (0, record_1.merge)(p, {
|
|
757
|
+
[n.namespace.value]: (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n))
|
|
758
|
+
}), {});
|
|
759
|
+
return _attrs2String(nns.reduce((p, n) => (0, record_1.merge)(p, {
|
|
760
|
+
[(0, exports.attributeName2TS)(ctx, n)]: (0, exports.attributeValue2TS)(ctx, n)
|
|
761
|
+
}), nso));
|
|
821
762
|
};
|
|
822
763
|
exports.attrs2String = attrs2String;
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
var nso = ns.reduce(function (p, n) {
|
|
829
|
-
var _a;
|
|
830
|
-
return (0, record_1.merge)(p, (_a = {},
|
|
831
|
-
_a[n.namespace.value] = (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n)),
|
|
832
|
-
_a));
|
|
833
|
-
}, {});
|
|
834
|
-
return nns.reduce(function (p, n) {
|
|
835
|
-
var _a;
|
|
836
|
-
return (0, record_1.merge)(p, (_a = {},
|
|
837
|
-
_a[(0, exports.attributeName2TS)(ctx, n)] = (0, exports.attributeValue2TS)(ctx, n),
|
|
838
|
-
_a));
|
|
839
|
-
}, nso);
|
|
840
|
-
};
|
|
841
|
-
exports.groupAttrs = groupAttrs;
|
|
764
|
+
const _attrs2String = (attrs) => '{' +
|
|
765
|
+
Object.keys(attrs).map(name => Array.isArray(attrs[name]) ?
|
|
766
|
+
`${name} : { ${attrs[name].join(',')} }` :
|
|
767
|
+
`${name}: ${attrs[name]}`) +
|
|
768
|
+
'}';
|
|
842
769
|
/**
|
|
843
770
|
* interpolation2TS
|
|
844
771
|
*/
|
|
845
|
-
|
|
846
|
-
return n.filters.reduce(function (p, c) {
|
|
847
|
-
return "".concat((0, exports.expression2TS)(ctx, c), " (").concat(p, ")");
|
|
848
|
-
}, (0, exports.expression2TS)(ctx, n.expression));
|
|
849
|
-
};
|
|
772
|
+
const interpolation2TS = (ctx, n) => n.filters.reduce((p, c) => `${(0, exports.expression2TS)(ctx, c)} (${p})`, (0, exports.expression2TS)(ctx, n.expression));
|
|
850
773
|
exports.interpolation2TS = interpolation2TS;
|
|
851
774
|
/**
|
|
852
775
|
* ifStatementTS converts an IfStatement to its typescript representation.
|
|
853
776
|
*/
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
777
|
+
const ifStatement2TS = (ctx, n) => {
|
|
778
|
+
let condition = (0, exports.expression2TS)(ctx, n.condition);
|
|
779
|
+
let conseq = (0, exports.children2TS)(ctx, n.then);
|
|
780
|
+
let alt = (n.elseClause instanceof ast.ElseIfClause) ?
|
|
781
|
+
`[${(0, exports.ifStatement2TS)(ctx, n.elseClause)}]` :
|
|
859
782
|
(n.elseClause instanceof ast.ElseClause) ?
|
|
860
783
|
(0, exports.children2TS)(ctx, n.elseClause.children) :
|
|
861
784
|
'[]';
|
|
862
785
|
return [
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
786
|
+
`...((${condition}) ?`,
|
|
787
|
+
`(()=>(${conseq}))() :`,
|
|
788
|
+
`(()=>(${alt}))())`
|
|
866
789
|
].join(ctx.options.EOL);
|
|
867
790
|
};
|
|
868
791
|
exports.ifStatement2TS = ifStatement2TS;
|
|
869
792
|
/**
|
|
870
793
|
* forInStatement2TS converts a ForInStatement to its typescript representation.
|
|
871
794
|
*/
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
795
|
+
const forInStatement2TS = (ctx, n) => {
|
|
796
|
+
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
797
|
+
let value = (0, exports.parameter2TS)(n.variables[0]);
|
|
798
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : '_$$i';
|
|
799
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : '_$$all';
|
|
800
|
+
let body = (0, exports.children2TS)(ctx, n.body);
|
|
801
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : '[]';
|
|
879
802
|
return [
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
803
|
+
`...${FOR_IN} (${expr}, (${value}, ${key}, ${all})=> `,
|
|
804
|
+
`(${body}), `,
|
|
805
|
+
`()=> (${alt}))`
|
|
883
806
|
].join(ctx.options.EOL);
|
|
884
807
|
};
|
|
885
808
|
exports.forInStatement2TS = forInStatement2TS;
|
|
886
809
|
/**
|
|
887
810
|
* forOfStatement2TS
|
|
888
811
|
*/
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
812
|
+
const forOfStatement2TS = (ctx, n) => {
|
|
813
|
+
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
814
|
+
let value = (0, exports.parameter2TS)(n.variables[0]);
|
|
815
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : '_$$k';
|
|
816
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : '_$$all';
|
|
817
|
+
let body = (0, exports.children2TS)(ctx, n.body);
|
|
818
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : '[]';
|
|
896
819
|
return [
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
820
|
+
`...${FOR_OF} (${expr}, (${value}, ${key}, ${all}) => `,
|
|
821
|
+
` (${body}), `,
|
|
822
|
+
` ()=> (${alt}))`
|
|
900
823
|
].join(eol(ctx));
|
|
901
824
|
};
|
|
902
825
|
exports.forOfStatement2TS = forOfStatement2TS;
|
|
903
826
|
/**
|
|
904
827
|
* forFromStatement2TS
|
|
905
828
|
*/
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
829
|
+
const forFromStatement2TS = (ctx, node) => {
|
|
830
|
+
let value = (0, exports.parameter2TS)(node.variable);
|
|
831
|
+
let start = (0, exports.expression2TS)(ctx, node.start);
|
|
832
|
+
let end = (0, exports.expression2TS)(ctx, node.end);
|
|
833
|
+
let body = (0, exports.children2TS)(ctx, node.body);
|
|
911
834
|
return [
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
835
|
+
`...(function forFrom() {`,
|
|
836
|
+
` let result:${exports.WML}.Content[] = [];`,
|
|
837
|
+
` for(let ${value}:number=${start}; ${value}<=${end}; ${value}++)`,
|
|
838
|
+
` result.push(`,
|
|
839
|
+
` ...${body}`,
|
|
840
|
+
` );`,
|
|
841
|
+
` return result;`,
|
|
842
|
+
`})()`
|
|
920
843
|
].join(eol(ctx));
|
|
921
844
|
};
|
|
922
845
|
exports.forFromStatement2TS = forFromStatement2TS;
|
|
923
846
|
/**
|
|
924
847
|
* characters2TS converts character text to a typescript string.
|
|
925
848
|
*/
|
|
926
|
-
|
|
927
|
-
return "".concat(exports.DOCUMENT, ".createTextNode('").concat(breakLines(n.value), "')");
|
|
928
|
-
};
|
|
849
|
+
const characters2TS = (n) => `${exports.DOCUMENT}.createTextNode('${breakLines(n.value)}')`;
|
|
929
850
|
exports.characters2TS = characters2TS;
|
|
930
|
-
|
|
851
|
+
const breakLines = (str) => str.split('\n').join('\\u000a');
|
|
931
852
|
/**
|
|
932
853
|
* expression2TS
|
|
933
854
|
*/
|
|
934
|
-
|
|
855
|
+
const expression2TS = (ctx, n) => {
|
|
935
856
|
if (n instanceof ast.IfThenExpression)
|
|
936
857
|
return (0, exports.ifThenExpression2TS)(ctx, n);
|
|
937
858
|
else if (n instanceof ast.BinaryExpression)
|
|
938
859
|
return (0, exports.binaryExpression2TS)(ctx, n);
|
|
939
860
|
else if (n instanceof ast.UnaryExpression)
|
|
940
861
|
return (0, exports.unaryExpression2TS)(ctx, n);
|
|
941
|
-
else if (n instanceof ast.TypeAssertion)
|
|
942
|
-
return (0, exports.typeAssertion2TS)(ctx, n);
|
|
943
862
|
else if (n instanceof ast.ViewConstruction)
|
|
944
863
|
return (0, exports.viewConstruction2TS)(ctx, n);
|
|
945
864
|
else if (n instanceof ast.FunApplication)
|
|
@@ -981,119 +900,104 @@ exports.expression2TS = expression2TS;
|
|
|
981
900
|
/**
|
|
982
901
|
* ifThenExpression2TS
|
|
983
902
|
*/
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
return
|
|
903
|
+
const ifThenExpression2TS = (ctx, n) => {
|
|
904
|
+
let condition = (0, exports.expression2TS)(ctx, n.condition);
|
|
905
|
+
let conseq = (0, exports.expression2TS)(ctx, n.iftrue);
|
|
906
|
+
let alt = (0, exports.expression2TS)(ctx, n.iffalse);
|
|
907
|
+
return `(${condition}) ? ${conseq} : ${alt}`;
|
|
989
908
|
};
|
|
990
909
|
exports.ifThenExpression2TS = ifThenExpression2TS;
|
|
991
910
|
/**
|
|
992
911
|
* binaryExpression2TS
|
|
993
912
|
*/
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
913
|
+
const binaryExpression2TS = (ctx, n) => {
|
|
914
|
+
let left = (0, exports.expression2TS)(ctx, n.left);
|
|
915
|
+
let right = (0, exports.expression2TS)(ctx, n.right);
|
|
916
|
+
let op = operators.hasOwnProperty(n.operator) ?
|
|
998
917
|
operators[n.operator] :
|
|
999
918
|
n.operator;
|
|
1000
|
-
return
|
|
919
|
+
return (n.operator.toLowerCase() === 'as') ?
|
|
920
|
+
`<${right}>(${left})` :
|
|
921
|
+
`(${left} ${op} ${right})`;
|
|
1001
922
|
};
|
|
1002
923
|
exports.binaryExpression2TS = binaryExpression2TS;
|
|
1003
924
|
/**
|
|
1004
925
|
* unaryExpression2TS
|
|
1005
926
|
*/
|
|
1006
|
-
|
|
1007
|
-
|
|
927
|
+
const unaryExpression2TS = (ctx, n) => {
|
|
928
|
+
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
1008
929
|
return (n.operator === '??') ?
|
|
1009
|
-
|
|
1010
|
-
|
|
930
|
+
`(${expr}) != null` :
|
|
931
|
+
`${n.operator}(${expr})`;
|
|
1011
932
|
};
|
|
1012
933
|
exports.unaryExpression2TS = unaryExpression2TS;
|
|
1013
|
-
/**
|
|
1014
|
-
* typeAssertion2TS
|
|
1015
|
-
*/
|
|
1016
|
-
var typeAssertion2TS = function (ctx, n) {
|
|
1017
|
-
return "<".concat((0, exports.type2TS)(n.target), ">(").concat((0, exports.expression2TS)(ctx, n.expression), ")");
|
|
1018
|
-
};
|
|
1019
|
-
exports.typeAssertion2TS = typeAssertion2TS;
|
|
1020
934
|
/**
|
|
1021
935
|
* viewConstruction2TS
|
|
1022
936
|
*/
|
|
1023
|
-
|
|
1024
|
-
return "".concat(exports.THIS, ".registerView(").concat((0, exports.expression2TS)(ctx, n.expression), ").render()");
|
|
1025
|
-
};
|
|
937
|
+
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.registerView(${(0, exports.expression2TS)(ctx, n.expression)}).render()`;
|
|
1026
938
|
exports.viewConstruction2TS = viewConstruction2TS;
|
|
1027
939
|
/**
|
|
1028
940
|
* funApplication2TS
|
|
1029
941
|
*/
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
"(".concat((0, exports.args2TS)(ctx, n.args), ")(").concat(exports.THIS, ")");
|
|
1033
|
-
};
|
|
942
|
+
const funApplication2TS = (ctx, n) => `${(0, exports.expression2TS)(ctx, n.target)}${(0, exports.typeArgs2TS)(n.typeArgs)} ` +
|
|
943
|
+
`(${(0, exports.args2TS)(ctx, n.args)})(${exports.THIS})`;
|
|
1034
944
|
exports.funApplication2TS = funApplication2TS;
|
|
1035
945
|
/**
|
|
1036
946
|
* constructExpression2TS
|
|
1037
947
|
*/
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
return "new ".concat(cons, "(").concat(args, ")");
|
|
1047
|
-
}
|
|
948
|
+
const constructExpression2TS = (ctx, n) => {
|
|
949
|
+
let cons = (0, exports.constructor2TS)(n.cons);
|
|
950
|
+
let consOriginal = `${cons[0].toUpperCase()}${cons.slice(1)}`;
|
|
951
|
+
let typeArgs = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
952
|
+
let args = (0, exports.args2TS)(ctx, n.args);
|
|
953
|
+
return (0, array_1.contains)(casters, consOriginal) ?
|
|
954
|
+
`${consOriginal}${typeArgs}(${args})` :
|
|
955
|
+
`new ${cons}${typeArgs}(${args})`;
|
|
1048
956
|
};
|
|
1049
957
|
exports.constructExpression2TS = constructExpression2TS;
|
|
1050
958
|
/**
|
|
1051
959
|
* callExpression2TS
|
|
1052
960
|
*/
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
return
|
|
961
|
+
const callExpression2TS = (ctx, n) => {
|
|
962
|
+
let target = (0, exports.expression2TS)(ctx, n.target);
|
|
963
|
+
let typeArgs = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
964
|
+
let args = (0, exports.args2TS)(ctx, n.args);
|
|
965
|
+
return `${target}${typeArgs}(${args})`;
|
|
1058
966
|
};
|
|
1059
967
|
exports.callExpression2TS = callExpression2TS;
|
|
1060
968
|
/**
|
|
1061
969
|
* typeArgs2TS
|
|
1062
970
|
*/
|
|
1063
|
-
|
|
1064
|
-
return ns.length === 0 ? '' : "<".concat(ns.map(exports.type2TS).join(','), ">");
|
|
1065
|
-
};
|
|
971
|
+
const typeArgs2TS = (ns) => (0, array_1.empty)(ns) ? '' : `<${ns.map(exports.type2TS).join(',')}>`;
|
|
1066
972
|
exports.typeArgs2TS = typeArgs2TS;
|
|
1067
973
|
/**
|
|
1068
974
|
* args2TS converts a list of arguments to a typescript argument tupple.
|
|
1069
975
|
*/
|
|
1070
|
-
|
|
1071
|
-
return (ns.length === 0) ? '' : ns.map(function (e) { return (0, exports.expression2TS)(ctx, e); }).join(',');
|
|
1072
|
-
};
|
|
976
|
+
const args2TS = (ctx, ns) => (ns.length === 0) ? '' : ns.map(e => (0, exports.expression2TS)(ctx, e)).join(',');
|
|
1073
977
|
exports.args2TS = args2TS;
|
|
1074
978
|
/**
|
|
1075
979
|
* memberExpression2TS
|
|
1076
980
|
*/
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
return (n.
|
|
1080
|
-
|
|
1081
|
-
|
|
981
|
+
const memberExpression2TS = (ctx, n) => {
|
|
982
|
+
let target = (0, exports.expression2TS)(ctx, n.head);
|
|
983
|
+
return (n.tail instanceof ast.StringLiteral) ?
|
|
984
|
+
`${target}[${(0, exports.string2TS)(n.tail)}]` :
|
|
985
|
+
`${target}.${(0, exports.expression2TS)(ctx, n.tail)}`;
|
|
1082
986
|
};
|
|
1083
987
|
exports.memberExpression2TS = memberExpression2TS;
|
|
1084
988
|
/**
|
|
1085
989
|
* functionExpression2TS
|
|
1086
990
|
*/
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
return
|
|
991
|
+
const functionExpression2TS = (ctx, n) => {
|
|
992
|
+
let params = n.parameters.map(exports.parameter2TS).join(',');
|
|
993
|
+
let body = (0, exports.expression2TS)(ctx, n.body);
|
|
994
|
+
return `(${params}) => ${body}`;
|
|
1091
995
|
};
|
|
1092
996
|
exports.functionExpression2TS = functionExpression2TS;
|
|
1093
997
|
/**
|
|
1094
998
|
* literal2TS
|
|
1095
999
|
*/
|
|
1096
|
-
|
|
1000
|
+
const literal2TS = (ctx, n) => {
|
|
1097
1001
|
if (n instanceof ast.BooleanLiteral)
|
|
1098
1002
|
return (0, exports.boolean2TS)(n);
|
|
1099
1003
|
else if (n instanceof ast.StringLiteral)
|
|
@@ -1111,66 +1015,64 @@ exports.literal2TS = literal2TS;
|
|
|
1111
1015
|
/**
|
|
1112
1016
|
* boolean2TS
|
|
1113
1017
|
*/
|
|
1114
|
-
|
|
1018
|
+
const boolean2TS = (n) => `${n.value} `;
|
|
1115
1019
|
exports.boolean2TS = boolean2TS;
|
|
1116
1020
|
/**
|
|
1117
1021
|
* string2TS
|
|
1118
1022
|
*/
|
|
1119
|
-
|
|
1023
|
+
const string2TS = (n) => `'${n.value}'`;
|
|
1120
1024
|
exports.string2TS = string2TS;
|
|
1121
1025
|
/**
|
|
1122
1026
|
* number2TS
|
|
1123
1027
|
*/
|
|
1124
|
-
|
|
1028
|
+
const number2TS = (n) => `${parseFloat(n.value)}`;
|
|
1125
1029
|
exports.number2TS = number2TS;
|
|
1126
1030
|
/**
|
|
1127
1031
|
* record2TS
|
|
1128
1032
|
*/
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1033
|
+
const record2TS = (ctx, n) => `{${ctx.options.EOL}
|
|
1034
|
+
${n.properties.map(p => (0, exports.property2TS)(ctx, p)).join(',' + ctx.options.EOL)}
|
|
1035
|
+
}`;
|
|
1132
1036
|
exports.record2TS = record2TS;
|
|
1133
1037
|
/**
|
|
1134
1038
|
* list2TS
|
|
1135
1039
|
*/
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
return
|
|
1040
|
+
const list2TS = (ctx, n) => {
|
|
1041
|
+
let mems = n.members.map(e => (0, exports.expression2TS)(ctx, e));
|
|
1042
|
+
return `[${ctx.options.EOL}
|
|
1043
|
+
${mems.join(',' + ctx.options.EOL)}
|
|
1044
|
+
]`;
|
|
1139
1045
|
};
|
|
1140
1046
|
exports.list2TS = list2TS;
|
|
1141
1047
|
/**
|
|
1142
1048
|
* property2TS
|
|
1143
1049
|
*/
|
|
1144
|
-
|
|
1145
|
-
return "'".concat((0, exports.key2TS)(n.key), "' : ").concat((0, exports.expression2TS)(ctx, n.value));
|
|
1146
|
-
};
|
|
1050
|
+
const property2TS = (ctx, n) => `'${(0, exports.key2TS)(n.key)}' : ${(0, exports.expression2TS)(ctx, n.value)}`;
|
|
1147
1051
|
exports.property2TS = property2TS;
|
|
1148
1052
|
/**
|
|
1149
1053
|
* key2TS
|
|
1150
1054
|
*/
|
|
1151
|
-
|
|
1152
|
-
return (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1153
|
-
};
|
|
1055
|
+
const key2TS = (n) => (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1154
1056
|
exports.key2TS = key2TS;
|
|
1155
1057
|
/**
|
|
1156
1058
|
* contextProperty2TS
|
|
1157
1059
|
*/
|
|
1158
|
-
|
|
1159
|
-
|
|
1060
|
+
const contextProperty2TS = (n) => {
|
|
1061
|
+
let member = (n.member instanceof ast.StringLiteral) ?
|
|
1160
1062
|
n.member.value :
|
|
1161
1063
|
(0, exports.identifier2TS)(n.member);
|
|
1162
|
-
return
|
|
1064
|
+
return `${exports.CONTEXT}.${member}`;
|
|
1163
1065
|
};
|
|
1164
1066
|
exports.contextProperty2TS = contextProperty2TS;
|
|
1165
1067
|
/**
|
|
1166
1068
|
* contextVariable2TS
|
|
1167
1069
|
*/
|
|
1168
|
-
|
|
1070
|
+
const contextVariable2TS = (_) => `${exports.CONTEXT}`;
|
|
1169
1071
|
exports.contextVariable2TS = contextVariable2TS;
|
|
1170
1072
|
/**
|
|
1171
1073
|
* identifierOrConstructor2TS
|
|
1172
1074
|
*/
|
|
1173
|
-
|
|
1075
|
+
const identifierOrConstructor2TS = (n) => {
|
|
1174
1076
|
if ((n instanceof ast.UnqualifiedIdentifier) ||
|
|
1175
1077
|
(n instanceof ast.QualifiedIdentifier))
|
|
1176
1078
|
return (0, exports.identifier2TS)(n);
|
|
@@ -1184,7 +1086,7 @@ exports.identifierOrConstructor2TS = identifierOrConstructor2TS;
|
|
|
1184
1086
|
/**
|
|
1185
1087
|
* constructor2TS
|
|
1186
1088
|
*/
|
|
1187
|
-
|
|
1089
|
+
const constructor2TS = (n) => {
|
|
1188
1090
|
if (n instanceof ast.QualifiedConstructor)
|
|
1189
1091
|
return (0, exports.qualifiedConstructor2TS)(n);
|
|
1190
1092
|
else if (n instanceof ast.UnqualifiedConstructor)
|
|
@@ -1196,21 +1098,17 @@ exports.constructor2TS = constructor2TS;
|
|
|
1196
1098
|
/**
|
|
1197
1099
|
* unqualifiedConstructor2TS
|
|
1198
1100
|
*/
|
|
1199
|
-
|
|
1200
|
-
return toPrim(n.value);
|
|
1201
|
-
};
|
|
1101
|
+
const unqualifiedConstructor2TS = (n) => toPrim(n.value);
|
|
1202
1102
|
exports.unqualifiedConstructor2TS = unqualifiedConstructor2TS;
|
|
1203
1103
|
/**
|
|
1204
1104
|
* qualifiedConstructor
|
|
1205
1105
|
*/
|
|
1206
|
-
|
|
1207
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1208
|
-
};
|
|
1106
|
+
const qualifiedConstructor2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1209
1107
|
exports.qualifiedConstructor2TS = qualifiedConstructor2TS;
|
|
1210
1108
|
/**
|
|
1211
1109
|
* identifier2TS
|
|
1212
1110
|
*/
|
|
1213
|
-
|
|
1111
|
+
const identifier2TS = (n) => {
|
|
1214
1112
|
if (n instanceof ast.QualifiedIdentifier)
|
|
1215
1113
|
return (0, exports.qualifiedIdentifier2TS)(n);
|
|
1216
1114
|
else if (n instanceof ast.UnqualifiedIdentifier)
|
|
@@ -1222,16 +1120,12 @@ exports.identifier2TS = identifier2TS;
|
|
|
1222
1120
|
/**
|
|
1223
1121
|
* qualifiedIdentifier2TS
|
|
1224
1122
|
*/
|
|
1225
|
-
|
|
1226
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1227
|
-
};
|
|
1123
|
+
const qualifiedIdentifier2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1228
1124
|
exports.qualifiedIdentifier2TS = qualifiedIdentifier2TS;
|
|
1229
1125
|
/**
|
|
1230
1126
|
* unqualifiedIdentifier2TS
|
|
1231
1127
|
*/
|
|
1232
|
-
|
|
1233
|
-
return "".concat(toPrim(n.value));
|
|
1234
|
-
};
|
|
1128
|
+
const unqualifiedIdentifier2TS = (n) => `${toPrim(n.value)}`;
|
|
1235
1129
|
exports.unqualifiedIdentifier2TS = unqualifiedIdentifier2TS;
|
|
1236
|
-
|
|
1130
|
+
const toPrim = (id) => prims.indexOf(id) > -1 ? id.toLowerCase() : id;
|
|
1237
1131
|
//# sourceMappingURL=codegen.js.map
|