Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use import type for the typescript abstraction library code where relevant #3040

Merged
merged 8 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public bool IsExternal
{
get => Declaration?.IsExternal ?? true;
}
public bool IsErasable
{
get; set;
}
public string Alias { get; set; } = string.Empty;
public object Clone()
{
Expand All @@ -25,6 +29,7 @@ public object Clone()
Alias = Alias,
Name = Name,
Parent = Parent,
IsErasable = IsErasable,
};
}
}
7 changes: 5 additions & 2 deletions src/Kiota.Builder/Refiners/AdditionalUsingEvaluator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;

using System.Runtime.InteropServices;
baywet marked this conversation as resolved.
Show resolved Hide resolved
using Kiota.Builder.CodeDOM;

namespace Kiota.Builder.Refiners;

#pragma warning disable CA1819
public record AdditionalUsingEvaluator(Func<CodeElement, bool> CodeElementEvaluator, string NamespaceName, params string[] ImportSymbols);
public record AdditionalUsingEvaluator(Func<CodeElement, bool> CodeElementEvaluator, string NamespaceName, bool IsErasable = false, params string[] ImportSymbols)
{
public AdditionalUsingEvaluator(Func<CodeElement, bool> CodeElementEvaluator, string NamespaceName, params string[] ImportSymbols) : this(CodeElementEvaluator, NamespaceName, false, ImportSymbols) { }
}
#pragma warning restore CA1819
1 change: 1 addition & 0 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ private static IEnumerable<CodeUsing> usingSelector(AdditionalUsingEvaluator x)
{
Name = y,
Declaration = new CodeType { Name = x.NamespaceName, IsExternal = true },
IsErasable = false,
});
protected static void AddDefaultImports(CodeElement current, IEnumerable<AdditionalUsingEvaluator> evaluators)
{
Expand Down
30 changes: 19 additions & 11 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,30 +192,38 @@ currentInterface.StartBlock is InterfaceDeclaration interfaceDeclaration &&
}
private const string GuidPackageName = "guid-typescript";
private const string AbstractionsPackageName = "@microsoft/kiota-abstractions";
// for Kiota abstration library if the code is not required for runtime purposes e.g. interfaces then the IsErassable flag is set to true
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsPackageName, "RequestAdapter"),
AbstractionsPackageName, true, "RequestAdapter"),
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Options),
AbstractionsPackageName, "RequestOption"),
AbstractionsPackageName, true, "RequestOption"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
AbstractionsPackageName, "HttpMethod", "RequestInformation", "RequestOption"),
AbstractionsPackageName, false, "HttpMethod", "RequestInformation"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestGenerator),
AbstractionsPackageName, true, "RequestOption"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Serializer),
AbstractionsPackageName, "SerializationWriter"),
AbstractionsPackageName, true,"SerializationWriter"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.Deserializer, CodeMethodKind.Factory),
AbstractionsPackageName, "ParseNode"),
AbstractionsPackageName, true, "ParseNode"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility),
AbstractionsPackageName, "getPathParameters"),
AbstractionsPackageName, false, "getPathParameters"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor),
AbstractionsPackageName, "Parsable", "ParsableFactory"),
AbstractionsPackageName, true, "Parsable", "ParsableFactory"),
new (x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model),
AbstractionsPackageName, "Parsable"),
AbstractionsPackageName, true, "Parsable"),
new (x => x is CodeClass @class && @class.IsOfKind(CodeClassKind.Model) && @class.Properties.Any(x => x.IsOfKind(CodePropertyKind.AdditionalData)),
AbstractionsPackageName, "AdditionalDataHolder"),
AbstractionsPackageName, true, "AdditionalDataHolder"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) &&
method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)),
AbstractionsPackageName, true, "BackingStoreFactory"),
new (x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.ClientConstructor) &&
method.Parameters.Any(y => y.IsOfKind(CodeParameterKind.BackingStore)),
AbstractionsPackageName, "BackingStoreFactory", "BackingStoreFactorySingleton"),
AbstractionsPackageName, false, "BackingStoreFactorySingleton"),
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
AbstractionsPackageName, true, "BackingStore", "BackedModel"),
new (x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.BackingStore),
AbstractionsPackageName, "BackingStore", "BackedModel", "BackingStoreFactorySingleton"),
AbstractionsPackageName, false, "BackingStoreFactorySingleton"),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) && method.Parameters.Any(static y => y.IsOfKind(CodeParameterKind.RequestBody) && y.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase)),
AbstractionsPackageName, MultipartBodyClassName, $"serialize{MultipartBodyClassName}")
};
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeUsingWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public void WriteCodeElement(IEnumerable<CodeUsing> usings, CodeNamespace parent
**/
private static bool GetShouldUseTypeImport(CodeUsing codeUsing)
{
// Check if codeUsing.Declaration is an instance of CodeType and if codeType.TypeDefinition is an instance of CodeInterface
return codeUsing.Declaration is CodeType codeType && codeType.TypeDefinition is CodeInterface;
// Check if codeUsing is Erassable or codeUsing.Declaration is an instance of CodeType and if codeType.TypeDefinition is an instance of CodeInterface
return codeUsing.IsErasable || codeUsing.Declaration is CodeType codeType && codeType.TypeDefinition is CodeInterface;
}

private static string GetAliasedSymbol(string symbol, string alias)
Expand Down
Loading