Skip to content

Commit

Permalink
Merge pull request #167 from backfromexile/import-type
Browse files Browse the repository at this point in the history
Use `import type` for type-only imports
  • Loading branch information
nenoNaninu authored Feb 1, 2024
2 parents 8a270ad + 96686fc commit 07b3f92
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/TypedSignalR.Client.TypeScript/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public IReadOnlyList<GeneratedSourceCode> Generate(IReadOnlyList<INamedTypeSymbo
private string GenerateHeader(IReadOnlyList<INamedTypeSymbol> hubTypes, IReadOnlyList<INamedTypeSymbol> receiverTypes)
{
var sb = new StringBuilder();
sb.AppendLine(@"import { HubConnection, IStreamResult, Subject } from '@microsoft/signalr';");
sb.AppendLine(@"import type { HubConnection, IStreamResult, Subject } from '@microsoft/signalr';");

var interfaceLookup = hubTypes.Concat(receiverTypes)
.Distinct<INamedTypeSymbol>(SymbolEqualityComparer.Default)
.ToLookup<INamedTypeSymbol, INamespaceSymbol>(static x => x.ContainingNamespace, SymbolEqualityComparer.Default);

foreach (var group in interfaceLookup)
{
sb.AppendLine($"import {{ {string.Join(", ", group.Select(x => x.Name))} }} from './{group.Key.ToDisplayString()}';");
sb.AppendLine($"import type {{ {string.Join(", ", group.Select(x => x.Name))} }} from './{group.Key.ToDisplayString()}';");
}

var hubParametersAndReturnTypes = hubTypes
Expand Down Expand Up @@ -82,7 +82,7 @@ private string GenerateHeader(IReadOnlyList<INamedTypeSymbol> hubTypes, IReadOnl
// TypedSignalR.Client.TypeScript creates a directory named TypedSignalR.Client in the specified directory
// and generates TypeScript files there. (e.g. generated/TypedSignalR.Client/index.ts)
// Therefore, in order to refer to the TypeScript file created by Tapper, we have to specify the directory one level up.
sb.AppendLine($"import {{ {string.Join(", ", groupingType.Select(x => x.Name))} }} from '../{groupingType.Key.ToDisplayString()}';");
sb.AppendLine($"import type {{ {string.Join(", ", groupingType.Select(x => x.Name))} }} from '../{groupingType.Key.ToDisplayString()}';");
}

return sb.ToString();
Expand Down
4 changes: 2 additions & 2 deletions src/TypedSignalR.Client.TypeScript/InterfaceTranspiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void AddHeader(IGrouping<INamespaceSymbol, INamedTypeSymbol> interfaceTy
codeWriter.AppendLine("/* eslint-disable */");
codeWriter.AppendLine("/* tslint:disable */");
codeWriter.AppendLine("// @ts-nocheck");
codeWriter.AppendLine("import { IStreamResult, Subject } from '@microsoft/signalr';");
codeWriter.AppendLine("import type { IStreamResult, Subject } from '@microsoft/signalr';");

var appearTypes = interfaceTypes
.SelectMany(static x => x.GetMethods())
Expand All @@ -80,7 +80,7 @@ private void AddHeader(IGrouping<INamespaceSymbol, INamedTypeSymbol> interfaceTy
// TypedSignalR.Client.TypeScript creates a directory named TypedSignalR.Client in the specified directory
// and generates TypeScript files there. (e.g. generated/TypedSignalR.Client/index.ts)
// Therefore, in order to refer to the TypeScript file created by Tapper, we have to specify the directory one level up.
codeWriter.AppendLine($"import {{ {string.Join(", ", groupingType.Select(x => x.Name))} }} from '../{groupingType.Key.ToDisplayString()}';");
codeWriter.AppendLine($"import type {{ {string.Join(", ", groupingType.Select(x => x.Name))} }} from '../{groupingType.Key.ToDisplayString()}';");
}

codeWriter.AppendLine();
Expand Down

0 comments on commit 07b3f92

Please sign in to comment.