-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typescript - introducing index files namespace wise fixing circular d…
…ependency (#1274) * record requestoption * adding typescript namespace gen * adding gets enums to namespace * recursive inheritance ordering * checking namespace of parent * adding rendering condition * adding comments * - removes testing file * adding language specific code renderer * relativeimport language specific, model only barrel * extending relativeimportmanager; relative imports only for class * adding unit test for codenamespace writer * resetting files * resetting launcsettings * ussing addinnerclass for test * Apply suggestions from code review Co-authored-by: Vincent Biret <[email protected]> * reset relativeimport code, update working test * missing using * Update tests/Kiota.Builder.Tests/Writers/TypeScript/CodeNamespaceWriterTests.cs Co-authored-by: Mustafa Zengin <[email protected]> * Apply suggestions from code review * - fixes a bug wwhere the root namespace would have a null name causing unit tests to fail * - code linting Signed-off-by: Vincent Biret <[email protected]> * renaming relativeimportmanager test * Update CHANGELOG.md * - moves the changelog entry to the right place Co-authored-by: Vincent Biret <[email protected]> Co-authored-by: Mustafa Zengin <[email protected]>
- Loading branch information
1 parent
ff9526e
commit dbb3616
Showing
13 changed files
with
367 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Linq; | ||
|
||
namespace Kiota.Builder.CodeRenderers | ||
{ | ||
public class TypeScriptCodeRenderer : CodeRenderer | ||
{ | ||
public TypeScriptCodeRenderer(GenerationConfiguration configuration) : base(configuration) { } | ||
public override bool ShouldRenderNamespaceFile(CodeNamespace codeNamespace) | ||
{ | ||
return codeNamespace.Classes.Any(c => c.IsOfKind(CodeClassKind.Model)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/Kiota.Builder/Writers/TypeScript/CodeNameSpaceWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Kiota.Builder.Extensions; | ||
|
||
namespace Kiota.Builder.Writers.TypeScript | ||
{ | ||
public class CodeNameSpaceWriter : BaseElementWriter<CodeNamespace, TypeScriptConventionService> | ||
{ | ||
public CodeNameSpaceWriter(TypeScriptConventionService conventionService) : base(conventionService) { } | ||
|
||
/// <summary> | ||
/// Writes export statements for classes and enums belonging to a namespace into a generated index.ts file. | ||
/// The classes should be export in the order of inheritance so as to avoid circular dependency issues in javascript. | ||
/// </summary> | ||
/// <param name="codeElement">Code element is a code namespace</param> | ||
/// <param name="writer"></param> | ||
public override void WriteCodeElement(CodeNamespace codeElement, LanguageWriter writer) | ||
{ | ||
var sortedClassNames = SortClassesInOrderOfInheritance(codeElement.Classes.ToList()); | ||
|
||
foreach (var className in sortedClassNames) | ||
{ | ||
writer.WriteLine($"export * from './{className.ToFirstCharacterLowerCase()}'"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Visits every child for a given parent class and recursively inserts each class into a list ordered based on inheritance. | ||
/// </summary> | ||
/// <param name="parentListChildren"></param> | ||
/// <param name="visited"></param> | ||
/// <param name="orderedList"> Lis</param> | ||
/// <param name="current"></param> | ||
private void VisitEveryChild(Dictionary<string, List<string>> parentChildrenMap, HashSet<string> visited, List<string> inheritanceOrderList, string current) | ||
{ | ||
if (!visited.Contains(current)) | ||
{ | ||
visited.Add(current); | ||
|
||
foreach (var child in parentChildrenMap[current].Where(x => parentChildrenMap.ContainsKey(x))) | ||
{ | ||
VisitEveryChild(parentChildrenMap, visited, inheritanceOrderList, child); | ||
} | ||
inheritanceOrderList.Insert(0, current); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Orders given list of classes in a namespace based on inheritance. | ||
/// That is, if class B extends class A then A should exported before class B. | ||
/// </summary> | ||
/// <param name="classes"> Classes in a given code namespace</param> | ||
/// <returns> List of class names in the code name space ordered based on inheritance</returns> | ||
private List<string> SortClassesInOrderOfInheritance(List<CodeClass> classes) | ||
{ | ||
var visited = new HashSet<string>(); | ||
var parentChildrenMap = new Dictionary<string, List<string>>(); | ||
var inheritanceOrderList = new List<string>(); | ||
|
||
/* | ||
* 1. Create a dictionary containing all the parent classes. | ||
*/ | ||
foreach (var @class in classes.Where(c => c.IsOfKind(CodeClassKind.Model))) | ||
{ | ||
// Verify if parent class is from the same namespace | ||
var inheritsFrom = @class.Parent.Name.Equals(@class.StartBlock.Inherits?.TypeDefinition?.Parent?.Name, StringComparison.OrdinalIgnoreCase) ? @class.StartBlock.Inherits?.Name : null; | ||
|
||
if (!string.IsNullOrEmpty(inheritsFrom)) | ||
{ | ||
if (!parentChildrenMap.ContainsKey(inheritsFrom)) | ||
{ | ||
parentChildrenMap[inheritsFrom] = new List<string>(); | ||
} | ||
parentChildrenMap[inheritsFrom].Add(@class.Name); | ||
} | ||
} | ||
|
||
/* | ||
* 2. Print the export command for every parent node before the child node. | ||
*/ | ||
foreach (var key in parentChildrenMap.Keys) | ||
{ | ||
VisitEveryChild(parentChildrenMap, visited, inheritanceOrderList, key); | ||
} | ||
|
||
/* | ||
* 3. Print all remaining classes which have not been visted or those do not have any parent/child relationship. | ||
*/ | ||
foreach (var className in classes.Where(c => c.IsOfKind(CodeClassKind.Model) && !visited.Contains(c.Name)).Select(x => x.Name)) | ||
{ | ||
visited.Add(className); | ||
inheritanceOrderList.Add(className); | ||
} | ||
return inheritanceOrderList; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.