Allow same CodeWriterDeclaration to be written twice (#3854)
This came up when testing out the plugin extensibility where a visitor would write out a method body statement. The code declaration would be declared twice - once when actually writing the library, and the second time when writing the method body statement from the visitor.
This commit is contained in:
Родитель
27ed13de58
Коммит
9c282369f9
|
@ -666,7 +666,11 @@ namespace Microsoft.Generator.CSharp
|
|||
throw new InvalidOperationException("Can't declare variables inside documentation.");
|
||||
}
|
||||
|
||||
declaration.SetActualName(GetTemporaryVariable(declaration.RequestedName));
|
||||
if (!declaration.HasBeenDeclared)
|
||||
{
|
||||
declaration.SetActualName(GetTemporaryVariable(declaration.RequestedName));
|
||||
}
|
||||
|
||||
return WriteDeclaration(declaration.ActualName);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using Microsoft.Generator.CSharp.Expressions;
|
||||
using Microsoft.Generator.CSharp.Primitives;
|
||||
using Microsoft.Generator.CSharp.Providers;
|
||||
using Microsoft.Generator.CSharp.Statements;
|
||||
using NUnit.Framework;
|
||||
using static Microsoft.Generator.CSharp.Snippets.Snippet;
|
||||
|
||||
namespace Microsoft.Generator.CSharp.Tests.Writers
|
||||
{
|
||||
public class CodeWriterDeclarationTests
|
||||
{
|
||||
[Test]
|
||||
public void DeclarationTest()
|
||||
{
|
||||
var expr = Declare(new VariableExpression(typeof(int), "foo"), Literal(5));
|
||||
var ifStatement = new IfStatement(Literal(true)) { expr };
|
||||
var codeWriter = new CodeWriter();
|
||||
ifStatement.Write(codeWriter);
|
||||
Assert.AreEqual(Helpers.GetExpectedFromFile(), codeWriter.ToString(false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeclarationCanBeUsedTwice()
|
||||
{
|
||||
var declaration = new CodeWriterDeclaration("foo");
|
||||
var localFunctionStatement = new DeclareLocalFunctionStatement(declaration, Array.Empty<ParameterProvider>(), typeof(int), Literal(5));
|
||||
|
||||
var codeWriter = new CodeWriter();
|
||||
localFunctionStatement.Write(codeWriter);
|
||||
codeWriter = new CodeWriter();
|
||||
localFunctionStatement = new DeclareLocalFunctionStatement(declaration, Array.Empty<ParameterProvider>(), typeof(int), Literal(5));
|
||||
localFunctionStatement.Write(codeWriter);
|
||||
Assert.AreEqual(Helpers.GetExpectedFromFile(), codeWriter.ToString(false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Ignore("https://github.com/microsoft/typespec/issues/3853")]
|
||||
public void AlreadyDeclaredVariableIsRenamedInScopeWithConflictingName()
|
||||
{
|
||||
var expr = Declare(new VariableExpression(typeof(int), "foo"), Literal(5));
|
||||
var usingExpr = new IfStatement(Literal(true)) { expr };
|
||||
var codeWriter = new CodeWriter();
|
||||
usingExpr.Write(codeWriter);
|
||||
|
||||
var ifStatement = new IfStatement(Literal(true))
|
||||
{
|
||||
Declare(new VariableExpression(typeof(string), "foo"), Literal("bar")),
|
||||
expr
|
||||
};
|
||||
codeWriter = new CodeWriter();
|
||||
ifStatement.Write(codeWriter);
|
||||
Assert.AreEqual(Helpers.GetExpectedFromFile(), codeWriter.ToString(false));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
if (true)
|
||||
{
|
||||
string foo = "bar";
|
||||
int foo0 = 5;
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
int foo() => 5;
|
|
@ -0,0 +1,4 @@
|
|||
if (true)
|
||||
{
|
||||
int foo = 5;
|
||||
}
|
Загрузка…
Ссылка в новой задаче