@dacely/toildefender 0.1.5 → 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.
- package/README.md +1 -1
- package/esutils.js +12 -8
- package/obfuscator.js +101 -12
- package/package.json +1 -1
- package/processors/deadCode.js +32 -0
- package/processors/flattener.js +73 -8
- package/processors/identifiers.js +26 -6
- package/processors/literals.js +82 -2
- package/processors/methods.js +47 -34
- package/processors/modules.js +2 -2
- package/processors/normalizer.js +466 -73
- package/processors/numericVm.js +270 -83
- package/processors/postprocessing.js +4 -4
- package/processors/scopes.js +366 -50
- package/processors/uglifier.js +219 -2
- package/processors/variables.js +51 -8
package/processors/normalizer.js
CHANGED
|
@@ -46,8 +46,10 @@ function blockToArray (node) {
|
|
|
46
46
|
|
|
47
47
|
if (Array.isArray(node.body)) {
|
|
48
48
|
return node.body;
|
|
49
|
-
} else {
|
|
49
|
+
} else if (node.body) {
|
|
50
50
|
return [ node.body ];
|
|
51
|
+
} else {
|
|
52
|
+
return [ node ];
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
@@ -133,7 +135,7 @@ function withFinalizerBefore(node, finalizer) {
|
|
|
133
135
|
declarations: [
|
|
134
136
|
{
|
|
135
137
|
type: "VariableDeclarator",
|
|
136
|
-
id: { type: "Identifier", name: "
|
|
138
|
+
id: { type: "Identifier", name: "toildefender$return" },
|
|
137
139
|
init: node.argument
|
|
138
140
|
}
|
|
139
141
|
]
|
|
@@ -141,7 +143,7 @@ function withFinalizerBefore(node, finalizer) {
|
|
|
141
143
|
body.push(utils.cloneISwearIKnowWhatImDoing(finalizer));
|
|
142
144
|
body.push({
|
|
143
145
|
type: "ReturnStatement",
|
|
144
|
-
argument: { type: "Identifier", name: "
|
|
146
|
+
argument: { type: "Identifier", name: "toildefender$return" }
|
|
145
147
|
});
|
|
146
148
|
} else {
|
|
147
149
|
body.push(utils.cloneISwearIKnowWhatImDoing(finalizer));
|
|
@@ -282,6 +284,29 @@ function hasObjectRest(pattern) {
|
|
|
282
284
|
return pattern.type == "ObjectPattern" && pattern.properties.some(prop => prop.type == "RestElement");
|
|
283
285
|
}
|
|
284
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
|
+
|
|
285
310
|
function canLowerObjectRest(pattern) {
|
|
286
311
|
return pattern.type == "ObjectPattern" && pattern.properties.every(prop => {
|
|
287
312
|
if (prop.type == "RestElement") {
|
|
@@ -317,7 +342,7 @@ function objectAssignCall(parts) {
|
|
|
317
342
|
function objectWithoutKeysCall(source, keys) {
|
|
318
343
|
return {
|
|
319
344
|
type: "CallExpression",
|
|
320
|
-
callee: { type: "Identifier", name: "
|
|
345
|
+
callee: { type: "Identifier", name: "toildefender$objectWithoutKeys" },
|
|
321
346
|
arguments: [
|
|
322
347
|
source,
|
|
323
348
|
{
|
|
@@ -328,6 +353,162 @@ function objectWithoutKeysCall(source, keys) {
|
|
|
328
353
|
};
|
|
329
354
|
}
|
|
330
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
|
+
|
|
331
512
|
function objectPatternPropertyDeclaration(kind, sourceName, prop) {
|
|
332
513
|
var member = propertyMemberExpression(
|
|
333
514
|
{ type: "Identifier", name: sourceName },
|
|
@@ -376,6 +557,130 @@ function containsThisExpression(node) {
|
|
|
376
557
|
return found;
|
|
377
558
|
}
|
|
378
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
|
+
|
|
662
|
+
function blockNeedsLexicalScope(node) {
|
|
663
|
+
if (node.type != "BlockStatement") {
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
var needsScope = false;
|
|
668
|
+
traverser.traverseEx(node, [], function (child) {
|
|
669
|
+
if (child != node && estest.isFunction(child)) {
|
|
670
|
+
return child;
|
|
671
|
+
}
|
|
672
|
+
if (
|
|
673
|
+
(child.type == "VariableDeclaration" && child.kind != "var")
|
|
674
|
+
|| child.type == "ClassDeclaration"
|
|
675
|
+
) {
|
|
676
|
+
needsScope = true;
|
|
677
|
+
this.abort();
|
|
678
|
+
}
|
|
679
|
+
return child;
|
|
680
|
+
});
|
|
681
|
+
return needsScope;
|
|
682
|
+
}
|
|
683
|
+
|
|
379
684
|
module.exports = class Normalizer {
|
|
380
685
|
|
|
381
686
|
constructor (logger) {
|
|
@@ -412,6 +717,8 @@ module.exports = class Normalizer {
|
|
|
412
717
|
return this.simplifyTryStatement(node);
|
|
413
718
|
case "CallExpression":
|
|
414
719
|
return this.simplifyCallExpression(node);
|
|
720
|
+
case "ExpressionStatement":
|
|
721
|
+
return this.simplifyExpressionStatement(node);
|
|
415
722
|
case "ChainExpression":
|
|
416
723
|
return this.simplifyChainExpression(node);
|
|
417
724
|
case "LogicalExpression":
|
|
@@ -420,6 +727,9 @@ module.exports = class Normalizer {
|
|
|
420
727
|
return this.simplifyObjectExpression(node);
|
|
421
728
|
case "VariableDeclaration":
|
|
422
729
|
return this.simplifyVariableDeclaration(node, stack);
|
|
730
|
+
case "FunctionDeclaration":
|
|
731
|
+
case "FunctionExpression":
|
|
732
|
+
return lowerFunctionParameters(node);
|
|
423
733
|
case "ArrowFunctionExpression":
|
|
424
734
|
return this.simplifyArrowFunctionExpression(node);
|
|
425
735
|
case "ClassDeclaration":
|
|
@@ -439,10 +749,13 @@ module.exports = class Normalizer {
|
|
|
439
749
|
simplifyBlockStatement (node) {
|
|
440
750
|
assert.ok(estest.isNode(node));
|
|
441
751
|
|
|
442
|
-
function getBlockBodys(node) {
|
|
752
|
+
function getBlockBodys(node, isRoot) {
|
|
443
753
|
if (node.type == "Program" || node.type == "BlockStatement") {
|
|
754
|
+
if (!isRoot && blockNeedsLexicalScope(node)) {
|
|
755
|
+
return [ node ];
|
|
756
|
+
}
|
|
444
757
|
var stmts = [];
|
|
445
|
-
node.body.forEach(stmt => utils.push(stmts, getBlockBodys(stmt)));
|
|
758
|
+
node.body.forEach(stmt => utils.push(stmts, getBlockBodys(stmt, false)));
|
|
446
759
|
return stmts;
|
|
447
760
|
} else {
|
|
448
761
|
return [ node ];
|
|
@@ -451,7 +764,7 @@ module.exports = class Normalizer {
|
|
|
451
764
|
|
|
452
765
|
return {
|
|
453
766
|
type: node.type,
|
|
454
|
-
body: getBlockBodys(node)
|
|
767
|
+
body: getBlockBodys(node, true)
|
|
455
768
|
};
|
|
456
769
|
}
|
|
457
770
|
|
|
@@ -550,6 +863,56 @@ module.exports = class Normalizer {
|
|
|
550
863
|
assert.ok(estest.isNode(node));
|
|
551
864
|
|
|
552
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
|
+
}
|
|
553
916
|
|
|
554
917
|
var forStmt = {
|
|
555
918
|
type: "ForStatement",
|
|
@@ -599,42 +962,7 @@ module.exports = class Normalizer {
|
|
|
599
962
|
},
|
|
600
963
|
body: {
|
|
601
964
|
type: "BlockStatement",
|
|
602
|
-
body: [
|
|
603
|
-
node.left.type == "VariableDeclaration"
|
|
604
|
-
?
|
|
605
|
-
{
|
|
606
|
-
type: "VariableDeclaration",
|
|
607
|
-
kind: "var",
|
|
608
|
-
declarations: [
|
|
609
|
-
{
|
|
610
|
-
type: "VariableDeclarator",
|
|
611
|
-
id: node.left.declarations[0].id,
|
|
612
|
-
init: {
|
|
613
|
-
type: "MemberExpression",
|
|
614
|
-
object: { type: "Identifier", name: propsName },
|
|
615
|
-
property: { type: "Identifier", name: iterName },
|
|
616
|
-
computed: true
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
]
|
|
620
|
-
}
|
|
621
|
-
:
|
|
622
|
-
{
|
|
623
|
-
type: "ExpressionStatement",
|
|
624
|
-
expression: {
|
|
625
|
-
type: "AssignmentExpression",
|
|
626
|
-
operator: "=",
|
|
627
|
-
left: node.left,
|
|
628
|
-
right: {
|
|
629
|
-
type: "MemberExpression",
|
|
630
|
-
object: { type: "Identifier", name: propsName },
|
|
631
|
-
property: { type: "Identifier", name: iterName },
|
|
632
|
-
computed: true
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
},
|
|
636
|
-
node.body
|
|
637
|
-
]
|
|
965
|
+
body: assignStatements.concat([node.body])
|
|
638
966
|
}
|
|
639
967
|
};
|
|
640
968
|
return forStmt;
|
|
@@ -655,29 +983,50 @@ module.exports = class Normalizer {
|
|
|
655
983
|
property: { type: "Identifier", name: iterName },
|
|
656
984
|
computed: true
|
|
657
985
|
};
|
|
658
|
-
var
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
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 = [
|
|
664
1019
|
{
|
|
665
|
-
type: "
|
|
666
|
-
|
|
667
|
-
|
|
1020
|
+
type: "ExpressionStatement",
|
|
1021
|
+
expression: {
|
|
1022
|
+
type: "AssignmentExpression",
|
|
1023
|
+
operator: "=",
|
|
1024
|
+
left: node.left,
|
|
1025
|
+
right: valueAtIndex
|
|
1026
|
+
}
|
|
668
1027
|
}
|
|
669
|
-
]
|
|
1028
|
+
];
|
|
670
1029
|
}
|
|
671
|
-
:
|
|
672
|
-
{
|
|
673
|
-
type: "ExpressionStatement",
|
|
674
|
-
expression: {
|
|
675
|
-
type: "AssignmentExpression",
|
|
676
|
-
operator: "=",
|
|
677
|
-
left: node.left,
|
|
678
|
-
right: valueAtIndex
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
1030
|
|
|
682
1031
|
return {
|
|
683
1032
|
type: "BlockStatement",
|
|
@@ -720,7 +1069,7 @@ module.exports = class Normalizer {
|
|
|
720
1069
|
},
|
|
721
1070
|
body: {
|
|
722
1071
|
type: "BlockStatement",
|
|
723
|
-
body:
|
|
1072
|
+
body: assignStatements.concat(blockToArray(node.body))
|
|
724
1073
|
}
|
|
725
1074
|
}
|
|
726
1075
|
]
|
|
@@ -859,7 +1208,7 @@ module.exports = class Normalizer {
|
|
|
859
1208
|
block: node.block,
|
|
860
1209
|
handler: {
|
|
861
1210
|
type: "CatchClause",
|
|
862
|
-
param: { type: "Identifier", name: "
|
|
1211
|
+
param: { type: "Identifier", name: "toildefender$e" },
|
|
863
1212
|
body: {
|
|
864
1213
|
type: "BlockStatement",
|
|
865
1214
|
body: [
|
|
@@ -869,8 +1218,8 @@ module.exports = class Normalizer {
|
|
|
869
1218
|
declarations: [
|
|
870
1219
|
{
|
|
871
1220
|
type: "VariableDeclarator",
|
|
872
|
-
id: { type: "Identifier", name: "
|
|
873
|
-
init: { type: "Identifier", name: "
|
|
1221
|
+
id: { type: "Identifier", name: "toildefender$_e" },
|
|
1222
|
+
init: { type: "Identifier", name: "toildefender$e" }
|
|
874
1223
|
}
|
|
875
1224
|
]
|
|
876
1225
|
}
|
|
@@ -881,10 +1230,10 @@ module.exports = class Normalizer {
|
|
|
881
1230
|
node.finalizer,
|
|
882
1231
|
{
|
|
883
1232
|
type: "IfStatement",
|
|
884
|
-
test: { type: "Identifier", name: "
|
|
1233
|
+
test: { type: "Identifier", name: "toildefender$_e" },
|
|
885
1234
|
consequent: {
|
|
886
1235
|
type: "ThrowStatement",
|
|
887
|
-
argument: { type: "Identifier", name: "
|
|
1236
|
+
argument: { type: "Identifier", name: "toildefender$_e" }
|
|
888
1237
|
}
|
|
889
1238
|
}
|
|
890
1239
|
]
|
|
@@ -932,6 +1281,28 @@ module.exports = class Normalizer {
|
|
|
932
1281
|
};
|
|
933
1282
|
}
|
|
934
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
|
+
|
|
935
1306
|
/**
|
|
936
1307
|
* Lower optional chains to conditional expressions before legacy passes.
|
|
937
1308
|
* This intentionally targets deterministic AST compatibility rather than
|
|
@@ -1116,10 +1487,14 @@ module.exports = class Normalizer {
|
|
|
1116
1487
|
simplifyVariableDeclaration (node, stack) {
|
|
1117
1488
|
assert.ok(estest.isNode(node));
|
|
1118
1489
|
|
|
1119
|
-
|
|
1490
|
+
var needsLowering = node.declarations.some(decl => hasObjectPattern(decl.id) || hasArrayPattern(decl.id));
|
|
1491
|
+
if (!needsLowering) {
|
|
1120
1492
|
return node;
|
|
1121
1493
|
}
|
|
1122
|
-
if (node.declarations.some(decl =>
|
|
1494
|
+
if (node.declarations.some(decl => hasObjectPattern(decl.id) && !canLowerObjectRest(decl.id))) {
|
|
1495
|
+
return node;
|
|
1496
|
+
}
|
|
1497
|
+
if (node.declarations.some(decl => hasArrayPattern(decl.id) && !canLowerArrayPattern(decl.id))) {
|
|
1123
1498
|
return node;
|
|
1124
1499
|
}
|
|
1125
1500
|
|
|
@@ -1127,10 +1502,17 @@ module.exports = class Normalizer {
|
|
|
1127
1502
|
if (parentFrame && parentFrame.node.type == "ForStatement" && parentFrame.key == "init") {
|
|
1128
1503
|
return node;
|
|
1129
1504
|
}
|
|
1505
|
+
if (
|
|
1506
|
+
parentFrame &&
|
|
1507
|
+
(parentFrame.node.type == "ForOfStatement" || parentFrame.node.type == "ForInStatement") &&
|
|
1508
|
+
parentFrame.key == "left"
|
|
1509
|
+
) {
|
|
1510
|
+
return node;
|
|
1511
|
+
}
|
|
1130
1512
|
|
|
1131
1513
|
var statements = [];
|
|
1132
1514
|
var normalDeclarations = [];
|
|
1133
|
-
var declarationKind =
|
|
1515
|
+
var declarationKind = "var";
|
|
1134
1516
|
|
|
1135
1517
|
function flushNormalDeclarations() {
|
|
1136
1518
|
if (normalDeclarations.length > 0) {
|
|
@@ -1144,13 +1526,23 @@ module.exports = class Normalizer {
|
|
|
1144
1526
|
}
|
|
1145
1527
|
|
|
1146
1528
|
node.declarations.forEach(decl => {
|
|
1147
|
-
if (!
|
|
1529
|
+
if (!hasObjectPattern(decl.id) && !hasArrayPattern(decl.id)) {
|
|
1148
1530
|
normalDeclarations.push(decl);
|
|
1149
1531
|
return;
|
|
1150
1532
|
}
|
|
1151
1533
|
|
|
1152
1534
|
flushNormalDeclarations();
|
|
1153
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
|
+
|
|
1154
1546
|
var sourceName = `$$destructure$obj$${this.rngAlpha.get()}`;
|
|
1155
1547
|
statements.push({
|
|
1156
1548
|
type: "VariableDeclaration",
|
|
@@ -1224,6 +1616,7 @@ module.exports = class Normalizer {
|
|
|
1224
1616
|
expression: false,
|
|
1225
1617
|
async: node.async === true
|
|
1226
1618
|
};
|
|
1619
|
+
fn = lowerFunctionParameters(fn);
|
|
1227
1620
|
|
|
1228
1621
|
if (!containsThisExpression(fn.body)) {
|
|
1229
1622
|
return fn;
|