Skip to content

Commit

Permalink
Merge pull request #3318 from microsoft/feature/typescript-auto-gener…
Browse files Browse the repository at this point in the history
…ated

feature/typescript auto generated
  • Loading branch information
baywet authored Sep 19, 2023
2 parents e01d1ab + 5f2cb2c commit c700d0e
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 82 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added auto-generated comment for TypeScript generation. [#3244](https://github.com/microsoft/kiota/issues/3244)

### Changed

- Localhost based descriptions are not cached anymore to facilitate development workflows. [#3316](https://github.com/microsoft/kiota/issues/3316)
- Removed redundant undefined qualifier in TypeScript for properties. [#3244](https://github.com/microsoft/kiota/issues/3244)

## [1.6.1] - 2023-09-11

Expand Down
9 changes: 7 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeBlockEndWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
using Kiota.Builder.CodeDOM;

namespace Kiota.Builder.Writers.TypeScript;
public class CodeBlockEndWriter : ICodeElementWriter<BlockEnd>
public class CodeBlockEndWriter : BaseElementWriter<BlockEnd, TypeScriptConventionService>
{
public void WriteCodeElement(BlockEnd codeElement, LanguageWriter writer)
public CodeBlockEndWriter(TypeScriptConventionService conventionService) : base(conventionService)
{
}
public override void WriteCodeElement(BlockEnd codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.Parent is CodeNamespace) return;
writer.CloseBlock();
if (codeElement.Parent?.Parent is CodeNamespace)
conventions.WriteAutoGeneratedEnd(writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.Parent?.Parent is CodeNamespace)
conventions.WriteAutoGeneratedStart(writer);
var parentNamespace = codeElement.GetImmediateParentOfType<CodeNamespace>();
_codeUsingWriter.WriteCodeElement(codeElement.Usings, parentNamespace, writer);

Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Writers/TypeScript/CodeFunctionWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public override void WriteCodeElement(CodeFunction codeElement, LanguageWriter w
if (codeElement.OriginalLocalMethod == null) throw new InvalidOperationException($"{nameof(codeElement.OriginalLocalMethod)} should not be null");
ArgumentNullException.ThrowIfNull(writer);
if (codeElement.Parent is not CodeNamespace) throw new InvalidOperationException("the parent of a function should be a namespace");
conventions.WriteAutoGeneratedStart(writer);
_codeUsingWriter.WriteCodeElement(codeElement.StartBlock.Usings, codeElement.GetImmediateParentOfType<CodeNamespace>(), writer);
var codeMethod = codeElement.OriginalLocalMethod;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;

namespace Kiota.Builder.Writers.TypeScript
namespace Kiota.Builder.Writers.TypeScript;
public class CodeInterfaceDeclarationWriter : BaseElementWriter<InterfaceDeclaration, TypeScriptConventionService>
{
public class CodeInterfaceDeclarationWriter : BaseElementWriter<InterfaceDeclaration, TypeScriptConventionService>
private readonly CodeUsingWriter _codeUsingWriter;
public CodeInterfaceDeclarationWriter(TypeScriptConventionService conventionService, string clientNamespaceName) : base(conventionService)
{
private readonly CodeUsingWriter _codeUsingWriter;
public CodeInterfaceDeclarationWriter(TypeScriptConventionService conventionService, string clientNamespaceName) : base(conventionService)
{
_codeUsingWriter = new(clientNamespaceName);
}
_codeUsingWriter = new(clientNamespaceName);
}

public override void WriteCodeElement(InterfaceDeclaration codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);
public override void WriteCodeElement(InterfaceDeclaration codeElement, LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(codeElement);
ArgumentNullException.ThrowIfNull(writer);

var parentNamespace = codeElement.GetImmediateParentOfType<CodeNamespace>();
_codeUsingWriter.WriteCodeElement(codeElement.Usings, parentNamespace, writer);
var parentNamespace = codeElement.GetImmediateParentOfType<CodeNamespace>();
if (codeElement.Parent?.Parent is CodeNamespace)
conventions.WriteAutoGeneratedStart(writer);
_codeUsingWriter.WriteCodeElement(codeElement.Usings, parentNamespace, writer);

var derivation = codeElement.Implements.Any() ? $" extends {codeElement.Implements.Select(static x => x.Name).Aggregate(static (x, y) => x + ", " + y)}" : string.Empty;
writer.StartBlock($"export interface {codeElement.Name.ToFirstCharacterUpperCase()}{derivation} {{");
}
var derivation = codeElement.Implements.Any() ? $" extends {codeElement.Implements.Select(static x => x.Name).Aggregate(static (x, y) => x + ", " + y)}" : string.Empty;
writer.StartBlock($"export interface {codeElement.Name.ToFirstCharacterUpperCase()}{derivation} {{");
}
}
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w

private static void WriteCodePropertyForInterface(CodeProperty codeElement, LanguageWriter writer, string returnType, bool isFlagEnum)
{
writer.WriteLine($"{codeElement.Name.ToFirstCharacterLowerCase()}?: {returnType}{(isFlagEnum ? "[]" : string.Empty)}{(codeElement.Type.IsNullable ? " | undefined" : string.Empty)};");
writer.WriteLine($"{codeElement.Name.ToFirstCharacterLowerCase()}?: {returnType}{(isFlagEnum ? "[]" : string.Empty)};");
}

private void WriteCodePropertyForClass(CodeProperty codeElement, CodeClass parentClass, LanguageWriter writer, string returnType, bool isFlagEnum)
Expand All @@ -45,7 +45,7 @@ private void WriteCodePropertyForClass(CodeProperty codeElement, CodeClass paren
writer.CloseBlock();
break;
default:
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {codeElement.NamePrefix}{codeElement.Name.ToFirstCharacterLowerCase()}{(codeElement.Type.IsNullable ? "?" : string.Empty)}: {returnType}{(isFlagEnum ? "[]" : string.Empty)}{(codeElement.Type.IsNullable ? " | undefined" : string.Empty)};");
writer.WriteLine($"{conventions.GetAccessModifier(codeElement.Access)} {codeElement.NamePrefix}{codeElement.Name.ToFirstCharacterLowerCase()}{(codeElement.Type.IsNullable ? "?" : string.Empty)}: {returnType}{(isFlagEnum ? "[]" : string.Empty)};");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ public TypeScriptConventionService(LanguageWriter languageWriter)
{
writer = languageWriter;
}
internal void WriteAutoGeneratedStart(LanguageWriter writer)
{
writer.WriteLine("// tslint:disable");
writer.WriteLine("// eslint-disable");
writer.WriteLine("// Generated by Microsoft Kiota");
}
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";
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/TypeScript/TypeScriptWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public TypeScriptWriter(string rootPath, string clientNamespaceName, bool usesBa
PathSegmenter = new TypeScriptPathSegmenter(rootPath, clientNamespaceName);
var conventionService = new TypeScriptConventionService(this);
AddOrReplaceCodeElementWriter(new CodeClassDeclarationWriter(conventionService, clientNamespaceName));
AddOrReplaceCodeElementWriter(new CodeBlockEndWriter());
AddOrReplaceCodeElementWriter(new CodeBlockEndWriter(conventionService));
AddOrReplaceCodeElementWriter(new CodeEnumWriter(conventionService));
AddOrReplaceCodeElementWriter(new CodeMethodWriter(conventionService, usesBackingStore));
AddOrReplaceCodeElementWriter(new CodeFunctionWriter(conventionService, clientNamespaceName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public void Dispose()
GC.SuppressFinalize(this);
}
[Fact]
public void WritesAutoGenerationStart()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
var result = tw.ToString();
Assert.Contains("// eslint-disable", result);
Assert.Contains("// tslint:disable", result);
}
[Fact]
public void WritesSimpleDeclaration()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class CodeClassEndWriterTests : IDisposable
private readonly CodeClass parentClass;
public CodeClassEndWriterTests()
{
codeElementWriter = new CodeBlockEndWriter();
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
codeElementWriter = new CodeBlockEndWriter(new(writer));
tw = new StringWriter();
writer.SetTextWriter(tw);
var root = CodeNamespace.InitRootNamespace();
Expand All @@ -45,12 +45,16 @@ public void ClosesNestedClasses()
codeElementWriter.WriteCodeElement(child.EndBlock, writer);
var result = tw.ToString();
Assert.Equal(1, result.Count(x => x == '}'));
Assert.DoesNotContain("// eslint-enable", result);
Assert.DoesNotContain("// tslint:enable", result);
}
[Fact]
public void ClosesNonNestedClasses()
{
codeElementWriter.WriteCodeElement(parentClass.EndBlock, writer);
var result = tw.ToString();
Assert.Contains("// eslint-enable", result);
Assert.Contains("// tslint:enable", result);
Assert.Equal(1, result.Count(x => x == '}'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ public void Dispose()
GC.SuppressFinalize(this);
}

[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 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);
}
[Fact]
public async Task WritesAutoGenerationEnd()
{
var parentClass = TestHelper.CreateModelClass(root, "parentClass", true);
TestHelper.AddSerializationPropertiesToModelClass(parentClass);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);
var serializeFunction = root.FindChildByName<CodeFunction>($"deserializeInto{parentClass.Name.ToFirstCharacterUpperCase()}");
writer.Write(serializeFunction);
var result = tw.ToString();
Assert.DoesNotContain("// eslint-enable", result); //written by code end block writer
Assert.DoesNotContain("// tslint:enable", result);
}

[Fact]
public async Task WritesModelFactoryBody()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,77 @@
using Kiota.Builder.CodeDOM;
using Xunit;

namespace Kiota.Builder.Writers.TypeScript.Tests
namespace Kiota.Builder.Writers.TypeScript.Tests;
public class CodeInterfaceDeclaraterWriterTests : IDisposable
{
public class CodeInterfaceDeclaraterWriterTests : IDisposable
{
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeInterface parentInterface;
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeInterface parentInterface;

public CodeInterfaceDeclaraterWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
var root = CodeNamespace.InitRootNamespace();
var ns = root.AddNamespace("graphtests.models");
parentInterface = new CodeInterface()
{
Name = "parent"
};
ns.AddInterface(parentInterface);
}
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}
[Fact]
public void WritesSimpleDeclaration()
public CodeInterfaceDeclaraterWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.TypeScript, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
var root = CodeNamespace.InitRootNamespace();
var ns = root.AddNamespace("graphtests.models");
parentInterface = new CodeInterface()
{
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("export interface", result);
}
Name = "parent"
};
ns.AddInterface(parentInterface);
}
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}
[Fact]
public void WritesAutoGenerationStart()
{
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("// eslint-disable", result);
Assert.Contains("// tslint:disable", result);
}
[Fact]
public void WritesSimpleDeclaration()
{
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("export interface", result);
}

[Fact]
public void WritesInheritance()
{
[Fact]
public void WritesInheritance()
{

parentInterface.StartBlock.AddImplements(new CodeType()
{
Name = "someInterface"
});
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("extends", result);
}
[Fact]
public void WritesImports()
parentInterface.StartBlock.AddImplements(new CodeType()
{
Name = "someInterface"
});
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("extends", result);
}
[Fact]
public void WritesImports()
{
parentInterface.StartBlock.AddUsings(new CodeUsing
{
parentInterface.StartBlock.AddUsings(new CodeUsing
Name = "Objects",
Declaration = new()
{
Name = "Objects",
Declaration = new()
{
Name = "util",
IsExternal = true,
}
});
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("import", result);
Assert.Contains("from", result);
Assert.Contains("'util'", result);
}
Name = "util",
IsExternal = true,
}
});
writer.Write(parentInterface.StartBlock);
var result = tw.ToString();
Assert.Contains("import", result);
Assert.Contains("from", result);
Assert.Contains("'util'", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public void WritesCustomProperty()
property.Kind = CodePropertyKind.Custom;
writer.Write(property);
var result = tw.ToString();
Assert.Contains($"{PropertyName}?: {TypeName} | undefined", result);
Assert.Contains($"{PropertyName}?: {TypeName}", result);
Assert.DoesNotContain("| undefined", result); // redundant with ?
}
[Fact]
public void WritesFlagEnums()
Expand Down

0 comments on commit c700d0e

Please sign in to comment.