Skip to content

Commit

Permalink
fix: resolve import collisions for enums (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
doochik committed Aug 3, 2021
1 parent a55944b commit 8118748
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
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

0 comments on commit 8118748

Please sign in to comment.