コード例 #1
0
        private ParametrizedCode FormatJavascript(JsExpression node, bool allowObservableResult = true)
        {
            var expr = new JsParenthesizedExpression(node.Clone());

            expr.AcceptVisitor(new KnockoutObservableHandlingVisitor(allowObservableResult));
            JavascriptNullCheckAdder.AddNullChecks(expr);
            expr = new JsParenthesizedExpression((JsExpression)JsTemporaryVariableResolver.ResolveVariables(expr.Expression.Detach()));
            return((StartsWithStatementLikeExpression(expr.Expression) ? expr : expr.Expression).FormatParametrizedScript(niceMode: configuration.Debug));
        }
コード例 #2
0
        public void JsNullCheck_SimpleObjectInvocation()
        {
            var expr =
                new JsIdentifierExpression("a").WithAnnotation(MayBeNullAnnotation.Instance).WithAnnotation(new ViewModelInfoAnnotation(typeof(JsNullChecksTests)))
                .Invoke(new JsIdentifierExpression("G")).WithAnnotation(MayBeNullAnnotation.Instance).WithAnnotation(new ViewModelInfoAnnotation(typeof(JsNullChecksTests)))
                .Invoke();

            expr = JavascriptNullCheckAdder.AddNullChecks(expr);
            var node = JsTemporaryVariableResolver.ResolveVariables(expr);

            Assert.AreEqual("(a&&a(G)||eval)()", node.FormatScript(), node.FormatScript(niceMode: true));
        }
コード例 #3
0
        public void JsNullCheck_SimpleMemeberAccess2()
        {
            var expr =
                new JsIdentifierExpression("a").WithAnnotation(MayBeNullAnnotation.Instance)
                .Member("b").WithAnnotation(MayBeNullAnnotation.Instance)
                .Member("c");

            expr = JavascriptNullCheckAdder.AddNullChecks(expr);
            var node = JsTemporaryVariableResolver.ResolveVariables(expr);

            Assert.AreEqual("function(b){return (b=a==null?null:a.b)==null?null:b.c;}()", node.FormatScript(), node.FormatScript(niceMode: true));
        }
コード例 #4
0
        public void JsNullCheck_SimpleObjectMemeberAccess3()
        {
            var expr =
                new JsIdentifierExpression("a").WithAnnotation(MayBeNullAnnotation.Instance).WithAnnotation(new ViewModelInfoAnnotation(typeof(JsNullChecksTests)))
                .Member("b").Invoke().WithAnnotation(MayBeNullAnnotation.Instance).WithAnnotation(new ViewModelInfoAnnotation(typeof(JsNullChecksTests)))
                .Member("c").Invoke();

            expr = JavascriptNullCheckAdder.AddNullChecks(expr);
            var node = JsTemporaryVariableResolver.ResolveVariables(expr);

            Assert.AreEqual("function(b){return (b=a&&a.b())&&b.c();}()", node.FormatScript(), node.FormatScript(niceMode: true));
        }
コード例 #5
0
        public void NestedConditionals()
        {
            JsExpression expr =
                new JsMemberAccessExpression(
                    new JsConditionalExpression(
                        new JsIdentifierExpression("c"),
                        new JsIdentifierExpression("a"),
                        new JsConditionalExpression(
                            new JsIdentifierExpression("c2"),
                            new JsIdentifierExpression("a2").WithAnnotation(MayBeNullAnnotation.Instance),
                            new JsIdentifierExpression("a3"))),
                    "length");

            expr = JavascriptNullCheckAdder.AddNullChecks(expr);
            var node = JsTemporaryVariableResolver.ResolveVariables(expr);

            Assert.AreEqual("function(b){return (b=c?a:c2?a2:a3)==null?null:b.length;}()", node.FormatScript(), node.FormatScript(niceMode: true));
        }