From 4953c1e82c01e68e135c2e280114342381c06f58 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 20 Sep 2023 14:33:31 -0400 Subject: [PATCH] - refactoring: removes inline type writing in typescript --- .../Writers/TypeScript/CodeMethodWriter.cs | 21 ++++++-------- .../TypeScript/TypeScriptConventionService.cs | 28 ------------------- .../Writers/TypeScript/TypeScriptWriter.cs | 2 +- .../CodeClassDeclarationWriterTests.cs | 2 +- .../TypeScript/CodeClassEndWriterTests.cs | 2 +- .../TypeScript/CodeMethodWriterTests.cs | 3 +- 6 files changed, 12 insertions(+), 46 deletions(-) diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs index e26e45f012..e68845ae38 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs @@ -12,7 +12,6 @@ public CodeMethodWriter(TypeScriptConventionService conventionService, bool uses { _usesBackingStore = usesBackingStore; } - private TypeScriptConventionService? localConventions; private readonly bool _usesBackingStore; public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter writer) @@ -23,8 +22,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri if (codeElement.Parent is CodeFunction) return; if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("the parent of a method should be a class"); - localConventions = new TypeScriptConventionService(writer); //because we allow inline type definitions for methods parameters - var returnType = localConventions.GetTypeString(codeElement.ReturnType, codeElement); + var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement); var isVoid = "void".EqualsIgnoreCase(returnType); WriteMethodDocumentation(codeElement, writer, isVoid); WriteMethodPrototype(codeElement, writer, returnType, isVoid); @@ -126,10 +124,9 @@ internal static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWr private void WriteIndexerBody(CodeMethod codeElement, CodeClass parentClass, string returnType, LanguageWriter writer) { if (parentClass.GetPropertyOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty && - localConventions != null && codeElement.OriginalIndexer != null) { - localConventions.AddParametersAssignment(writer, pathParametersProperty.Type, $"this.{pathParametersProperty.Name}", + conventions.AddParametersAssignment(writer, pathParametersProperty.Type, $"this.{pathParametersProperty.Name}", parameters: (codeElement.OriginalIndexer.IndexParameter.Type, codeElement.OriginalIndexer.IndexParameter.SerializationName, codeElement.OriginalIndexer.IndexParameter.Name.ToFirstCharacterLowerCase())); } conventions.AddRequestBuilderBody(parentClass, returnType, writer, conventions.TempDictionaryVarName); @@ -242,7 +239,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho currentMethod.Parameters.FirstOrDefault(static x => x.IsOfKind(CodeParameterKind.PathParameters)) is CodeParameter pathParametersParam && parentClass.Properties.FirstOrDefaultOfKind(CodePropertyKind.PathParameters) is CodeProperty pathParametersProperty) { - localConventions?.AddParametersAssignment(writer, + conventions.AddParametersAssignment(writer, pathParametersParam.Type.AllTypes.OfType().First(), pathParametersParam.Name.ToFirstCharacterLowerCase(), $"this.{pathParametersProperty.Name.ToFirstCharacterLowerCase()}", @@ -385,7 +382,7 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req private void ComposeContentInRequestGeneratorBody(CodeParameter requestBody, CodeProperty requestAdapterProperty, string contentType, LanguageWriter writer) { - if (requestBody.Type.Name.Equals(localConventions?.StreamTypeName, StringComparison.OrdinalIgnoreCase)) + if (requestBody.Type.Name.Equals(conventions.StreamTypeName, StringComparison.OrdinalIgnoreCase)) { writer.WriteLine($"{RequestInfoVarName}.setStreamContent({requestBody.Name});"); return; @@ -423,8 +420,7 @@ private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, bo private static readonly BaseCodeParameterOrderComparer parameterOrderComparer = new(); private void WriteMethodPrototype(CodeMethod code, LanguageWriter writer, string returnType, bool isVoid) { - if (localConventions != null) - WriteMethodPrototypeInternal(code, writer, returnType, isVoid, localConventions, false); + WriteMethodPrototypeInternal(code, writer, returnType, isVoid, conventions, false); } internal static void WriteMethodPrototypeInternal(CodeMethod code, LanguageWriter writer, string returnType, bool isVoid, TypeScriptConventionService pConventions, bool isFunction) { @@ -455,9 +451,8 @@ internal static void WriteMethodPrototypeInternal(CodeMethod code, LanguageWrite } private string GetFactoryMethodName(CodeTypeBase targetClassType, CodeMethod currentElement, LanguageWriter writer) { - if (localConventions == null) throw new InvalidOperationException("localConventions is null"); - var returnType = localConventions.GetTypeString(targetClassType, currentElement, false, writer); - var targetClassName = localConventions.TranslateType(targetClassType); + var returnType = conventions.GetTypeString(targetClassType, currentElement, false, writer); + var targetClassName = conventions.TranslateType(targetClassType); var resultName = $"create{targetClassName.ToFirstCharacterUpperCase()}FromDiscriminatorValue"; if (targetClassName.Equals(returnType, StringComparison.OrdinalIgnoreCase)) return resultName; @@ -466,7 +461,7 @@ currentType.TypeDefinition is CodeClass definitionClass && definitionClass.GetImmediateParentOfType() is CodeNamespace parentNamespace && parentNamespace.FindChildByName(resultName) is CodeFunction factoryMethod) { - var methodName = localConventions.GetTypeString(new CodeType + var methodName = conventions.GetTypeString(new CodeType { Name = resultName, TypeDefinition = factoryMethod diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs index 5d0d63f64c..9100efc20c 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptConventionService.cs @@ -11,10 +11,6 @@ namespace Kiota.Builder.Writers.TypeScript; public class TypeScriptConventionService : CommonLanguageConventionService { - public TypeScriptConventionService(LanguageWriter languageWriter) - { - writer = languageWriter; - } internal void WriteAutoGeneratedStart(LanguageWriter writer) { writer.WriteLine("// tslint:disable"); @@ -26,7 +22,6 @@ internal void WriteAutoGeneratedEnd(LanguageWriter writer) writer.WriteLine("// tslint:enable"); writer.WriteLine("// eslint-enable"); } - private readonly LanguageWriter writer; public override string StreamTypeName => "ArrayBuffer"; public override string VoidTypeName => "void"; public override string DocCommentPrefix => " * "; @@ -86,8 +81,6 @@ public override string GetTypeString(CodeTypeBase code, CodeElement targetElemen if (code is CodeType currentType) { var typeName = GetTypeAlias(currentType, targetElement) is string alias && !string.IsNullOrEmpty(alias) ? alias : TranslateType(currentType); - if (code.ActionOf) - return WriteInlineDeclaration(currentType, targetElement); return $"{typeName}{collectionSuffix}"; } @@ -104,27 +97,6 @@ private static string GetTypeAlias(CodeType targetType, CodeElement targetElemen return aliasedUsing.Alias; return string.Empty; } - private string WriteInlineDeclaration(CodeType currentType, CodeElement targetElement) - { - writer.IncreaseIndent(4); - var childElements = (currentType?.TypeDefinition as CodeClass) - ?.Properties - ?.OrderBy(x => x.Name) - ?.Select(x => $"{x.Name}?: {GetTypeString(x.Type, targetElement)}"); - var innerDeclaration = childElements?.Any() ?? false ? - LanguageWriter.NewLine + - writer.GetIndent() + - childElements - .Aggregate((x, y) => $"{x};{LanguageWriter.NewLine}{writer.GetIndent()}{y}") - .Replace(';', ',') + - LanguageWriter.NewLine + - writer.GetIndent() - : string.Empty; - writer.DecreaseIndent(); - if (string.IsNullOrEmpty(innerDeclaration)) - return "object"; - return $"{{{innerDeclaration}}}"; - } public override string TranslateType(CodeType type) { diff --git a/src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs b/src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs index d941c5ec81..1ff6e07ca2 100644 --- a/src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs @@ -7,7 +7,7 @@ public class TypeScriptWriter : LanguageWriter public TypeScriptWriter(string rootPath, string clientNamespaceName, bool usesBackingStore = false) { PathSegmenter = new TypeScriptPathSegmenter(rootPath, clientNamespaceName); - var conventionService = new TypeScriptConventionService(this); + var conventionService = new TypeScriptConventionService(); AddOrReplaceCodeElementWriter(new CodeClassDeclarationWriter(conventionService, clientNamespaceName)); AddOrReplaceCodeElementWriter(new CodeBlockEndWriter(conventionService)); AddOrReplaceCodeElementWriter(new CodeEnumWriter(conventionService)); diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassDeclarationWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassDeclarationWriterTests.cs index a33b8f81fe..3abb2c3022 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassDeclarationWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassDeclarationWriterTests.cs @@ -20,7 +20,7 @@ public class CodeClassDeclarationWriterTests : IDisposable public CodeClassDeclarationWriterTests() { writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName); - codeElementWriter = new CodeClassDeclarationWriter(new TypeScriptConventionService(writer), "graphtests"); + codeElementWriter = new CodeClassDeclarationWriter(new TypeScriptConventionService(), "graphtests"); tw = new StringWriter(); writer.SetTextWriter(tw); var root = CodeNamespace.InitRootNamespace(); diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassEndWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassEndWriterTests.cs index 1513b0e70c..f1c1eb388c 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassEndWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeClassEndWriterTests.cs @@ -20,7 +20,7 @@ public class CodeClassEndWriterTests : IDisposable public CodeClassEndWriterTests() { writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName); - codeElementWriter = new CodeBlockEndWriter(new(writer)); + codeElementWriter = new CodeBlockEndWriter(new()); tw = new StringWriter(); writer.SetTextWriter(tw); var root = CodeNamespace.InitRootNamespace(); diff --git a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeMethodWriterTests.cs index 1925687097..eb5b715349 100644 --- a/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/TypeScript/CodeMethodWriterTests.cs @@ -131,7 +131,6 @@ private void AddRequestBodyParameters(bool useComplexTypeForBody = false) { Name = "RequestConfig", TypeDefinition = requestConfigClass, - ActionOf = true, }, Optional = true, }); @@ -392,7 +391,7 @@ public void WritesMethodDescriptionLink() [Fact] public void Defensive() { - var codeMethodWriter = new CodeMethodWriter(new TypeScriptConventionService(writer), false); + var codeMethodWriter = new CodeMethodWriter(new TypeScriptConventionService(), false); Assert.Throws(() => codeMethodWriter.WriteCodeElement(null, writer)); Assert.Throws(() => codeMethodWriter.WriteCodeElement(method, null)); var originalParent = method.Parent;