@quenk/wml 2.11.2 → 2.12.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 +6 -10
- package/lib/compile/codegen.js +556 -633
- package/lib/compile/codegen.js.map +1 -1
- package/lib/compile/codegen.ts +58 -25
- package/lib/compile/index.js +6 -11
- package/lib/compile/index.js.map +1 -1
- package/lib/compile/transform.js +19 -25
- package/lib/compile/transform.js.map +1 -1
- package/lib/compile/transform.ts +12 -6
- 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 +25 -5
- package/lib/parse/ast.js +186 -214
- package/lib/parse/ast.js.map +1 -1
- package/lib/parse/ast.ts +33 -5
- package/lib/parse/generated.js +2527 -2454
- package/lib/parse/index.js +7 -10
- package/lib/parse/index.js.map +1 -1
- package/lib/parse/test.js +279 -53
- package/lib/parse/test.js.map +1 -1
- package/lib/parse/test.ts +20 -1
- package/lib/parse/wml.y +60 -50
- 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.
|
|
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;
|
|
16
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.typeAssertion2TS = 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
|
-
|
|
335
|
-
|
|
336
|
-
var c = (0, exports.type2TS)(n.context);
|
|
337
|
-
var template = (0, exports.tag2TS)(ctx, n.root);
|
|
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) ?
|
|
308
|
+
n.context.cons : n.context);
|
|
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)
|
|
@@ -735,6 +677,8 @@ var child2TS = function (ctx, n) {
|
|
|
735
677
|
return (0, exports.forInStatement2TS)(ctx, n);
|
|
736
678
|
else if (n instanceof ast.ForOfStatement)
|
|
737
679
|
return (0, exports.forOfStatement2TS)(ctx, n);
|
|
680
|
+
else if (n instanceof ast.ForFromStatement)
|
|
681
|
+
return (0, exports.forFromStatement2TS)(ctx, n);
|
|
738
682
|
else if (n instanceof ast.Characters)
|
|
739
683
|
return (0, exports.characters2TS)(n);
|
|
740
684
|
else if (n instanceof ast.ContextProperty)
|
|
@@ -754,20 +698,20 @@ exports.child2TS = child2TS;
|
|
|
754
698
|
/**
|
|
755
699
|
* tag2TS converts a tag to typescript.
|
|
756
700
|
*/
|
|
757
|
-
|
|
758
|
-
(0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
701
|
+
const tag2TS = (ctx, n) => (n.type === 'widget') ?
|
|
702
|
+
(0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
759
703
|
exports.tag2TS = tag2TS;
|
|
760
704
|
/**
|
|
761
705
|
* widget2TS converts a Widget node into its typescript representation.
|
|
762
706
|
*
|
|
763
707
|
* This is simply a call to the View's widget method.
|
|
764
708
|
*/
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
return
|
|
770
|
-
|
|
709
|
+
const widget2TS = (ctx, n) => {
|
|
710
|
+
let name = (0, exports.constructor2TS)(n.open);
|
|
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}(${attrs}, ${childs}),` +
|
|
714
|
+
`<${exports.WML}.Attrs>${attrs})`;
|
|
771
715
|
};
|
|
772
716
|
exports.widget2TS = widget2TS;
|
|
773
717
|
/**
|
|
@@ -775,141 +719,140 @@ exports.widget2TS = widget2TS;
|
|
|
775
719
|
*
|
|
776
720
|
* This is simply a call to the View's node method.
|
|
777
721
|
*/
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
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})`;
|
|
783
727
|
};
|
|
784
728
|
exports.node2TS = node2TS;
|
|
785
729
|
/**
|
|
786
730
|
* attribute2Value
|
|
787
731
|
*/
|
|
788
|
-
|
|
789
|
-
return "".concat((0, exports.attributeName2TS)(ctx, n), " : ").concat((0, exports.attributeValue2TS)(ctx, n), " ");
|
|
790
|
-
};
|
|
732
|
+
const attribute2TS = (ctx, n) => `${(0, exports.attributeName2TS)(ctx, n)} : ${(0, exports.attributeValue2TS)(ctx, n)} `;
|
|
791
733
|
exports.attribute2TS = attribute2TS;
|
|
792
734
|
/**
|
|
793
735
|
* attributeValue2TS
|
|
794
736
|
*/
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
(0, exports.literal2TS)(ctx, n.value);
|
|
799
|
-
};
|
|
737
|
+
const attributeValue2TS = (ctx, n) => (n.value instanceof ast.Interpolation) ?
|
|
738
|
+
(0, exports.interpolation2TS)(ctx, n.value) :
|
|
739
|
+
(0, exports.literal2TS)(ctx, n.value);
|
|
800
740
|
exports.attributeValue2TS = attributeValue2TS;
|
|
801
741
|
/**
|
|
802
742
|
* attributeName2TS
|
|
803
743
|
*/
|
|
804
|
-
|
|
805
|
-
return "'".concat((0, exports.unqualifiedIdentifier2TS)(n.name), "'");
|
|
806
|
-
};
|
|
744
|
+
const attributeName2TS = (_, n) => `'${(0, exports.unqualifiedIdentifier2TS)(n.name)}'`;
|
|
807
745
|
exports.attributeName2TS = attributeName2TS;
|
|
808
746
|
/**
|
|
809
747
|
* attrs2String
|
|
810
748
|
*/
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
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));
|
|
819
762
|
};
|
|
820
763
|
exports.attrs2String = attrs2String;
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
var nso = ns.reduce(function (p, n) {
|
|
827
|
-
var _a;
|
|
828
|
-
return (0, record_1.merge)(p, (_a = {},
|
|
829
|
-
_a[n.namespace.value] = (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n)),
|
|
830
|
-
_a));
|
|
831
|
-
}, {});
|
|
832
|
-
return nns.reduce(function (p, n) {
|
|
833
|
-
var _a;
|
|
834
|
-
return (0, record_1.merge)(p, (_a = {},
|
|
835
|
-
_a[(0, exports.attributeName2TS)(ctx, n)] = (0, exports.attributeValue2TS)(ctx, n),
|
|
836
|
-
_a));
|
|
837
|
-
}, nso);
|
|
838
|
-
};
|
|
839
|
-
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
|
+
'}';
|
|
840
769
|
/**
|
|
841
770
|
* interpolation2TS
|
|
842
771
|
*/
|
|
843
|
-
|
|
844
|
-
return n.filters.reduce(function (p, c) {
|
|
845
|
-
return "".concat((0, exports.expression2TS)(ctx, c), " (").concat(p, ")");
|
|
846
|
-
}, (0, exports.expression2TS)(ctx, n.expression));
|
|
847
|
-
};
|
|
772
|
+
const interpolation2TS = (ctx, n) => n.filters.reduce((p, c) => `${(0, exports.expression2TS)(ctx, c)} (${p})`, (0, exports.expression2TS)(ctx, n.expression));
|
|
848
773
|
exports.interpolation2TS = interpolation2TS;
|
|
849
774
|
/**
|
|
850
775
|
* ifStatementTS converts an IfStatement to its typescript representation.
|
|
851
776
|
*/
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
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)}]` :
|
|
857
782
|
(n.elseClause instanceof ast.ElseClause) ?
|
|
858
783
|
(0, exports.children2TS)(ctx, n.elseClause.children) :
|
|
859
784
|
'[]';
|
|
860
785
|
return [
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
786
|
+
`...((${condition}) ?`,
|
|
787
|
+
`(()=>(${conseq}))() :`,
|
|
788
|
+
`(()=>(${alt}))())`
|
|
864
789
|
].join(ctx.options.EOL);
|
|
865
790
|
};
|
|
866
791
|
exports.ifStatement2TS = ifStatement2TS;
|
|
867
792
|
/**
|
|
868
793
|
* forInStatement2TS converts a ForInStatement to its typescript representation.
|
|
869
794
|
*/
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
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) : '[]';
|
|
877
802
|
return [
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
803
|
+
`...${FOR_IN} (${expr}, (${value}, ${key}, ${all})=> `,
|
|
804
|
+
`(${body}), `,
|
|
805
|
+
`()=> (${alt}))`
|
|
881
806
|
].join(ctx.options.EOL);
|
|
882
807
|
};
|
|
883
808
|
exports.forInStatement2TS = forInStatement2TS;
|
|
884
809
|
/**
|
|
885
810
|
* forOfStatement2TS
|
|
886
811
|
*/
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
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) : '[]';
|
|
894
819
|
return [
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
820
|
+
`...${FOR_OF} (${expr}, (${value}, ${key}, ${all}) => `,
|
|
821
|
+
` (${body}), `,
|
|
822
|
+
` ()=> (${alt}))`
|
|
898
823
|
].join(eol(ctx));
|
|
899
824
|
};
|
|
900
825
|
exports.forOfStatement2TS = forOfStatement2TS;
|
|
901
826
|
/**
|
|
902
|
-
*
|
|
827
|
+
* forFromStatement2TS
|
|
903
828
|
*/
|
|
904
|
-
|
|
905
|
-
|
|
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);
|
|
834
|
+
return [
|
|
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
|
+
`})()`
|
|
843
|
+
].join(eol(ctx));
|
|
906
844
|
};
|
|
845
|
+
exports.forFromStatement2TS = forFromStatement2TS;
|
|
846
|
+
/**
|
|
847
|
+
* characters2TS converts character text to a typescript string.
|
|
848
|
+
*/
|
|
849
|
+
const characters2TS = (n) => `${exports.DOCUMENT}.createTextNode('${breakLines(n.value)}')`;
|
|
907
850
|
exports.characters2TS = characters2TS;
|
|
908
|
-
|
|
851
|
+
const breakLines = (str) => str.split('\n').join('\\u000a');
|
|
909
852
|
/**
|
|
910
853
|
* expression2TS
|
|
911
854
|
*/
|
|
912
|
-
|
|
855
|
+
const expression2TS = (ctx, n) => {
|
|
913
856
|
if (n instanceof ast.IfThenExpression)
|
|
914
857
|
return (0, exports.ifThenExpression2TS)(ctx, n);
|
|
915
858
|
else if (n instanceof ast.BinaryExpression)
|
|
@@ -959,119 +902,109 @@ exports.expression2TS = expression2TS;
|
|
|
959
902
|
/**
|
|
960
903
|
* ifThenExpression2TS
|
|
961
904
|
*/
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
return
|
|
905
|
+
const ifThenExpression2TS = (ctx, n) => {
|
|
906
|
+
let condition = (0, exports.expression2TS)(ctx, n.condition);
|
|
907
|
+
let conseq = (0, exports.expression2TS)(ctx, n.iftrue);
|
|
908
|
+
let alt = (0, exports.expression2TS)(ctx, n.iffalse);
|
|
909
|
+
return `(${condition}) ? ${conseq} : ${alt}`;
|
|
967
910
|
};
|
|
968
911
|
exports.ifThenExpression2TS = ifThenExpression2TS;
|
|
969
912
|
/**
|
|
970
913
|
* binaryExpression2TS
|
|
971
914
|
*/
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
915
|
+
const binaryExpression2TS = (ctx, n) => {
|
|
916
|
+
let left = (0, exports.expression2TS)(ctx, n.left);
|
|
917
|
+
let right = (0, exports.expression2TS)(ctx, n.right);
|
|
918
|
+
let op = operators.hasOwnProperty(n.operator) ?
|
|
976
919
|
operators[n.operator] :
|
|
977
920
|
n.operator;
|
|
978
|
-
return
|
|
921
|
+
return `(${left} ${op} ${right})`;
|
|
979
922
|
};
|
|
980
923
|
exports.binaryExpression2TS = binaryExpression2TS;
|
|
981
924
|
/**
|
|
982
925
|
* unaryExpression2TS
|
|
983
926
|
*/
|
|
984
|
-
|
|
985
|
-
|
|
927
|
+
const unaryExpression2TS = (ctx, n) => {
|
|
928
|
+
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
986
929
|
return (n.operator === '??') ?
|
|
987
|
-
|
|
988
|
-
|
|
930
|
+
`(${expr}) != null` :
|
|
931
|
+
`${n.operator}(${expr})`;
|
|
989
932
|
};
|
|
990
933
|
exports.unaryExpression2TS = unaryExpression2TS;
|
|
991
934
|
/**
|
|
992
935
|
* typeAssertion2TS
|
|
993
936
|
*/
|
|
994
|
-
|
|
995
|
-
return "<".concat((0, exports.type2TS)(n.target), ">(").concat((0, exports.expression2TS)(ctx, n.expression), ")");
|
|
996
|
-
};
|
|
937
|
+
const typeAssertion2TS = (ctx, n) => `<${(0, exports.type2TS)(n.target)}>(${(0, exports.expression2TS)(ctx, n.expression)})`;
|
|
997
938
|
exports.typeAssertion2TS = typeAssertion2TS;
|
|
998
939
|
/**
|
|
999
940
|
* viewConstruction2TS
|
|
1000
941
|
*/
|
|
1001
|
-
|
|
1002
|
-
return "".concat(exports.THIS, ".registerView(").concat((0, exports.expression2TS)(ctx, n.expression), ").render()");
|
|
1003
|
-
};
|
|
942
|
+
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.registerView(${(0, exports.expression2TS)(ctx, n.expression)}).render()`;
|
|
1004
943
|
exports.viewConstruction2TS = viewConstruction2TS;
|
|
1005
944
|
/**
|
|
1006
945
|
* funApplication2TS
|
|
1007
946
|
*/
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
"(".concat((0, exports.args2TS)(ctx, n.args), ")(").concat(exports.THIS, ")");
|
|
1011
|
-
};
|
|
947
|
+
const funApplication2TS = (ctx, n) => `${(0, exports.expression2TS)(ctx, n.target)}${(0, exports.typeArgs2TS)(n.typeArgs)} ` +
|
|
948
|
+
`(${(0, exports.args2TS)(ctx, n.args)})(${exports.THIS})`;
|
|
1012
949
|
exports.funApplication2TS = funApplication2TS;
|
|
1013
950
|
/**
|
|
1014
951
|
* constructExpression2TS
|
|
1015
952
|
*/
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
953
|
+
const constructExpression2TS = (ctx, n) => {
|
|
954
|
+
let cons = (0, exports.constructor2TS)(n.cons);
|
|
955
|
+
let consOriginal = `${cons[0].toUpperCase()}${cons.slice(1)}`;
|
|
956
|
+
let args = (0, exports.args2TS)(ctx, n.args);
|
|
1020
957
|
if ((0, array_1.contains)(casters, consOriginal)) {
|
|
1021
|
-
return
|
|
958
|
+
return `${consOriginal}(${args})`;
|
|
1022
959
|
}
|
|
1023
960
|
else {
|
|
1024
|
-
return
|
|
961
|
+
return `new ${cons}(${args})`;
|
|
1025
962
|
}
|
|
1026
963
|
};
|
|
1027
964
|
exports.constructExpression2TS = constructExpression2TS;
|
|
1028
965
|
/**
|
|
1029
966
|
* callExpression2TS
|
|
1030
967
|
*/
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
return
|
|
968
|
+
const callExpression2TS = (ctx, n) => {
|
|
969
|
+
let target = (0, exports.expression2TS)(ctx, n.target);
|
|
970
|
+
let typeArgs = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
971
|
+
let args = (0, exports.args2TS)(ctx, n.args);
|
|
972
|
+
return `${target}${typeArgs}(${args})`;
|
|
1036
973
|
};
|
|
1037
974
|
exports.callExpression2TS = callExpression2TS;
|
|
1038
975
|
/**
|
|
1039
976
|
* typeArgs2TS
|
|
1040
977
|
*/
|
|
1041
|
-
|
|
1042
|
-
return ns.length === 0 ? '' : "<".concat(ns.map(exports.type2TS).join(','), ">");
|
|
1043
|
-
};
|
|
978
|
+
const typeArgs2TS = (ns) => ns.length === 0 ? '' : `<${ns.map(exports.type2TS).join(',')}>`;
|
|
1044
979
|
exports.typeArgs2TS = typeArgs2TS;
|
|
1045
980
|
/**
|
|
1046
981
|
* args2TS converts a list of arguments to a typescript argument tupple.
|
|
1047
982
|
*/
|
|
1048
|
-
|
|
1049
|
-
return (ns.length === 0) ? '' : ns.map(function (e) { return (0, exports.expression2TS)(ctx, e); }).join(',');
|
|
1050
|
-
};
|
|
983
|
+
const args2TS = (ctx, ns) => (ns.length === 0) ? '' : ns.map(e => (0, exports.expression2TS)(ctx, e)).join(',');
|
|
1051
984
|
exports.args2TS = args2TS;
|
|
1052
985
|
/**
|
|
1053
986
|
* memberExpression2TS
|
|
1054
987
|
*/
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
return (n.
|
|
1058
|
-
|
|
1059
|
-
|
|
988
|
+
const memberExpression2TS = (ctx, n) => {
|
|
989
|
+
let target = (0, exports.expression2TS)(ctx, n.head);
|
|
990
|
+
return (n.tail instanceof ast.StringLiteral) ?
|
|
991
|
+
`${target}[${(0, exports.string2TS)(n.tail)}]` :
|
|
992
|
+
`${target}.${(0, exports.expression2TS)(ctx, n.tail)}`;
|
|
1060
993
|
};
|
|
1061
994
|
exports.memberExpression2TS = memberExpression2TS;
|
|
1062
995
|
/**
|
|
1063
996
|
* functionExpression2TS
|
|
1064
997
|
*/
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
return
|
|
998
|
+
const functionExpression2TS = (ctx, n) => {
|
|
999
|
+
let params = n.parameters.map(exports.parameter2TS).join(',');
|
|
1000
|
+
let body = (0, exports.expression2TS)(ctx, n.body);
|
|
1001
|
+
return `(${params}) => ${body}`;
|
|
1069
1002
|
};
|
|
1070
1003
|
exports.functionExpression2TS = functionExpression2TS;
|
|
1071
1004
|
/**
|
|
1072
1005
|
* literal2TS
|
|
1073
1006
|
*/
|
|
1074
|
-
|
|
1007
|
+
const literal2TS = (ctx, n) => {
|
|
1075
1008
|
if (n instanceof ast.BooleanLiteral)
|
|
1076
1009
|
return (0, exports.boolean2TS)(n);
|
|
1077
1010
|
else if (n instanceof ast.StringLiteral)
|
|
@@ -1089,66 +1022,64 @@ exports.literal2TS = literal2TS;
|
|
|
1089
1022
|
/**
|
|
1090
1023
|
* boolean2TS
|
|
1091
1024
|
*/
|
|
1092
|
-
|
|
1025
|
+
const boolean2TS = (n) => `${n.value} `;
|
|
1093
1026
|
exports.boolean2TS = boolean2TS;
|
|
1094
1027
|
/**
|
|
1095
1028
|
* string2TS
|
|
1096
1029
|
*/
|
|
1097
|
-
|
|
1030
|
+
const string2TS = (n) => `'${n.value}'`;
|
|
1098
1031
|
exports.string2TS = string2TS;
|
|
1099
1032
|
/**
|
|
1100
1033
|
* number2TS
|
|
1101
1034
|
*/
|
|
1102
|
-
|
|
1035
|
+
const number2TS = (n) => `${parseFloat(n.value)}`;
|
|
1103
1036
|
exports.number2TS = number2TS;
|
|
1104
1037
|
/**
|
|
1105
1038
|
* record2TS
|
|
1106
1039
|
*/
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
}
|
|
1040
|
+
const record2TS = (ctx, n) => `{${ctx.options.EOL}
|
|
1041
|
+
${n.properties.map(p => (0, exports.property2TS)(ctx, p)).join(',' + ctx.options.EOL)}
|
|
1042
|
+
}`;
|
|
1110
1043
|
exports.record2TS = record2TS;
|
|
1111
1044
|
/**
|
|
1112
1045
|
* list2TS
|
|
1113
1046
|
*/
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
return
|
|
1047
|
+
const list2TS = (ctx, n) => {
|
|
1048
|
+
let mems = n.members.map(e => (0, exports.expression2TS)(ctx, e));
|
|
1049
|
+
return `[${ctx.options.EOL}
|
|
1050
|
+
${mems.join(',' + ctx.options.EOL)}
|
|
1051
|
+
]`;
|
|
1117
1052
|
};
|
|
1118
1053
|
exports.list2TS = list2TS;
|
|
1119
1054
|
/**
|
|
1120
1055
|
* property2TS
|
|
1121
1056
|
*/
|
|
1122
|
-
|
|
1123
|
-
return "'".concat((0, exports.key2TS)(n.key), "' : ").concat((0, exports.expression2TS)(ctx, n.value));
|
|
1124
|
-
};
|
|
1057
|
+
const property2TS = (ctx, n) => `'${(0, exports.key2TS)(n.key)}' : ${(0, exports.expression2TS)(ctx, n.value)}`;
|
|
1125
1058
|
exports.property2TS = property2TS;
|
|
1126
1059
|
/**
|
|
1127
1060
|
* key2TS
|
|
1128
1061
|
*/
|
|
1129
|
-
|
|
1130
|
-
return (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1131
|
-
};
|
|
1062
|
+
const key2TS = (n) => (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1132
1063
|
exports.key2TS = key2TS;
|
|
1133
1064
|
/**
|
|
1134
1065
|
* contextProperty2TS
|
|
1135
1066
|
*/
|
|
1136
|
-
|
|
1137
|
-
|
|
1067
|
+
const contextProperty2TS = (n) => {
|
|
1068
|
+
let member = (n.member instanceof ast.StringLiteral) ?
|
|
1138
1069
|
n.member.value :
|
|
1139
1070
|
(0, exports.identifier2TS)(n.member);
|
|
1140
|
-
return
|
|
1071
|
+
return `${exports.CONTEXT}.${member}`;
|
|
1141
1072
|
};
|
|
1142
1073
|
exports.contextProperty2TS = contextProperty2TS;
|
|
1143
1074
|
/**
|
|
1144
1075
|
* contextVariable2TS
|
|
1145
1076
|
*/
|
|
1146
|
-
|
|
1077
|
+
const contextVariable2TS = (_) => `${exports.CONTEXT}`;
|
|
1147
1078
|
exports.contextVariable2TS = contextVariable2TS;
|
|
1148
1079
|
/**
|
|
1149
1080
|
* identifierOrConstructor2TS
|
|
1150
1081
|
*/
|
|
1151
|
-
|
|
1082
|
+
const identifierOrConstructor2TS = (n) => {
|
|
1152
1083
|
if ((n instanceof ast.UnqualifiedIdentifier) ||
|
|
1153
1084
|
(n instanceof ast.QualifiedIdentifier))
|
|
1154
1085
|
return (0, exports.identifier2TS)(n);
|
|
@@ -1162,7 +1093,7 @@ exports.identifierOrConstructor2TS = identifierOrConstructor2TS;
|
|
|
1162
1093
|
/**
|
|
1163
1094
|
* constructor2TS
|
|
1164
1095
|
*/
|
|
1165
|
-
|
|
1096
|
+
const constructor2TS = (n) => {
|
|
1166
1097
|
if (n instanceof ast.QualifiedConstructor)
|
|
1167
1098
|
return (0, exports.qualifiedConstructor2TS)(n);
|
|
1168
1099
|
else if (n instanceof ast.UnqualifiedConstructor)
|
|
@@ -1174,21 +1105,17 @@ exports.constructor2TS = constructor2TS;
|
|
|
1174
1105
|
/**
|
|
1175
1106
|
* unqualifiedConstructor2TS
|
|
1176
1107
|
*/
|
|
1177
|
-
|
|
1178
|
-
return toPrim(n.value);
|
|
1179
|
-
};
|
|
1108
|
+
const unqualifiedConstructor2TS = (n) => toPrim(n.value);
|
|
1180
1109
|
exports.unqualifiedConstructor2TS = unqualifiedConstructor2TS;
|
|
1181
1110
|
/**
|
|
1182
1111
|
* qualifiedConstructor
|
|
1183
1112
|
*/
|
|
1184
|
-
|
|
1185
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1186
|
-
};
|
|
1113
|
+
const qualifiedConstructor2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1187
1114
|
exports.qualifiedConstructor2TS = qualifiedConstructor2TS;
|
|
1188
1115
|
/**
|
|
1189
1116
|
* identifier2TS
|
|
1190
1117
|
*/
|
|
1191
|
-
|
|
1118
|
+
const identifier2TS = (n) => {
|
|
1192
1119
|
if (n instanceof ast.QualifiedIdentifier)
|
|
1193
1120
|
return (0, exports.qualifiedIdentifier2TS)(n);
|
|
1194
1121
|
else if (n instanceof ast.UnqualifiedIdentifier)
|
|
@@ -1200,16 +1127,12 @@ exports.identifier2TS = identifier2TS;
|
|
|
1200
1127
|
/**
|
|
1201
1128
|
* qualifiedIdentifier2TS
|
|
1202
1129
|
*/
|
|
1203
|
-
|
|
1204
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1205
|
-
};
|
|
1130
|
+
const qualifiedIdentifier2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1206
1131
|
exports.qualifiedIdentifier2TS = qualifiedIdentifier2TS;
|
|
1207
1132
|
/**
|
|
1208
1133
|
* unqualifiedIdentifier2TS
|
|
1209
1134
|
*/
|
|
1210
|
-
|
|
1211
|
-
return "".concat(toPrim(n.value));
|
|
1212
|
-
};
|
|
1135
|
+
const unqualifiedIdentifier2TS = (n) => `${toPrim(n.value)}`;
|
|
1213
1136
|
exports.unqualifiedIdentifier2TS = unqualifiedIdentifier2TS;
|
|
1214
|
-
|
|
1137
|
+
const toPrim = (id) => prims.indexOf(id) > -1 ? id.toLowerCase() : id;
|
|
1215
1138
|
//# sourceMappingURL=codegen.js.map
|