@quenk/wml 2.11.4 → 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 +1 -9
- package/lib/compile/codegen.js +547 -646
- package/lib/compile/codegen.js.map +1 -1
- package/lib/compile/codegen.ts +26 -22
- 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 +3 -3
- package/lib/parse/ast.js +166 -220
- package/lib/parse/ast.js.map +1 -1
- package/lib/parse/ast.ts +2 -2
- package/lib/parse/generated.js +1649 -1685
- package/lib/parse/index.js +7 -10
- package/lib/parse/index.js.map +1 -1
- package/lib/parse/test.js +277 -54
- package/lib/parse/test.js.map +1 -1
- package/lib/parse/test.ts +10 -1
- package/lib/parse/wml.y +30 -39
- 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.typeAssertion2TS = exports.unaryExpression2TS = exports.binaryExpression2TS = exports.ifThenExpression2TS = exports.expression2TS = exports.characters2TS =
|
|
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.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
|
-
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.type === 'widget') ?
|
|
702
|
+
(0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
761
703
|
exports.tag2TS = tag2TS;
|
|
762
704
|
/**
|
|
763
705
|
* widget2TS converts a Widget node into its typescript representation.
|
|
764
706
|
*
|
|
765
707
|
* This is simply a call to the View's widget method.
|
|
766
708
|
*/
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
return
|
|
772
|
-
|
|
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})`;
|
|
773
715
|
};
|
|
774
716
|
exports.widget2TS = widget2TS;
|
|
775
717
|
/**
|
|
@@ -777,161 +719,140 @@ 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)
|
|
@@ -981,119 +902,109 @@ exports.expression2TS = expression2TS;
|
|
|
981
902
|
/**
|
|
982
903
|
* ifThenExpression2TS
|
|
983
904
|
*/
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
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}`;
|
|
989
910
|
};
|
|
990
911
|
exports.ifThenExpression2TS = ifThenExpression2TS;
|
|
991
912
|
/**
|
|
992
913
|
* binaryExpression2TS
|
|
993
914
|
*/
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
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) ?
|
|
998
919
|
operators[n.operator] :
|
|
999
920
|
n.operator;
|
|
1000
|
-
return
|
|
921
|
+
return `(${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
934
|
/**
|
|
1014
935
|
* typeAssertion2TS
|
|
1015
936
|
*/
|
|
1016
|
-
|
|
1017
|
-
return "<".concat((0, exports.type2TS)(n.target), ">(").concat((0, exports.expression2TS)(ctx, n.expression), ")");
|
|
1018
|
-
};
|
|
937
|
+
const typeAssertion2TS = (ctx, n) => `<${(0, exports.type2TS)(n.target)}>(${(0, exports.expression2TS)(ctx, n.expression)})`;
|
|
1019
938
|
exports.typeAssertion2TS = typeAssertion2TS;
|
|
1020
939
|
/**
|
|
1021
940
|
* viewConstruction2TS
|
|
1022
941
|
*/
|
|
1023
|
-
|
|
1024
|
-
return "".concat(exports.THIS, ".registerView(").concat((0, exports.expression2TS)(ctx, n.expression), ").render()");
|
|
1025
|
-
};
|
|
942
|
+
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.registerView(${(0, exports.expression2TS)(ctx, n.expression)}).render()`;
|
|
1026
943
|
exports.viewConstruction2TS = viewConstruction2TS;
|
|
1027
944
|
/**
|
|
1028
945
|
* funApplication2TS
|
|
1029
946
|
*/
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
"(".concat((0, exports.args2TS)(ctx, n.args), ")(").concat(exports.THIS, ")");
|
|
1033
|
-
};
|
|
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})`;
|
|
1034
949
|
exports.funApplication2TS = funApplication2TS;
|
|
1035
950
|
/**
|
|
1036
951
|
* constructExpression2TS
|
|
1037
952
|
*/
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
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);
|
|
1042
957
|
if ((0, array_1.contains)(casters, consOriginal)) {
|
|
1043
|
-
return
|
|
958
|
+
return `${consOriginal}(${args})`;
|
|
1044
959
|
}
|
|
1045
960
|
else {
|
|
1046
|
-
return
|
|
961
|
+
return `new ${cons}(${args})`;
|
|
1047
962
|
}
|
|
1048
963
|
};
|
|
1049
964
|
exports.constructExpression2TS = constructExpression2TS;
|
|
1050
965
|
/**
|
|
1051
966
|
* callExpression2TS
|
|
1052
967
|
*/
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
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})`;
|
|
1058
973
|
};
|
|
1059
974
|
exports.callExpression2TS = callExpression2TS;
|
|
1060
975
|
/**
|
|
1061
976
|
* typeArgs2TS
|
|
1062
977
|
*/
|
|
1063
|
-
|
|
1064
|
-
return ns.length === 0 ? '' : "<".concat(ns.map(exports.type2TS).join(','), ">");
|
|
1065
|
-
};
|
|
978
|
+
const typeArgs2TS = (ns) => ns.length === 0 ? '' : `<${ns.map(exports.type2TS).join(',')}>`;
|
|
1066
979
|
exports.typeArgs2TS = typeArgs2TS;
|
|
1067
980
|
/**
|
|
1068
981
|
* args2TS converts a list of arguments to a typescript argument tupple.
|
|
1069
982
|
*/
|
|
1070
|
-
|
|
1071
|
-
return (ns.length === 0) ? '' : ns.map(function (e) { return (0, exports.expression2TS)(ctx, e); }).join(',');
|
|
1072
|
-
};
|
|
983
|
+
const args2TS = (ctx, ns) => (ns.length === 0) ? '' : ns.map(e => (0, exports.expression2TS)(ctx, e)).join(',');
|
|
1073
984
|
exports.args2TS = args2TS;
|
|
1074
985
|
/**
|
|
1075
986
|
* memberExpression2TS
|
|
1076
987
|
*/
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
return (n.
|
|
1080
|
-
|
|
1081
|
-
|
|
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)}`;
|
|
1082
993
|
};
|
|
1083
994
|
exports.memberExpression2TS = memberExpression2TS;
|
|
1084
995
|
/**
|
|
1085
996
|
* functionExpression2TS
|
|
1086
997
|
*/
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
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}`;
|
|
1091
1002
|
};
|
|
1092
1003
|
exports.functionExpression2TS = functionExpression2TS;
|
|
1093
1004
|
/**
|
|
1094
1005
|
* literal2TS
|
|
1095
1006
|
*/
|
|
1096
|
-
|
|
1007
|
+
const literal2TS = (ctx, n) => {
|
|
1097
1008
|
if (n instanceof ast.BooleanLiteral)
|
|
1098
1009
|
return (0, exports.boolean2TS)(n);
|
|
1099
1010
|
else if (n instanceof ast.StringLiteral)
|
|
@@ -1111,66 +1022,64 @@ exports.literal2TS = literal2TS;
|
|
|
1111
1022
|
/**
|
|
1112
1023
|
* boolean2TS
|
|
1113
1024
|
*/
|
|
1114
|
-
|
|
1025
|
+
const boolean2TS = (n) => `${n.value} `;
|
|
1115
1026
|
exports.boolean2TS = boolean2TS;
|
|
1116
1027
|
/**
|
|
1117
1028
|
* string2TS
|
|
1118
1029
|
*/
|
|
1119
|
-
|
|
1030
|
+
const string2TS = (n) => `'${n.value}'`;
|
|
1120
1031
|
exports.string2TS = string2TS;
|
|
1121
1032
|
/**
|
|
1122
1033
|
* number2TS
|
|
1123
1034
|
*/
|
|
1124
|
-
|
|
1035
|
+
const number2TS = (n) => `${parseFloat(n.value)}`;
|
|
1125
1036
|
exports.number2TS = number2TS;
|
|
1126
1037
|
/**
|
|
1127
1038
|
* record2TS
|
|
1128
1039
|
*/
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
}
|
|
1040
|
+
const record2TS = (ctx, n) => `{${ctx.options.EOL}
|
|
1041
|
+
${n.properties.map(p => (0, exports.property2TS)(ctx, p)).join(',' + ctx.options.EOL)}
|
|
1042
|
+
}`;
|
|
1132
1043
|
exports.record2TS = record2TS;
|
|
1133
1044
|
/**
|
|
1134
1045
|
* list2TS
|
|
1135
1046
|
*/
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
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
|
+
]`;
|
|
1139
1052
|
};
|
|
1140
1053
|
exports.list2TS = list2TS;
|
|
1141
1054
|
/**
|
|
1142
1055
|
* property2TS
|
|
1143
1056
|
*/
|
|
1144
|
-
|
|
1145
|
-
return "'".concat((0, exports.key2TS)(n.key), "' : ").concat((0, exports.expression2TS)(ctx, n.value));
|
|
1146
|
-
};
|
|
1057
|
+
const property2TS = (ctx, n) => `'${(0, exports.key2TS)(n.key)}' : ${(0, exports.expression2TS)(ctx, n.value)}`;
|
|
1147
1058
|
exports.property2TS = property2TS;
|
|
1148
1059
|
/**
|
|
1149
1060
|
* key2TS
|
|
1150
1061
|
*/
|
|
1151
|
-
|
|
1152
|
-
return (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1153
|
-
};
|
|
1062
|
+
const key2TS = (n) => (n instanceof ast.StringLiteral) ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1154
1063
|
exports.key2TS = key2TS;
|
|
1155
1064
|
/**
|
|
1156
1065
|
* contextProperty2TS
|
|
1157
1066
|
*/
|
|
1158
|
-
|
|
1159
|
-
|
|
1067
|
+
const contextProperty2TS = (n) => {
|
|
1068
|
+
let member = (n.member instanceof ast.StringLiteral) ?
|
|
1160
1069
|
n.member.value :
|
|
1161
1070
|
(0, exports.identifier2TS)(n.member);
|
|
1162
|
-
return
|
|
1071
|
+
return `${exports.CONTEXT}.${member}`;
|
|
1163
1072
|
};
|
|
1164
1073
|
exports.contextProperty2TS = contextProperty2TS;
|
|
1165
1074
|
/**
|
|
1166
1075
|
* contextVariable2TS
|
|
1167
1076
|
*/
|
|
1168
|
-
|
|
1077
|
+
const contextVariable2TS = (_) => `${exports.CONTEXT}`;
|
|
1169
1078
|
exports.contextVariable2TS = contextVariable2TS;
|
|
1170
1079
|
/**
|
|
1171
1080
|
* identifierOrConstructor2TS
|
|
1172
1081
|
*/
|
|
1173
|
-
|
|
1082
|
+
const identifierOrConstructor2TS = (n) => {
|
|
1174
1083
|
if ((n instanceof ast.UnqualifiedIdentifier) ||
|
|
1175
1084
|
(n instanceof ast.QualifiedIdentifier))
|
|
1176
1085
|
return (0, exports.identifier2TS)(n);
|
|
@@ -1184,7 +1093,7 @@ exports.identifierOrConstructor2TS = identifierOrConstructor2TS;
|
|
|
1184
1093
|
/**
|
|
1185
1094
|
* constructor2TS
|
|
1186
1095
|
*/
|
|
1187
|
-
|
|
1096
|
+
const constructor2TS = (n) => {
|
|
1188
1097
|
if (n instanceof ast.QualifiedConstructor)
|
|
1189
1098
|
return (0, exports.qualifiedConstructor2TS)(n);
|
|
1190
1099
|
else if (n instanceof ast.UnqualifiedConstructor)
|
|
@@ -1196,21 +1105,17 @@ exports.constructor2TS = constructor2TS;
|
|
|
1196
1105
|
/**
|
|
1197
1106
|
* unqualifiedConstructor2TS
|
|
1198
1107
|
*/
|
|
1199
|
-
|
|
1200
|
-
return toPrim(n.value);
|
|
1201
|
-
};
|
|
1108
|
+
const unqualifiedConstructor2TS = (n) => toPrim(n.value);
|
|
1202
1109
|
exports.unqualifiedConstructor2TS = unqualifiedConstructor2TS;
|
|
1203
1110
|
/**
|
|
1204
1111
|
* qualifiedConstructor
|
|
1205
1112
|
*/
|
|
1206
|
-
|
|
1207
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1208
|
-
};
|
|
1113
|
+
const qualifiedConstructor2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1209
1114
|
exports.qualifiedConstructor2TS = qualifiedConstructor2TS;
|
|
1210
1115
|
/**
|
|
1211
1116
|
* identifier2TS
|
|
1212
1117
|
*/
|
|
1213
|
-
|
|
1118
|
+
const identifier2TS = (n) => {
|
|
1214
1119
|
if (n instanceof ast.QualifiedIdentifier)
|
|
1215
1120
|
return (0, exports.qualifiedIdentifier2TS)(n);
|
|
1216
1121
|
else if (n instanceof ast.UnqualifiedIdentifier)
|
|
@@ -1222,16 +1127,12 @@ exports.identifier2TS = identifier2TS;
|
|
|
1222
1127
|
/**
|
|
1223
1128
|
* qualifiedIdentifier2TS
|
|
1224
1129
|
*/
|
|
1225
|
-
|
|
1226
|
-
return "".concat(n.qualifier, ".").concat(n.member);
|
|
1227
|
-
};
|
|
1130
|
+
const qualifiedIdentifier2TS = (n) => `${n.qualifier}.${n.member}`;
|
|
1228
1131
|
exports.qualifiedIdentifier2TS = qualifiedIdentifier2TS;
|
|
1229
1132
|
/**
|
|
1230
1133
|
* unqualifiedIdentifier2TS
|
|
1231
1134
|
*/
|
|
1232
|
-
|
|
1233
|
-
return "".concat(toPrim(n.value));
|
|
1234
|
-
};
|
|
1135
|
+
const unqualifiedIdentifier2TS = (n) => `${toPrim(n.value)}`;
|
|
1235
1136
|
exports.unqualifiedIdentifier2TS = unqualifiedIdentifier2TS;
|
|
1236
|
-
|
|
1137
|
+
const toPrim = (id) => prims.indexOf(id) > -1 ? id.toLowerCase() : id;
|
|
1237
1138
|
//# sourceMappingURL=codegen.js.map
|