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

Avoid unused definition imports from proto-loader #1826

Merged
merged 2 commits into from
Jun 23, 2021
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
30 changes: 29 additions & 1 deletion packages/proto-loader/bin/proto-loader-gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,34 @@ function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protob
generateServiceDefinitionInterface(formatter, serviceType);
}

function containsDefinition(definitionType: typeof Protobuf.Type | typeof Protobuf.Enum, namespace: Protobuf.NamespaceBase): boolean {
for (const nested of namespace.nestedArray.sort(compareName)) {
if (nested instanceof definitionType) {
return true;
} else if (isNamespaceBase(nested) && !(nested instanceof Protobuf.Type) && !(nested instanceof Protobuf.Enum)) {
return containsDefinition(definitionType, nested);
}
}

return false;
}

function generateDefinitionImports(formatter: TextFormatter, namespace: Protobuf.NamespaceBase, options: GeneratorOptions) {
const imports = [];

if (containsDefinition(Protobuf.Enum, namespace)) {
imports.push('EnumTypeDefinition');
}

if (containsDefinition(Protobuf.Type, namespace)) {
imports.push('MessageTypeDefinition');
}

if (imports.length) {
formatter.writeLine(`import type { ${imports.join(', ')} } from '@grpc/proto-loader';`);
}
}

function generateServiceImports(formatter: TextFormatter, namespace: Protobuf.NamespaceBase, options: GeneratorOptions) {
for (const nested of namespace.nestedArray.sort(compareName)) {
if (nested instanceof Protobuf.Service) {
Expand Down Expand Up @@ -617,7 +645,7 @@ function generateLoadedDefinitionTypes(formatter: TextFormatter, namespace: Prot

function generateRootFile(formatter: TextFormatter, root: Protobuf.Root, options: GeneratorOptions) {
formatter.writeLine(`import type * as grpc from '${options.grpcLib}';`);
formatter.writeLine("import type { ServiceDefinition, EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';");
Copy link
Contributor Author

@mtlewis mtlewis Jun 23, 2021

Choose a reason for hiding this comment

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

Note that the ServiceDefinition import in the old version of the line appears to always be unused in the generated code.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I think it became unused in #1745.

generateDefinitionImports(formatter, root, options);
formatter.writeLine('');

generateServiceImports(formatter, root, options);
Expand Down
2 changes: 1 addition & 1 deletion packages/proto-loader/golden-generated/echo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as grpc from '@grpc/grpc-js';
import type { ServiceDefinition, EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';
import type { EnumTypeDefinition, MessageTypeDefinition } from '@grpc/proto-loader';

import type { OperationsClient as _google_longrunning_OperationsClient, OperationsDefinition as _google_longrunning_OperationsDefinition } from './google/longrunning/Operations';
import type { EchoClient as _google_showcase_v1beta1_EchoClient, EchoDefinition as _google_showcase_v1beta1_EchoDefinition } from './google/showcase/v1beta1/Echo';
Expand Down