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)); }
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)); }
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)); }
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)); }
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)); }