diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs index 155623381..aff506698 100644 --- a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Writers/CodeWriter.cs @@ -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); } diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs new file mode 100644 index 000000000..c2e5b4d74 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/CodeWriterDeclarationTests.cs @@ -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(), typeof(int), Literal(5)); + + var codeWriter = new CodeWriter(); + localFunctionStatement.Write(codeWriter); + codeWriter = new CodeWriter(); + localFunctionStatement = new DeclareLocalFunctionStatement(declaration, Array.Empty(), 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)); + } + } +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/AlreadyDeclaredVariableIsRenamedInScopeWithConflictingName.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/AlreadyDeclaredVariableIsRenamedInScopeWithConflictingName.cs new file mode 100644 index 000000000..62903fc6f --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/AlreadyDeclaredVariableIsRenamedInScopeWithConflictingName.cs @@ -0,0 +1,5 @@ +if (true) +{ + string foo = "bar"; + int foo0 = 5; +} diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationCanBeUsedTwice.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationCanBeUsedTwice.cs new file mode 100644 index 000000000..ceb120fe1 --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationCanBeUsedTwice.cs @@ -0,0 +1 @@ +int foo() => 5; diff --git a/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationTest.cs b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationTest.cs new file mode 100644 index 000000000..83ec9d99b --- /dev/null +++ b/packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Writers/TestData/CodeWriterDeclarationTests/DeclarationTest.cs @@ -0,0 +1,4 @@ +if (true) +{ + int foo = 5; +}