@quenk/wml 2.13.11 → 2.14.1
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.d.ts +8 -8
- package/lib/cli.js +1 -3
- package/lib/cli.js.map +1 -1
- package/lib/cli.ts +48 -60
- package/lib/compile/codegen.d.ts +4 -9
- package/lib/compile/codegen.js +161 -367
- package/lib/compile/codegen.js.map +1 -1
- package/lib/compile/codegen.ts +643 -952
- package/lib/compile/index.d.ts +2 -2
- package/lib/compile/index.js +4 -4
- package/lib/compile/index.js.map +1 -1
- package/lib/compile/index.ts +14 -17
- package/lib/compile/transform.d.ts +1 -1
- package/lib/compile/transform.js +9 -11
- package/lib/compile/transform.js.map +1 -1
- package/lib/compile/transform.ts +136 -143
- package/lib/{dom.d.ts → dom/index.d.ts} +8 -2
- package/lib/{dom.js → dom/index.js} +84 -70
- package/lib/dom/index.js.map +1 -0
- package/lib/dom/index.ts +425 -0
- package/lib/dom/monitor.d.ts +33 -0
- package/lib/dom/monitor.js +60 -0
- package/lib/dom/monitor.js.map +1 -0
- package/lib/dom/monitor.ts +75 -0
- package/lib/index.d.ts +10 -95
- package/lib/index.js +10 -10
- package/lib/index.js.map +1 -1
- package/lib/index.ts +57 -182
- package/lib/main.js +17 -17
- package/lib/main.js.map +1 -1
- package/lib/main.ts +38 -44
- package/lib/parse/ast.d.ts +2 -2
- package/lib/parse/ast.js +52 -53
- package/lib/parse/ast.js.map +1 -1
- package/lib/parse/ast.ts +396 -483
- package/lib/parse/generated.d.ts +3 -5
- package/lib/parse/generated.js +9504 -9264
- package/lib/parse/index.d.ts +2 -3
- package/lib/parse/index.js.map +1 -1
- package/lib/parse/index.ts +7 -9
- package/lib/parse/test.js +194 -192
- package/lib/parse/test.js.map +1 -1
- package/lib/parse/test.ts +294 -404
- package/lib/parse/wml.y +4 -0
- package/lib/tsconfig.json +19 -20
- package/lib/util.d.ts +10 -0
- package/lib/util.js +21 -0
- package/lib/util.js.map +1 -0
- package/lib/util.ts +39 -0
- package/lib/view/frame.d.ts +127 -0
- package/lib/view/frame.js +214 -0
- package/lib/view/frame.js.map +1 -0
- package/lib/view/frame.ts +295 -0
- package/lib/view/index.d.ts +58 -0
- package/lib/view/index.js +48 -0
- package/lib/view/index.js.map +1 -0
- package/lib/view/index.ts +98 -0
- package/package.json +3 -3
- package/lib/dom.js.map +0 -1
- package/lib/dom.ts +0 -479
package/lib/compile/codegen.js
CHANGED
|
@@ -3,54 +3,40 @@
|
|
|
3
3
|
* Typescript code generator.
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
7
|
-
exports.unqualifiedIdentifier2TS = exports.qualifiedIdentifier2TS = exports.identifier2TS = exports.qualifiedConstructor2TS = exports.unqualifiedConstructor2TS = exports.constructor2TS = exports.identifierOrConstructor2TS = exports.contextVariable2TS = exports.contextProperty2TS = exports.key2TS = exports.property2TS = exports.list2TS = exports.record2TS = exports.number2TS = exports.string2TS = exports.boolean2TS = exports.literal2TS = exports.functionExpression2TS = exports.memberExpression2TS = exports.args2TS = exports.typeArgs2TS = exports.callExpression2TS = exports.constructExpression2TS = exports.funApplication2TS = exports.viewConstruction2TS = exports.unaryExpression2TS = exports.binaryExpression2TS = exports.ifThenExpression2TS = exports.expression2TS = exports.characters2TS = void 0;
|
|
6
|
+
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.letStatement2TS = exports.contextStatement2TS = exports.aliasStatement2TS = exports.export2TS = exports.exports2TS = exports.compositeMember2TS = exports.aliasedMember2TS = exports.aggregateMember2TS = exports.importMember2TS = exports.importStatement2TS = exports.importStatements2TS = exports.CodeGenerator = exports.VIEW_FRAME = exports.THIS = exports.UTILS = exports.DOCUMENT = exports.WML = exports.VIEW = exports.CONTEXT = void 0;
|
|
7
|
+
exports.unqualifiedIdentifier2TS = exports.qualifiedIdentifier2TS = exports.identifier2TS = exports.qualifiedConstructor2TS = exports.unqualifiedConstructor2TS = exports.constructor2TS = exports.identifierOrConstructor2TS = exports.contextVariable2TS = exports.contextProperty2TS = exports.key2TS = exports.property2TS = exports.list2TS = exports.record2TS = exports.number2TS = exports.string2TS = exports.boolean2TS = exports.literal2TS = exports.functionExpression2TS = exports.memberExpression2TS = exports.args2TS = exports.typeArgs2TS = exports.callExpression2TS = exports.constructExpression2TS = exports.funApplication2TS = exports.viewConstruction2TS = exports.unaryExpression2TS = exports.binaryExpression2TS = exports.ifThenExpression2TS = exports.expression2TS = exports.characters2TS = exports.forFromStatement2TS = void 0;
|
|
8
8
|
/** imports */
|
|
9
9
|
const ast = require("../parse/ast");
|
|
10
10
|
const path_1 = require("@quenk/noni/lib/data/record/path");
|
|
11
11
|
const record_1 = require("@quenk/noni/lib/data/record");
|
|
12
12
|
const array_1 = require("@quenk/noni/lib/data/array");
|
|
13
13
|
const transform_1 = require("./transform");
|
|
14
|
-
exports.CONTEXT =
|
|
15
|
-
exports.VIEW =
|
|
16
|
-
exports.WML =
|
|
17
|
-
exports.DOCUMENT =
|
|
18
|
-
exports.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const
|
|
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_INVALIDATE_ERR = ` throw new Error('invalidate(): cannot ` +
|
|
29
|
-
`invalidate this view, it has no parent node!');`;
|
|
30
|
-
const IGNORE_UNUSED = '//@ts-ignore:6192';
|
|
31
|
-
const RECORD = '__Record<A>';
|
|
32
|
-
const IF = '__if';
|
|
33
|
-
const IFARG = `__IfArg`;
|
|
34
|
-
const FOR_OF = '__forOf';
|
|
35
|
-
const FOR_IN = '__forIn';
|
|
36
|
-
const FOR_ALT_TYPE = '__ForAlt';
|
|
37
|
-
const FOR_IN_BODY = '__ForInBody<A>';
|
|
38
|
-
const FOR_OF_BODY = '__ForOfBody<A>';
|
|
14
|
+
exports.CONTEXT = "__context";
|
|
15
|
+
exports.VIEW = "__view";
|
|
16
|
+
exports.WML = "__wml";
|
|
17
|
+
exports.DOCUMENT = "__document";
|
|
18
|
+
exports.UTILS = "__utils";
|
|
19
|
+
exports.THIS = "__this";
|
|
20
|
+
exports.VIEW_FRAME = "__viewFrame";
|
|
21
|
+
const VIEW_CLASS = "BaseView";
|
|
22
|
+
const FRAME_TYPE = "Frame";
|
|
23
|
+
const FOR_OF = `${exports.UTILS}.forOf`;
|
|
24
|
+
const FOR_IN = `${exports.UTILS}.forIn`;
|
|
39
25
|
const prims = [
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
26
|
+
"String",
|
|
27
|
+
"Boolean",
|
|
28
|
+
"Number",
|
|
29
|
+
"Object",
|
|
30
|
+
"Undefined",
|
|
31
|
+
"Null",
|
|
32
|
+
"Void",
|
|
33
|
+
"Never",
|
|
34
|
+
"Any",
|
|
49
35
|
];
|
|
50
|
-
const casters = [
|
|
36
|
+
const casters = ["String", "Boolean", "Number", "Object"];
|
|
51
37
|
const operators = {
|
|
52
|
-
|
|
53
|
-
|
|
38
|
+
"==": "===",
|
|
39
|
+
"!=": "!==",
|
|
54
40
|
};
|
|
55
41
|
/**
|
|
56
42
|
* CodeGenerator is the main entry point for turning a parsed ast into
|
|
@@ -75,87 +61,26 @@ class CodeGenerator {
|
|
|
75
61
|
generate(tree) {
|
|
76
62
|
tree = (0, transform_1.transformTree)(tree);
|
|
77
63
|
return [
|
|
64
|
+
`// @ts-ignore 6192`,
|
|
78
65
|
`import * as ${exports.WML} from '${this.options.module}';`,
|
|
79
|
-
|
|
80
|
-
|
|
66
|
+
`// @ts-ignore 6192`,
|
|
67
|
+
`import * as ${exports.DOCUMENT} from '${this.options.module}/lib/dom';`,
|
|
68
|
+
`// @ts-ignore 6192`,
|
|
69
|
+
`import * as ${exports.UTILS} from '${this.options.module}/lib/util';`,
|
|
81
70
|
(0, exports.importStatements2TS)(this, tree.imports),
|
|
82
71
|
eol(this),
|
|
83
|
-
typeDefinitions(this),
|
|
84
|
-
eol(this),
|
|
85
72
|
`// @ts-ignore 6192`,
|
|
86
73
|
`const text = ${exports.DOCUMENT}.text;`,
|
|
87
74
|
`// @ts-ignore 6192`,
|
|
88
75
|
`const unsafe = ${exports.DOCUMENT}.unsafe`,
|
|
89
76
|
`// @ts-ignore 6192`,
|
|
90
|
-
`const isSet =
|
|
91
|
-
(0, exports.exports2TS)(this, tree.exports)
|
|
77
|
+
`const isSet = ${exports.UTILS}.isSet`,
|
|
78
|
+
(0, exports.exports2TS)(this, tree.exports),
|
|
92
79
|
].join(eol(this));
|
|
93
80
|
}
|
|
94
81
|
}
|
|
95
82
|
exports.CodeGenerator = CodeGenerator;
|
|
96
83
|
const eol = (ctx) => `${ctx.options.EOL}`;
|
|
97
|
-
const imports = (ctx) => [
|
|
98
|
-
`//@ts-ignore: 6192`,
|
|
99
|
-
`import {`,
|
|
100
|
-
`Maybe as ${MAYBE},`,
|
|
101
|
-
`fromNullable as ${FROM_NULLABLE},`,
|
|
102
|
-
`fromArray as ${FROM_ARRAY}`,
|
|
103
|
-
`}`,
|
|
104
|
-
`from '@quenk/noni/lib/data/maybe';`
|
|
105
|
-
].join(eol(ctx));
|
|
106
|
-
const typeDefinitions = (ctx) => [
|
|
107
|
-
`${IGNORE_UNUSED}`,
|
|
108
|
-
`type ${IFARG} = ()=>${exports.WML}.Content[]`,
|
|
109
|
-
``,
|
|
110
|
-
`${IGNORE_UNUSED}`,
|
|
111
|
-
`type ${FOR_ALT_TYPE} = ()=> ${exports.WML}.Content[]`,
|
|
112
|
-
``,
|
|
113
|
-
`${IGNORE_UNUSED}`,
|
|
114
|
-
`type ${FOR_IN_BODY} =(val:A, idx:number, all:A[])=>` +
|
|
115
|
-
`${exports.WML}.Content[]`,
|
|
116
|
-
``,
|
|
117
|
-
`${IGNORE_UNUSED}`,
|
|
118
|
-
`type ${FOR_OF_BODY} = (val:A, key:string, all:object) =>` +
|
|
119
|
-
`${exports.WML}.Content[]`,
|
|
120
|
-
``,
|
|
121
|
-
`${IGNORE_UNUSED}`,
|
|
122
|
-
`interface ${RECORD} {`,
|
|
123
|
-
``,
|
|
124
|
-
` [key:string]: A`,
|
|
125
|
-
``,
|
|
126
|
-
`}`,
|
|
127
|
-
``,
|
|
128
|
-
`${IGNORE_UNUSED}`,
|
|
129
|
-
`const ${IF} = (__expr:boolean, __conseq:${IFARG},__alt?:${IFARG}) ` +
|
|
130
|
-
`: Content[]=>`,
|
|
131
|
-
`(__expr) ? __conseq() : __alt ? __alt() : [];`,
|
|
132
|
-
``,
|
|
133
|
-
`${IGNORE_UNUSED}`,
|
|
134
|
-
`const ${FOR_IN} = <A>(list:A[], f:${FOR_IN_BODY}, alt:` +
|
|
135
|
-
`${FOR_ALT_TYPE}) : ${exports.WML}.Content[] => {`,
|
|
136
|
-
``,
|
|
137
|
-
` let ret:${exports.WML}.Content[] = [];`,
|
|
138
|
-
``,
|
|
139
|
-
` for(let i=0; i<list.length; i++)`,
|
|
140
|
-
` ret = ret.concat(f(list[i], i, list));`,
|
|
141
|
-
``,
|
|
142
|
-
` return ret.length === 0 ? alt() : ret;`,
|
|
143
|
-
``,
|
|
144
|
-
`}`,
|
|
145
|
-
`${IGNORE_UNUSED}`,
|
|
146
|
-
`const ${FOR_OF} = <A>(o:${RECORD}, f:${FOR_OF_BODY},` +
|
|
147
|
-
`alt:${FOR_ALT_TYPE}) : ${exports.WML}.Content[] => {`,
|
|
148
|
-
``,
|
|
149
|
-
` let ret:${exports.WML}.Content[] = [];`,
|
|
150
|
-
``,
|
|
151
|
-
` for(let key in o)`,
|
|
152
|
-
` if(o.hasOwnProperty(key)) `,
|
|
153
|
-
` ret = ret.concat(f((o)[key], key, o));`,
|
|
154
|
-
``,
|
|
155
|
-
` return ret.length === 0 ? alt(): ret;`,
|
|
156
|
-
``,
|
|
157
|
-
`}`
|
|
158
|
-
].join(eol(ctx));
|
|
159
84
|
/**
|
|
160
85
|
* importStatements2TS converts a list of ImportStatements into typescript.
|
|
161
86
|
*/
|
|
@@ -180,7 +105,7 @@ const importMember2TS = (n) => {
|
|
|
180
105
|
else if (n instanceof ast.CompositeMember)
|
|
181
106
|
return (0, exports.compositeMember2TS)(n);
|
|
182
107
|
else
|
|
183
|
-
return
|
|
108
|
+
return "";
|
|
184
109
|
};
|
|
185
110
|
exports.importMember2TS = importMember2TS;
|
|
186
111
|
/**
|
|
@@ -197,14 +122,18 @@ exports.aliasedMember2TS = aliasedMember2TS;
|
|
|
197
122
|
/**
|
|
198
123
|
* compositeMember2TS
|
|
199
124
|
*/
|
|
200
|
-
const compositeMember2TS = (n) =>
|
|
201
|
-
|
|
202
|
-
|
|
125
|
+
const compositeMember2TS = (n) => "{" +
|
|
126
|
+
n.members
|
|
127
|
+
.map((m) => m instanceof ast.AliasedMember
|
|
128
|
+
? (0, exports.aliasedMember2TS)(m)
|
|
129
|
+
: (0, exports.identifierOrConstructor2TS)(m))
|
|
130
|
+
.join(",") +
|
|
131
|
+
"}";
|
|
203
132
|
exports.compositeMember2TS = compositeMember2TS;
|
|
204
133
|
/**
|
|
205
134
|
* exports2TS converts a list of exports to typescript.
|
|
206
135
|
*/
|
|
207
|
-
const exports2TS = (ctx, list) => list.map(e => (0, exports.export2TS)(ctx, e)).join(
|
|
136
|
+
const exports2TS = (ctx, list) => list.map((e) => (0, exports.export2TS)(ctx, e)).join(";" + eol(ctx));
|
|
208
137
|
exports.exports2TS = exports2TS;
|
|
209
138
|
/**
|
|
210
139
|
* export2TS
|
|
@@ -216,24 +145,21 @@ const export2TS = (ctx, n) => {
|
|
|
216
145
|
return (0, exports.contextStatement2TS)(n);
|
|
217
146
|
else if (n instanceof ast.LetStatement)
|
|
218
147
|
return (0, exports.letStatement2TS)(ctx, n);
|
|
219
|
-
else if (n instanceof ast.FunStatement)
|
|
220
|
-
return (0, exports.funStatement2TS)(ctx, n);
|
|
221
148
|
else if (n instanceof ast.ViewStatement)
|
|
222
149
|
return (0, exports.viewStatement2TS)(ctx, n);
|
|
223
|
-
else if (
|
|
150
|
+
else if (n instanceof ast.Widget || n instanceof ast.Node)
|
|
224
151
|
return (0, exports.tag2TS)(ctx, n);
|
|
225
152
|
else
|
|
226
|
-
return
|
|
153
|
+
return "";
|
|
227
154
|
};
|
|
228
155
|
exports.export2TS = export2TS;
|
|
229
156
|
/**
|
|
230
157
|
* aliasStatement2TS
|
|
231
158
|
*/
|
|
232
159
|
const aliasStatement2TS = (n) => {
|
|
233
|
-
let typeArgs =
|
|
234
|
-
(0, exports.typeParameters2TS)(n.typeParameters) : '';
|
|
160
|
+
let typeArgs = n.typeParameters.length > 0 ? (0, exports.typeParameters2TS)(n.typeParameters) : "";
|
|
235
161
|
let preamble = `export type ${n.id.value}${typeArgs}`;
|
|
236
|
-
let members = n.members.map(m => (0, exports.type2TS)(m)).join(
|
|
162
|
+
let members = n.members.map((m) => (0, exports.type2TS)(m)).join("|");
|
|
237
163
|
return `${preamble} = ${members};`;
|
|
238
164
|
};
|
|
239
165
|
exports.aliasStatement2TS = aliasStatement2TS;
|
|
@@ -242,26 +168,26 @@ exports.aliasStatement2TS = aliasStatement2TS;
|
|
|
242
168
|
*/
|
|
243
169
|
const contextStatement2TS = (n) => {
|
|
244
170
|
let preamble = `export interface ${n.id.value}`;
|
|
245
|
-
let typeArgs =
|
|
246
|
-
|
|
247
|
-
let [parents, members] = (0, array_1.partition)(n.members, member => member instanceof ast.ConstructorType);
|
|
171
|
+
let typeArgs = n.typeParameters.length > 0 ? (0, exports.typeParameters2TS)(n.typeParameters) : "";
|
|
172
|
+
let [parents, members] = (0, array_1.partition)(n.members, (member) => member instanceof ast.ConstructorType);
|
|
248
173
|
let parentList = parents
|
|
249
|
-
.map(exports.constructorType2TS)
|
|
250
|
-
|
|
174
|
+
.map(exports.constructorType2TS)
|
|
175
|
+
.join(",");
|
|
176
|
+
parentList = parentList !== "" ? ` extends ${parentList}` : "";
|
|
251
177
|
return [
|
|
252
178
|
preamble,
|
|
253
179
|
typeArgs,
|
|
254
180
|
parentList,
|
|
255
|
-
|
|
181
|
+
"{",
|
|
256
182
|
(0, exports.memberDeclarations2TS)(members),
|
|
257
|
-
|
|
258
|
-
].join(
|
|
183
|
+
"}",
|
|
184
|
+
].join("");
|
|
259
185
|
};
|
|
260
186
|
exports.contextStatement2TS = contextStatement2TS;
|
|
261
187
|
/**
|
|
262
188
|
* letStatement2TS
|
|
263
189
|
*/
|
|
264
|
-
const letStatement2TS = (ctx, n) => _setStatement2TS(ctx, n,
|
|
190
|
+
const letStatement2TS = (ctx, n) => _setStatement2TS(ctx, n, "export const");
|
|
265
191
|
exports.letStatement2TS = letStatement2TS;
|
|
266
192
|
const _setStatement2TS = (ctx, n, preamble) => {
|
|
267
193
|
let id = (0, exports.identifier2TS)(n.id);
|
|
@@ -270,178 +196,41 @@ const _setStatement2TS = (ctx, n, preamble) => {
|
|
|
270
196
|
let value = (0, exports.expression2TS)(ctx, n.expression);
|
|
271
197
|
return `${preamble} = ${value}`;
|
|
272
198
|
};
|
|
273
|
-
/**
|
|
274
|
-
* funStatement2TS generates Typescript output for fun statements.
|
|
275
|
-
*
|
|
276
|
-
* This is a curried function that first accepts zero or more arguments then
|
|
277
|
-
* a single Registry, finally the content.
|
|
278
|
-
*/
|
|
279
|
-
const funStatement2TS = (ctx, n) => {
|
|
280
|
-
let id = (0, exports.unqualifiedIdentifier2TS)(n.id);
|
|
281
|
-
let typeParams = (0, exports.typeParameters2TS)(n.typeParameters);
|
|
282
|
-
let params = (0, exports.parameters2TS)(n.parameters);
|
|
283
|
-
let factory = `(${exports.THIS}:${exports.WML}.Registry) : ${exports.WML}.Content[] =>`;
|
|
284
|
-
let body = (0, exports.children2TS)(ctx, n.body);
|
|
285
|
-
return [
|
|
286
|
-
`export const ${id} = `,
|
|
287
|
-
``,
|
|
288
|
-
`${typeParams}(${params})=>${factory} {`,
|
|
289
|
-
``,
|
|
290
|
-
` return ${body};`,
|
|
291
|
-
``,
|
|
292
|
-
`};`
|
|
293
|
-
].join(eol(ctx));
|
|
294
|
-
};
|
|
295
|
-
exports.funStatement2TS = funStatement2TS;
|
|
296
199
|
/**
|
|
297
200
|
* viewStatement2TS converts a ViewStatement to its typescript form.
|
|
298
201
|
*
|
|
299
202
|
* This is a class with template and various useful helpers.
|
|
300
203
|
*/
|
|
301
204
|
const viewStatement2TS = (ctx, n) => {
|
|
302
|
-
let instances = n.directives
|
|
303
|
-
|
|
205
|
+
let instances = n.directives
|
|
206
|
+
.map((i) => _setStatement2TS(ctx, i, "let"))
|
|
207
|
+
.join(`;${ctx.options.EOL}`);
|
|
208
|
+
let id = n.id ? (0, exports.constructor2TS)(n.id) : "Main";
|
|
304
209
|
let typeParams = (0, exports.typeParameters2TS)(n.typeParameters);
|
|
305
|
-
let context =
|
|
306
|
-
n.context
|
|
210
|
+
let context = n.context
|
|
211
|
+
? (0, exports.type2TS)(n.context instanceof ast.ContextFromStatement
|
|
212
|
+
? n.context.cons
|
|
213
|
+
: n.context)
|
|
214
|
+
: "object";
|
|
215
|
+
let defaultContext = n.context ? "" : "={}";
|
|
307
216
|
let template = (0, exports.tag2TS)(ctx, n.root);
|
|
308
217
|
return [
|
|
309
|
-
`export class ${id} ${typeParams}
|
|
310
|
-
``,
|
|
311
|
-
` constructor(${exports.CONTEXT}: ${context}) {`,
|
|
312
|
-
``,
|
|
313
|
-
` this.template = (${exports.THIS}:${exports.WML}.Registry) => {`,
|
|
314
|
-
``,
|
|
315
|
-
` ${instances}`,
|
|
316
|
-
``,
|
|
317
|
-
` return ${template};`,
|
|
318
|
-
``,
|
|
319
|
-
` }`,
|
|
320
|
-
``,
|
|
321
|
-
` }`,
|
|
322
|
-
``,
|
|
323
|
-
` ids: { [key: string]: ${exports.WML}.WMLElement } = {};`,
|
|
324
|
-
``,
|
|
325
|
-
` groups: { [key: string]: ${exports.WML}.WMLElement[] } = {};`,
|
|
326
|
-
``,
|
|
327
|
-
` views: ${exports.WML}.View[] = [];`,
|
|
328
|
-
``,
|
|
329
|
-
` widgets: ${exports.WML}.Widget[] = [];`,
|
|
330
|
-
``,
|
|
331
|
-
` tree: Node = <Node>${exports.DOCUMENT}.createElement('div');`,
|
|
332
|
-
``,
|
|
333
|
-
` template: ${exports.WML}.Template;`,
|
|
334
|
-
``,
|
|
335
|
-
` registerView(${REGISTER_VIEW_PARAMS}) : ${exports.WML}.View {`,
|
|
336
|
-
``,
|
|
337
|
-
` this.views.push(v);`,
|
|
338
|
-
``,
|
|
339
|
-
` return v;`,
|
|
340
|
-
``,
|
|
341
|
-
`}`,
|
|
342
|
-
` register(${REGISTER_PARAMS}) : ${exports.WML}.WMLElement {`,
|
|
343
|
-
``,
|
|
344
|
-
` let attrsMap = (<${exports.WML}.Attrs><any>attrs)`,
|
|
218
|
+
`export class ${id} ${typeParams} extends ${exports.WML}.${VIEW_CLASS} {`,
|
|
345
219
|
``,
|
|
346
|
-
`
|
|
220
|
+
` constructor(${exports.CONTEXT}: ${context} ${defaultContext}) {`,
|
|
347
221
|
``,
|
|
348
|
-
`
|
|
222
|
+
` super(${exports.CONTEXT}, (${exports.THIS}:${exports.WML}.${FRAME_TYPE}) => {`,
|
|
349
223
|
``,
|
|
350
|
-
`
|
|
224
|
+
` ${instances}`,
|
|
351
225
|
``,
|
|
352
|
-
`
|
|
353
|
-
` throw new Error(\`Duplicate id '\${id}' detected!\`);`,
|
|
226
|
+
` ${exports.THIS}.root(${template});`,
|
|
354
227
|
``,
|
|
355
|
-
` this.ids[id] = e;`,
|
|
356
228
|
``,
|
|
357
|
-
`
|
|
229
|
+
` });`,
|
|
358
230
|
``,
|
|
359
|
-
`
|
|
231
|
+
` }`,
|
|
360
232
|
``,
|
|
361
|
-
` this.groups[group] = this.groups[group] || [];`,
|
|
362
|
-
` this.groups[group].push(e);`,
|
|
363
|
-
``,
|
|
364
|
-
` }`,
|
|
365
|
-
``,
|
|
366
|
-
` }`,
|
|
367
|
-
` return e;`,
|
|
368
233
|
`}`,
|
|
369
|
-
``,
|
|
370
|
-
` node(${NODE_PARAMS}): ${exports.WML}.Content {`,
|
|
371
|
-
``,
|
|
372
|
-
` let asDOMAttrs = <${exports.DOCUMENT}.WMLDOMAttrs><object>attrs`,
|
|
373
|
-
``,
|
|
374
|
-
` let e = ${exports.DOCUMENT}.createElement(tag, asDOMAttrs, children,`,
|
|
375
|
-
` attrs.wml && attrs.wml.ns || '');`,
|
|
376
|
-
``,
|
|
377
|
-
` this.register(e, attrs);`,
|
|
378
|
-
``,
|
|
379
|
-
` return e;`,
|
|
380
|
-
``,
|
|
381
|
-
` }`,
|
|
382
|
-
``,
|
|
383
|
-
``,
|
|
384
|
-
` widget(${WIDGET_PARAMS}) : ${exports.WML}.Content {`,
|
|
385
|
-
``,
|
|
386
|
-
` this.register(w, attrs);`,
|
|
387
|
-
``,
|
|
388
|
-
` this.widgets.push(w);`,
|
|
389
|
-
``,
|
|
390
|
-
` return w.render();`,
|
|
391
|
-
``,
|
|
392
|
-
` }`,
|
|
393
|
-
``,
|
|
394
|
-
` findById<E extends ${exports.WML}.WMLElement>(id: string): ${MAYBE}<E> {`,
|
|
395
|
-
``,
|
|
396
|
-
` let mW:${MAYBE}<E> = ${FROM_NULLABLE}<E>(<E>this.ids[id])`,
|
|
397
|
-
``,
|
|
398
|
-
` return this.views.reduce((p,c)=>`,
|
|
399
|
-
` p.isJust() ? p : c.findById(id), mW);`,
|
|
400
|
-
``,
|
|
401
|
-
` }`,
|
|
402
|
-
``,
|
|
403
|
-
` findGroupById<E extends ${exports.WML}.WMLElement>(name: string): E[] {` +
|
|
404
|
-
``,
|
|
405
|
-
` return this.groups.hasOwnProperty(name) ?`,
|
|
406
|
-
` <E[]>this.groups[name] : [];`,
|
|
407
|
-
``,
|
|
408
|
-
` }`,
|
|
409
|
-
``,
|
|
410
|
-
` invalidate() : void {`,
|
|
411
|
-
``,
|
|
412
|
-
` let {tree} = this;`,
|
|
413
|
-
` let parent = <Node>tree.parentNode;`,
|
|
414
|
-
``,
|
|
415
|
-
` if (tree == null)`,
|
|
416
|
-
` return console.warn('invalidate(): '+` +
|
|
417
|
-
` 'Missing DOM tree!');`,
|
|
418
|
-
``,
|
|
419
|
-
` if (tree.parentNode == null)`,
|
|
420
|
-
` ${THROW_INVALIDATE_ERR}`,
|
|
421
|
-
``,
|
|
422
|
-
` parent.replaceChild(<Node>this.render(), tree) `,
|
|
423
|
-
``,
|
|
424
|
-
` }`,
|
|
425
|
-
``,
|
|
426
|
-
` render(): ${exports.WML}.Content {`,
|
|
427
|
-
``,
|
|
428
|
-
` this.ids = {};`,
|
|
429
|
-
` this.widgets.forEach(w => w.removed());`,
|
|
430
|
-
` this.widgets = [];`,
|
|
431
|
-
` this.views = [];`,
|
|
432
|
-
` this.tree = <Node>this.template(this);`,
|
|
433
|
-
``,
|
|
434
|
-
` this.ids['root'] = (this.ids['root']) ?`,
|
|
435
|
-
` this.ids['root'] : `,
|
|
436
|
-
` this.tree;`,
|
|
437
|
-
``,
|
|
438
|
-
` this.widgets.forEach(w => w.rendered());`,
|
|
439
|
-
``,
|
|
440
|
-
` return this.tree;`,
|
|
441
|
-
``,
|
|
442
|
-
` }`,
|
|
443
|
-
``,
|
|
444
|
-
`}`
|
|
445
234
|
].join(eol(ctx));
|
|
446
235
|
};
|
|
447
236
|
exports.viewStatement2TS = viewStatement2TS;
|
|
@@ -449,13 +238,13 @@ exports.viewStatement2TS = viewStatement2TS;
|
|
|
449
238
|
* typeParameters2TS converts a list of typeParameters2TS into the a list of
|
|
450
239
|
* typescript typeParameters2TS.
|
|
451
240
|
*/
|
|
452
|
-
const typeParameters2TS = (ns) =>
|
|
241
|
+
const typeParameters2TS = (ns) => ns.length === 0 ? "" : `<${ns.map(exports.typeParameter2TS).join(",")}> `;
|
|
453
242
|
exports.typeParameters2TS = typeParameters2TS;
|
|
454
243
|
/**
|
|
455
244
|
* typeParameter2TS converts a type parameter into a typescript type parameter.
|
|
456
245
|
*/
|
|
457
246
|
const typeParameter2TS = (n) => `${toPrim((0, exports.identifierOrConstructor2TS)(n.id))} ` +
|
|
458
|
-
`${n.constraint ?
|
|
247
|
+
`${n.constraint ? "extends " + (0, exports.type2TS)(n.constraint) : ""} `;
|
|
459
248
|
exports.typeParameter2TS = typeParameter2TS;
|
|
460
249
|
/**
|
|
461
250
|
* type2TS
|
|
@@ -477,7 +266,7 @@ const type2TS = (n) => {
|
|
|
477
266
|
return (0, exports.boolean2TS)(n);
|
|
478
267
|
else if (n instanceof ast.FunctionType)
|
|
479
268
|
return (0, exports.functionType2TS)(n);
|
|
480
|
-
return
|
|
269
|
+
return "void";
|
|
481
270
|
};
|
|
482
271
|
exports.type2TS = type2TS;
|
|
483
272
|
/**
|
|
@@ -487,15 +276,16 @@ exports.type2TS = type2TS;
|
|
|
487
276
|
*/
|
|
488
277
|
const constructorType2TS = (n) => {
|
|
489
278
|
let id = toPrim((0, exports.identifierOrConstructor2TS)(n.id));
|
|
490
|
-
return
|
|
491
|
-
id + (0, exports.typeParameters2TS)(n.typeParameters)
|
|
279
|
+
return n.typeParameters.length > 0
|
|
280
|
+
? id + (0, exports.typeParameters2TS)(n.typeParameters)
|
|
281
|
+
: id;
|
|
492
282
|
};
|
|
493
283
|
exports.constructorType2TS = constructorType2TS;
|
|
494
284
|
/**
|
|
495
285
|
* functionType2TS
|
|
496
286
|
*/
|
|
497
287
|
const functionType2TS = (n) => {
|
|
498
|
-
let params = n.parameters.map((t, k) => `$${k}:${(0, exports.type2TS)(t)}`).join(
|
|
288
|
+
let params = n.parameters.map((t, k) => `$${k}:${(0, exports.type2TS)(t)}`).join(",");
|
|
499
289
|
let ret = (0, exports.type2TS)(n.returnType);
|
|
500
290
|
return `(${params}) => ${ret}`;
|
|
501
291
|
};
|
|
@@ -508,13 +298,13 @@ exports.listType2TS = listType2TS;
|
|
|
508
298
|
/**
|
|
509
299
|
* tupleType2TS
|
|
510
300
|
*/
|
|
511
|
-
const tupleType2TS = (n) => `[${n.members.map(exports.type2TS).join(
|
|
301
|
+
const tupleType2TS = (n) => `[${n.members.map(exports.type2TS).join(",")}]`;
|
|
512
302
|
exports.tupleType2TS = tupleType2TS;
|
|
513
303
|
/**
|
|
514
304
|
* recordType2TS converts the RecordType node to the body of a TypeScript
|
|
515
305
|
* record interface.
|
|
516
306
|
*/
|
|
517
|
-
const recordType2Ts = (n) =>
|
|
307
|
+
const recordType2Ts = (n) => "{" + (0, exports.memberDeclarations2TS)(n.members) + "}";
|
|
518
308
|
exports.recordType2Ts = recordType2Ts;
|
|
519
309
|
/**
|
|
520
310
|
* memberDeclarations2TS converts a list of MemberDeclarations to TypeScript.
|
|
@@ -530,7 +320,7 @@ exports.memberDeclarations2TS = memberDeclarations2TS;
|
|
|
530
320
|
* This works recursively and any RecordTypes encountered will be flattened.
|
|
531
321
|
*/
|
|
532
322
|
const typeMapFromMemberDecs = (list) => list.reduce((p, m) => {
|
|
533
|
-
let paths = m.path.map(p => p.value);
|
|
323
|
+
let paths = m.path.map((p) => p.value);
|
|
534
324
|
if (m.kind instanceof ast.RecordType) {
|
|
535
325
|
return (0, exports.typeMapFromRecordType)(m.kind, p, paths);
|
|
536
326
|
}
|
|
@@ -549,7 +339,7 @@ exports.typeMapFromMemberDecs = typeMapFromMemberDecs;
|
|
|
549
339
|
* Any encountered RecordTypes will be flattened.
|
|
550
340
|
*/
|
|
551
341
|
const typeMapFromRecordType = (n, init, prefix) => n.members.reduce((p, m) => {
|
|
552
|
-
let path = [...prefix, ...
|
|
342
|
+
let path = [...prefix, ...m.path.map((p) => p.value)];
|
|
553
343
|
if (m.kind instanceof ast.RecordType) {
|
|
554
344
|
return (0, exports.typeMapFromRecordType)(m.kind, init, path);
|
|
555
345
|
}
|
|
@@ -559,7 +349,7 @@ const typeMapFromRecordType = (n, init, prefix) => n.members.reduce((p, m) => {
|
|
|
559
349
|
}
|
|
560
350
|
}, init);
|
|
561
351
|
exports.typeMapFromRecordType = typeMapFromRecordType;
|
|
562
|
-
const paths2String = (paths) => paths.join(
|
|
352
|
+
const paths2String = (paths) => paths.join(".");
|
|
563
353
|
/**
|
|
564
354
|
* expandTypeMap to an ExpandedTypeMap.
|
|
565
355
|
*/
|
|
@@ -576,14 +366,14 @@ const typeMap2TS = (m) => (0, record_1.mapTo)(m, (t, k) => {
|
|
|
576
366
|
else {
|
|
577
367
|
return `${k} : ${t}`;
|
|
578
368
|
}
|
|
579
|
-
}).join(
|
|
369
|
+
}).join(",\n");
|
|
580
370
|
exports.typeMap2TS = typeMap2TS;
|
|
581
|
-
const isOptional = (m) => (0, record_1.reduce)(m, false, (p, _, k) => p ? p : k.indexOf(
|
|
371
|
+
const isOptional = (m) => (0, record_1.reduce)(m, false, (p, _, k) => (p ? p : k.indexOf("?") > -1));
|
|
582
372
|
/**
|
|
583
373
|
* parameters2TS converts a list Parameter nodes into an parameter list
|
|
584
374
|
* (without parens).
|
|
585
375
|
*/
|
|
586
|
-
const parameters2TS = (list) => list.map(p => (0, exports.parameter2TS)(p)).join(
|
|
376
|
+
const parameters2TS = (list) => list.map((p) => (0, exports.parameter2TS)(p)).join(",");
|
|
587
377
|
exports.parameters2TS = parameters2TS;
|
|
588
378
|
/**
|
|
589
379
|
* parameter2TS
|
|
@@ -594,7 +384,7 @@ const parameter2TS = (n) => {
|
|
|
594
384
|
else if (n instanceof ast.UntypedParameter)
|
|
595
385
|
return (0, exports.untypedParameter2TS)(n);
|
|
596
386
|
else
|
|
597
|
-
return
|
|
387
|
+
return "";
|
|
598
388
|
};
|
|
599
389
|
exports.parameter2TS = parameter2TS;
|
|
600
390
|
/**
|
|
@@ -611,14 +401,14 @@ exports.untypedParameter2TS = untypedParameter2TS;
|
|
|
611
401
|
* children2TS
|
|
612
402
|
*/
|
|
613
403
|
const children2TS = (ctx, list) => `[${ctx.options.EOL}
|
|
614
|
-
${list.map(l => (0, exports.child2TS)(ctx, l)).join(
|
|
404
|
+
${list.map((l) => (0, exports.child2TS)(ctx, l)).join("," + ctx.options.EOL)}
|
|
615
405
|
]`;
|
|
616
406
|
exports.children2TS = children2TS;
|
|
617
407
|
/**
|
|
618
408
|
* child2TS converts children to typescript.
|
|
619
409
|
*/
|
|
620
410
|
const child2TS = (ctx, n) => {
|
|
621
|
-
if (
|
|
411
|
+
if (n instanceof ast.Node || n instanceof ast.Widget)
|
|
622
412
|
return (0, exports.tag2TS)(ctx, n);
|
|
623
413
|
else if (n instanceof ast.Interpolation)
|
|
624
414
|
return (0, exports.interpolation2TS)(ctx, n);
|
|
@@ -643,13 +433,13 @@ const child2TS = (ctx, n) => {
|
|
|
643
433
|
else if (n instanceof ast.QualifiedIdentifier)
|
|
644
434
|
return (0, exports.qualifiedIdentifier2TS)(n);
|
|
645
435
|
else
|
|
646
|
-
return
|
|
436
|
+
return "";
|
|
647
437
|
};
|
|
648
438
|
exports.child2TS = child2TS;
|
|
649
439
|
/**
|
|
650
440
|
* tag2TS converts a tag to typescript.
|
|
651
441
|
*/
|
|
652
|
-
const tag2TS = (ctx, n) =>
|
|
442
|
+
const tag2TS = (ctx, n) => n instanceof ast.Widget ? (0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
653
443
|
exports.tag2TS = tag2TS;
|
|
654
444
|
/**
|
|
655
445
|
* widget2TS converts a Widget node into its typescript representation.
|
|
@@ -661,8 +451,8 @@ const widget2TS = (ctx, n) => {
|
|
|
661
451
|
let typeParams = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
662
452
|
let attrs = (0, exports.attrs2String)(ctx, n.attributes);
|
|
663
453
|
let childs = (0, exports.children2TS)(ctx, n.children);
|
|
664
|
-
return `${exports.THIS}.widget(new ${name}${typeParams}(${attrs}, ${childs}),` +
|
|
665
|
-
`<${exports.WML}.Attrs>${attrs})
|
|
454
|
+
return (`${exports.THIS}.widget(new ${name}${typeParams}(${attrs}, ${childs}),` +
|
|
455
|
+
`<${exports.WML}.Attrs>${attrs})`);
|
|
666
456
|
};
|
|
667
457
|
exports.widget2TS = widget2TS;
|
|
668
458
|
/**
|
|
@@ -685,9 +475,9 @@ exports.attribute2TS = attribute2TS;
|
|
|
685
475
|
/**
|
|
686
476
|
* attributeValue2TS
|
|
687
477
|
*/
|
|
688
|
-
const attributeValue2TS = (ctx, n) =>
|
|
689
|
-
(0, exports.interpolation2TS)(ctx, n.value)
|
|
690
|
-
(0, exports.literal2TS)(ctx, n.value);
|
|
478
|
+
const attributeValue2TS = (ctx, n) => n.value instanceof ast.Interpolation
|
|
479
|
+
? (0, exports.interpolation2TS)(ctx, n.value)
|
|
480
|
+
: (0, exports.literal2TS)(ctx, n.value);
|
|
691
481
|
exports.attributeValue2TS = attributeValue2TS;
|
|
692
482
|
/**
|
|
693
483
|
* attributeName2TS
|
|
@@ -699,24 +489,23 @@ exports.attributeName2TS = attributeName2TS;
|
|
|
699
489
|
*/
|
|
700
490
|
const attrs2String = (ctx, attrs) => {
|
|
701
491
|
// Check for the special wml:attrs attribute.
|
|
702
|
-
let mallAttrs = (0, array_1.find)(attrs, attr =>
|
|
703
|
-
(attr.name.value === 'attrs')));
|
|
492
|
+
let mallAttrs = (0, array_1.find)(attrs, (attr) => attr.namespace.value === "wml" && attr.name.value === "attrs");
|
|
704
493
|
if (mallAttrs.isJust())
|
|
705
494
|
return (0, exports.attributeValue2TS)(ctx, mallAttrs.get());
|
|
706
|
-
let [nns, ns] = (0, array_1.partition)(attrs, a =>
|
|
495
|
+
let [nns, ns] = (0, array_1.partition)(attrs, (a) => a.namespace.value === "");
|
|
707
496
|
let nso = ns.reduce((p, n) => (0, record_1.merge)(p, {
|
|
708
|
-
[n.namespace.value]: (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n))
|
|
497
|
+
[n.namespace.value]: (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n)),
|
|
709
498
|
}), {});
|
|
710
499
|
return _attrs2String(nns.reduce((p, n) => (0, record_1.merge)(p, {
|
|
711
|
-
[(0, exports.attributeName2TS)(ctx, n)]: (0, exports.attributeValue2TS)(ctx, n)
|
|
500
|
+
[(0, exports.attributeName2TS)(ctx, n)]: (0, exports.attributeValue2TS)(ctx, n),
|
|
712
501
|
}), nso));
|
|
713
502
|
};
|
|
714
503
|
exports.attrs2String = attrs2String;
|
|
715
|
-
const _attrs2String = (attrs) =>
|
|
716
|
-
Object.keys(attrs).map(name => Array.isArray(attrs[name])
|
|
717
|
-
`${name} : { ${attrs[name].join(
|
|
718
|
-
`${name}: ${attrs[name]}`) +
|
|
719
|
-
|
|
504
|
+
const _attrs2String = (attrs) => "{" +
|
|
505
|
+
Object.keys(attrs).map((name) => Array.isArray(attrs[name])
|
|
506
|
+
? `${name} : { ${attrs[name].join(",")} }`
|
|
507
|
+
: `${name}: ${attrs[name]}`) +
|
|
508
|
+
"}";
|
|
720
509
|
/**
|
|
721
510
|
* interpolation2TS
|
|
722
511
|
*/
|
|
@@ -728,15 +517,15 @@ exports.interpolation2TS = interpolation2TS;
|
|
|
728
517
|
const ifStatement2TS = (ctx, n) => {
|
|
729
518
|
let condition = (0, exports.expression2TS)(ctx, n.condition);
|
|
730
519
|
let conseq = (0, exports.children2TS)(ctx, n.then);
|
|
731
|
-
let alt =
|
|
732
|
-
`[${(0, exports.ifStatement2TS)(ctx, n.elseClause)}]`
|
|
733
|
-
|
|
734
|
-
(0, exports.children2TS)(ctx, n.elseClause.children)
|
|
735
|
-
|
|
520
|
+
let alt = n.elseClause instanceof ast.ElseIfClause
|
|
521
|
+
? `[${(0, exports.ifStatement2TS)(ctx, n.elseClause)}]`
|
|
522
|
+
: n.elseClause instanceof ast.ElseClause
|
|
523
|
+
? (0, exports.children2TS)(ctx, n.elseClause.children)
|
|
524
|
+
: "[]";
|
|
736
525
|
return [
|
|
737
526
|
`...((${condition}) ?`,
|
|
738
527
|
`(()=>(${conseq}))() :`,
|
|
739
|
-
`(()=>(${alt}))())
|
|
528
|
+
`(()=>(${alt}))())`,
|
|
740
529
|
].join(ctx.options.EOL);
|
|
741
530
|
};
|
|
742
531
|
exports.ifStatement2TS = ifStatement2TS;
|
|
@@ -746,14 +535,14 @@ exports.ifStatement2TS = ifStatement2TS;
|
|
|
746
535
|
const forInStatement2TS = (ctx, n) => {
|
|
747
536
|
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
748
537
|
let value = (0, exports.parameter2TS)(n.variables[0]);
|
|
749
|
-
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) :
|
|
750
|
-
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) :
|
|
538
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : "_$$i";
|
|
539
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : "_$$all";
|
|
751
540
|
let body = (0, exports.children2TS)(ctx, n.body);
|
|
752
|
-
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) :
|
|
541
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : "[]";
|
|
753
542
|
return [
|
|
754
543
|
`...${FOR_IN} (${expr}, (${value}, ${key}, ${all})=> `,
|
|
755
544
|
`(${body}), `,
|
|
756
|
-
`()=> (${alt}))
|
|
545
|
+
`()=> (${alt}))`,
|
|
757
546
|
].join(ctx.options.EOL);
|
|
758
547
|
};
|
|
759
548
|
exports.forInStatement2TS = forInStatement2TS;
|
|
@@ -763,14 +552,14 @@ exports.forInStatement2TS = forInStatement2TS;
|
|
|
763
552
|
const forOfStatement2TS = (ctx, n) => {
|
|
764
553
|
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
765
554
|
let value = (0, exports.parameter2TS)(n.variables[0]);
|
|
766
|
-
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) :
|
|
767
|
-
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) :
|
|
555
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : "_$$k";
|
|
556
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : "_$$all";
|
|
768
557
|
let body = (0, exports.children2TS)(ctx, n.body);
|
|
769
|
-
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) :
|
|
558
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : "[]";
|
|
770
559
|
return [
|
|
771
560
|
`...${FOR_OF} (${expr}, (${value}, ${key}, ${all}) => `,
|
|
772
561
|
` (${body}), `,
|
|
773
|
-
` ()=> (${alt}))
|
|
562
|
+
` ()=> (${alt}))`,
|
|
774
563
|
].join(eol(ctx));
|
|
775
564
|
};
|
|
776
565
|
exports.forOfStatement2TS = forOfStatement2TS;
|
|
@@ -790,7 +579,7 @@ const forFromStatement2TS = (ctx, node) => {
|
|
|
790
579
|
` ...${body}`,
|
|
791
580
|
` );`,
|
|
792
581
|
` return result;`,
|
|
793
|
-
`})()
|
|
582
|
+
`})()`,
|
|
794
583
|
].join(eol(ctx));
|
|
795
584
|
};
|
|
796
585
|
exports.forFromStatement2TS = forFromStatement2TS;
|
|
@@ -799,7 +588,7 @@ exports.forFromStatement2TS = forFromStatement2TS;
|
|
|
799
588
|
*/
|
|
800
589
|
const characters2TS = (n) => `${exports.DOCUMENT}.createTextNode('${breakLines(n.value)}')`;
|
|
801
590
|
exports.characters2TS = characters2TS;
|
|
802
|
-
const breakLines = (str) => str.split(
|
|
591
|
+
const breakLines = (str) => str.split("\n").join("\\u000a");
|
|
803
592
|
/**
|
|
804
593
|
* expression2TS
|
|
805
594
|
*/
|
|
@@ -845,7 +634,7 @@ const expression2TS = (ctx, n) => {
|
|
|
845
634
|
else if (n instanceof ast.ContextVariable)
|
|
846
635
|
return (0, exports.contextVariable2TS)(n);
|
|
847
636
|
else
|
|
848
|
-
return
|
|
637
|
+
return "";
|
|
849
638
|
};
|
|
850
639
|
exports.expression2TS = expression2TS;
|
|
851
640
|
/**
|
|
@@ -863,13 +652,15 @@ exports.ifThenExpression2TS = ifThenExpression2TS;
|
|
|
863
652
|
*/
|
|
864
653
|
const binaryExpression2TS = (ctx, n) => {
|
|
865
654
|
let left = (0, exports.expression2TS)(ctx, n.left);
|
|
866
|
-
let right = n.operator ===
|
|
867
|
-
(0, exports.type2TS)(n.right)
|
|
868
|
-
(0, exports.expression2TS)(ctx, n.right);
|
|
869
|
-
let op = operators.hasOwnProperty(n.operator)
|
|
870
|
-
operators[n.operator]
|
|
871
|
-
n.operator;
|
|
872
|
-
return
|
|
655
|
+
let right = n.operator === "as"
|
|
656
|
+
? (0, exports.type2TS)(n.right)
|
|
657
|
+
: (0, exports.expression2TS)(ctx, n.right);
|
|
658
|
+
let op = operators.hasOwnProperty(n.operator)
|
|
659
|
+
? operators[n.operator]
|
|
660
|
+
: n.operator;
|
|
661
|
+
return n.operator === "as"
|
|
662
|
+
? `<${right}>(${left})`
|
|
663
|
+
: `(${left} ${op} ${right})`;
|
|
873
664
|
};
|
|
874
665
|
exports.binaryExpression2TS = binaryExpression2TS;
|
|
875
666
|
/**
|
|
@@ -883,7 +674,7 @@ exports.unaryExpression2TS = unaryExpression2TS;
|
|
|
883
674
|
/**
|
|
884
675
|
* viewConstruction2TS
|
|
885
676
|
*/
|
|
886
|
-
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.
|
|
677
|
+
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.view(${(0, exports.expression2TS)(ctx, n.expression)})`;
|
|
887
678
|
exports.viewConstruction2TS = viewConstruction2TS;
|
|
888
679
|
/**
|
|
889
680
|
* funApplication2TS
|
|
@@ -899,9 +690,9 @@ const constructExpression2TS = (ctx, n) => {
|
|
|
899
690
|
let consOriginal = `${cons[0].toUpperCase()}${cons.slice(1)}`;
|
|
900
691
|
let typeArgs = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
901
692
|
let args = (0, exports.args2TS)(ctx, n.args);
|
|
902
|
-
return (0, array_1.contains)(casters, consOriginal)
|
|
903
|
-
`${consOriginal}${typeArgs}(${args})`
|
|
904
|
-
`new ${cons}${typeArgs}(${args})`;
|
|
693
|
+
return (0, array_1.contains)(casters, consOriginal)
|
|
694
|
+
? `${consOriginal}${typeArgs}(${args})`
|
|
695
|
+
: `new ${cons}${typeArgs}(${args})`;
|
|
905
696
|
};
|
|
906
697
|
exports.constructExpression2TS = constructExpression2TS;
|
|
907
698
|
/**
|
|
@@ -917,28 +708,28 @@ exports.callExpression2TS = callExpression2TS;
|
|
|
917
708
|
/**
|
|
918
709
|
* typeArgs2TS
|
|
919
710
|
*/
|
|
920
|
-
const typeArgs2TS = (ns) => (0, array_1.empty)(ns) ?
|
|
711
|
+
const typeArgs2TS = (ns) => (0, array_1.empty)(ns) ? "" : `<${ns.map(exports.type2TS).join(",")}>`;
|
|
921
712
|
exports.typeArgs2TS = typeArgs2TS;
|
|
922
713
|
/**
|
|
923
714
|
* args2TS converts a list of arguments to a typescript argument tupple.
|
|
924
715
|
*/
|
|
925
|
-
const args2TS = (ctx, ns) =>
|
|
716
|
+
const args2TS = (ctx, ns) => ns.length === 0 ? "" : ns.map((e) => (0, exports.expression2TS)(ctx, e)).join(",");
|
|
926
717
|
exports.args2TS = args2TS;
|
|
927
718
|
/**
|
|
928
719
|
* memberExpression2TS
|
|
929
720
|
*/
|
|
930
721
|
const memberExpression2TS = (ctx, n) => {
|
|
931
722
|
let target = (0, exports.expression2TS)(ctx, n.head);
|
|
932
|
-
return
|
|
933
|
-
`${target}[${(0, exports.string2TS)(n.tail)}]`
|
|
934
|
-
`${target}.${(0, exports.expression2TS)(ctx, n.tail)}`;
|
|
723
|
+
return n.tail instanceof ast.StringLiteral
|
|
724
|
+
? `${target}[${(0, exports.string2TS)(n.tail)}]`
|
|
725
|
+
: `${target}.${(0, exports.expression2TS)(ctx, n.tail)}`;
|
|
935
726
|
};
|
|
936
727
|
exports.memberExpression2TS = memberExpression2TS;
|
|
937
728
|
/**
|
|
938
729
|
* functionExpression2TS
|
|
939
730
|
*/
|
|
940
731
|
const functionExpression2TS = (ctx, n) => {
|
|
941
|
-
let params = n.parameters.map(exports.parameter2TS).join(
|
|
732
|
+
let params = n.parameters.map(exports.parameter2TS).join(",");
|
|
942
733
|
let body = (0, exports.expression2TS)(ctx, n.body);
|
|
943
734
|
return `(${params}) => ${body}`;
|
|
944
735
|
};
|
|
@@ -958,7 +749,7 @@ const literal2TS = (ctx, n) => {
|
|
|
958
749
|
else if (n instanceof ast.List)
|
|
959
750
|
return (0, exports.list2TS)(ctx, n);
|
|
960
751
|
else
|
|
961
|
-
return
|
|
752
|
+
return "";
|
|
962
753
|
};
|
|
963
754
|
exports.literal2TS = literal2TS;
|
|
964
755
|
/**
|
|
@@ -969,7 +760,10 @@ exports.boolean2TS = boolean2TS;
|
|
|
969
760
|
/**
|
|
970
761
|
* string2TS
|
|
971
762
|
*/
|
|
972
|
-
const string2TS = (n) => n.value
|
|
763
|
+
const string2TS = (n) => n.value
|
|
764
|
+
.split("\n")
|
|
765
|
+
.map((str) => `"${str}"`)
|
|
766
|
+
.join("+");
|
|
973
767
|
exports.string2TS = string2TS;
|
|
974
768
|
/**
|
|
975
769
|
* number2TS
|
|
@@ -980,16 +774,16 @@ exports.number2TS = number2TS;
|
|
|
980
774
|
* record2TS
|
|
981
775
|
*/
|
|
982
776
|
const record2TS = (ctx, n) => `{${ctx.options.EOL}
|
|
983
|
-
${n.properties.map(p => (0, exports.property2TS)(ctx, p)).join(
|
|
777
|
+
${n.properties.map((p) => (0, exports.property2TS)(ctx, p)).join("," + ctx.options.EOL)}
|
|
984
778
|
}`;
|
|
985
779
|
exports.record2TS = record2TS;
|
|
986
780
|
/**
|
|
987
781
|
* list2TS
|
|
988
782
|
*/
|
|
989
783
|
const list2TS = (ctx, n) => {
|
|
990
|
-
let mems = n.members.map(e => (0, exports.expression2TS)(ctx, e));
|
|
784
|
+
let mems = n.members.map((e) => (0, exports.expression2TS)(ctx, e));
|
|
991
785
|
return `[${ctx.options.EOL}
|
|
992
|
-
${mems.join(
|
|
786
|
+
${mems.join("," + ctx.options.EOL)}
|
|
993
787
|
]`;
|
|
994
788
|
};
|
|
995
789
|
exports.list2TS = list2TS;
|
|
@@ -1001,15 +795,15 @@ exports.property2TS = property2TS;
|
|
|
1001
795
|
/**
|
|
1002
796
|
* key2TS
|
|
1003
797
|
*/
|
|
1004
|
-
const key2TS = (n) =>
|
|
798
|
+
const key2TS = (n) => n instanceof ast.StringLiteral ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1005
799
|
exports.key2TS = key2TS;
|
|
1006
800
|
/**
|
|
1007
801
|
* contextProperty2TS
|
|
1008
802
|
*/
|
|
1009
803
|
const contextProperty2TS = (n) => {
|
|
1010
|
-
let member =
|
|
1011
|
-
n.member.value
|
|
1012
|
-
(0, exports.identifier2TS)(n.member);
|
|
804
|
+
let member = n.member instanceof ast.StringLiteral
|
|
805
|
+
? n.member.value
|
|
806
|
+
: (0, exports.identifier2TS)(n.member);
|
|
1013
807
|
return `${exports.CONTEXT}.${member}`;
|
|
1014
808
|
};
|
|
1015
809
|
exports.contextProperty2TS = contextProperty2TS;
|
|
@@ -1022,14 +816,14 @@ exports.contextVariable2TS = contextVariable2TS;
|
|
|
1022
816
|
* identifierOrConstructor2TS
|
|
1023
817
|
*/
|
|
1024
818
|
const identifierOrConstructor2TS = (n) => {
|
|
1025
|
-
if (
|
|
1026
|
-
|
|
819
|
+
if (n instanceof ast.UnqualifiedIdentifier ||
|
|
820
|
+
n instanceof ast.QualifiedIdentifier)
|
|
1027
821
|
return (0, exports.identifier2TS)(n);
|
|
1028
|
-
else if (
|
|
1029
|
-
|
|
822
|
+
else if (n instanceof ast.UnqualifiedConstructor ||
|
|
823
|
+
n instanceof ast.QualifiedConstructor)
|
|
1030
824
|
return (0, exports.constructor2TS)(n);
|
|
1031
825
|
else
|
|
1032
|
-
return
|
|
826
|
+
return "";
|
|
1033
827
|
};
|
|
1034
828
|
exports.identifierOrConstructor2TS = identifierOrConstructor2TS;
|
|
1035
829
|
/**
|
|
@@ -1041,7 +835,7 @@ const constructor2TS = (n) => {
|
|
|
1041
835
|
else if (n instanceof ast.UnqualifiedConstructor)
|
|
1042
836
|
return (0, exports.unqualifiedConstructor2TS)(n);
|
|
1043
837
|
else
|
|
1044
|
-
return
|
|
838
|
+
return "";
|
|
1045
839
|
};
|
|
1046
840
|
exports.constructor2TS = constructor2TS;
|
|
1047
841
|
/**
|
|
@@ -1063,7 +857,7 @@ const identifier2TS = (n) => {
|
|
|
1063
857
|
else if (n instanceof ast.UnqualifiedIdentifier)
|
|
1064
858
|
return (0, exports.unqualifiedIdentifier2TS)(n);
|
|
1065
859
|
else
|
|
1066
|
-
return
|
|
860
|
+
return "";
|
|
1067
861
|
};
|
|
1068
862
|
exports.identifier2TS = identifier2TS;
|
|
1069
863
|
/**
|
|
@@ -1076,5 +870,5 @@ exports.qualifiedIdentifier2TS = qualifiedIdentifier2TS;
|
|
|
1076
870
|
*/
|
|
1077
871
|
const unqualifiedIdentifier2TS = (n) => `${toPrim(n.value)}`;
|
|
1078
872
|
exports.unqualifiedIdentifier2TS = unqualifiedIdentifier2TS;
|
|
1079
|
-
const toPrim = (id) => prims.indexOf(id) > -1 ? id.toLowerCase() : id;
|
|
873
|
+
const toPrim = (id) => (prims.indexOf(id) > -1 ? id.toLowerCase() : id);
|
|
1080
874
|
//# sourceMappingURL=codegen.js.map
|