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

Remove unused imported types for generated typescript. #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions example/ts/src/exampleApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// DO NOT EDIT: generated by fsdgenjs

import { HttpClientUtility, IServiceResult, IHttpClientOptions } from 'facility-core';
import { IExampleApi, IGetWidgetsRequest, IGetWidgetsResponse, ICreateWidgetRequest, ICreateWidgetResponse, IGetWidgetRequest, IGetWidgetResponse, IDeleteWidgetRequest, IDeleteWidgetResponse, IEditWidgetRequest, IEditWidgetResponse, IGetWidgetBatchRequest, IGetWidgetBatchResponse, IGetWidgetWeightRequest, IGetWidgetWeightResponse, IGetPreferenceRequest, IGetPreferenceResponse, ISetPreferenceRequest, ISetPreferenceResponse, IGetInfoRequest, IGetInfoResponse, INotRestfulRequest, INotRestfulResponse, IKitchenRequest, IKitchenResponse, IWidget, IWidgetJob, IPreference, IObsoleteData, IKitchenSink } from './exampleApiTypes';
import { HttpClientUtility, IHttpClientOptions, IServiceResult } from 'facility-core';
import { ICreateWidgetRequest, ICreateWidgetResponse, IDeleteWidgetRequest, IDeleteWidgetResponse, IEditWidgetRequest, IEditWidgetResponse, IExampleApi, IGetInfoRequest, IGetInfoResponse, IGetPreferenceRequest, IGetPreferenceResponse, IGetWidgetBatchRequest, IGetWidgetBatchResponse, IGetWidgetRequest, IGetWidgetResponse, IGetWidgetsRequest, IGetWidgetsResponse, IGetWidgetWeightRequest, IGetWidgetWeightResponse, IKitchenRequest, IKitchenResponse, INotRestfulRequest, INotRestfulResponse, ISetPreferenceRequest, ISetPreferenceResponse } from './exampleApiTypes';
export * from './exampleApiTypes';

/** Provides access to ExampleApi over HTTP via fetch. */
Expand Down
3 changes: 1 addition & 2 deletions example/ts/src/exampleApiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import * as bodyParser from 'body-parser';
import * as express from 'express';
import { IServiceResult } from 'facility-core';
import { IExampleApi, IGetWidgetsRequest, IGetWidgetsResponse, ICreateWidgetRequest, ICreateWidgetResponse, IGetWidgetRequest, IGetWidgetResponse, IDeleteWidgetRequest, IDeleteWidgetResponse, IEditWidgetRequest, IEditWidgetResponse, IGetWidgetBatchRequest, IGetWidgetBatchResponse, IGetWidgetWeightRequest, IGetWidgetWeightResponse, IGetPreferenceRequest, IGetPreferenceResponse, ISetPreferenceRequest, ISetPreferenceResponse, IGetInfoRequest, IGetInfoResponse, INotRestfulRequest, INotRestfulResponse, IKitchenRequest, IKitchenResponse, IWidget, IWidgetJob, IPreference, IObsoleteData, IKitchenSink } from './exampleApiTypes';
import { ICreateWidgetRequest, IDeleteWidgetRequest, IEditWidgetRequest, IExampleApi, IGetInfoRequest, IGetPreferenceRequest, IGetWidgetBatchRequest, IGetWidgetRequest, IGetWidgetsRequest, IGetWidgetWeightRequest, IKitchenRequest, INotRestfulRequest, ISetPreferenceRequest } from './exampleApiTypes';
export * from './exampleApiTypes';

const standardErrorCodes: { [code: string]: number } = {
Expand Down
2 changes: 1 addition & 1 deletion example/ts/src/exampleApiTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT EDIT: generated by fsdgenjs

import { IServiceResult, IServiceError } from 'facility-core';
import { IServiceError, IServiceResult } from 'facility-core';

/** Example service for widgets. */
export interface IExampleApi {
Expand Down
22 changes: 10 additions & 12 deletions src/Facility.CodeGen.JavaScript/JavaScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
string serverFileName = Uncapitalize(moduleName) + "Server" + (TypeScript ? ".ts" : ".js");

var namedTexts = new List<CodeGenFile>();
var typeNames = new List<string>();
var requestTypeNames = new List<string>();
var responseTypeNames = new List<string>();
var dtoTypeNames = new List<string>();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I broke up the types and made this before I realized this wasn't actually being used anywhere.

if (TypeScript)
{
namedTexts.Add(CreateFile(typesFileName, code =>
Expand All @@ -61,7 +63,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)

code.WriteLine();
WriteJsDoc(code, service);
typeNames.Add($"I{capModuleName}");
requestTypeNames.Add($"I{capModuleName}");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't technically a request type, but wherever I needed the request types I also needed this so I included it here.

using (code.Block($"export interface I{capModuleName} {{", "}"))
{
foreach (var httpMethodInfo in httpServiceInfo.Methods)
Expand All @@ -77,14 +79,14 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
foreach (var methodInfo in service.Methods)
{
var requestDtoName = $"{CodeGenUtility.Capitalize(methodInfo.Name)}Request";
typeNames.Add($"I{requestDtoName}");
requestTypeNames.Add($"I{requestDtoName}");
WriteDto(code, new ServiceDtoInfo(
name: requestDtoName,
fields: methodInfo.RequestFields,
summary: $"Request for {CodeGenUtility.Capitalize(methodInfo.Name)}."), service);

var responseDtoName = $"{CodeGenUtility.Capitalize(methodInfo.Name)}Response";
typeNames.Add($"I{responseDtoName}");
responseTypeNames.Add($"I{responseDtoName}");
WriteDto(code, new ServiceDtoInfo(
name: responseDtoName,
fields: methodInfo.ResponseFields,
Expand All @@ -93,7 +95,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)

foreach (var dtoInfo in service.Dtos)
{
typeNames.Add($"I{dtoInfo.Name}");
dtoTypeNames.Add($"I{dtoInfo.Name}");
WriteDto(code, dtoInfo, service);
}
code.WriteLine();
Expand All @@ -119,7 +121,7 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)

if (TypeScript)
{
WriteImports(code, typeNames, $"./{Uncapitalize(moduleName)}Types");
WriteImports(code, requestTypeNames.Concat(responseTypeNames).ToList(), $"./{Uncapitalize(moduleName)}Types");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming the order in which we import things is arbitrary, and we don't care that all the requests are imported first and then response, as opposed to what it was previously import { IRequestOne, IResponseOne, ...IRequestN, IResponseN }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should sort them. Maybe WriteImports should sort them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sorted them alphabetically which maybe is better. Though maybe even better would be to sort them by where they would be used in the file. Which would probably more closely correspond to where they are specified in the fsd file is my guess.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do something like this and replace all this code

		private static void WriteImports(CodeWriter code, IReadOnlyList<string> imports, string from, string fileMinusImports)
		{
			var neededImports = imports.Where(x => fileMinusImports.Contains(x)).ToList();
			if (neededImports.Count != 0)
				code.WriteLine($"import {{ {string.Join(", ", neededImports.OrderBy(x => fileMinusImports.IndexOf(x)))} }} from '{from}';");
		}

though I'd actually have to use a regular expression instead of Contains and IndexOf to ensure IFoo doesn't match IFooBar and I'd have to create a separate StringWriter to get fileMinusImports, which all seems worse.

code.WriteLine($"export * from './{Uncapitalize(moduleName)}Types';");
}

Expand Down Expand Up @@ -314,13 +316,9 @@ protected override CodeGenOutput GenerateOutputCore(ServiceInfo service)
code.WriteLine();
code.WriteLine("import * as bodyParser from 'body-parser';");
code.WriteLine("import * as express from 'express';");
var facilityImports = new List<string>();
if (TypeScript)
facilityImports.Add("IServiceResult");
WriteImports(code, facilityImports, "facility-core");
if (TypeScript)
{
WriteImports(code, typeNames, $"./{Uncapitalize(moduleName)}Types");
WriteImports(code, requestTypeNames, $"./{Uncapitalize(moduleName)}Types");
code.WriteLine($"export * from './{Uncapitalize(moduleName)}Types';");
}

Expand Down Expand Up @@ -628,7 +626,7 @@ private static void WriteJsDoc(CodeWriter code, IReadOnlyList<string> lines)
private static void WriteImports(CodeWriter code, IReadOnlyList<string> imports, string from)
{
if (imports.Count != 0)
code.WriteLine($"import {{ {string.Join(", ", imports)} }} from '{from}';");
code.WriteLine($"import {{ {string.Join(", ", imports.OrderBy(x => x))} }} from '{from}';");
}

private static string Uncapitalize(string value)
Expand Down