提交 9ea3dde4 编写于 作者: B Balaji Krishnan

Fix a null reference in BaseFormattingRule...

... that was introduced with PR #4644

Method `SomeParentHasMissingCloseBrace` expects a fully formed node that
has a compilation unit, which a roslyn document would have, but this
code is also in a formatter public api path, that can possibly `an
arbitrary` syntax node that may not be associated with a syntaxtree or a
compilation unit.

The fix is simple, it adds a missing null check to this method.
上级 56034006
......@@ -267,5 +267,18 @@ protected async Task AssertFormatWithBaseIndentAsync(string expected, string mar
new List<TextSpan> { span },
baseIndentation: baseIndentation);
}
/// <summary>
/// Asserts formatting on an arbitrary <see cref="SyntaxNode"/> that is not part of a <see cref="SyntaxTree"/>
/// </summary>
/// <param name="node">the <see cref="SyntaxNode"/> to format.</param>
/// <remarks>uses an <see cref="AdhocWorkspace"/> for formatting context, since the <paramref name="node"/> is not associated with a <see cref="SyntaxTree"/> </remarks>
protected async Task AssertFormatOnArbitraryNodeAsync(SyntaxNode node, string expected)
{
var result = await Formatter.FormatAsync(node, new AdhocWorkspace());
var actual = result.GetText().ToString();
Assert.Equal(expected, actual);
}
}
}
......@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Editor.Commands;
using Microsoft.CodeAnalysis.Editor.Implementation.Formatting;
using Microsoft.CodeAnalysis.Editor.Options;
......@@ -534,8 +535,6 @@ static void Main(int a, int b)
await AssertFormatAfterTypeCharAsync(code, expected);
}
[WorkItem(449, "https://github.com/dotnet/roslyn/issues/449")]
[WorkItem(1077103, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1077103")]
[WpfFact, Trait(Traits.Feature, Traits.Features.Formatting)]
......@@ -1405,6 +1404,18 @@ public void M()
await AssertFormatAfterTypeCharAsync(code, expected);
}
[WorkItem(11642, "https://github.com/dotnet/roslyn/issues/11642")]
[WpfFact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatArbitraryNodeParenthesizedLambdaExpression()
{
// code equivalent to an expression synthesized like so:
// ParenthesizedExpression(ParenthesizedLambdaExpression(ParameterList(), Block()))
var code = @"(()=>{})";
var node = SyntaxFactory.ParseExpression(code);
var expected = @"(() => { })";
await AssertFormatOnArbitraryNodeAsync(node, expected);
}
private static async Task AssertFormatAfterTypeCharAsync(string code, string expected, Dictionary<OptionKey, object> changedOptionSet = null)
{
using (var workspace = await TestWorkspace.CreateCSharpAsync(code))
......
......@@ -257,7 +257,7 @@ protected void AddBraceSuppressOperations(List<SuppressOperation> list, SyntaxNo
private bool SomeParentHasMissingCloseBrace(SyntaxNode node)
{
while (node.Kind() != SyntaxKind.CompilationUnit)
while (node != null && node.Kind() != SyntaxKind.CompilationUnit)
{
var bracePair = node.GetBracePair();
if (bracePair.Item2.IsMissing)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册