예제 #1
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            // !(a = false) → true
            if (p is AstUnary)
            {
                return(true);
            }
            // 1 + (a = 2) + 3 → 6, side effect setting a = 2
            if (p is AstBinary && !(p is AstAssign))
            {
                return(true);
            }
            // (a = func)() —or— new (a = Object)()
            if (p is AstCall call && call.Expression == this)
            {
                return(true);
            }
            // (a = foo) ? bar : baz
            if (p is AstConditional conditional && conditional.Condition == this)
            {
                return(true);
            }
            // (a = foo)["prop"] —or— (a = foo).prop
            if (p is AstPropAccess propAccess && propAccess.Expression == this)
            {
                return(true);
            }
            return(false);
        }
예제 #2
0
        protected void DoPrint(OutputContext output, string kind)
        {
            output.Print(kind);
            output.Space();
            for (var i = 0u; i < Definitions.Count; i++)
            {
                if (i > 0)
                {
                    output.Comma();
                }
                Definitions[i].Print(output);
            }

            var p = output.Parent();

            if (p is AstFor astFor && astFor.Init == this)
            {
                return;
            }
            if (p is AstForIn astForIn && astForIn.Init == this)
            {
                return;
            }
            if (p == null)
            {
                return;
            }
            output.Semicolon();
        }
예제 #3
0
파일: AstYield.cs 프로젝트: 0papen0/bbcore
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            // (yield 1) + (yield 2)
            // a = yield 3
            if (p is AstBinary binary && binary.Operator != Operator.Assignment)
            {
                return(true);
            }
            // (yield 1)()
            // new (yield 1)()
            if (p is AstCall call && call.Expression == this)
            {
                return(true);
            }
            // (yield 1) ? yield 2 : yield 3
            if (p is AstConditional conditional && conditional.Condition == this)
            {
                return(true);
            }
            // -(yield 4)
            if (p is AstUnary)
            {
                return(true);
            }
            // (yield x).foo
            // (yield x)['foo']
            if (p is AstPropAccess propAccess && propAccess.Expression == this)
            {
                return(true);
            }
            return(false);
        }
예제 #4
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            return(!output.NeedConstructorParens(this) &&
                   (p is AstPropAccess || // (new Date).getTime(), (new Date)["getTime"]()
                    p is AstCall call && call.Expression == this));      // (new foo)(bar)
        }
예제 #5
0
파일: AstAwait.cs 프로젝트: 0papen0/bbcore
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            return(p is AstPropAccess propAccess && propAccess.Expression == this ||
                   p is AstCall call && call.Expression == this ||
                   output.Options.Safari10 && p is AstUnaryPrefix);
        }
예제 #6
0
파일: AstCall.cs 프로젝트: Bobris/Njsast
    public override bool NeedParens(OutputContext output)
    {
        var p = output.Parent();

        if (p is AstNew aNew && aNew.Expression == this ||
            p is AstExport {
            IsDefault : true
        } && Expression is AstFunction)
예제 #7
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            if (p is AstNew aNew && aNew.Expression == this ||
                p is AstExport export && export.IsDefault && Expression is AstFunction)
            {
                return(true);
            }

            // workaround for Safari bug https://bugs.webkit.org/show_bug.cgi?id=123506
            return(Expression is AstFunction &&
                   p is AstPropAccess propAccess &&
                   propAccess.Expression == this &&
                   output.Parent(1) is AstAssign assign &&
                   assign.Left == p);
        }
예제 #8
0
    public override bool NeedParens(OutputContext output)
    {
        var p = output.Parent();

        return(p is AstPropAccess propAccess && propAccess.Expression == this ||
               p is AstCall call && call.Expression == this ||
               p is AstBinary binary &&
               binary.Operator == Operator.Power &&
               this is AstUnaryPrefix thisUnaryPrefix &&
               binary.Left == this &&
               thisUnaryPrefix.Operator != Operator.Increment &&
               thisUnaryPrefix.Operator != Operator.Decrement);
    }
예제 #9
0
        public override void CodeGen(OutputContext output)
        {
            var isTagged = output.Parent() is AstPrefixedTemplateString;

            output.Print("`");
            for (var i = 0u; i < Segments.Count; i++)
            {
                if (!(Segments[i] is AstTemplateSegment seg))
                {
                    output.Print("${");
                    Segments[i].Print(output);
                    output.Print("}");
                }
예제 #10
0
 public override void CodeGen(OutputContext output)
 {
     Name.Print(output);
     if (Value != null)
     {
         output.Space();
         output.Print("=");
         output.Space();
         var p    = output.Parent(1);
         var noin = p is AstFor || p is AstForIn;
         output.ParenthesizeForNoin(Value, noin);
     }
 }
예제 #11
0
    public override bool NeedParens(OutputContext output)
    {
        if (output.Options.ShortenBooleans)
        {
            var p = output.Parent();
            return(p is AstPropAccess propAccess && propAccess.Expression == this ||
                   p is AstCall call && call.Expression == this ||
                   p is AstBinary binary &&
                   binary.Operator == Operator.Power &&
                   binary.Left == this);
        }

        return(false);
    }
예제 #12
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            if (p is AstPropAccess propAccess && propAccess.Expression == this)
            {
                if (Value < 0)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #13
0
        public override bool NeedParens(OutputContext output)
        {
            if (output.FirstInStatement())
            {
                return(true);
            }

            if (output.Options.Webkit)
            {
                var p = output.Parent();
                if (p is AstPropAccess propAccess && propAccess.Expression == this)
                {
                    return(true);
                }
            }

            if (output.Options.WrapIife)
            {
                var p = output.Parent();
                return(p is AstCall call && call.Expression == this);
            }

            return(false);
        }
예제 #14
0
        public override void DoPrint(OutputContext output, bool nokeyword = false)
        {
            var parent      = output.Parent();
            var needsParens = parent is AstBinary ||
                              parent is AstUnary ||
                              (parent is AstCall call && this == call.Expression);

            if (needsParens)
            {
                output.Print("(");
            }
            if (Async)
            {
                output.Print("async");
                output.Space();
            }

            if (ArgNames.Count == 1 && ArgNames[0] is AstSymbol)
            {
                ArgNames[0].Print(output);
            }
            else
            {
                output.Print("(");
                for (var i = 0u; i < ArgNames.Count; i++)
                {
                    if (i > 0)
                    {
                        output.Comma();
                    }
                    ArgNames[i].Print(output);
                }

                output.Print(")");
            }

            output.Space();
            output.Print("=>");
            output.Space();
            output.PrintBraced(Body, false);
            if (needsParens)
            {
                output.Print(")");
            }
        }
예제 #15
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            if (p is AstNew aNew && aNew.Expression == this)
            {
                // i.e. new (foo.bar().baz)
                //
                // if there's one call into this subtree, then we need
                // parens around it too, otherwise the call will be
                // interpreted as passing the arguments to the upper New
                // expression.
                var walker = new WalkForParens();
                walker.Walk(this);
                return(walker.Parens);
            }

            return(false);
        }
예제 #16
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            return(p is AstCall || // (foo, bar)() or foo(1, (2, 3), 4)
                   p is AstUnary || // !(foo, bar, baz)
                   p is AstBinary || // 1 + (2, 3) + 4 ==> 8
                   p is AstVarDef    // var a = (1, 2), b = a + a; ==> b == 4
                   // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
                   || p is AstPropAccess propAccess && propAccess.Expression == this ||
                   p is AstArray || // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
                   p is AstObjectProperty    // { foo: (1, 2) }.foo ==> 2
                   // (false, true) ? (a = 10, b = 20) : (c = 30) ==> 20 (side effect, set a := 10 and b := 20)
                   || p is AstConditional ||
                   p is AstArrow || // x => (x, x)
                   p is AstDefaultAssign || // x => (x = (0, function(){}))
                   p is AstExpansion || // [...(a, b)]
                   p is AstForOf forOf && this == forOf.Object || // for (e of (foo, bar)) {}
                   p is AstYield || // yield (foo, bar)
                   p is AstExport    // export default (foo, bar)
                   );
        }
예제 #17
0
        public override void CodeGen(OutputContext output)
        {
            var isImport          = output.Parent() is AstImport;
            var definition        = Name.Thedef;
            var namesAreDifferent =
                (definition?.MangledName ?? Name.Name) !=
                ForeignName.Name;

            if (namesAreDifferent)
            {
                if (isImport)
                {
                    output.Print(ForeignName.Name);
                }
                else
                {
                    Name.Print(output);
                }

                output.Space();
                output.Print("as");
                output.Space();
                if (isImport)
                {
                    Name.Print(output);
                }
                else
                {
                    output.Print(ForeignName.Name);
                }
            }
            else
            {
                Name.Print(output);
            }
        }
예제 #18
0
        public override bool NeedParens(OutputContext output)
        {
            var p = output.Parent();

            return(p is AstPropAccess propAccess && propAccess.Expression == this);
        }