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
Changes from 1 commit
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
20 changes: 9 additions & 11 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