@quenk/wml 2.13.10 → 2.14.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.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 +164 -369
- package/lib/compile/codegen.js.map +1 -1
- package/lib/compile/codegen.ts +644 -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 +8 -6
- package/lib/compile/transform.js +46 -18
- package/lib/compile/transform.js.map +1 -1
- package/lib/compile/transform.ts +139 -116
- package/lib/{dom.d.ts → dom/index.d.ts} +8 -2
- package/lib/{dom.js → dom/index.js} +84 -66
- 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 +12 -6
- package/lib/parse/ast.js +68 -58
- package/lib/parse/ast.js.map +1 -1
- package/lib/parse/ast.ts +400 -482
- 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 +103 -0
- package/lib/view/frame.js +206 -0
- package/lib/view/frame.js.map +1 -0
- package/lib/view/frame.ts +249 -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 +97 -0
- package/package.json +4 -3
- package/lib/dom.js.map +0 -1
- package/lib/dom.ts +0 -475
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 VIEW_FRAME_CLASS = "ViewFrame";
|
|
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
|
|
@@ -73,89 +59,28 @@ class CodeGenerator {
|
|
|
73
59
|
* generate a Typescript module from an WML AST.
|
|
74
60
|
*/
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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';`,
|
|
70
|
+
(0, exports.importStatements2TS)(this, tree.imports),
|
|
84
71
|
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,
|
|
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,42 @@ 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}.${VIEW_FRAME_CLASS}) => {`,
|
|
349
223
|
``,
|
|
350
|
-
`
|
|
224
|
+
` ${instances}`,
|
|
351
225
|
``,
|
|
352
|
-
`
|
|
353
|
-
` throw new Error(\`Duplicate id '\${id}' detected!\`);`,
|
|
226
|
+
` ${exports.THIS}.root(${template});`,
|
|
354
227
|
``,
|
|
355
|
-
`
|
|
228
|
+
` return ${exports.THIS};`,
|
|
356
229
|
``,
|
|
357
|
-
`
|
|
230
|
+
` });`,
|
|
358
231
|
``,
|
|
359
|
-
`
|
|
232
|
+
` }`,
|
|
360
233
|
``,
|
|
361
|
-
` this.groups[group] = this.groups[group] || [];`,
|
|
362
|
-
` this.groups[group].push(e);`,
|
|
363
|
-
``,
|
|
364
|
-
` }`,
|
|
365
|
-
``,
|
|
366
|
-
` }`,
|
|
367
|
-
` return e;`,
|
|
368
234
|
`}`,
|
|
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
235
|
].join(eol(ctx));
|
|
446
236
|
};
|
|
447
237
|
exports.viewStatement2TS = viewStatement2TS;
|
|
@@ -449,13 +239,13 @@ exports.viewStatement2TS = viewStatement2TS;
|
|
|
449
239
|
* typeParameters2TS converts a list of typeParameters2TS into the a list of
|
|
450
240
|
* typescript typeParameters2TS.
|
|
451
241
|
*/
|
|
452
|
-
const typeParameters2TS = (ns) =>
|
|
242
|
+
const typeParameters2TS = (ns) => ns.length === 0 ? "" : `<${ns.map(exports.typeParameter2TS).join(",")}> `;
|
|
453
243
|
exports.typeParameters2TS = typeParameters2TS;
|
|
454
244
|
/**
|
|
455
245
|
* typeParameter2TS converts a type parameter into a typescript type parameter.
|
|
456
246
|
*/
|
|
457
247
|
const typeParameter2TS = (n) => `${toPrim((0, exports.identifierOrConstructor2TS)(n.id))} ` +
|
|
458
|
-
`${n.constraint ?
|
|
248
|
+
`${n.constraint ? "extends " + (0, exports.type2TS)(n.constraint) : ""} `;
|
|
459
249
|
exports.typeParameter2TS = typeParameter2TS;
|
|
460
250
|
/**
|
|
461
251
|
* type2TS
|
|
@@ -477,7 +267,7 @@ const type2TS = (n) => {
|
|
|
477
267
|
return (0, exports.boolean2TS)(n);
|
|
478
268
|
else if (n instanceof ast.FunctionType)
|
|
479
269
|
return (0, exports.functionType2TS)(n);
|
|
480
|
-
return
|
|
270
|
+
return "void";
|
|
481
271
|
};
|
|
482
272
|
exports.type2TS = type2TS;
|
|
483
273
|
/**
|
|
@@ -487,15 +277,16 @@ exports.type2TS = type2TS;
|
|
|
487
277
|
*/
|
|
488
278
|
const constructorType2TS = (n) => {
|
|
489
279
|
let id = toPrim((0, exports.identifierOrConstructor2TS)(n.id));
|
|
490
|
-
return
|
|
491
|
-
id + (0, exports.typeParameters2TS)(n.typeParameters)
|
|
280
|
+
return n.typeParameters.length > 0
|
|
281
|
+
? id + (0, exports.typeParameters2TS)(n.typeParameters)
|
|
282
|
+
: id;
|
|
492
283
|
};
|
|
493
284
|
exports.constructorType2TS = constructorType2TS;
|
|
494
285
|
/**
|
|
495
286
|
* functionType2TS
|
|
496
287
|
*/
|
|
497
288
|
const functionType2TS = (n) => {
|
|
498
|
-
let params = n.parameters.map((t, k) => `$${k}:${(0, exports.type2TS)(t)}`).join(
|
|
289
|
+
let params = n.parameters.map((t, k) => `$${k}:${(0, exports.type2TS)(t)}`).join(",");
|
|
499
290
|
let ret = (0, exports.type2TS)(n.returnType);
|
|
500
291
|
return `(${params}) => ${ret}`;
|
|
501
292
|
};
|
|
@@ -508,13 +299,13 @@ exports.listType2TS = listType2TS;
|
|
|
508
299
|
/**
|
|
509
300
|
* tupleType2TS
|
|
510
301
|
*/
|
|
511
|
-
const tupleType2TS = (n) => `[${n.members.map(exports.type2TS).join(
|
|
302
|
+
const tupleType2TS = (n) => `[${n.members.map(exports.type2TS).join(",")}]`;
|
|
512
303
|
exports.tupleType2TS = tupleType2TS;
|
|
513
304
|
/**
|
|
514
305
|
* recordType2TS converts the RecordType node to the body of a TypeScript
|
|
515
306
|
* record interface.
|
|
516
307
|
*/
|
|
517
|
-
const recordType2Ts = (n) =>
|
|
308
|
+
const recordType2Ts = (n) => "{" + (0, exports.memberDeclarations2TS)(n.members) + "}";
|
|
518
309
|
exports.recordType2Ts = recordType2Ts;
|
|
519
310
|
/**
|
|
520
311
|
* memberDeclarations2TS converts a list of MemberDeclarations to TypeScript.
|
|
@@ -530,7 +321,7 @@ exports.memberDeclarations2TS = memberDeclarations2TS;
|
|
|
530
321
|
* This works recursively and any RecordTypes encountered will be flattened.
|
|
531
322
|
*/
|
|
532
323
|
const typeMapFromMemberDecs = (list) => list.reduce((p, m) => {
|
|
533
|
-
let paths = m.path.map(p => p.value);
|
|
324
|
+
let paths = m.path.map((p) => p.value);
|
|
534
325
|
if (m.kind instanceof ast.RecordType) {
|
|
535
326
|
return (0, exports.typeMapFromRecordType)(m.kind, p, paths);
|
|
536
327
|
}
|
|
@@ -549,7 +340,7 @@ exports.typeMapFromMemberDecs = typeMapFromMemberDecs;
|
|
|
549
340
|
* Any encountered RecordTypes will be flattened.
|
|
550
341
|
*/
|
|
551
342
|
const typeMapFromRecordType = (n, init, prefix) => n.members.reduce((p, m) => {
|
|
552
|
-
let path = [...prefix, ...
|
|
343
|
+
let path = [...prefix, ...m.path.map((p) => p.value)];
|
|
553
344
|
if (m.kind instanceof ast.RecordType) {
|
|
554
345
|
return (0, exports.typeMapFromRecordType)(m.kind, init, path);
|
|
555
346
|
}
|
|
@@ -559,7 +350,7 @@ const typeMapFromRecordType = (n, init, prefix) => n.members.reduce((p, m) => {
|
|
|
559
350
|
}
|
|
560
351
|
}, init);
|
|
561
352
|
exports.typeMapFromRecordType = typeMapFromRecordType;
|
|
562
|
-
const paths2String = (paths) => paths.join(
|
|
353
|
+
const paths2String = (paths) => paths.join(".");
|
|
563
354
|
/**
|
|
564
355
|
* expandTypeMap to an ExpandedTypeMap.
|
|
565
356
|
*/
|
|
@@ -576,14 +367,14 @@ const typeMap2TS = (m) => (0, record_1.mapTo)(m, (t, k) => {
|
|
|
576
367
|
else {
|
|
577
368
|
return `${k} : ${t}`;
|
|
578
369
|
}
|
|
579
|
-
}).join(
|
|
370
|
+
}).join(",\n");
|
|
580
371
|
exports.typeMap2TS = typeMap2TS;
|
|
581
|
-
const isOptional = (m) => (0, record_1.reduce)(m, false, (p, _, k) => p ? p : k.indexOf(
|
|
372
|
+
const isOptional = (m) => (0, record_1.reduce)(m, false, (p, _, k) => (p ? p : k.indexOf("?") > -1));
|
|
582
373
|
/**
|
|
583
374
|
* parameters2TS converts a list Parameter nodes into an parameter list
|
|
584
375
|
* (without parens).
|
|
585
376
|
*/
|
|
586
|
-
const parameters2TS = (list) => list.map(p => (0, exports.parameter2TS)(p)).join(
|
|
377
|
+
const parameters2TS = (list) => list.map((p) => (0, exports.parameter2TS)(p)).join(",");
|
|
587
378
|
exports.parameters2TS = parameters2TS;
|
|
588
379
|
/**
|
|
589
380
|
* parameter2TS
|
|
@@ -594,7 +385,7 @@ const parameter2TS = (n) => {
|
|
|
594
385
|
else if (n instanceof ast.UntypedParameter)
|
|
595
386
|
return (0, exports.untypedParameter2TS)(n);
|
|
596
387
|
else
|
|
597
|
-
return
|
|
388
|
+
return "";
|
|
598
389
|
};
|
|
599
390
|
exports.parameter2TS = parameter2TS;
|
|
600
391
|
/**
|
|
@@ -611,14 +402,14 @@ exports.untypedParameter2TS = untypedParameter2TS;
|
|
|
611
402
|
* children2TS
|
|
612
403
|
*/
|
|
613
404
|
const children2TS = (ctx, list) => `[${ctx.options.EOL}
|
|
614
|
-
${list.map(l => (0, exports.child2TS)(ctx, l)).join(
|
|
405
|
+
${list.map((l) => (0, exports.child2TS)(ctx, l)).join("," + ctx.options.EOL)}
|
|
615
406
|
]`;
|
|
616
407
|
exports.children2TS = children2TS;
|
|
617
408
|
/**
|
|
618
409
|
* child2TS converts children to typescript.
|
|
619
410
|
*/
|
|
620
411
|
const child2TS = (ctx, n) => {
|
|
621
|
-
if (
|
|
412
|
+
if (n instanceof ast.Node || n instanceof ast.Widget)
|
|
622
413
|
return (0, exports.tag2TS)(ctx, n);
|
|
623
414
|
else if (n instanceof ast.Interpolation)
|
|
624
415
|
return (0, exports.interpolation2TS)(ctx, n);
|
|
@@ -643,13 +434,13 @@ const child2TS = (ctx, n) => {
|
|
|
643
434
|
else if (n instanceof ast.QualifiedIdentifier)
|
|
644
435
|
return (0, exports.qualifiedIdentifier2TS)(n);
|
|
645
436
|
else
|
|
646
|
-
return
|
|
437
|
+
return "";
|
|
647
438
|
};
|
|
648
439
|
exports.child2TS = child2TS;
|
|
649
440
|
/**
|
|
650
441
|
* tag2TS converts a tag to typescript.
|
|
651
442
|
*/
|
|
652
|
-
const tag2TS = (ctx, n) =>
|
|
443
|
+
const tag2TS = (ctx, n) => n instanceof ast.Widget ? (0, exports.widget2TS)(ctx, n) : (0, exports.node2TS)(ctx, n);
|
|
653
444
|
exports.tag2TS = tag2TS;
|
|
654
445
|
/**
|
|
655
446
|
* widget2TS converts a Widget node into its typescript representation.
|
|
@@ -661,8 +452,8 @@ const widget2TS = (ctx, n) => {
|
|
|
661
452
|
let typeParams = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
662
453
|
let attrs = (0, exports.attrs2String)(ctx, n.attributes);
|
|
663
454
|
let childs = (0, exports.children2TS)(ctx, n.children);
|
|
664
|
-
return `${exports.THIS}.widget(new ${name}${typeParams}(${attrs}, ${childs}),` +
|
|
665
|
-
`<${exports.WML}.Attrs>${attrs})
|
|
455
|
+
return (`${exports.THIS}.widget(new ${name}${typeParams}(${attrs}, ${childs}),` +
|
|
456
|
+
`<${exports.WML}.Attrs>${attrs})`);
|
|
666
457
|
};
|
|
667
458
|
exports.widget2TS = widget2TS;
|
|
668
459
|
/**
|
|
@@ -685,9 +476,9 @@ exports.attribute2TS = attribute2TS;
|
|
|
685
476
|
/**
|
|
686
477
|
* attributeValue2TS
|
|
687
478
|
*/
|
|
688
|
-
const attributeValue2TS = (ctx, n) =>
|
|
689
|
-
(0, exports.interpolation2TS)(ctx, n.value)
|
|
690
|
-
(0, exports.literal2TS)(ctx, n.value);
|
|
479
|
+
const attributeValue2TS = (ctx, n) => n.value instanceof ast.Interpolation
|
|
480
|
+
? (0, exports.interpolation2TS)(ctx, n.value)
|
|
481
|
+
: (0, exports.literal2TS)(ctx, n.value);
|
|
691
482
|
exports.attributeValue2TS = attributeValue2TS;
|
|
692
483
|
/**
|
|
693
484
|
* attributeName2TS
|
|
@@ -699,24 +490,23 @@ exports.attributeName2TS = attributeName2TS;
|
|
|
699
490
|
*/
|
|
700
491
|
const attrs2String = (ctx, attrs) => {
|
|
701
492
|
// Check for the special wml:attrs attribute.
|
|
702
|
-
let mallAttrs = (0, array_1.find)(attrs, attr =>
|
|
703
|
-
(attr.name.value === 'attrs')));
|
|
493
|
+
let mallAttrs = (0, array_1.find)(attrs, (attr) => attr.namespace.value === "wml" && attr.name.value === "attrs");
|
|
704
494
|
if (mallAttrs.isJust())
|
|
705
495
|
return (0, exports.attributeValue2TS)(ctx, mallAttrs.get());
|
|
706
|
-
let [nns, ns] = (0, array_1.partition)(attrs, a =>
|
|
496
|
+
let [nns, ns] = (0, array_1.partition)(attrs, (a) => a.namespace.value === "");
|
|
707
497
|
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))
|
|
498
|
+
[n.namespace.value]: (p[n.namespace.value] || []).concat((0, exports.attribute2TS)(ctx, n)),
|
|
709
499
|
}), {});
|
|
710
500
|
return _attrs2String(nns.reduce((p, n) => (0, record_1.merge)(p, {
|
|
711
|
-
[(0, exports.attributeName2TS)(ctx, n)]: (0, exports.attributeValue2TS)(ctx, n)
|
|
501
|
+
[(0, exports.attributeName2TS)(ctx, n)]: (0, exports.attributeValue2TS)(ctx, n),
|
|
712
502
|
}), nso));
|
|
713
503
|
};
|
|
714
504
|
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
|
-
|
|
505
|
+
const _attrs2String = (attrs) => "{" +
|
|
506
|
+
Object.keys(attrs).map((name) => Array.isArray(attrs[name])
|
|
507
|
+
? `${name} : { ${attrs[name].join(",")} }`
|
|
508
|
+
: `${name}: ${attrs[name]}`) +
|
|
509
|
+
"}";
|
|
720
510
|
/**
|
|
721
511
|
* interpolation2TS
|
|
722
512
|
*/
|
|
@@ -728,15 +518,15 @@ exports.interpolation2TS = interpolation2TS;
|
|
|
728
518
|
const ifStatement2TS = (ctx, n) => {
|
|
729
519
|
let condition = (0, exports.expression2TS)(ctx, n.condition);
|
|
730
520
|
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
|
-
|
|
521
|
+
let alt = n.elseClause instanceof ast.ElseIfClause
|
|
522
|
+
? `[${(0, exports.ifStatement2TS)(ctx, n.elseClause)}]`
|
|
523
|
+
: n.elseClause instanceof ast.ElseClause
|
|
524
|
+
? (0, exports.children2TS)(ctx, n.elseClause.children)
|
|
525
|
+
: "[]";
|
|
736
526
|
return [
|
|
737
527
|
`...((${condition}) ?`,
|
|
738
528
|
`(()=>(${conseq}))() :`,
|
|
739
|
-
`(()=>(${alt}))())
|
|
529
|
+
`(()=>(${alt}))())`,
|
|
740
530
|
].join(ctx.options.EOL);
|
|
741
531
|
};
|
|
742
532
|
exports.ifStatement2TS = ifStatement2TS;
|
|
@@ -746,14 +536,14 @@ exports.ifStatement2TS = ifStatement2TS;
|
|
|
746
536
|
const forInStatement2TS = (ctx, n) => {
|
|
747
537
|
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
748
538
|
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]) :
|
|
539
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : "_$$i";
|
|
540
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : "_$$all";
|
|
751
541
|
let body = (0, exports.children2TS)(ctx, n.body);
|
|
752
|
-
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) :
|
|
542
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : "[]";
|
|
753
543
|
return [
|
|
754
544
|
`...${FOR_IN} (${expr}, (${value}, ${key}, ${all})=> `,
|
|
755
545
|
`(${body}), `,
|
|
756
|
-
`()=> (${alt}))
|
|
546
|
+
`()=> (${alt}))`,
|
|
757
547
|
].join(ctx.options.EOL);
|
|
758
548
|
};
|
|
759
549
|
exports.forInStatement2TS = forInStatement2TS;
|
|
@@ -763,14 +553,14 @@ exports.forInStatement2TS = forInStatement2TS;
|
|
|
763
553
|
const forOfStatement2TS = (ctx, n) => {
|
|
764
554
|
let expr = (0, exports.expression2TS)(ctx, n.expression);
|
|
765
555
|
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]) :
|
|
556
|
+
let key = n.variables.length > 1 ? (0, exports.parameter2TS)(n.variables[1]) : "_$$k";
|
|
557
|
+
let all = n.variables.length > 2 ? (0, exports.parameter2TS)(n.variables[2]) : "_$$all";
|
|
768
558
|
let body = (0, exports.children2TS)(ctx, n.body);
|
|
769
|
-
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) :
|
|
559
|
+
let alt = n.otherwise.length > 0 ? (0, exports.children2TS)(ctx, n.otherwise) : "[]";
|
|
770
560
|
return [
|
|
771
561
|
`...${FOR_OF} (${expr}, (${value}, ${key}, ${all}) => `,
|
|
772
562
|
` (${body}), `,
|
|
773
|
-
` ()=> (${alt}))
|
|
563
|
+
` ()=> (${alt}))`,
|
|
774
564
|
].join(eol(ctx));
|
|
775
565
|
};
|
|
776
566
|
exports.forOfStatement2TS = forOfStatement2TS;
|
|
@@ -790,7 +580,7 @@ const forFromStatement2TS = (ctx, node) => {
|
|
|
790
580
|
` ...${body}`,
|
|
791
581
|
` );`,
|
|
792
582
|
` return result;`,
|
|
793
|
-
`})()
|
|
583
|
+
`})()`,
|
|
794
584
|
].join(eol(ctx));
|
|
795
585
|
};
|
|
796
586
|
exports.forFromStatement2TS = forFromStatement2TS;
|
|
@@ -799,7 +589,7 @@ exports.forFromStatement2TS = forFromStatement2TS;
|
|
|
799
589
|
*/
|
|
800
590
|
const characters2TS = (n) => `${exports.DOCUMENT}.createTextNode('${breakLines(n.value)}')`;
|
|
801
591
|
exports.characters2TS = characters2TS;
|
|
802
|
-
const breakLines = (str) => str.split(
|
|
592
|
+
const breakLines = (str) => str.split("\n").join("\\u000a");
|
|
803
593
|
/**
|
|
804
594
|
* expression2TS
|
|
805
595
|
*/
|
|
@@ -845,7 +635,7 @@ const expression2TS = (ctx, n) => {
|
|
|
845
635
|
else if (n instanceof ast.ContextVariable)
|
|
846
636
|
return (0, exports.contextVariable2TS)(n);
|
|
847
637
|
else
|
|
848
|
-
return
|
|
638
|
+
return "";
|
|
849
639
|
};
|
|
850
640
|
exports.expression2TS = expression2TS;
|
|
851
641
|
/**
|
|
@@ -863,13 +653,15 @@ exports.ifThenExpression2TS = ifThenExpression2TS;
|
|
|
863
653
|
*/
|
|
864
654
|
const binaryExpression2TS = (ctx, n) => {
|
|
865
655
|
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
|
|
656
|
+
let right = n.operator === "as"
|
|
657
|
+
? (0, exports.type2TS)(n.right)
|
|
658
|
+
: (0, exports.expression2TS)(ctx, n.right);
|
|
659
|
+
let op = operators.hasOwnProperty(n.operator)
|
|
660
|
+
? operators[n.operator]
|
|
661
|
+
: n.operator;
|
|
662
|
+
return n.operator === "as"
|
|
663
|
+
? `<${right}>(${left})`
|
|
664
|
+
: `(${left} ${op} ${right})`;
|
|
873
665
|
};
|
|
874
666
|
exports.binaryExpression2TS = binaryExpression2TS;
|
|
875
667
|
/**
|
|
@@ -883,7 +675,7 @@ exports.unaryExpression2TS = unaryExpression2TS;
|
|
|
883
675
|
/**
|
|
884
676
|
* viewConstruction2TS
|
|
885
677
|
*/
|
|
886
|
-
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.
|
|
678
|
+
const viewConstruction2TS = (ctx, n) => `${exports.THIS}.view(${(0, exports.expression2TS)(ctx, n.expression)})`;
|
|
887
679
|
exports.viewConstruction2TS = viewConstruction2TS;
|
|
888
680
|
/**
|
|
889
681
|
* funApplication2TS
|
|
@@ -899,9 +691,9 @@ const constructExpression2TS = (ctx, n) => {
|
|
|
899
691
|
let consOriginal = `${cons[0].toUpperCase()}${cons.slice(1)}`;
|
|
900
692
|
let typeArgs = (0, exports.typeArgs2TS)(n.typeArgs);
|
|
901
693
|
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})`;
|
|
694
|
+
return (0, array_1.contains)(casters, consOriginal)
|
|
695
|
+
? `${consOriginal}${typeArgs}(${args})`
|
|
696
|
+
: `new ${cons}${typeArgs}(${args})`;
|
|
905
697
|
};
|
|
906
698
|
exports.constructExpression2TS = constructExpression2TS;
|
|
907
699
|
/**
|
|
@@ -917,28 +709,28 @@ exports.callExpression2TS = callExpression2TS;
|
|
|
917
709
|
/**
|
|
918
710
|
* typeArgs2TS
|
|
919
711
|
*/
|
|
920
|
-
const typeArgs2TS = (ns) => (0, array_1.empty)(ns) ?
|
|
712
|
+
const typeArgs2TS = (ns) => (0, array_1.empty)(ns) ? "" : `<${ns.map(exports.type2TS).join(",")}>`;
|
|
921
713
|
exports.typeArgs2TS = typeArgs2TS;
|
|
922
714
|
/**
|
|
923
715
|
* args2TS converts a list of arguments to a typescript argument tupple.
|
|
924
716
|
*/
|
|
925
|
-
const args2TS = (ctx, ns) =>
|
|
717
|
+
const args2TS = (ctx, ns) => ns.length === 0 ? "" : ns.map((e) => (0, exports.expression2TS)(ctx, e)).join(",");
|
|
926
718
|
exports.args2TS = args2TS;
|
|
927
719
|
/**
|
|
928
720
|
* memberExpression2TS
|
|
929
721
|
*/
|
|
930
722
|
const memberExpression2TS = (ctx, n) => {
|
|
931
723
|
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)}`;
|
|
724
|
+
return n.tail instanceof ast.StringLiteral
|
|
725
|
+
? `${target}[${(0, exports.string2TS)(n.tail)}]`
|
|
726
|
+
: `${target}.${(0, exports.expression2TS)(ctx, n.tail)}`;
|
|
935
727
|
};
|
|
936
728
|
exports.memberExpression2TS = memberExpression2TS;
|
|
937
729
|
/**
|
|
938
730
|
* functionExpression2TS
|
|
939
731
|
*/
|
|
940
732
|
const functionExpression2TS = (ctx, n) => {
|
|
941
|
-
let params = n.parameters.map(exports.parameter2TS).join(
|
|
733
|
+
let params = n.parameters.map(exports.parameter2TS).join(",");
|
|
942
734
|
let body = (0, exports.expression2TS)(ctx, n.body);
|
|
943
735
|
return `(${params}) => ${body}`;
|
|
944
736
|
};
|
|
@@ -958,7 +750,7 @@ const literal2TS = (ctx, n) => {
|
|
|
958
750
|
else if (n instanceof ast.List)
|
|
959
751
|
return (0, exports.list2TS)(ctx, n);
|
|
960
752
|
else
|
|
961
|
-
return
|
|
753
|
+
return "";
|
|
962
754
|
};
|
|
963
755
|
exports.literal2TS = literal2TS;
|
|
964
756
|
/**
|
|
@@ -969,7 +761,10 @@ exports.boolean2TS = boolean2TS;
|
|
|
969
761
|
/**
|
|
970
762
|
* string2TS
|
|
971
763
|
*/
|
|
972
|
-
const string2TS = (n) => n.value
|
|
764
|
+
const string2TS = (n) => n.value
|
|
765
|
+
.split("\n")
|
|
766
|
+
.map((str) => `"${str}"`)
|
|
767
|
+
.join("+");
|
|
973
768
|
exports.string2TS = string2TS;
|
|
974
769
|
/**
|
|
975
770
|
* number2TS
|
|
@@ -980,16 +775,16 @@ exports.number2TS = number2TS;
|
|
|
980
775
|
* record2TS
|
|
981
776
|
*/
|
|
982
777
|
const record2TS = (ctx, n) => `{${ctx.options.EOL}
|
|
983
|
-
${n.properties.map(p => (0, exports.property2TS)(ctx, p)).join(
|
|
778
|
+
${n.properties.map((p) => (0, exports.property2TS)(ctx, p)).join("," + ctx.options.EOL)}
|
|
984
779
|
}`;
|
|
985
780
|
exports.record2TS = record2TS;
|
|
986
781
|
/**
|
|
987
782
|
* list2TS
|
|
988
783
|
*/
|
|
989
784
|
const list2TS = (ctx, n) => {
|
|
990
|
-
let mems = n.members.map(e => (0, exports.expression2TS)(ctx, e));
|
|
785
|
+
let mems = n.members.map((e) => (0, exports.expression2TS)(ctx, e));
|
|
991
786
|
return `[${ctx.options.EOL}
|
|
992
|
-
${mems.join(
|
|
787
|
+
${mems.join("," + ctx.options.EOL)}
|
|
993
788
|
]`;
|
|
994
789
|
};
|
|
995
790
|
exports.list2TS = list2TS;
|
|
@@ -1001,15 +796,15 @@ exports.property2TS = property2TS;
|
|
|
1001
796
|
/**
|
|
1002
797
|
* key2TS
|
|
1003
798
|
*/
|
|
1004
|
-
const key2TS = (n) =>
|
|
799
|
+
const key2TS = (n) => n instanceof ast.StringLiteral ? (0, exports.string2TS)(n) : (0, exports.identifier2TS)(n);
|
|
1005
800
|
exports.key2TS = key2TS;
|
|
1006
801
|
/**
|
|
1007
802
|
* contextProperty2TS
|
|
1008
803
|
*/
|
|
1009
804
|
const contextProperty2TS = (n) => {
|
|
1010
|
-
let member =
|
|
1011
|
-
n.member.value
|
|
1012
|
-
(0, exports.identifier2TS)(n.member);
|
|
805
|
+
let member = n.member instanceof ast.StringLiteral
|
|
806
|
+
? n.member.value
|
|
807
|
+
: (0, exports.identifier2TS)(n.member);
|
|
1013
808
|
return `${exports.CONTEXT}.${member}`;
|
|
1014
809
|
};
|
|
1015
810
|
exports.contextProperty2TS = contextProperty2TS;
|
|
@@ -1022,14 +817,14 @@ exports.contextVariable2TS = contextVariable2TS;
|
|
|
1022
817
|
* identifierOrConstructor2TS
|
|
1023
818
|
*/
|
|
1024
819
|
const identifierOrConstructor2TS = (n) => {
|
|
1025
|
-
if (
|
|
1026
|
-
|
|
820
|
+
if (n instanceof ast.UnqualifiedIdentifier ||
|
|
821
|
+
n instanceof ast.QualifiedIdentifier)
|
|
1027
822
|
return (0, exports.identifier2TS)(n);
|
|
1028
|
-
else if (
|
|
1029
|
-
|
|
823
|
+
else if (n instanceof ast.UnqualifiedConstructor ||
|
|
824
|
+
n instanceof ast.QualifiedConstructor)
|
|
1030
825
|
return (0, exports.constructor2TS)(n);
|
|
1031
826
|
else
|
|
1032
|
-
return
|
|
827
|
+
return "";
|
|
1033
828
|
};
|
|
1034
829
|
exports.identifierOrConstructor2TS = identifierOrConstructor2TS;
|
|
1035
830
|
/**
|
|
@@ -1041,7 +836,7 @@ const constructor2TS = (n) => {
|
|
|
1041
836
|
else if (n instanceof ast.UnqualifiedConstructor)
|
|
1042
837
|
return (0, exports.unqualifiedConstructor2TS)(n);
|
|
1043
838
|
else
|
|
1044
|
-
return
|
|
839
|
+
return "";
|
|
1045
840
|
};
|
|
1046
841
|
exports.constructor2TS = constructor2TS;
|
|
1047
842
|
/**
|
|
@@ -1063,7 +858,7 @@ const identifier2TS = (n) => {
|
|
|
1063
858
|
else if (n instanceof ast.UnqualifiedIdentifier)
|
|
1064
859
|
return (0, exports.unqualifiedIdentifier2TS)(n);
|
|
1065
860
|
else
|
|
1066
|
-
return
|
|
861
|
+
return "";
|
|
1067
862
|
};
|
|
1068
863
|
exports.identifier2TS = identifier2TS;
|
|
1069
864
|
/**
|
|
@@ -1076,5 +871,5 @@ exports.qualifiedIdentifier2TS = qualifiedIdentifier2TS;
|
|
|
1076
871
|
*/
|
|
1077
872
|
const unqualifiedIdentifier2TS = (n) => `${toPrim(n.value)}`;
|
|
1078
873
|
exports.unqualifiedIdentifier2TS = unqualifiedIdentifier2TS;
|
|
1079
|
-
const toPrim = (id) => prims.indexOf(id) > -1 ? id.toLowerCase() : id;
|
|
874
|
+
const toPrim = (id) => (prims.indexOf(id) > -1 ? id.toLowerCase() : id);
|
|
1080
875
|
//# sourceMappingURL=codegen.js.map
|