@canvasengine/compiler 2.0.0-beta.6 → 2.0.0-beta.8
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/grammar.pegjs +8 -3
- package/package.json +1 -1
- package/tests/compiler.spec.ts +24 -12
package/grammar.pegjs
CHANGED
|
@@ -138,7 +138,7 @@ eventAttribute
|
|
|
138
138
|
staticValue
|
|
139
139
|
= [^"]+ {
|
|
140
140
|
var val = text();
|
|
141
|
-
return
|
|
141
|
+
return `'${val}'`
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
content
|
|
@@ -162,8 +162,13 @@ textElement
|
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
forLoop
|
|
165
|
-
= _ "@for" _ "(" _ variableName:identifier _ "of" _ iterable:identifier _ ")" _ "{" _ content:content _ "}" _ {
|
|
166
|
-
return `loop(${iterable},
|
|
165
|
+
= _ "@for" _ "(" _ variableName:(tupleDestructuring / identifier) _ "of" _ iterable:identifier _ ")" _ "{" _ content:content _ "}" _ {
|
|
166
|
+
return `loop(${iterable}, ${variableName} => ${content})`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
tupleDestructuring
|
|
170
|
+
= "(" _ first:identifier _ "," _ second:identifier _ ")" {
|
|
171
|
+
return `(${first}, ${second})`;
|
|
167
172
|
}
|
|
168
173
|
|
|
169
174
|
ifCondition
|
package/package.json
CHANGED
package/tests/compiler.spec.ts
CHANGED
|
@@ -123,18 +123,18 @@ describe("Compiler", () => {
|
|
|
123
123
|
expect(output).toBe(`h(Canvas, { width: computed(() => x() * 2 * y()) })`);
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
test("should compile component with static numeric attribute", () => {
|
|
127
|
-
const input = `<Canvas width="10" />`;
|
|
128
|
-
const output = parser.parse(input);
|
|
129
|
-
expect(output).toBe(`h(Canvas, { width: 10 })`);
|
|
130
|
-
});
|
|
131
|
-
|
|
132
126
|
test("should compile component with static string attribute", () => {
|
|
133
127
|
const input = `<Canvas width="val" />`;
|
|
134
128
|
const output = parser.parse(input);
|
|
135
129
|
expect(output).toBe(`h(Canvas, { width: 'val' })`);
|
|
136
130
|
});
|
|
137
131
|
|
|
132
|
+
test("should compile component with static attribute (with number)", () => {
|
|
133
|
+
const input = `<Canvas width="10" />`;
|
|
134
|
+
const output = parser.parse(input);
|
|
135
|
+
expect(output).toBe(`h(Canvas, { width: '10' })`);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
138
|
test("should compile component with children", () => {
|
|
139
139
|
const input = `
|
|
140
140
|
<Canvas>
|
|
@@ -236,7 +236,7 @@ describe("Loop", () => {
|
|
|
236
236
|
`;
|
|
237
237
|
const output = parser.parse(input);
|
|
238
238
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
239
|
-
`h(Canvas,null,loop(sprites,
|
|
239
|
+
`h(Canvas,null,loop(sprites,sprite=>h(Sprite)))`.replace(/\s+/g, "")
|
|
240
240
|
);
|
|
241
241
|
});
|
|
242
242
|
|
|
@@ -248,7 +248,19 @@ describe("Loop", () => {
|
|
|
248
248
|
`;
|
|
249
249
|
const output = parser.parse(input);
|
|
250
250
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
251
|
-
`loop(sprites,
|
|
251
|
+
`loop(sprites,sprite=>h(Sprite))`.replace(/\s+/g, "")
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("should compile loop with destructuring", () => {
|
|
256
|
+
const input = `
|
|
257
|
+
@for ((sprite, index) of sprites) {
|
|
258
|
+
<Sprite key={index} />
|
|
259
|
+
}
|
|
260
|
+
`;
|
|
261
|
+
const output = parser.parse(input);
|
|
262
|
+
expect(output.replace(/\s+/g, "")).toBe(
|
|
263
|
+
`loop(sprites,(sprite,index)=>h(Sprite, { key: index }))`.replace(/\s+/g, "")
|
|
252
264
|
);
|
|
253
265
|
});
|
|
254
266
|
|
|
@@ -262,7 +274,7 @@ describe("Loop", () => {
|
|
|
262
274
|
`;
|
|
263
275
|
const output = parser.parse(input);
|
|
264
276
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
265
|
-
`loop(sprites,
|
|
277
|
+
`loop(sprites,sprite=>loop(others,other=>h(Sprite)))`.replace(
|
|
266
278
|
/\s+/g,
|
|
267
279
|
""
|
|
268
280
|
)
|
|
@@ -365,7 +377,7 @@ describe("Condition in Loops", () => {
|
|
|
365
377
|
`;
|
|
366
378
|
const output = parser.parse(input);
|
|
367
379
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
368
|
-
`h(Canvas,null,loop(sprites,
|
|
380
|
+
`h(Canvas,null,loop(sprites,sprite=>cond(sprite.visible,()=>h(Sprite))))`.replace(/\s+/g, "")
|
|
369
381
|
);
|
|
370
382
|
});
|
|
371
383
|
|
|
@@ -379,7 +391,7 @@ describe("Condition in Loops", () => {
|
|
|
379
391
|
`;
|
|
380
392
|
const output = parser.parse(input);
|
|
381
393
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
382
|
-
`h(Canvas,null,loop(sprites,
|
|
394
|
+
`h(Canvas,null,loop(sprites,sprite=>h(Sprite)))`.replace(/\s+/g, "")
|
|
383
395
|
);
|
|
384
396
|
});
|
|
385
397
|
|
|
@@ -396,7 +408,7 @@ describe("Condition in Loops", () => {
|
|
|
396
408
|
`;
|
|
397
409
|
const output = parser.parse(input);
|
|
398
410
|
expect(output.replace(/\s+/g, "")).toBe(
|
|
399
|
-
`h(Canvas,null,[loop(sprites,
|
|
411
|
+
`h(Canvas,null,[loop(sprites,sprite=>h(Sprite)),loop(others,other=>h(Sprite))])`.replace(/\s+/g, "")
|
|
400
412
|
);
|
|
401
413
|
});
|
|
402
414
|
|