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

fix: resolve import collisions for enums #339

Merged
merged 1 commit into from
Aug 3, 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
12 changes: 10 additions & 2 deletions integration/avoid-import-conflicts-types-only/simple-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Simple } from './simple';
import { Simple, SimpleEnum as LocalSimpleEnum, SimpleEnums } from './simple';
import { SimpleEnum as ImportSimpleEnum } from './simple2';

describe('Simple', () => {
it('type checking works correctly', () => {
it('type checking works correctly for interfaces', () => {
const simple: Simple = {
name: 'foo',
otherSimple: {
Expand All @@ -10,4 +11,11 @@ describe('Simple', () => {
},
}
});

it('type checking works correctly for enums', () => {
const simpleEnum: SimpleEnums = {
localEnum: LocalSimpleEnum.LOCAL_BAR,
importEnum: ImportSimpleEnum.IMPORT_FOO,
}
});
})
Binary file modified integration/avoid-import-conflicts-types-only/simple.bin
Binary file not shown.
13 changes: 13 additions & 0 deletions integration/avoid-import-conflicts-types-only/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ syntax = "proto3";
package simple;
import "simple2.proto";

enum SimpleEnum {
LOCAL_DEFAULT = 0;
LOCAL_FOO = 1;
LOCAL_BAR = 2;
}

message Simple {
string name = 1;
simple2.Simple otherSimple = 2;
}

message SimpleEnums {
SimpleEnum local_enum = 1;
simple2.SimpleEnum import_enum = 2;
}


16 changes: 14 additions & 2 deletions integration/avoid-import-conflicts-types-only/simple.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
/* eslint-disable */
import { Simple as Simple1 } from './simple2';
import { SimpleEnum as SimpleEnum1, Simple as Simple2 } from './simple2';

export const protobufPackage = 'simple';

export enum SimpleEnum {
LOCAL_DEFAULT = 0,
LOCAL_FOO = 1,
LOCAL_BAR = 2,
UNRECOGNIZED = -1,
}

export interface Simple {
name: string;
otherSimple: Simple1 | undefined;
otherSimple: Simple2 | undefined;
}

export interface SimpleEnums {
localEnum: SimpleEnum;
importEnum: SimpleEnum1;
}
Binary file modified integration/avoid-import-conflicts-types-only/simple2.bin
Binary file not shown.
6 changes: 6 additions & 0 deletions integration/avoid-import-conflicts-types-only/simple2.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
syntax = "proto3";
package simple2;

enum SimpleEnum {
IMPORT_DEFAULT = 0;
IMPORT_FOO = 10;
IMPORT_BAR = 11;
}

message Simple {
string simple2Name = 1;
int32 simple2Age = 2;
Expand Down
7 changes: 7 additions & 0 deletions integration/avoid-import-conflicts-types-only/simple2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-disable */
export const protobufPackage = 'simple2';

export enum SimpleEnum {
IMPORT_DEFAULT = 0,
IMPORT_FOO = 10,
IMPORT_BAR = 11,
UNRECOGNIZED = -1,
}

export interface Simple {
simple2Name: string;
simple2Age: number;
Expand Down
4 changes: 2 additions & 2 deletions src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { code, Code, joinCode } from 'ts-poet';
import { code, def, Code, joinCode } from 'ts-poet';
import { EnumDescriptorProto } from 'ts-proto-descriptors';
import { maybeAddComment } from './utils';
import { camelCase } from './case';
Expand All @@ -19,7 +19,7 @@ export function generateEnum(
const chunks: Code[] = [];

maybeAddComment(sourceInfo, chunks, enumDesc.options?.deprecated);
chunks.push(code`export ${options.constEnums ? 'const ' : ''}enum ${fullName} {`);
chunks.push(code`export ${options.constEnums ? 'const ' : ''}enum ${def(fullName)} {`);

enumDesc.value.forEach((valueDesc, index) => {
const info = sourceInfo.lookup(Fields.enum.value, index);
Expand Down