Skip to content

Commit

Permalink
Fix ts codefile linting
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Sep 26, 2023
1 parent 273361c commit 026c198
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Kiota.Builder/Writers/TypeScript/CodeFileBlockEndWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using Kiota.Builder.CodeDOM;
namespace Kiota.Builder.Writers.TypeScript;

public class CodeFileBlockEndWriter : BaseElementWriter<CodeFileBlockEnd, TypeScriptConventionService>
{
public CodeFileBlockEndWriter(TypeScriptConventionService conventionService) : base(conventionService)
{
}

public override void WriteCodeElement(CodeFileBlockEnd codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
conventions.WriteAutoGeneratedEnd(writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override void WriteCodeElement(CodeFileDeclaration codeElement, LanguageW
};
}
);
conventions.WriteAutoGeneratedStart(writer);
_codeUsingWriter.WriteCodeElement(usings, ns, writer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w

if (codeElement.Parent is not CodeNamespace && codeElement.Parent is not CodeFile) throw new InvalidOperationException("the parent of a function should be a namespace or file");

conventions.WriteAutoGeneratedStart(writer);
if (codeElement.Parent is CodeNamespace)
{
conventions.WriteAutoGeneratedStart(writer);
_codeUsingWriter.WriteCodeElement(codeElement.StartBlock.Usings, codeElement.GetImmediateParentOfType<CodeNamespace>(), writer);
}

Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public TypeScriptWriter(string rootPath, string clientNamespaceName, bool usesBa
AddOrReplaceCodeElementWriter(new CodeTypeWriter(conventionService));
AddOrReplaceCodeElementWriter(new CodeNameSpaceWriter(conventionService));
AddOrReplaceCodeElementWriter(new CodeInterfaceDeclarationWriter(conventionService, clientNamespaceName));
AddOrReplaceCodeElementWriter(new CodeFileBlockEndWriter(conventionService));
AddOrReplaceCodeElementWriter(new CodeFileDeclarationWriter(conventionService, clientNamespaceName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.Refiners;
using Kiota.Builder.Writers;
using Xunit;

namespace Kiota.Builder.Tests.Writers.TypeScript;

public class CodeFileWriterTests
{
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeNamespace root;
private const string MethodName = "methodName";
private const string ReturnTypeName = "Somecustomtype";

public CodeFileWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
}

[Fact]
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}

private void WriteCode(LanguageWriter writer, CodeElement element)
{
writer.Write(element);
if (element is not CodeNamespace)
foreach (var childElement in element.GetChildElements()
.Order(new CodeElementOrderComparer()))
{
WriteCode(writer, childElement);
}

}

[Fact]
public async Task WritesAutoGenerationStart()
{
var parentClass = TestHelper.CreateModelClass(root, "parentClass", true);
TestHelper.AddSerializationPropertiesToModelClass(parentClass);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);
var codeFile = root.FindChildByName<CodeFile>(parentClass.Name.ToFirstCharacterUpperCase());
WriteCode(writer, codeFile);

var result = tw.ToString();
Assert.Contains("// eslint-disable", result);
Assert.Contains("// tslint:disable", result);
Assert.Contains("export function deserializeIntoParentClass", result);
Assert.Contains("export interface ParentClass", result);
Assert.Contains("export function serializeParentClass", result);
Assert.Contains("// eslint-enable", result);
Assert.Contains("// tslint:enable", result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public async Task WritesAutoGenerationStart()
var serializeFunction = root.FindChildByName<CodeFunction>($"deserializeInto{parentClass.Name.ToFirstCharacterUpperCase()}");
writer.Write(serializeFunction);
var result = tw.ToString();
Assert.Contains("// eslint-disable", result);
Assert.Contains("// tslint:disable", result);
Assert.DoesNotContain("// eslint-disable", result);
Assert.DoesNotContain("// tslint:disable", result);
}
[Fact]
public async Task WritesAutoGenerationEnd()
Expand Down

0 comments on commit 026c198

Please sign in to comment.