From b762686ecafdac8b522d3ad8d89b7084c1bd2ec0 Mon Sep 17 00:00:00 2001 From: jonbretman Date: Sat, 24 Feb 2018 23:05:08 +0000 Subject: [PATCH] Dont output variables that are not used in typescript service definition --- src/ts/fileDescriptorTSServices.ts | 36 ++++++++++++++++------ test/proto/examplecom/simple_service.proto | 4 +++ test/ts_test/src/service.ts | 9 ++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/ts/fileDescriptorTSServices.ts b/src/ts/fileDescriptorTSServices.ts index 7d81e214..99af728f 100644 --- a/src/ts/fileDescriptorTSServices.ts +++ b/src/ts/fileDescriptorTSServices.ts @@ -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 ""; @@ -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()} {`); diff --git a/test/proto/examplecom/simple_service.proto b/test/proto/examplecom/simple_service.proto index 43893fd0..6c0f261b 100644 --- a/test/proto/examplecom/simple_service.proto +++ b/test/proto/examplecom/simple_service.proto @@ -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 { diff --git a/test/ts_test/src/service.ts b/test/ts_test/src/service.ts index b584f3dc..9cf93c32 100644 --- a/test/ts_test/src/service.ts +++ b/test/ts_test/src/service.ts @@ -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"; @@ -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"); + }); });