예제 #1
0
        public override void Visit(Conditional node)
        {
            if (node != null)
            {
                // we have two choices for the conditional. Either:
                //  1. we wrap the whole thing in a logical-not operator, which means we also need to
                //     add parentheses, since conditional is lower-precedence than the logicial not, or
                //  2. apply the logical-not to both the true- and false-branches.
                // The first is guaranteed 3 additional characters. We have to check the delta for
                // each branch and add them together to know how much the second would cost. If it's
                // greater than 3, then we just want to not the whole thing.
                var notTrue = new LogicalNotVisitor(node.TrueExpression)
                {
                    MinifyBooleans = this.MinifyBooleans
                };
                var notFalse = new LogicalNotVisitor(node.FalseExpression)
                {
                    MinifyBooleans = this.MinifyBooleans
                };
                var costNottingBoth = notTrue.Measure() + notFalse.Measure();

                if (m_measure)
                {
                    // we're just measuring -- adjust the delta accordingly
                    // (the lesser of the two options)
                    m_delta += (costNottingBoth > 3) ? 3 : costNottingBoth;
                }
                else if (costNottingBoth > 3)
                {
                    // just wrap the whole thing
                    WrapWithLogicalNot(node);
                }
                else
                {
                    // less bytes to wrap each branch separately
                    node.TrueExpression.Accept(this);
                    node.FalseExpression.Accept(this);
                }
            }
        }
예제 #2
0
        public static void Apply(AstNode node, CodeSettings codeSettings)
        {
            var logicalNot = new LogicalNotVisitor(node, codeSettings);

            logicalNot.Apply();
        }