Skip to content

Commit

Permalink
Merge branch 'main' into feat/ts-add-codefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev authored Sep 21, 2023
2 parents b480e36 + ba2f65d commit cc1b17a
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/graphprojects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@0914d50df753bbc42180d982a6550f195390069f
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a
with:
app_id: ${{ secrets.GRAPHBOT_APP_ID }}
private_key: ${{ secrets.GRAPHBOT_APP_PEM }}
Expand Down
6 changes: 3 additions & 3 deletions it/python/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ tomlkit==0.12.1 ; python_version >= '3.7'

typing-extensions==4.8.0 ; python_version >= '3.7'

urllib3==2.0.4 ; python_version >= '3.7'
urllib3==2.0.5 ; python_version >= '3.7'

wrapt==1.15.0 ; python_version < '3.11'

Expand All @@ -82,7 +82,7 @@ azure-identity==1.14.0

cffi==1.15.1

cryptography==41.0.3 ; python_version >= '3.7'
cryptography==41.0.4 ; python_version >= '3.7'

frozenlist==1.4.0 ; python_version >= '3.7'

Expand All @@ -98,7 +98,7 @@ httpx[http2]==0.25.0

hyperframe==6.0.1 ; python_full_version >= '3.6.1'

microsoft-kiota-abstractions==0.8.5
microsoft-kiota-abstractions==0.8.6

microsoft-kiota-authentication-azure==0.3.0

Expand Down
8 changes: 4 additions & 4 deletions it/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion it/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"prettier": "./.prettierrc.json",
"devDependencies": {
"@es-exec/esbuild-plugin-start": "^0.0.5",
"@types/node": "^20.6.2",
"@types/node": "^20.6.3",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"esbuild": "^0.19.3",
Expand Down
21 changes: 8 additions & 13 deletions src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<CodeType>().First(),
pathParametersParam.Name.ToFirstCharacterLowerCase(),
$"this.{pathParametersProperty.Name.ToFirstCharacterLowerCase()}",
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -466,7 +461,7 @@ currentType.TypeDefinition is CodeClass definitionClass &&
definitionClass.GetImmediateParentOfType<CodeNamespace>() is CodeNamespace parentNamespace &&
parentNamespace.FindChildByName<CodeFunction>(resultName) is CodeFunction factoryMethod)
{
var methodName = localConventions.GetTypeString(new CodeType
var methodName = conventions.GetTypeString(new CodeType
{
Name = resultName,
TypeDefinition = factoryMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 => " * ";
Expand Down Expand Up @@ -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}";
}

Expand All @@ -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)
{
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 @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ private void AddRequestBodyParameters(bool useComplexTypeForBody = false)
{
Name = "RequestConfig",
TypeDefinition = requestConfigClass,
ActionOf = true,
},
Optional = true,
});
Expand Down Expand Up @@ -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<ArgumentNullException>(() => codeMethodWriter.WriteCodeElement(null, writer));
Assert.Throws<ArgumentNullException>(() => codeMethodWriter.WriteCodeElement(method, null));
var originalParent = method.Parent;
Expand Down
14 changes: 7 additions & 7 deletions vscode/microsoft-kiota/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vscode/microsoft-kiota/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
"adm-zip": "^0.5.10",
"is-online": "^10.0.0",
"original-fs": "^1.2.0",
"vscode-jsonrpc": "^8.1.0"
"vscode-jsonrpc": "^8.2.0"
},
"preview": true,
"runtimeDependencies": [
Expand Down

0 comments on commit cc1b17a

Please sign in to comment.