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

Don't output variables that are not used in typescript service definition #38

Merged
merged 1 commit into from
Feb 27, 2018
Merged
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
36 changes: 27 additions & 9 deletions src/ts/fileDescriptorTSServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import {FileDescriptorProto} from "google-protobuf/google/protobuf/descriptor_pb
import {WellKnownTypesMap} from "../WellKnown";
import {getFieldType, MESSAGE_TYPE} from "./FieldTypes";

function isUsed(fileDescriptor: FileDescriptorProto, pseudoNamespace: string, exportMap: ExportMap) {
return fileDescriptor.getServiceList().some(service => {
return service.getMethodList().some(method => {
const requestMessageTypeName = getFieldType(MESSAGE_TYPE, method.getInputType().slice(1), "", exportMap);
const responseMessageTypeName = getFieldType(MESSAGE_TYPE, method.getOutputType().slice(1), "", exportMap);
const namespacePackage = pseudoNamespace + ".";
return (
requestMessageTypeName.indexOf(namespacePackage) === 0 ||
responseMessageTypeName.indexOf(namespacePackage) === 0
);
});
});
}

export function printFileDescriptorTSServices(fileDescriptor: FileDescriptorProto, exportMap: ExportMap) {
if (fileDescriptor.getServiceList().length === 0) {
return "";
Expand All @@ -23,15 +37,19 @@ export function printFileDescriptorTSServices(fileDescriptor: FileDescriptorProt
const asPseudoNamespace = filePathToPseudoNamespace(fileName);
printer.printLn(`import * as ${asPseudoNamespace} from "${upToRoot}${filePathFromProtoWithoutExtension(fileName)}";`);

fileDescriptor.getDependencyList().forEach((dependency: string) => {
const pseudoNamespace = filePathToPseudoNamespace(dependency);
if (dependency in WellKnownTypesMap) {
printer.printLn(`import * as ${pseudoNamespace} from "${WellKnownTypesMap[dependency]}";`);
} else {
const filePath = filePathFromProtoWithoutExtension(dependency);
printer.printLn(`import * as ${pseudoNamespace} from "${upToRoot + filePath}";`);
}
});
fileDescriptor.getDependencyList()
.filter((dependency: string) => {
return isUsed(fileDescriptor, filePathToPseudoNamespace(dependency), exportMap);
})
.forEach((dependency: string) => {
const pseudoNamespace = filePathToPseudoNamespace(dependency);
if (dependency in WellKnownTypesMap) {
printer.printLn(`import * as ${pseudoNamespace} from "${WellKnownTypesMap[dependency]}";`);
} else {
const filePath = filePathFromProtoWithoutExtension(dependency);
printer.printLn(`import * as ${pseudoNamespace} from "${upToRoot + filePath}";`);
}
});

fileDescriptor.getServiceList().forEach(service => {
printer.printLn(`export class ${service.getName()} {`);
Expand Down
4 changes: 4 additions & 0 deletions test/proto/examplecom/simple_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ package examplecom;

import "othercom/external_child_message.proto";

// this import should not be output in the generated typescript service
import "google/protobuf/timestamp.proto";

message UnaryRequest {
int64 some_int64 = 1;
google.protobuf.Timestamp some_timestamp = 2;
}

message StreamRequest {
Expand Down
9 changes: 9 additions & 0 deletions test/ts_test/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {resolve} from "path";
import {readFileSync} from "fs";
import {assert} from "chai";
import * as simple_service_pb_service from "../generated/examplecom/simple_service_pb_service";
import * as simple_service_pb from "../generated/examplecom/simple_service_pb";
Expand All @@ -21,4 +23,11 @@ describe("ts service", () => {
assert.strictEqual(simple_service_pb_service.SimpleService.DoStream.requestType, simple_service_pb.StreamRequest);
assert.strictEqual(simple_service_pb_service.SimpleService.DoStream.responseType, external_child_message_pb.ExternalChildMessage);
});
it("should not output imports for namespaces that are not used in the service definition", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

A better solution is to pull

fileDescriptor.getDependencyList().forEach((dependency: string) => {
const pseudoNamespace = filePathToPseudoNamespace(dependency);
if (dependency in WellKnownTypesMap) {
printer.printLn(`import * as ${pseudoNamespace} from "${WellKnownTypesMap[dependency]}";`);
} else {
const filePath = filePathFromProtoWithoutExtension(dependency);
printer.printLn(`import * as ${pseudoNamespace} from "${upToRoot + filePath}";`);
}
});

into a separate method which accepts the fileDescriptor. We can then mock the fileDescriptor and assert the output string doesn't contain the import.

I realize most of that testing style isn't in place so your solution will do for now and we can address it later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree this is a far better solution and happy to make a subsequent PR to try to improve the tests.

const generatedService = readFileSync(resolve(__dirname, "../../generated/examplecom/simple_service_pb_service.ts"), "utf8");
assert.notInclude(generatedService, "google-protobuf/google/protobuf/timestamp_pb");

const generatedProto = readFileSync(resolve(__dirname, "../../generated/examplecom/simple_service_pb.js"), "utf8");
assert.include(generatedProto, "google-protobuf/google/protobuf/timestamp_pb");
});
});