@canvasengine/compiler 2.0.0-beta.6 → 2.0.0-beta.7

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 CHANGED
@@ -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}, (${variableName}) => ${content})`;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canvasengine/compiler",
3
- "version": "2.0.0-beta.6",
3
+ "version": "2.0.0-beta.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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,(sprite)=>h(Sprite)))`.replace(/\s+/g, "")
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,(sprite)=>h(Sprite))`.replace(/\s+/g, "")
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,(sprite)=>loop(others,(other)=>h(Sprite)))`.replace(
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,(sprite)=>cond(sprite.visible,()=>h(Sprite))))`.replace(/\s+/g, "")
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,(sprite)=>h(Sprite)))`.replace(/\s+/g, "")
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,(sprite)=>h(Sprite)),loop(others,(other)=>h(Sprite))])`.replace(/\s+/g, "")
411
+ `h(Canvas,null,[loop(sprites,sprite=>h(Sprite)),loop(others,other=>h(Sprite))])`.replace(/\s+/g, "")
400
412
  );
401
413
  });
402
414