Skip to content

Commit

Permalink
Dont output variables that are not used in typescript service definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbretman committed Feb 26, 2018
1 parent 0135ec7 commit 6efe91e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ts/fileDescriptorTSServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ 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);
if (requestMessageTypeName.indexOf(pseudoNamespace + ".") === 0) {
return true;
}
if (responseMessageTypeName.indexOf(pseudoNamespace + ".") === 0) {
return true;
}
return false;
});
});
}

export function printFileDescriptorTSServices(fileDescriptor: FileDescriptorProto, exportMap: ExportMap) {
if (fileDescriptor.getServiceList().length === 0) {
return "";
Expand All @@ -25,6 +41,10 @@ export function printFileDescriptorTSServices(fileDescriptor: FileDescriptorProt

fileDescriptor.getDependencyList().forEach((dependency: string) => {
const pseudoNamespace = filePathToPseudoNamespace(dependency);
// Don't output unused variables as that causes TS6133 error
if (!isUsed(fileDescriptor, pseudoNamespace, exportMap)) {
return;
}
if (dependency in WellKnownTypesMap) {
printer.printLn(`import * as ${pseudoNamespace} from "${WellKnownTypesMap[dependency]}";`);
} else {
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", () => {
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");
});
});

0 comments on commit 6efe91e

Please sign in to comment.