|
1 | 1 | /*
|
| 2 | + Copyright (C) 2012 Mihai Bazon <[email protected]> |
2 | 3 | Copyright (C) 2012 Yusuke Suzuki <[email protected]>
|
3 | 4 |
|
4 | 5 | Redistribution and use in source and binary forms, with or without
|
|
43 | 44 | continue;
|
44 | 45 | }
|
45 | 46 | } else if (expr.type === Syntax.LogicalExpression) {
|
46 |
| - if (expr.left.type === Syntax.UnaryExpression) { |
47 |
| - if (expr.left.operator === '!') { |
48 |
| - // !cond && ok() => cond || ok() |
49 |
| - modified = true; |
50 |
| - expr.left = expr.left.argument; |
51 |
| - expr.operator = (expr.operator === '||') ? '&&' : '||'; |
52 |
| - } |
| 47 | + if (expr.left.type === Syntax.UnaryExpression && expr.left.operator === '!' && |
| 48 | + expr.right.type === Syntax.UnaryExpression && expr.right.operator === '!') { |
| 49 | + // !cond && !ok() => !(cond || ok()) |
| 50 | + // this introduces more optimizations |
| 51 | + modified = true; |
| 52 | + expr.left = expr.left.argument; |
| 53 | + expr.right = expr.right.argument; |
| 54 | + expr.operator = (expr.operator === '||') ? '&&' : '||'; |
| 55 | + expr = common.moveLocation(expr, { |
| 56 | + type: Syntax.UnaryExpression, |
| 57 | + operator: '!', |
| 58 | + argument: expr |
| 59 | + }); |
| 60 | + continue; |
53 | 61 | }
|
54 | 62 | } else if (expr.type === Syntax.ConditionalExpression) {
|
55 | 63 | if (expr.test.type === Syntax.UnaryExpression && expr.test.operator === '!') {
|
|
74 | 82 | expr = expr.argument;
|
75 | 83 | continue;
|
76 | 84 | }
|
| 85 | + } else if (expr.type === Syntax.LogicalExpression) { |
| 86 | + if (expr.left.type === Syntax.UnaryExpression && expr.left.operator === '!') { |
| 87 | + // !cond && ok() => cond || ok() |
| 88 | + modified = true; |
| 89 | + expr.left = expr.left.argument; |
| 90 | + expr.operator = (expr.operator === '||') ? '&&' : '||'; |
| 91 | + } |
77 | 92 | }
|
78 | 93 | break;
|
79 | 94 | } while (true);
|
|
0 commit comments