Exemple #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));
        }
Exemple #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));
        }
Exemple #3
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));
        }
Exemple #4
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));
        }
Exemple #5
0
 public string CompileBinding(Func<Dictionary<string, Expression>, Expression> expr, Type[] contexts)
 {
     var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[]{
         new BindingPageInfoExtensionParameter()
         });
     for (int i = 1; i < contexts.Length; i++)
     {
         context = DataContextStack.Create(contexts[i], context);
     }
     var expressionTree = expr(BindingExpressionBuilder.GetParameters(context).ToDictionary(e => e.Name, e => (Expression)e));
     var configuration = DotvvmTestHelper.CreateConfiguration();
     var jsExpression = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService<JavascriptTranslator>().CompileToJavascript(expressionTree, context));
     jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
     JsTemporaryVariableResolver.ResolveVariables(jsExpression);
     return JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression);
 }
        public string CompileBinding(string expression, Type[] contexts, Type expectedType)
        {
            var context = DataContextStack.Create(contexts.FirstOrDefault() ?? typeof(object), extensionParameters: new BindingExtensionParameter[] {
                new BindingPageInfoExtensionParameter(),
            }.Concat(configuration.Markup.DefaultExtensionParameters).ToArray());

            for (int i = 1; i < contexts.Length; i++)
            {
                context = DataContextStack.Create(contexts[i], context);
            }
            var parser         = new BindingExpressionBuilder();
            var expressionTree = TypeConversion.ImplicitConversion(parser.Parse(expression, context, BindingParserOptions.Create <ValueBindingExpression>()), expectedType, true, true);
            var jsExpression   = new JsParenthesizedExpression(configuration.ServiceProvider.GetRequiredService <JavascriptTranslator>().CompileToJavascript(expressionTree, context));

            jsExpression.AcceptVisitor(new KnockoutObservableHandlingVisitor(true));
            JsTemporaryVariableResolver.ResolveVariables(jsExpression);
            return(JavascriptTranslator.FormatKnockoutScript(jsExpression.Expression));
        }
Exemple #7
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));
        }