Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Fix #597 Allow multiple tokens in attributes
Browse files Browse the repository at this point in the history
The issue here is that we were missing tests for the design time code
path. We have tests that the bind-... cases work at runtime but were
missing coverage for the editor.

I took the most relevant set of the tests for running bind-... code and
added them to the tests for codegen.
  • Loading branch information
rynowak authored and SteveSandersonMS committed Apr 19, 2018
1 parent ccf8c34 commit 953a29c
Show file tree
Hide file tree
Showing 120 changed files with 3,995 additions and 885 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
Expand Down Expand Up @@ -411,52 +412,54 @@ public override void WriteComponentAttribute(CodeRenderingContext context, Compo
// to handle here, since there are a few different cases for how an attribute might be structured.
//
// This roughly follows the design of the runtime writer for simplicity.
HtmlContentIntermediateNode htmlNode;
CSharpExpressionIntermediateNode cSharpNode;
if (node.AttributeStructure == AttributeStructure.Minimized)
{
// Do nothing
}
else if (
node.Children.Count != 1 ||
node.Children[0] is HtmlContentIntermediateNode htmlNode && htmlNode.Children.Count != 1 ||
node.Children[0] is CSharpExpressionIntermediateNode cSharpNode && cSharpNode.Children.Count != 1)
else if (node.Children.Count != 1)
{
// We don't expect this to happen, we just want to know if it can.
throw new InvalidOperationException("Attribute nodes should either be minimized or a single content node.");
throw new InvalidOperationException("Attribute nodes should either be minimized or a single type of content." + node.Children[0].ToString());
}
else if (node.BoundAttribute?.IsDelegateProperty() ?? false)
{
// We always surround the expression with the delegate constructor. This makes type
// inference inside lambdas, and method group conversion do the right thing.
IntermediateToken token = null;
var tokens = node.Children;
if ((cSharpNode = node.Children[0] as CSharpExpressionIntermediateNode) != null)
{
token = cSharpNode.Children[0] as IntermediateToken;
}
else
{
token = node.Children[0] as IntermediateToken;
tokens = node.Children[0].Children;
}

if (token != null)
// We always surround the expression with the delegate constructor. This makes type
// inference inside lambdas, and method group conversion do the right thing.
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
context.CodeWriter.Write("new ");
context.CodeWriter.Write(node.BoundAttribute.TypeName);
context.CodeWriter.Write("(");
context.CodeWriter.WriteLine();

for (var i = 0; i < tokens.Count; i++)
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
context.CodeWriter.Write("new ");
context.CodeWriter.Write(node.BoundAttribute.TypeName);
context.CodeWriter.Write("(");
context.CodeWriter.WriteLine();
WriteCSharpToken(context, token);
context.CodeWriter.Write(");");
context.CodeWriter.WriteLine();
WriteCSharpToken(context, (IntermediateToken)tokens[i]);
}

context.CodeWriter.Write(");");
context.CodeWriter.WriteLine();
}
else if ((cSharpNode = node.Children[0] as CSharpExpressionIntermediateNode) != null)
{
// This is the case when an attribute has an explicit C# transition like:
// <MyComponent Foo="@bar" />
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
WriteCSharpToken(context, ((IntermediateToken)cSharpNode.Children[0]));

for (var i = 0; i < cSharpNode.Children.Count; i++)
{
WriteCSharpToken(context, (IntermediateToken)cSharpNode.Children[i]);
}

context.CodeWriter.Write(";");
context.CodeWriter.WriteLine();
}
Expand All @@ -468,7 +471,12 @@ public override void WriteComponentAttribute(CodeRenderingContext context, Compo
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");
WriteCSharpToken(context, token);

for (var i = 0; i < node.Children.Count; i++)
{
WriteCSharpToken(context, (IntermediateToken)node.Children[i]);
}

context.CodeWriter.Write(";");
context.CodeWriter.WriteLine();
}
Expand Down
Loading

0 comments on commit 953a29c

Please sign in to comment.