@dacely/toildefender 0.1.6 → 0.1.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.
@@ -135,7 +135,7 @@ function withFinalizerBefore(node, finalizer) {
135
135
  declarations: [
136
136
  {
137
137
  type: "VariableDeclarator",
138
- id: { type: "Identifier", name: "veilmark$return" },
138
+ id: { type: "Identifier", name: "toildefender$return" },
139
139
  init: node.argument
140
140
  }
141
141
  ]
@@ -143,7 +143,7 @@ function withFinalizerBefore(node, finalizer) {
143
143
  body.push(utils.cloneISwearIKnowWhatImDoing(finalizer));
144
144
  body.push({
145
145
  type: "ReturnStatement",
146
- argument: { type: "Identifier", name: "veilmark$return" }
146
+ argument: { type: "Identifier", name: "toildefender$return" }
147
147
  });
148
148
  } else {
149
149
  body.push(utils.cloneISwearIKnowWhatImDoing(finalizer));
@@ -284,6 +284,29 @@ function hasObjectRest(pattern) {
284
284
  return pattern.type == "ObjectPattern" && pattern.properties.some(prop => prop.type == "RestElement");
285
285
  }
286
286
 
287
+ function hasObjectPattern(pattern) {
288
+ return pattern.type == "ObjectPattern";
289
+ }
290
+
291
+ function hasArrayPattern(pattern) {
292
+ return pattern.type == "ArrayPattern";
293
+ }
294
+
295
+ function canLowerArrayPattern(pattern) {
296
+ return pattern.type == "ArrayPattern" && pattern.elements.every(element => {
297
+ if (element == null) {
298
+ return true;
299
+ }
300
+ if (element.type == "Identifier") {
301
+ return true;
302
+ }
303
+ if (element.type == "RestElement") {
304
+ return element.argument.type == "Identifier";
305
+ }
306
+ return element.type == "AssignmentPattern" && element.left.type == "Identifier";
307
+ });
308
+ }
309
+
287
310
  function canLowerObjectRest(pattern) {
288
311
  return pattern.type == "ObjectPattern" && pattern.properties.every(prop => {
289
312
  if (prop.type == "RestElement") {
@@ -319,7 +342,7 @@ function objectAssignCall(parts) {
319
342
  function objectWithoutKeysCall(source, keys) {
320
343
  return {
321
344
  type: "CallExpression",
322
- callee: { type: "Identifier", name: "veilmark$objectWithoutKeys" },
345
+ callee: { type: "Identifier", name: "toildefender$objectWithoutKeys" },
323
346
  arguments: [
324
347
  source,
325
348
  {
@@ -330,6 +353,162 @@ function objectWithoutKeysCall(source, keys) {
330
353
  };
331
354
  }
332
355
 
356
+ function arrayElementExpression(sourceName, index) {
357
+ return {
358
+ type: "MemberExpression",
359
+ object: { type: "Identifier", name: sourceName },
360
+ property: { type: "Literal", value: index },
361
+ computed: true
362
+ };
363
+ }
364
+
365
+ function arrayRestExpression(sourceName, index) {
366
+ return {
367
+ type: "CallExpression",
368
+ callee: {
369
+ type: "MemberExpression",
370
+ object: { type: "Identifier", name: sourceName },
371
+ property: { type: "Identifier", name: "slice" },
372
+ computed: false
373
+ },
374
+ arguments: [
375
+ { type: "Literal", value: index }
376
+ ]
377
+ };
378
+ }
379
+
380
+ function arrayPatternElementDeclaration(kind, sourceName, element, index) {
381
+ if (element == null) {
382
+ return null;
383
+ }
384
+
385
+ if (element.type == "RestElement") {
386
+ return {
387
+ type: "VariableDeclaration",
388
+ kind: kind,
389
+ declarations: [
390
+ {
391
+ type: "VariableDeclarator",
392
+ id: element.argument,
393
+ init: arrayRestExpression(sourceName, index)
394
+ }
395
+ ]
396
+ };
397
+ }
398
+
399
+ var id = element;
400
+ var init = arrayElementExpression(sourceName, index);
401
+ if (element.type == "AssignmentPattern") {
402
+ id = element.left;
403
+ init = {
404
+ type: "ConditionalExpression",
405
+ test: {
406
+ type: "BinaryExpression",
407
+ operator: "===",
408
+ left: arrayElementExpression(sourceName, index),
409
+ right: { type: "Identifier", name: "undefined" }
410
+ },
411
+ consequent: element.right,
412
+ alternate: arrayElementExpression(sourceName, index)
413
+ };
414
+ }
415
+
416
+ return {
417
+ type: "VariableDeclaration",
418
+ kind: kind,
419
+ declarations: [
420
+ {
421
+ type: "VariableDeclarator",
422
+ id: id,
423
+ init: init
424
+ }
425
+ ]
426
+ };
427
+ }
428
+
429
+ function arrayPatternStatements(kind, pattern, init, rngAlpha) {
430
+ var sourceName = `$$destructure$arr$${rngAlpha.get()}`;
431
+ var statements = [
432
+ {
433
+ type: "VariableDeclaration",
434
+ kind: "var",
435
+ declarations: [
436
+ {
437
+ type: "VariableDeclarator",
438
+ id: { type: "Identifier", name: sourceName },
439
+ init: init || { type: "ArrayExpression", elements: [] }
440
+ }
441
+ ]
442
+ }
443
+ ];
444
+
445
+ pattern.elements.forEach((element, index) => {
446
+ var lowered = arrayPatternElementDeclaration(kind, sourceName, element, index);
447
+ if (lowered) {
448
+ statements.push(lowered);
449
+ }
450
+ });
451
+
452
+ return statements;
453
+ }
454
+
455
+ function arrayPatternAssignmentStatement(sourceName, element, index) {
456
+ if (element == null) {
457
+ return null;
458
+ }
459
+
460
+ var left;
461
+ var right;
462
+ if (element.type == "RestElement") {
463
+ left = element.argument;
464
+ right = arrayRestExpression(sourceName, index);
465
+ } else if (element.type == "AssignmentPattern") {
466
+ left = element.left;
467
+ right = {
468
+ type: "ConditionalExpression",
469
+ test: {
470
+ type: "BinaryExpression",
471
+ operator: "===",
472
+ left: arrayElementExpression(sourceName, index),
473
+ right: { type: "Identifier", name: "undefined" }
474
+ },
475
+ consequent: element.right,
476
+ alternate: arrayElementExpression(sourceName, index)
477
+ };
478
+ } else {
479
+ left = element;
480
+ right = arrayElementExpression(sourceName, index);
481
+ }
482
+
483
+ return assignmentStatement(left, right);
484
+ }
485
+
486
+ function arrayPatternAssignmentStatements(pattern, init, rngAlpha) {
487
+ var sourceName = `$$destructure$arr$${rngAlpha.get()}`;
488
+ var statements = [
489
+ {
490
+ type: "VariableDeclaration",
491
+ kind: "var",
492
+ declarations: [
493
+ {
494
+ type: "VariableDeclarator",
495
+ id: { type: "Identifier", name: sourceName },
496
+ init: init || { type: "ArrayExpression", elements: [] }
497
+ }
498
+ ]
499
+ }
500
+ ];
501
+
502
+ pattern.elements.forEach((element, index) => {
503
+ var lowered = arrayPatternAssignmentStatement(sourceName, element, index);
504
+ if (lowered) {
505
+ statements.push(lowered);
506
+ }
507
+ });
508
+
509
+ return statements;
510
+ }
511
+
333
512
  function objectPatternPropertyDeclaration(kind, sourceName, prop) {
334
513
  var member = propertyMemberExpression(
335
514
  { type: "Identifier", name: sourceName },
@@ -378,6 +557,108 @@ function containsThisExpression(node) {
378
557
  return found;
379
558
  }
380
559
 
560
+ function defaultParameterStatement(param) {
561
+ return {
562
+ type: "IfStatement",
563
+ test: {
564
+ type: "BinaryExpression",
565
+ operator: "===",
566
+ left: utils.cloneISwearIKnowWhatImDoing(param.left),
567
+ right: { type: "Identifier", name: "undefined" }
568
+ },
569
+ consequent: {
570
+ type: "BlockStatement",
571
+ body: [
572
+ {
573
+ type: "ExpressionStatement",
574
+ expression: {
575
+ type: "AssignmentExpression",
576
+ operator: "=",
577
+ left: utils.cloneISwearIKnowWhatImDoing(param.left),
578
+ right: param.right
579
+ }
580
+ }
581
+ ]
582
+ }
583
+ };
584
+ }
585
+
586
+ function restParameterStatement(param, index) {
587
+ return {
588
+ type: "VariableDeclaration",
589
+ kind: "var",
590
+ declarations: [
591
+ {
592
+ type: "VariableDeclarator",
593
+ id: param.argument,
594
+ init: {
595
+ type: "CallExpression",
596
+ callee: {
597
+ type: "MemberExpression",
598
+ object: {
599
+ type: "MemberExpression",
600
+ object: {
601
+ type: "MemberExpression",
602
+ object: { type: "Identifier", name: "Array" },
603
+ property: { type: "Identifier", name: "prototype" },
604
+ computed: false
605
+ },
606
+ property: { type: "Identifier", name: "slice" },
607
+ computed: false
608
+ },
609
+ property: { type: "Identifier", name: "call" },
610
+ computed: false
611
+ },
612
+ arguments: [
613
+ { type: "Identifier", name: "arguments" },
614
+ { type: "Literal", value: index }
615
+ ]
616
+ }
617
+ }
618
+ ]
619
+ };
620
+ }
621
+
622
+ function lowerFunctionParameters(node) {
623
+ if (!estest.isFunction(node) || !Array.isArray(node.params)) {
624
+ return node;
625
+ }
626
+
627
+ var prefix = [];
628
+ var params = [];
629
+ node.params.forEach((param, index) => {
630
+ if (param.type == "AssignmentPattern" && param.left.type == "Identifier") {
631
+ prefix.push(defaultParameterStatement(param));
632
+ params.push(param.left);
633
+ return;
634
+ }
635
+ if (param.type == "RestElement" && param.argument.type == "Identifier") {
636
+ prefix.push(restParameterStatement(param, index));
637
+ return;
638
+ }
639
+ params.push(param);
640
+ });
641
+ node.params = params;
642
+
643
+ if (prefix.length == 0) {
644
+ return node;
645
+ }
646
+
647
+ if (node.body.type != "BlockStatement") {
648
+ node.body = {
649
+ type: "BlockStatement",
650
+ body: [
651
+ {
652
+ type: "ReturnStatement",
653
+ argument: node.body
654
+ }
655
+ ]
656
+ };
657
+ }
658
+ node.body.body = prefix.concat(node.body.body);
659
+ return node;
660
+ }
661
+
381
662
  function blockNeedsLexicalScope(node) {
382
663
  if (node.type != "BlockStatement") {
383
664
  return false;
@@ -436,6 +717,8 @@ module.exports = class Normalizer {
436
717
  return this.simplifyTryStatement(node);
437
718
  case "CallExpression":
438
719
  return this.simplifyCallExpression(node);
720
+ case "ExpressionStatement":
721
+ return this.simplifyExpressionStatement(node);
439
722
  case "ChainExpression":
440
723
  return this.simplifyChainExpression(node);
441
724
  case "LogicalExpression":
@@ -444,6 +727,9 @@ module.exports = class Normalizer {
444
727
  return this.simplifyObjectExpression(node);
445
728
  case "VariableDeclaration":
446
729
  return this.simplifyVariableDeclaration(node, stack);
730
+ case "FunctionDeclaration":
731
+ case "FunctionExpression":
732
+ return lowerFunctionParameters(node);
447
733
  case "ArrowFunctionExpression":
448
734
  return this.simplifyArrowFunctionExpression(node);
449
735
  case "ClassDeclaration":
@@ -577,6 +863,56 @@ module.exports = class Normalizer {
577
863
  assert.ok(estest.isNode(node));
578
864
 
579
865
  var propsName = `$$forin$props$${this.rngAlpha.get()}`, iterName = `$$forin$iter$${this.rngAlpha.get()}`;
866
+ var valueAtIndex = {
867
+ type: "MemberExpression",
868
+ object: { type: "Identifier", name: propsName },
869
+ property: { type: "Identifier", name: iterName },
870
+ computed: true
871
+ };
872
+ var assignStatements;
873
+ if (node.left.type == "VariableDeclaration") {
874
+ var declaration = node.left.declarations[0];
875
+ if (hasArrayPattern(declaration.id) && canLowerArrayPattern(declaration.id)) {
876
+ assignStatements = arrayPatternStatements(
877
+ node.left.kind == "const" ? "let" : node.left.kind,
878
+ declaration.id,
879
+ valueAtIndex,
880
+ this.rngAlpha
881
+ );
882
+ } else {
883
+ assignStatements = [
884
+ {
885
+ type: "VariableDeclaration",
886
+ kind: "var",
887
+ declarations: [
888
+ {
889
+ type: "VariableDeclarator",
890
+ id: declaration.id,
891
+ init: valueAtIndex
892
+ }
893
+ ]
894
+ }
895
+ ];
896
+ }
897
+ } else if (hasArrayPattern(node.left) && canLowerArrayPattern(node.left)) {
898
+ assignStatements = arrayPatternAssignmentStatements(
899
+ node.left,
900
+ valueAtIndex,
901
+ this.rngAlpha
902
+ );
903
+ } else {
904
+ assignStatements = [
905
+ {
906
+ type: "ExpressionStatement",
907
+ expression: {
908
+ type: "AssignmentExpression",
909
+ operator: "=",
910
+ left: node.left,
911
+ right: valueAtIndex
912
+ }
913
+ }
914
+ ];
915
+ }
580
916
 
581
917
  var forStmt = {
582
918
  type: "ForStatement",
@@ -626,42 +962,7 @@ module.exports = class Normalizer {
626
962
  },
627
963
  body: {
628
964
  type: "BlockStatement",
629
- body: [
630
- node.left.type == "VariableDeclaration"
631
- ?
632
- {
633
- type: "VariableDeclaration",
634
- kind: "var",
635
- declarations: [
636
- {
637
- type: "VariableDeclarator",
638
- id: node.left.declarations[0].id,
639
- init: {
640
- type: "MemberExpression",
641
- object: { type: "Identifier", name: propsName },
642
- property: { type: "Identifier", name: iterName },
643
- computed: true
644
- }
645
- }
646
- ]
647
- }
648
- :
649
- {
650
- type: "ExpressionStatement",
651
- expression: {
652
- type: "AssignmentExpression",
653
- operator: "=",
654
- left: node.left,
655
- right: {
656
- type: "MemberExpression",
657
- object: { type: "Identifier", name: propsName },
658
- property: { type: "Identifier", name: iterName },
659
- computed: true
660
- }
661
- }
662
- },
663
- node.body
664
- ]
965
+ body: assignStatements.concat([node.body])
665
966
  }
666
967
  };
667
968
  return forStmt;
@@ -682,29 +983,50 @@ module.exports = class Normalizer {
682
983
  property: { type: "Identifier", name: iterName },
683
984
  computed: true
684
985
  };
685
- var assignValue = node.left.type == "VariableDeclaration"
686
- ?
687
- {
688
- type: "VariableDeclaration",
689
- kind: node.left.kind == "const" ? "let" : node.left.kind,
690
- declarations: [
986
+ var assignStatements;
987
+ if (node.left.type == "VariableDeclaration") {
988
+ var declaration = node.left.declarations[0];
989
+ if (hasArrayPattern(declaration.id) && canLowerArrayPattern(declaration.id)) {
990
+ assignStatements = arrayPatternStatements(
991
+ node.left.kind == "const" ? "let" : node.left.kind,
992
+ declaration.id,
993
+ valueAtIndex,
994
+ this.rngAlpha
995
+ );
996
+ } else {
997
+ assignStatements = [
998
+ {
999
+ type: "VariableDeclaration",
1000
+ kind: node.left.kind == "const" ? "let" : node.left.kind,
1001
+ declarations: [
1002
+ {
1003
+ type: "VariableDeclarator",
1004
+ id: declaration.id,
1005
+ init: valueAtIndex
1006
+ }
1007
+ ]
1008
+ }
1009
+ ];
1010
+ }
1011
+ } else if (hasArrayPattern(node.left) && canLowerArrayPattern(node.left)) {
1012
+ assignStatements = arrayPatternAssignmentStatements(
1013
+ node.left,
1014
+ valueAtIndex,
1015
+ this.rngAlpha
1016
+ );
1017
+ } else {
1018
+ assignStatements = [
691
1019
  {
692
- type: "VariableDeclarator",
693
- id: node.left.declarations[0].id,
694
- init: valueAtIndex
1020
+ type: "ExpressionStatement",
1021
+ expression: {
1022
+ type: "AssignmentExpression",
1023
+ operator: "=",
1024
+ left: node.left,
1025
+ right: valueAtIndex
1026
+ }
695
1027
  }
696
- ]
1028
+ ];
697
1029
  }
698
- :
699
- {
700
- type: "ExpressionStatement",
701
- expression: {
702
- type: "AssignmentExpression",
703
- operator: "=",
704
- left: node.left,
705
- right: valueAtIndex
706
- }
707
- };
708
1030
 
709
1031
  return {
710
1032
  type: "BlockStatement",
@@ -747,7 +1069,7 @@ module.exports = class Normalizer {
747
1069
  },
748
1070
  body: {
749
1071
  type: "BlockStatement",
750
- body: [assignValue].concat(blockToArray(node.body))
1072
+ body: assignStatements.concat(blockToArray(node.body))
751
1073
  }
752
1074
  }
753
1075
  ]
@@ -886,7 +1208,7 @@ module.exports = class Normalizer {
886
1208
  block: node.block,
887
1209
  handler: {
888
1210
  type: "CatchClause",
889
- param: { type: "Identifier", name: "veilmark$e" },
1211
+ param: { type: "Identifier", name: "toildefender$e" },
890
1212
  body: {
891
1213
  type: "BlockStatement",
892
1214
  body: [
@@ -896,8 +1218,8 @@ module.exports = class Normalizer {
896
1218
  declarations: [
897
1219
  {
898
1220
  type: "VariableDeclarator",
899
- id: { type: "Identifier", name: "veilmark$_e" },
900
- init: { type: "Identifier", name: "veilmark$e" }
1221
+ id: { type: "Identifier", name: "toildefender$_e" },
1222
+ init: { type: "Identifier", name: "toildefender$e" }
901
1223
  }
902
1224
  ]
903
1225
  }
@@ -908,10 +1230,10 @@ module.exports = class Normalizer {
908
1230
  node.finalizer,
909
1231
  {
910
1232
  type: "IfStatement",
911
- test: { type: "Identifier", name: "veilmark$_e" },
1233
+ test: { type: "Identifier", name: "toildefender$_e" },
912
1234
  consequent: {
913
1235
  type: "ThrowStatement",
914
- argument: { type: "Identifier", name: "veilmark$_e" }
1236
+ argument: { type: "Identifier", name: "toildefender$_e" }
915
1237
  }
916
1238
  }
917
1239
  ]
@@ -959,6 +1281,28 @@ module.exports = class Normalizer {
959
1281
  };
960
1282
  }
961
1283
 
1284
+ simplifyExpressionStatement (node) {
1285
+ assert.ok(estest.isNode(node));
1286
+
1287
+ if (
1288
+ node.expression.type == "AssignmentExpression" &&
1289
+ node.expression.operator == "=" &&
1290
+ hasArrayPattern(node.expression.left) &&
1291
+ canLowerArrayPattern(node.expression.left)
1292
+ ) {
1293
+ return {
1294
+ type: "BlockStatement",
1295
+ body: arrayPatternAssignmentStatements(
1296
+ node.expression.left,
1297
+ node.expression.right,
1298
+ this.rngAlpha
1299
+ )
1300
+ };
1301
+ }
1302
+
1303
+ return node;
1304
+ }
1305
+
962
1306
  /**
963
1307
  * Lower optional chains to conditional expressions before legacy passes.
964
1308
  * This intentionally targets deterministic AST compatibility rather than
@@ -1143,10 +1487,14 @@ module.exports = class Normalizer {
1143
1487
  simplifyVariableDeclaration (node, stack) {
1144
1488
  assert.ok(estest.isNode(node));
1145
1489
 
1146
- if (!node.declarations.some(decl => hasObjectRest(decl.id))) {
1490
+ var needsLowering = node.declarations.some(decl => hasObjectPattern(decl.id) || hasArrayPattern(decl.id));
1491
+ if (!needsLowering) {
1492
+ return node;
1493
+ }
1494
+ if (node.declarations.some(decl => hasObjectPattern(decl.id) && !canLowerObjectRest(decl.id))) {
1147
1495
  return node;
1148
1496
  }
1149
- if (node.declarations.some(decl => hasObjectRest(decl.id) && !canLowerObjectRest(decl.id))) {
1497
+ if (node.declarations.some(decl => hasArrayPattern(decl.id) && !canLowerArrayPattern(decl.id))) {
1150
1498
  return node;
1151
1499
  }
1152
1500
 
@@ -1154,10 +1502,17 @@ module.exports = class Normalizer {
1154
1502
  if (parentFrame && parentFrame.node.type == "ForStatement" && parentFrame.key == "init") {
1155
1503
  return node;
1156
1504
  }
1505
+ if (
1506
+ parentFrame &&
1507
+ (parentFrame.node.type == "ForOfStatement" || parentFrame.node.type == "ForInStatement") &&
1508
+ parentFrame.key == "left"
1509
+ ) {
1510
+ return node;
1511
+ }
1157
1512
 
1158
1513
  var statements = [];
1159
1514
  var normalDeclarations = [];
1160
- var declarationKind = node.kind == "const" ? "var" : node.kind;
1515
+ var declarationKind = "var";
1161
1516
 
1162
1517
  function flushNormalDeclarations() {
1163
1518
  if (normalDeclarations.length > 0) {
@@ -1171,13 +1526,23 @@ module.exports = class Normalizer {
1171
1526
  }
1172
1527
 
1173
1528
  node.declarations.forEach(decl => {
1174
- if (!hasObjectRest(decl.id)) {
1529
+ if (!hasObjectPattern(decl.id) && !hasArrayPattern(decl.id)) {
1175
1530
  normalDeclarations.push(decl);
1176
1531
  return;
1177
1532
  }
1178
1533
 
1179
1534
  flushNormalDeclarations();
1180
1535
 
1536
+ if (hasArrayPattern(decl.id)) {
1537
+ statements = statements.concat(arrayPatternStatements(
1538
+ "var",
1539
+ decl.id,
1540
+ decl.init,
1541
+ this.rngAlpha
1542
+ ));
1543
+ return;
1544
+ }
1545
+
1181
1546
  var sourceName = `$$destructure$obj$${this.rngAlpha.get()}`;
1182
1547
  statements.push({
1183
1548
  type: "VariableDeclaration",
@@ -1251,6 +1616,7 @@ module.exports = class Normalizer {
1251
1616
  expression: false,
1252
1617
  async: node.async === true
1253
1618
  };
1619
+ fn = lowerFunctionParameters(fn);
1254
1620
 
1255
1621
  if (!containsThisExpression(fn.body)) {
1256
1622
  return fn;